Skip to content

Commit

Permalink
Add continent, continent name
Browse files Browse the repository at this point in the history
  • Loading branch information
Михаил Исаев committed Dec 1, 2023
1 parent 8083a6b commit 0d424d7
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 30 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
```yaml
listen: :3333 # env var: HTTP_LISTEN, IP and port to listen
citydb: geoip/GeoLite2-City.mmdb # env var: GEOIP_CITY, path to City database
countrydb: geoip/GeoLite2-Country.mmdb # env var: GEOIP_COUNTRY, path to Country database
asndb: geoip/GeoLite2-ASN.mmdb # env var: GEOIP_ASN, path to ASN database
```
Expand Down Expand Up @@ -59,6 +58,12 @@ asndb: geoip/GeoLite2-ASN.mmdb # env var: GEOIP_ASN, path to ASN database
"country_name": {
"type": "string"
},
"continent": {
"type": "string"
},
"continent_name": {
"type": "string"
},
"city": {
"type": "string"
},
Expand Down Expand Up @@ -114,6 +119,8 @@ asndb: geoip/GeoLite2-ASN.mmdb # env var: GEOIP_ASN, path to ASN database
"ip",
"country",
"country_name",
"continent",
"continent_name",
"city",
"asn",
"org",
Expand Down
1 change: 0 additions & 1 deletion config.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
listen: :3333
citydb: geoip/GeoLite2-City.mmdb
countrydb: geoip/GeoLite2-Country.mmdb
asndb: geoip/GeoLite2-ASN.mmdb
9 changes: 2 additions & 7 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,21 @@ package main

import "github.com/oschwald/geoip2-golang"

func OpenGeoipDatabases() (geoip2.Reader, geoip2.Reader, geoip2.Reader) {
func OpenGeoipDatabases() (geoip2.Reader, geoip2.Reader) {
// Open databases
CityDB, err := geoip2.Open(settings.CityDB)
if err != nil {
logger.Fatal(err.Error())
}
CountryDB, err := geoip2.Open(settings.CountryDB)
if err != nil {
logger.Fatal(err.Error())
}
AsnDB, err := geoip2.Open(settings.AsnDB)
if err != nil {
logger.Fatal(err.Error())
}
return *CityDB, *CountryDB, *AsnDB
return *CityDB, *AsnDB
}

func CloseGeoipDatabases() {
CityDB.Close()
CountryDB.Close()
AsnDB.Close()
logger.Info("Closed GeoIP Databases")
}
13 changes: 4 additions & 9 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,10 @@ func GetIPInfo(ip net.IP, c chan IpResponse) (IpResponse, error) {
return resp, err
}
resp.City = CityRecord.City.Names["en"]

// Get info from country
CountryRecord, err := CountryDB.Country(ip)
if err != nil {
logger.Panic(err.Error())
return resp, err
}
resp.Country = CountryRecord.Country.IsoCode
resp.CountryName = CountryRecord.Country.Names["en"]
resp.Country = CityRecord.Country.IsoCode
resp.CountryName = CityRecord.Country.Names["en"]
resp.Continent = CityRecord.Continent.Code
resp.ContinentName = CityRecord.Continent.Names["en"]

// Get info from ASN
AsnRecord, err := AsnDB.ASN(ip)
Expand Down
2 changes: 1 addition & 1 deletion goip.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

var logger = zap.Must(zap.NewProduction())
var settings = LoadSettings()
var CityDB, CountryDB, AsnDB = OpenGeoipDatabases()
var CityDB, AsnDB = OpenGeoipDatabases()

func main() {
// Watch signals and close
Expand Down
23 changes: 12 additions & 11 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package main
import "net"

type IpResponse struct {
Ip net.IP `json:"ip"`
Country string `json:"country"`
CountryName string `json:"country_name"`
City string `json:"city"`
ASN int `json:"asn"`
Org string `json:"org"`
Properties IpProperties `json:"properties"`
Ip net.IP `json:"ip"`
Country string `json:"country"`
CountryName string `json:"country_name"`
Continent string `json:"continent"`
ContinentName string `json:"continent_name"`
City string `json:"city"`
ASN int `json:"asn"`
Org string `json:"org"`
Properties IpProperties `json:"properties"`
}

type IpProperties struct {
Expand All @@ -24,8 +26,7 @@ type IpProperties struct {
}

type Settings struct {
Listen string `default:"${HTTP_LISTEN | :3333}"`
CityDB string `default:"${GEOIP_CITY | geoip/GeoLite2-City.mmdb}"`
CountryDB string `default:"${GEOIP_COUNTRY | geoip/GeoLite2-Country.mmdb}"`
AsnDB string `default:"${GEOIP_ASN | geoip/GeoLite2-ASN.mmdb}"`
Listen string `default:"${HTTP_LISTEN | :3333}"`
CityDB string `default:"${GEOIP_CITY | geoip/GeoLite2-City.mmdb}"`
AsnDB string `default:"${GEOIP_ASN | geoip/GeoLite2-ASN.mmdb}"`
}

0 comments on commit 0d424d7

Please sign in to comment.