2021-11-17 08:03:47 +00:00
|
|
|
|
package geodata
|
|
|
|
|
|
|
|
|
|
import (
|
2022-05-15 05:16:45 +00:00
|
|
|
|
"fmt"
|
2021-11-17 08:03:47 +00:00
|
|
|
|
"github.com/Dreamacro/clash/component/geodata/router"
|
2022-03-15 14:25:33 +00:00
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2021-11-17 08:03:47 +00:00
|
|
|
|
)
|
|
|
|
|
|
2022-02-04 16:51:06 +00:00
|
|
|
|
var geoLoaderName = "memconservative"
|
|
|
|
|
|
|
|
|
|
// geoLoaderName = "standard"
|
|
|
|
|
|
|
|
|
|
func LoaderName() string {
|
|
|
|
|
return geoLoaderName
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func SetLoader(newLoader string) {
|
2022-04-11 05:23:59 +00:00
|
|
|
|
if newLoader == "memc" {
|
|
|
|
|
newLoader = "memconservative"
|
|
|
|
|
}
|
2022-02-04 16:51:06 +00:00
|
|
|
|
geoLoaderName = newLoader
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-15 14:25:33 +00:00
|
|
|
|
func Verify(name string) bool {
|
|
|
|
|
switch name {
|
|
|
|
|
case C.GeositeName:
|
|
|
|
|
_, _, err := LoadGeoSiteMatcher("CN")
|
|
|
|
|
return err == nil
|
|
|
|
|
case C.GeoipName:
|
|
|
|
|
_, _, err := LoadGeoIPMatcher("CN")
|
|
|
|
|
return err == nil
|
|
|
|
|
default:
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-17 08:03:47 +00:00
|
|
|
|
func LoadGeoSiteMatcher(countryCode string) (*router.DomainMatcher, int, error) {
|
2022-05-15 05:16:45 +00:00
|
|
|
|
if len(countryCode) == 0 {
|
|
|
|
|
return nil, 0, fmt.Errorf("country code could not be empty")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
not := false
|
|
|
|
|
if countryCode[0] == '!' {
|
|
|
|
|
not = true
|
|
|
|
|
countryCode = countryCode[1:]
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-17 08:03:47 +00:00
|
|
|
|
geoLoader, err := GetGeoDataLoader(geoLoaderName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, 0, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
domains, err := geoLoader.LoadGeoSite(countryCode)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, 0, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
linear: linear algorithm
|
|
|
|
|
matcher, err := router.NewDomainMatcher(domains)
|
|
|
|
|
mph:minimal perfect hash algorithm
|
|
|
|
|
*/
|
2022-05-15 05:16:45 +00:00
|
|
|
|
matcher, err := router.NewMphMatcherGroup(domains, not)
|
2021-11-17 08:03:47 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, 0, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return matcher, len(domains), nil
|
|
|
|
|
}
|
2022-02-04 15:33:36 +00:00
|
|
|
|
|
|
|
|
|
func LoadGeoIPMatcher(country string) (*router.GeoIPMatcher, int, error) {
|
2022-05-15 15:07:06 +00:00
|
|
|
|
if len(country) == 0 {
|
|
|
|
|
return nil, 0, fmt.Errorf("country code could not be empty")
|
|
|
|
|
}
|
2022-02-04 15:33:36 +00:00
|
|
|
|
geoLoader, err := GetGeoDataLoader(geoLoaderName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, 0, err
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-15 15:07:06 +00:00
|
|
|
|
not := false
|
|
|
|
|
if country[0] == '!' {
|
|
|
|
|
not = true
|
|
|
|
|
country = country[1:]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
records, err := geoLoader.LoadGeoIP(country)
|
2022-02-04 15:33:36 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, 0, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
geoIP := &router.GeoIP{
|
|
|
|
|
CountryCode: country,
|
|
|
|
|
Cidr: records,
|
2022-05-15 15:07:06 +00:00
|
|
|
|
ReverseMatch: not,
|
2022-02-04 15:33:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
matcher, err := router.NewGeoIPMatcher(geoIP)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, 0, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return matcher, len(records), nil
|
|
|
|
|
}
|