Skip to content

Commit

Permalink
add code to load country code by user IP address
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Mar 19, 2022
1 parent b5b9039 commit fa7ac45
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/bunker.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,14 @@ func (e mainEnv) setupRouter() *httprouter.Router {
// next step: https://www.sanarias.com/blog/115LearningHTTPcachinginGo
w.Header().Set("Cache-Control", "public, max-age=7776000")
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))
}
})
Expand Down Expand Up @@ -624,6 +632,7 @@ func main() {
db := &dbcon{store, masterKey, hash[:]}
e := mainEnv{db, cfg, make(chan struct{})}
e.dbCleanup()
initGeoIP()
initCaptcha(hash)
router := e.setupRouter()
router = e.setupConfRouter(router)
Expand Down
75 changes: 75 additions & 0 deletions src/geoip.go
Original file line number Diff line number Diff line change
@@ -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
}
Binary file added ui/site/GeoLite2-Country.mmdb
Binary file not shown.

0 comments on commit fa7ac45

Please sign in to comment.