/
geo.go
57 lines (51 loc) · 1 KB
/
geo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package httpd
import (
"encoding/json"
"fmt"
"net/http"
"github.com/mehrdadrad/mylg/ripe"
)
func getGeo(w http.ResponseWriter, r *http.Request) {
var geo = struct {
CitySrc string
CityDst string
CountrySrc string
CountryDst string
LatSrc float64
LonSrc float64
LatDst float64
LonDst float64
}{}
r.ParseForm()
ipDst := r.FormValue("ip")
// find station public ip address and geo
ip, err := ripe.MyIPAddr()
// find src, dst geo
if err == nil {
p := new(ripe.Prefix)
p.Set(ip)
p.GetGeoData()
for _, g := range p.GeoData.Data.Locations {
if g.City != "" {
geo.CitySrc = g.City
geo.CountrySrc = g.Country
geo.LatSrc = g.Latitude
geo.LonSrc = g.Longitude
break
}
}
p.Set(ipDst)
p.GetGeoData()
for _, g := range p.GeoData.Data.Locations {
if g.City != "" {
geo.CityDst = g.City
geo.CountryDst = g.Country
geo.LatDst = g.Latitude
geo.LonDst = g.Longitude
break
}
}
b, _ := json.Marshal(geo)
fmt.Fprintf(w, string(b))
}
}