From 5e8c3bd70a604bcad9d2f7f91e3393cf815b63a4 Mon Sep 17 00:00:00 2001 From: Evan Chen Date: Tue, 19 Oct 2021 00:47:09 +0800 Subject: [PATCH] fix: algo on geting ip --- server.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/server.go b/server.go index f360f61..5a302ce 100644 --- a/server.go +++ b/server.go @@ -7,6 +7,7 @@ import ( "net" "net/http" "os" + "strings" "time" "github.com/spf13/cobra" @@ -70,7 +71,9 @@ func StartServer() error { func GetIP(r *http.Request) string { ip := r.Header.Get("X-Real-Ip") if ip == "" { - ip = r.Header.Get("X-Forwarded-For") + ips := r.Header.Get("X-Forwarded-For") + ipArr := strings.Split(ips, ",") + ip = strings.Trim(ipArr[len(ipArr)-1], " ") } if ip == "" { var err error