initial commit
authorkali kaneko <kali@leap.se>
Mon, 10 Dec 2018 23:18:40 +0000 (00:18 +0100)
committerkali kaneko <kali@leap.se>
Mon, 10 Dec 2018 23:18:40 +0000 (00:18 +0100)
README.txt [new file with mode: 0644]
getmyip [new file with mode: 0755]
main.go [new file with mode: 0644]

diff --git a/README.txt b/README.txt
new file mode 100644 (file)
index 0000000..3b8ecdc
--- /dev/null
@@ -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 (executable)
index 0000000..86fadc3
Binary files /dev/null and b/getmyip differ
diff --git a/main.go b/main.go
new file mode 100644 (file)
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)
+}