summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkali kaneko <kali@leap.se>2018-12-11 00:18:40 +0100
committerkali kaneko <kali@leap.se>2018-12-11 00:18:40 +0100
commitd55221c93350d37cf9dd33df28f03d968c0494be (patch)
treea487e3a87a667f93a47a04f6bad7bdfa99d08cd0
initial commit
-rw-r--r--README.txt7
-rwxr-xr-xgetmyipbin0 -> 7039362 bytes
-rw-r--r--main.go93
3 files changed, 100 insertions, 0 deletions
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
--- /dev/null
+++ b/getmyip
Binary files 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)
+}