mirror of
https://github.com/apernet/OpenGFW.git
synced 2024-11-13 13:59:24 +08:00
f07a38bc47
* feat: copy something from hysteria/extras/outbounds/acl * feat: add geoip and geosite support for expr * refactor: geo matcher * fix: typo * refactor: geo matcher * feat: expose config options to specify local geoip/geosite db files * refactor: engine.Config should not contains geo * feat: make geosite and geoip lazy downloaded * chore: minor code improvement * docs: add geoip/geosite usage --------- Co-authored-by: Toby <tobyxdd@gmail.com>
45 lines
1.1 KiB
Go
45 lines
1.1 KiB
Go
package v2geo
|
|
|
|
import (
|
|
"os"
|
|
"strings"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
// LoadGeoIP loads a GeoIP data file and converts it to a map.
|
|
// The keys of the map (country codes) are all normalized to lowercase.
|
|
func LoadGeoIP(filename string) (map[string]*GeoIP, error) {
|
|
bs, err := os.ReadFile(filename)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var list GeoIPList
|
|
if err := proto.Unmarshal(bs, &list); err != nil {
|
|
return nil, err
|
|
}
|
|
m := make(map[string]*GeoIP)
|
|
for _, entry := range list.Entry {
|
|
m[strings.ToLower(entry.CountryCode)] = entry
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// LoadGeoSite loads a GeoSite data file and converts it to a map.
|
|
// The keys of the map (site keys) are all normalized to lowercase.
|
|
func LoadGeoSite(filename string) (map[string]*GeoSite, error) {
|
|
bs, err := os.ReadFile(filename)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var list GeoSiteList
|
|
if err := proto.Unmarshal(bs, &list); err != nil {
|
|
return nil, err
|
|
}
|
|
m := make(map[string]*GeoSite)
|
|
for _, entry := range list.Entry {
|
|
m[strings.ToLower(entry.CountryCode)] = entry
|
|
}
|
|
return m, nil
|
|
}
|