sing-box/common/geoip/reader.go

39 lines
789 B
Go
Raw Permalink Normal View History

2022-07-05 05:23:47 +00:00
package geoip
import (
"net/netip"
E "github.com/sagernet/sing/common/exceptions"
2022-07-06 07:01:09 +00:00
"github.com/oschwald/maxminddb-golang"
2022-07-05 05:23:47 +00:00
)
type Reader struct {
reader *maxminddb.Reader
}
func Open(path string) (*Reader, []string, error) {
database, err := maxminddb.Open(path)
if err != nil {
return nil, nil, err
}
if database.Metadata.DatabaseType != "sing-geoip" {
database.Close()
return nil, nil, E.New("incorrect database type, expected sing-geoip, got ", database.Metadata.DatabaseType)
}
return &Reader{database}, database.Metadata.Languages, nil
}
func (r *Reader) Lookup(addr netip.Addr) string {
var code string
_ = r.reader.Lookup(addr.AsSlice(), &code)
if code != "" {
return code
}
return "unknown"
}
2024-01-01 13:36:06 +00:00
func (r *Reader) Close() error {
return r.reader.Close()
}