From d55221c93350d37cf9dd33df28f03d968c0494be Mon Sep 17 00:00:00 2001 From: kali kaneko Date: Tue, 11 Dec 2018 00:18:40 +0100 Subject: initial commit --- README.txt | 7 +++++ getmyip | Bin 0 -> 7039362 bytes main.go | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 README.txt create mode 100755 getmyip create mode 100644 main.go diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..3b8ecdc --- /dev/null +++ b/README.txt @@ -0,0 +1,7 @@ +Get GeoIP City database +----------------------- + +sudo apt install geoipupdate +sudo cp /usr/share/doc/geoipupdate/examples/GeoIP.conf.default /etc/GeoIP.conf +sudo mkdir -p /usr/local/share/GeoIP +sudo geoipupdate -v diff --git a/getmyip b/getmyip new file mode 100755 index 0000000..86fadc3 Binary files /dev/null and b/getmyip differ diff --git a/main.go b/main.go new file mode 100644 index 0000000..0205a9a --- /dev/null +++ b/main.go @@ -0,0 +1,93 @@ +package main + +import ( + "encoding/json" + "fmt" + "log" + "net" + "net/http" + "strconv" + + "github.com/oschwald/geoip2-golang" +) + +type geodb struct { + db *geoip2.Reader +} + +func floatToString(num float64) string { + return strconv.FormatFloat(num, 'f', 6, 64) +} + +func (g *geodb) getRecordForIP(ipstr string) *geoip2.City { + ip := net.ParseIP(ipstr) + record, err := g.db.City(ip) + if err != nil { + log.Fatal(err) + } + return record +} + +type jsonHandler struct { + geoipdb *geodb +} + +func (jh *jsonHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { + ip, _, err := net.SplitHostPort(req.RemoteAddr) + if err != nil { + log.Fatal(err) + } + netIP := net.ParseIP(ip) + ipstr := netIP.String() + record := jh.geoipdb.getRecordForIP(ipstr) + data := map[string]string{ + "ip": ipstr, + "cc": record.Country.IsoCode, + "city": record.City.Names["en"], + "lat": floatToString(record.Location.Latitude), + "lon": floatToString(record.Location.Longitude), + } + dataJSON, _ := json.Marshal(data) + fmt.Fprintf(w, string(dataJSON)) +} + +type txtHandler struct { + geoipdb *geodb +} + +func (th *txtHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { + ip, _, err := net.SplitHostPort(req.RemoteAddr) + if err != nil { + log.Fatal(err) + } + netIP := net.ParseIP(ip) + ipstr := netIP.String() + record := th.geoipdb.getRecordForIP(ipstr) + + fmt.Fprintf(w, "Your IP: %s\n", ipstr) + fmt.Fprintf(w, "Your Country: %s\n", record.Country.IsoCode) + fmt.Fprintf(w, "Your City: %s\n", record.City.Names["en"]) + fmt.Fprintf(w, "Your Coordinates: %s, %s\n", + floatToString(record.Location.Latitude), + floatToString(record.Location.Longitude)) +} + +func main() { + db, err := geoip2.Open("/usr/local/share/GeoIP/GeoLite2-City.mmdb") + if err != nil { + log.Fatal(err) + } + defer db.Close() + + geoipdb := geodb{db} + + mux := http.NewServeMux() + jh := &jsonHandler{&geoipdb} + mux.Handle("/json", jh) + + th := &txtHandler{&geoipdb} + mux.Handle("/", th) + + log.Println("Listening...") + http.ListenAndServe(":9001", mux) +} -- cgit v1.2.3