From 137e164d028a18a9f4f251e2250553a6a3524d6c Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Thu, 1 May 2025 20:55:00 +0000 Subject: added the proxy protocol to the server --- docker-compose.yaml | 1 - go.mod | 4 +++- icanhazallips.go | 15 ++++++++++++++- nginx.conf | 2 ++ 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/docker-compose.yaml b/docker-compose.yaml index 15b75f7..22f5dd0 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,4 +1,3 @@ -version: "3" services: icanhazallips: image: icanhazallips diff --git a/go.mod b/go.mod index 6f0768c..e8954e0 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module icanhazallips -go 1.19 +go 1.24.0 + +require github.com/pires/go-proxyproto v0.8.0 diff --git a/icanhazallips.go b/icanhazallips.go index 1564ef4..48222bf 100644 --- a/icanhazallips.go +++ b/icanhazallips.go @@ -10,6 +10,8 @@ import ( "strconv" "strings" "time" + + "github.com/pires/go-proxyproto" ) const ( @@ -166,5 +168,16 @@ func main() { Handler: nil, } - log.Fatal(server.ListenAndServe()) + ln, err := net.Listen("tcp", config.Addr) + if err != nil { + log.Fatal(err) + } + + proxyListener := &proxyproto.Listener{ + Listener: ln, + ReadHeaderTimeout: time.Duration(config.ReadHeaderTimeout) * time.Second, + } + defer proxyListener.Close() + + log.Fatal(server.Serve(proxyListener)) } diff --git a/nginx.conf b/nginx.conf index 5a9c8a3..b917535 100644 --- a/nginx.conf +++ b/nginx.conf @@ -26,6 +26,8 @@ http { add_header X-XSS-Protection "1; mode=block" always; add_header Referrer-Policy "no-referrer"; fastcgi_hide_header X-Powered-By; + proxy_set_header X-Real-IP $proxy_protocol_addr; + proxy_set_header X-Forwarded-For $proxy_protocol_addr; error_page 401 403 404 /404.html; location / { -- cgit v1.2.3