Skip to content

Commit

Permalink
use client-IP if none was provided (fix #2)
Browse files Browse the repository at this point in the history
  • Loading branch information
superstes committed Sep 11, 2024
1 parent 331388f commit 9b7b49d
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
**/geoip-lookup-service
**/geoip-lookup
**/main
build/
build/
.idea/
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ curl "http://127.0.0.1:10069/?ip=8.8.8.8&lookup=country&filter=country.names.en"

curl "http://127.0.0.1:10069/?ip=8.8.8.8&lookup=city&filter=location"
> {"accuracy_radius":1000,"latitude":37.751,"longitude":-97.822,"time_zone":"America/Chicago"}

# listen on all external IPs
./geoip_lookup_service -l 0.0.0.0 -p 10069 ...
```

----
Expand Down
2 changes: 1 addition & 1 deletion cnf/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package cnf

const VERSION = 1.0
const VERSION = 1.1
42 changes: 42 additions & 0 deletions main/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"encoding/json"
"errors"
"fmt"
"io"
"log"
Expand Down Expand Up @@ -39,12 +40,53 @@ func returnResult(w http.ResponseWriter, data interface{}, logPrefix string) {
}
}

func getClientIP(r *http.Request) (string, error) {
fwdIPs := strings.Split(r.Header.Get("X-Forwarded-For"), ",")
if len(fwdIPs) > 0 {
netIP := net.ParseIP(fwdIPs[len(fwdIPs)-1])
if netIP != nil {
return netIP.String(), nil
}
}

realIP := r.Header.Get("X-Real-IP")
if realIP != "" {
netIP := net.ParseIP(realIP)
if netIP != nil {
return netIP.String(), nil
}
}

ip, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
return "", err
}

netIP := net.ParseIP(ip)
if netIP != nil {
ip := netIP.String()
if ip == "::1" {
return "127.0.0.1", nil
}
return ip, nil
}

return "", errors.New("IP not found")
}

func geoIpLookup(w http.ResponseWriter, r *http.Request) {
ipStr := r.URL.Query().Get("ip")
lookupStr := r.URL.Query().Get("lookup")
filterStr := r.URL.Query().Get("filter")
logPrefix := fmt.Sprintf("IP: '%v', Lookup: '%v', Filter: '%v'", ipStr, lookupStr, filterStr)

if ipStr == "" {
clientIpStr, err := getClientIP(r)
if err == nil {
ipStr = clientIpStr
}
}

if lookupStr == "" || ipStr == "" {
errorResponse(w, "Either 'lookup' or 'ip' were not provided")
return
Expand Down
4 changes: 2 additions & 2 deletions script/build_all_bin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ VERSION="$(cat "../cnf/main.go" | grep VERSION | cut -d '=' -f2 | tr -d ' ')"

mkdir -p "../build"

rm ../build/*
rm -f ../build/*

function compile() {
os="$1" arch="$2"
Expand All @@ -30,6 +30,7 @@ compile "linux" "amd64"
compile "linux" "arm"
compile "linux" "arm64"

# untested
compile "freebsd" "386"
compile "freebsd" "amd64"
compile "freebsd" "arm"
Expand All @@ -41,6 +42,5 @@ compile "openbsd" "arm"
compile "darwin" "amd64"
compile "darwin" "arm64"

# untested
compile "windows" "386"
compile "windows" "amd64"

0 comments on commit 9b7b49d

Please sign in to comment.