mirror of
https://github.com/tobychui/zoraxy.git
synced 2025-06-06 07:37:21 +02:00
40 lines
785 B
Go
40 lines
785 B
Go
package main
|
|
|
|
import (
|
|
"net"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/oschwald/geoip2-golang"
|
|
)
|
|
|
|
func getCountryCodeFromRequest(r *http.Request) string {
|
|
countryCode := ""
|
|
|
|
// Get the IP address of the user from the request headers
|
|
ipAddress := r.Header.Get("X-Forwarded-For")
|
|
if ipAddress == "" {
|
|
ipAddress = strings.Split(r.RemoteAddr, ":")[0]
|
|
}
|
|
|
|
// Open the GeoIP database
|
|
db, err := geoip2.Open("./tmp/GeoIP2-Country.mmdb")
|
|
if err != nil {
|
|
// Handle the error
|
|
return countryCode
|
|
}
|
|
defer db.Close()
|
|
|
|
// Look up the country code for the IP address
|
|
record, err := db.Country(net.ParseIP(ipAddress))
|
|
if err != nil {
|
|
// Handle the error
|
|
return countryCode
|
|
}
|
|
|
|
// Get the ISO country code from the record
|
|
countryCode = record.Country.IsoCode
|
|
|
|
return countryCode
|
|
}
|