mirror of
https://github.com/optim-enterprises-bv/databunker.git
synced 2025-11-02 02:48:05 +00:00
add code to load country code by user IP address
This commit is contained in:
@@ -299,6 +299,14 @@ func (e mainEnv) setupRouter() *httprouter.Router {
|
|||||||
// next step: https://www.sanarias.com/blog/115LearningHTTPcachinginGo
|
// next step: https://www.sanarias.com/blog/115LearningHTTPcachinginGo
|
||||||
w.Header().Set("Cache-Control", "public, max-age=7776000")
|
w.Header().Set("Cache-Control", "public, max-age=7776000")
|
||||||
w.WriteHeader(200)
|
w.WriteHeader(200)
|
||||||
|
if strings.HasSuffix(r.URL.Path, ".js") || strings.HasSuffix(r.URL.Path, ".html") {
|
||||||
|
if bytes.Contains(data, []byte("%IPCOUNTRY%")) {
|
||||||
|
ipCountry := getCountry(r)
|
||||||
|
data2 := bytes.ReplaceAll(data, []byte("%IPCOUNTRY%"), []byte(ipCountry))
|
||||||
|
w.Write([]byte(data2))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
w.Write([]byte(data))
|
w.Write([]byte(data))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -624,6 +632,7 @@ func main() {
|
|||||||
db := &dbcon{store, masterKey, hash[:]}
|
db := &dbcon{store, masterKey, hash[:]}
|
||||||
e := mainEnv{db, cfg, make(chan struct{})}
|
e := mainEnv{db, cfg, make(chan struct{})}
|
||||||
e.dbCleanup()
|
e.dbCleanup()
|
||||||
|
initGeoIP()
|
||||||
initCaptcha(hash)
|
initCaptcha(hash)
|
||||||
router := e.setupRouter()
|
router := e.setupRouter()
|
||||||
router = e.setupConfRouter(router)
|
router = e.setupConfRouter(router)
|
||||||
|
|||||||
75
src/geoip.go
Normal file
75
src/geoip.go
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"github.com/gobuffalo/packr"
|
||||||
|
"github.com/oschwald/geoip2-golang"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
geoipBytes []byte
|
||||||
|
geoip * geoip2.Reader
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
func initGeoIP() {
|
||||||
|
var err error
|
||||||
|
box := packr.NewBox("../ui")
|
||||||
|
geoipBytes, err = box.Find("site/GeoLite2-Country.mmdb")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to load geoip database file")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
geoip, err = geoip2.FromBytes(geoipBytes)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to load geoip database")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
//captchaKey = h
|
||||||
|
}
|
||||||
|
|
||||||
|
func getCountry(r *http.Request) string {
|
||||||
|
userIP := ""
|
||||||
|
//log.Printf("Headers %v", r.Header)
|
||||||
|
if len(r.Header.Get("CF-Connecting-IP")) > 1 {
|
||||||
|
userIP = strings.TrimSpace(r.Header.Get("CF-Connecting-IP"))
|
||||||
|
}
|
||||||
|
if len(r.Header.Get("X-Forwarded-For")) > 1 && len(userIP) == 0 {
|
||||||
|
userIP = strings.TrimSpace(r.Header.Get("X-Forwarded-For"))
|
||||||
|
}
|
||||||
|
if len(r.Header.Get("X-Real-IP")) > 1 && len(userIP) == 0 {
|
||||||
|
userIP = strings.TrimSpace(r.Header.Get("X-Real-IP"))
|
||||||
|
}
|
||||||
|
if len(userIP) == 0 {
|
||||||
|
userIP = r.RemoteAddr
|
||||||
|
}
|
||||||
|
if strings.Contains(userIP, ",") {
|
||||||
|
userIP = strings.Split(userIP, ",")[0]
|
||||||
|
}
|
||||||
|
if strings.Contains(userIP, " ") {
|
||||||
|
userIP = strings.Split(userIP, " ")[0]
|
||||||
|
}
|
||||||
|
if len(userIP) == 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
ip := net.ParseIP(userIP)
|
||||||
|
if ip == nil {
|
||||||
|
if strings.Count(userIP, ":") == 1 {
|
||||||
|
userIP = strings.Split(userIP, ":")[0]
|
||||||
|
ip = net.ParseIP(userIP)
|
||||||
|
}
|
||||||
|
if ip == nil {
|
||||||
|
log.Printf("Failed to parse userIP: %s", userIP)
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
record, err := geoip.Country(ip)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to detect country using IP address: %s", userIP)
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return record.Country.IsoCode
|
||||||
|
}
|
||||||
BIN
ui/site/GeoLite2-Country.mmdb
Normal file
BIN
ui/site/GeoLite2-Country.mmdb
Normal file
Binary file not shown.
Reference in New Issue
Block a user