github.com/inazumav/sing-box@v0.0.0-20230926072359-ab51429a14f1/common/geoip/reader.go (about) 1 package geoip 2 3 import ( 4 "net/netip" 5 6 E "github.com/sagernet/sing/common/exceptions" 7 8 "github.com/oschwald/maxminddb-golang" 9 ) 10 11 type Reader struct { 12 reader *maxminddb.Reader 13 } 14 15 func Open(path string) (*Reader, []string, error) { 16 database, err := maxminddb.Open(path) 17 if err != nil { 18 return nil, nil, err 19 } 20 if database.Metadata.DatabaseType != "sing-geoip" { 21 database.Close() 22 return nil, nil, E.New("incorrect database type, expected sing-geoip, got ", database.Metadata.DatabaseType) 23 } 24 return &Reader{database}, database.Metadata.Languages, nil 25 } 26 27 func (r *Reader) Lookup(addr netip.Addr) string { 28 var code string 29 _ = r.reader.Lookup(addr.AsSlice(), &code) 30 if code != "" { 31 return code 32 } 33 return "unknown" 34 }