From e364fdc7e3ea0da22c53c9b247b51ab54e06301d Mon Sep 17 00:00:00 2001 From: proffapt Date: Tue, 2 Jul 2024 18:41:11 +0530 Subject: [PATCH] fix: multiple ips in x-forwarded-for --- main.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/main.go b/main.go index 4057e5b..1231f70 100644 --- a/main.go +++ b/main.go @@ -125,6 +125,11 @@ func generateOtp(user User) (bool, error) { func handleCampusCheck(res http.ResponseWriter, req *http.Request) { clientIP := req.Header.Get("X-Forwarded-For") + if strings.Contains(clientIP, ",") { + ips := strings.Split(clientIP, ",") + clientIP = strings.TrimSpace(ips[0]) + } + whoisResponse, err := whois.Whois(clientIP) if err != nil { fmt.Println(err) @@ -145,6 +150,8 @@ func handleCampusCheck(res http.ResponseWriter, req *http.Request) { if len(match) >= 2 { netname := match[1] + fmt.Println("[NETNAME FOUND] ~", netname) + if netname == "IITKGP-IN" { response["is_inside_kgp"] = true res.WriteHeader(http.StatusAccepted) @@ -154,6 +161,7 @@ func handleCampusCheck(res http.ResponseWriter, req *http.Request) { } } else { fmt.Println("[NETNAME NOT FOUND]") + response["is_inside_kgp"] = false res.WriteHeader(http.StatusUnauthorized) }