From 553aca10745b76fb5d9c4bec60bfbc4c1f084eca Mon Sep 17 00:00:00 2001 From: ditatompel Date: Sun, 5 May 2024 02:20:54 +0700 Subject: [PATCH] Adding GeoIP support --- .gitignore | 1 + README.md | 6 ++++ go.mod | 2 ++ go.sum | 11 ++++++ internal/repo/geoip.go | 74 +++++++++++++++++++++++++++++++++++++++++ internal/repo/monero.go | 25 +++++++------- 6 files changed, 107 insertions(+), 12 deletions(-) create mode 100644 internal/repo/geoip.go diff --git a/.gitignore b/.gitignore index 19f6c4d..df7bc84 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ /bin /node_modules /tmp +/assets/geoip diff --git a/README.md b/README.md index a4cd3a1..4f36789 100644 --- a/README.md +++ b/README.md @@ -1 +1,7 @@ # XMR Nodes + +## Requirements + +- GeoIP Database (https://dev.maxmind.com/geoip/geoip2/geolite2/) (place it to `./assets/geoip`, see [./internal/repo/geoip.go](./internal/repo/geoip.go)). + + diff --git a/go.mod b/go.mod index 4841713..198d9aa 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/google/uuid v1.6.0 github.com/jmoiron/sqlx v1.4.0 github.com/joho/godotenv v1.5.1 + github.com/oschwald/geoip2-golang v1.9.0 github.com/spf13/cobra v1.8.0 golang.org/x/net v0.21.0 golang.org/x/term v0.19.0 @@ -22,6 +23,7 @@ require ( github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-runewidth v0.0.15 // indirect + github.com/oschwald/maxminddb-golang v1.11.0 // indirect github.com/rivo/uniseg v0.2.0 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect diff --git a/go.sum b/go.sum index 8c38525..b1f1a72 100644 --- a/go.sum +++ b/go.sum @@ -5,6 +5,8 @@ github.com/alexedwards/argon2id v1.0.0/go.mod h1:tYKkqIjzXvZdzPvADMWOEZ+l6+BD6Ct github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs= github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y= github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg= github.com/gofiber/fiber/v2 v2.52.4 h1:P+T+4iK7VaqUsq2PALYEfBBo6bJZ4q3FP8cZ84EggTM= @@ -30,6 +32,12 @@ github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZ github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= +github.com/oschwald/geoip2-golang v1.9.0 h1:uvD3O6fXAXs+usU+UGExshpdP13GAqp4GBrzN7IgKZc= +github.com/oschwald/geoip2-golang v1.9.0/go.mod h1:BHK6TvDyATVQhKNbQBdrj9eAvuwOMi2zSFXizL3K81Y= +github.com/oschwald/maxminddb-golang v1.11.0 h1:aSXMqYR/EPNjGE8epgqwDay+P30hCBZIveY0WZbAWh0= +github.com/oschwald/maxminddb-golang v1.11.0/go.mod h1:YmVI+H0zh3ySFR3w+oz8PCfglAFj3PuCmui13+P9zDg= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= @@ -37,6 +45,8 @@ github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0= github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/valyala/fasthttp v1.51.0 h1:8b30A5JlZ6C7AS81RsWjYMQmrZG6feChmgAolCl1SqA= @@ -92,4 +102,5 @@ golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/repo/geoip.go b/internal/repo/geoip.go new file mode 100644 index 0000000..f6fe9c0 --- /dev/null +++ b/internal/repo/geoip.go @@ -0,0 +1,74 @@ +package repo + +import ( + "errors" + "net" + + "github.com/oschwald/geoip2-golang" +) + +type GeoIpInfo struct { + Ip string `json:"ip"` + IsAnonymousProxy bool `json:"is_anonymous_proxy"` + IsSatelliteProvider bool `json:"is_satellite_provider"` + City string `json:"city"` + ContinentName string `json:"continent_name"` + ContinentCode string `json:"continent_code"` + IsInEuropeanUnion bool `json:"is_in_european_union"` + CountryName string `json:"country_name"` + CountryCode string `json:"country_code"` + TimeZone string `json:"timezone"` + Latitude float64 `json:"latitude"` + Longitude float64 `json:"longitude"` + AccuracyRadius uint16 `json:"accuracy_radius"` + AsnOrg string `json:"asn_org"` + Asn uint `json:"asn"` +} + +func GetGeoIpInfo(ipAddr string) (*GeoIpInfo, error) { + ip := net.ParseIP(ipAddr) + if ip == nil { + return nil, errors.New("Invalid IP address") + } + dbCity, err := geoip2.Open("./assets/geoip/GeoLite2-City.mmdb") + if err != nil { + return nil, errors.New("Cannot open GeoIP City database") + } + defer dbCity.Close() + + dbAsn, err := geoip2.Open("./assets/geoip/GeoLite2-ASN.mmdb") + if err != nil { + return nil, errors.New("Cannot open GeoIP ASN database") + } + defer dbAsn.Close() + + cityRecord, err := dbCity.City(ip) + if err != nil { + return nil, errors.New("Cannot read GeoIP City database") + } + + asnRecord, err := dbAsn.ASN(ip) + if err != nil { + return nil, errors.New("Cannot read GeoIP ASN database") + } + + qip := GeoIpInfo{ + Ip: ipAddr, + IsAnonymousProxy: cityRecord.Traits.IsAnonymousProxy, + IsSatelliteProvider: cityRecord.Traits.IsSatelliteProvider, + City: cityRecord.City.Names["en"], + ContinentName: cityRecord.Continent.Names["en"], + ContinentCode: cityRecord.Continent.Code, + IsInEuropeanUnion: cityRecord.Country.IsInEuropeanUnion, + CountryName: cityRecord.Country.Names["en"], + CountryCode: cityRecord.Country.IsoCode, + TimeZone: cityRecord.Location.TimeZone, + Latitude: cityRecord.Location.Latitude, + Longitude: cityRecord.Location.Longitude, + AccuracyRadius: cityRecord.Location.AccuracyRadius, + AsnOrg: asnRecord.AutonomousSystemOrganization, + Asn: asnRecord.AutonomousSystemNumber, + } + + return &qip, nil +} diff --git a/internal/repo/monero.go b/internal/repo/monero.go index 640df27..0214079 100644 --- a/internal/repo/monero.go +++ b/internal/repo/monero.go @@ -280,18 +280,19 @@ func (repo *MoneroRepo) ProcessJob(report ProbeReport, proberId int64) error { } // recheck IP - // TODO: Fill the data using GeoIP - - // if report.NodeInfo.Ip != "" { - // ipInfo, errGeoIp := GetGeoIpInfo(report.NodeInfo.Ip) - // if errGeoIp == nil { - // report.NodeInfo.Asn = ipInfo.Asn - // report.NodeInfo.AsnName = ipInfo.AsnOrg - // report.NodeInfo.CountryCode = ipInfo.CountryCode - // report.NodeInfo.CountryName = ipInfo.CountryName - // report.NodeInfo.City = ipInfo.City - // } - // } + if report.NodeInfo.Ip != "" { + if ipInfo, errGeoIp := GetGeoIpInfo(report.NodeInfo.Ip); errGeoIp != nil { + fmt.Println("WARN:", errGeoIp.Error()) + } else { + report.NodeInfo.Asn = ipInfo.Asn + report.NodeInfo.AsnName = ipInfo.AsnOrg + report.NodeInfo.CountryCode = ipInfo.CountryCode + report.NodeInfo.CountryName = ipInfo.CountryName + report.NodeInfo.City = ipInfo.City + report.NodeInfo.Lon = ipInfo.Longitude + report.NodeInfo.Lat = ipInfo.Latitude + } + } update := `UPDATE tbl_node SET is_available = ?, nettype = ?, height = ?, adjusted_time = ?,