mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-22 16:41:30 +00:00
39 lines
823 B
Go
39 lines
823 B
Go
package geoip
|
|
|
|
import (
|
|
"net/netip"
|
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
N "github.com/sagernet/sing/common/network"
|
|
|
|
"github.com/oschwald/maxminddb-golang"
|
|
)
|
|
|
|
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
|
|
}
|
|
if !N.IsPublicAddr(addr) {
|
|
return "private"
|
|
}
|
|
return "unknown"
|
|
}
|