github.com/zartbot/goflow@v0.0.0-20221001080520-456f7dfad288/service/geoipmap/geoipmap.go (about) 1 package geoipmap 2 3 import ( 4 "fmt" 5 "log" 6 "net" 7 8 "github.com/olivere/elastic" 9 "github.com/zartbot/goflow/lib/iputil" 10 11 geoip2 "github.com/oschwald/geoip2-golang" 12 ) 13 14 type GeoIPCollector struct { 15 CityDB *geoip2.Reader 16 ASNDB *geoip2.Reader 17 Default_Lat float64 18 Default_Long float64 19 } 20 21 func NewGeoIPCollector(citymmdb string, asnmmdb string, defaultLat float64, defaultLong float64) (*GeoIPCollector, error) { 22 var r = GeoIPCollector{} 23 var err error 24 r.CityDB, err = geoip2.Open(citymmdb) 25 r.ASNDB, err = geoip2.Open(asnmmdb) 26 r.Default_Lat = defaultLat 27 r.Default_Long = defaultLong 28 if err != nil { 29 log.Fatal(err) 30 } 31 return &r, err 32 } 33 34 /* Use the following address test hongkong/taiwan/Macau 35 test_ip = "14.0.207.94"//hongkong :location_region_name == "Hong Kong" 36 test_ip = "140.112.110.1"//taiwan :location_region_name == "Taiwan" 37 test_ip = "122.100.160.253" //Macau :location_region_name == "Macau" 38 */ 39 40 type GeoLocation struct { 41 City string 42 Region string 43 Country string 44 ASN string 45 AccuracyRadius uint16 46 Location *elastic.GeoPoint `json:"location"` 47 } 48 49 func (g *GeoIPCollector) Lookup(ip net.IP) GeoLocation { 50 var r GeoLocation 51 if ip.IsGlobalUnicast() { 52 if !iputil.IsPrivateIP(ip) { 53 c, _ := g.CityDB.City(ip) 54 asn, _ := g.ASNDB.ASN(ip) 55 if c.City.GeoNameID != 0 { 56 r.City = c.City.Names["en"] 57 58 } 59 if len(c.Subdivisions) > 0 { 60 if c.Subdivisions[0].GeoNameID != 0 { 61 r.Region = c.Subdivisions[0].Names["en"] 62 } 63 } 64 if c.Country.GeoNameID != 0 { 65 r.Country = c.Country.Names["en"] 66 } 67 if r.Country == "Hong Kong" { 68 r.Country = "China" 69 r.Region = "Hong Kong" 70 } 71 if r.Country == "Macau" { 72 r.Country = "China" 73 r.Region = "Macau" 74 } 75 if r.Country == "Taiwan" { 76 r.Country = "China" 77 r.Region = "Taiwan" 78 } 79 80 if r.Country == "" { 81 r.Country = "Unknown" 82 } 83 if r.Region == "" { 84 r.Region = "Unknown" 85 } 86 if r.City == "" { 87 r.City = "Unknown" 88 } 89 r.AccuracyRadius = c.Location.AccuracyRadius 90 r.Location = elastic.GeoPointFromLatLon(c.Location.Latitude, c.Location.Longitude) 91 92 r.ASN = fmt.Sprintf("%d::%s", asn.AutonomousSystemNumber, asn.AutonomousSystemOrganization) 93 } else { 94 r.City = "Local Private Network" 95 r.Region = "Local Private Network" 96 r.Country = "Local Private Network" 97 r.ASN = "0::LAN" 98 r.Location = elastic.GeoPointFromLatLon(g.Default_Lat, g.Default_Long) 99 } 100 } else { 101 r.City = "Other" 102 r.Region = "Other" 103 r.Country = "Other" 104 r.ASN = "0::Unknown" 105 r.Location = elastic.GeoPointFromLatLon(g.Default_Lat, g.Default_Long) 106 } 107 return r 108 } 109 110 func UpdateGeoLocationInfo(d map[string]interface{}, g *GeoIPCollector) error { 111 var err error 112 f, valid := d["flowinfo"] 113 if valid { 114 ipinfo := f.(iputil.FlowInfo) 115 d["Location_A"] = g.Lookup(ipinfo.IPAddress_A) 116 d["Location_B"] = g.Lookup(ipinfo.IPAddress_B) 117 } 118 return err 119 }