|
| 1 | +package geoip |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "net" |
| 7 | + |
| 8 | + "github.com/oschwald/geoip2-golang" |
| 9 | +) |
| 10 | + |
| 11 | +// Database provides the minimal useful interface for looking up relevant information so we |
| 12 | +// aren't tied tp the types / interface of a specific GeoIP library |
| 13 | +type Database interface { |
| 14 | + ASN(ip net.IP) (uint, error) |
| 15 | + CC(ip net.IP) (string, error) |
| 16 | +} |
| 17 | + |
| 18 | +var ( |
| 19 | + // ErrMissingDB indicates that one or more of the utility databases was not provided. The wrapped |
| 20 | + // error message should indicate which database was missing. |
| 21 | + ErrMissingDB = errors.New("missing one or more geoip DBs") |
| 22 | +) |
| 23 | + |
| 24 | +// DBConfig contains options used for GeoIP lookup - including paths to database files |
| 25 | +type DBConfig struct { |
| 26 | + CCDBPath string `toml:"geoip_cc_db_path"` |
| 27 | + ASNDBPath string `toml:"geoip_asn_db_path"` |
| 28 | +} |
| 29 | + |
| 30 | +// New returns a database given a config. If the provided config is nil, the Empty Database will |
| 31 | +// be returned. |
| 32 | +func New(conf *DBConfig) (Database, error) { |
| 33 | + |
| 34 | + // If no config is provided or the config provided contains no DB paths return the empty db. |
| 35 | + if conf == nil { |
| 36 | + return &EmptyDatabase{}, fmt.Errorf("%w: no config", ErrMissingDB) |
| 37 | + } else if conf.ASNDBPath == "" && conf.CCDBPath == "" { |
| 38 | + return &EmptyDatabase{}, fmt.Errorf("%w: no asn or cc db files", ErrMissingDB) |
| 39 | + } |
| 40 | + |
| 41 | + db := &maxMindDatabase{DBConfig: conf} |
| 42 | + |
| 43 | + err := db.init() |
| 44 | + if errors.Is(err, ErrMissingDB) { |
| 45 | + return db, err |
| 46 | + } else if err != nil { |
| 47 | + return nil, err |
| 48 | + } |
| 49 | + |
| 50 | + return db, nil |
| 51 | +} |
| 52 | + |
| 53 | +// maxMindDatabase provides the GeoIP functionality that we need using the MaxMind GeoIP service |
| 54 | +type maxMindDatabase struct { |
| 55 | + *DBConfig |
| 56 | + |
| 57 | + asnReader *geoip2.Reader |
| 58 | + ccReader *geoip2.Reader |
| 59 | +} |
| 60 | + |
| 61 | +// ASN returns the Autonomous System Number (ASN) associated with the provided IP. |
| 62 | +func (mmdb *maxMindDatabase) init() error { |
| 63 | + var err error |
| 64 | + |
| 65 | + if mmdb.ASNDBPath != "" { |
| 66 | + mmdb.asnReader, err = geoip2.Open(mmdb.DBConfig.ASNDBPath) |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + if mmdb.CCDBPath != "" { |
| 73 | + mmdb.ccReader, err = geoip2.Open(mmdb.DBConfig.CCDBPath) |
| 74 | + if err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + if mmdb.ccReader == nil && mmdb.asnReader != nil { |
| 80 | + return fmt.Errorf("%w: no cc db file", ErrMissingDB) |
| 81 | + } else if mmdb.ccReader != nil && mmdb.asnReader == nil { |
| 82 | + return fmt.Errorf("%w: no asn db file", ErrMissingDB) |
| 83 | + } |
| 84 | + |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +// ASN returns the Autonomous System Number (ASN) associated with the provided IP. |
| 89 | +func (mmdb *maxMindDatabase) ASN(ipAddress net.IP) (uint, error) { |
| 90 | + if mmdb == nil || mmdb.asnReader == nil { |
| 91 | + return 0, nil |
| 92 | + } |
| 93 | + |
| 94 | + record, err := mmdb.asnReader.ASN(ipAddress) |
| 95 | + if err != nil { |
| 96 | + return 0, err |
| 97 | + } |
| 98 | + |
| 99 | + return record.AutonomousSystemNumber, nil |
| 100 | +} |
| 101 | + |
| 102 | +// CC returns the ISO country code associated with the provided IP. |
| 103 | +func (mmdb *maxMindDatabase) CC(ipAddress net.IP) (string, error) { |
| 104 | + if mmdb == nil || mmdb.ccReader == nil { |
| 105 | + return "", nil |
| 106 | + } |
| 107 | + |
| 108 | + record, err := mmdb.ccReader.Country(ipAddress) |
| 109 | + if err != nil { |
| 110 | + return "", err |
| 111 | + } |
| 112 | + |
| 113 | + return record.Country.IsoCode, nil |
| 114 | +} |
0 commit comments