Skip to content

Commit

Permalink
fix: multiple ips in x-forwarded-for
Browse files Browse the repository at this point in the history
  • Loading branch information
proffapt committed Jul 2, 2024
1 parent 4d3de27 commit e364fdc
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
}
Expand Down

0 comments on commit e364fdc

Please sign in to comment.