2021-06-19 08:41:32 -04:00
|
|
|
package dns
|
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
//go:generate go run github.com/v2fly/v2ray-core/v5/common/errors/errorgen
|
2019-02-10 13:04:11 -05:00
|
|
|
|
|
|
|
import (
|
2021-05-03 22:15:11 -04:00
|
|
|
"context"
|
2019-02-10 13:04:11 -05:00
|
|
|
"encoding/json"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v5/app/dns"
|
2022-12-14 21:38:28 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v5/app/dns/fakedns"
|
2022-01-02 10:16:23 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v5/app/router/routercommon"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/net"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/platform"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/infra/conf/cfgcommon"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/infra/conf/geodata"
|
|
|
|
rule2 "github.com/v2fly/v2ray-core/v5/infra/conf/rule"
|
2019-02-10 13:04:11 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type NameServerConfig struct {
|
2022-11-29 19:43:39 -05:00
|
|
|
Address *cfgcommon.Address
|
|
|
|
ClientIP *cfgcommon.Address
|
|
|
|
Port uint16
|
|
|
|
Tag string
|
|
|
|
QueryStrategy string
|
|
|
|
CacheStrategy string
|
|
|
|
FallbackStrategy string
|
|
|
|
SkipFallback bool
|
|
|
|
Domains []string
|
|
|
|
ExpectIPs cfgcommon.StringList
|
2022-12-14 21:38:28 -05:00
|
|
|
FakeDNS FakeDNSConfigExtend
|
2021-05-03 22:15:11 -04:00
|
|
|
|
|
|
|
cfgctx context.Context
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *NameServerConfig) UnmarshalJSON(data []byte) error {
|
2021-05-03 22:15:11 -04:00
|
|
|
var address cfgcommon.Address
|
2019-02-10 13:04:11 -05:00
|
|
|
if err := json.Unmarshal(data, &address); err == nil {
|
|
|
|
c.Address = &address
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var advanced struct {
|
2022-11-29 19:43:39 -05:00
|
|
|
Address *cfgcommon.Address `json:"address"`
|
|
|
|
ClientIP *cfgcommon.Address `json:"clientIp"`
|
|
|
|
Port uint16 `json:"port"`
|
|
|
|
Tag string `json:"tag"`
|
|
|
|
QueryStrategy string `json:"queryStrategy"`
|
|
|
|
CacheStrategy string `json:"cacheStrategy"`
|
|
|
|
FallbackStrategy string `json:"fallbackStrategy"`
|
|
|
|
SkipFallback bool `json:"skipFallback"`
|
|
|
|
Domains []string `json:"domains"`
|
|
|
|
ExpectIPs cfgcommon.StringList `json:"expectIps"`
|
2022-12-14 21:38:28 -05:00
|
|
|
FakeDNS FakeDNSConfigExtend `json:"fakedns"`
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
|
|
|
if err := json.Unmarshal(data, &advanced); err == nil {
|
|
|
|
c.Address = advanced.Address
|
2020-12-11 18:15:29 -05:00
|
|
|
c.ClientIP = advanced.ClientIP
|
2019-02-10 13:04:11 -05:00
|
|
|
c.Port = advanced.Port
|
2022-11-29 19:43:39 -05:00
|
|
|
c.Tag = advanced.Tag
|
|
|
|
c.QueryStrategy = advanced.QueryStrategy
|
|
|
|
c.CacheStrategy = advanced.CacheStrategy
|
|
|
|
c.FallbackStrategy = advanced.FallbackStrategy
|
2021-04-08 22:35:26 -04:00
|
|
|
c.SkipFallback = advanced.SkipFallback
|
2019-02-10 13:04:11 -05:00
|
|
|
c.Domains = advanced.Domains
|
2019-11-20 12:09:23 -05:00
|
|
|
c.ExpectIPs = advanced.ExpectIPs
|
2022-12-14 21:38:28 -05:00
|
|
|
c.FakeDNS = advanced.FakeDNS
|
2019-02-10 13:04:11 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return newError("failed to parse name server: ", string(data))
|
|
|
|
}
|
|
|
|
|
2021-09-07 04:13:58 -04:00
|
|
|
func toDomainMatchingType(t routercommon.Domain_Type) dns.DomainMatchingType {
|
2019-02-10 13:04:11 -05:00
|
|
|
switch t {
|
2021-09-07 04:13:58 -04:00
|
|
|
case routercommon.Domain_RootDomain:
|
2019-02-10 13:04:11 -05:00
|
|
|
return dns.DomainMatchingType_Subdomain
|
2021-09-07 04:13:58 -04:00
|
|
|
case routercommon.Domain_Full:
|
2019-02-10 13:04:11 -05:00
|
|
|
return dns.DomainMatchingType_Full
|
2021-09-07 04:13:58 -04:00
|
|
|
case routercommon.Domain_Plain:
|
2019-02-10 13:04:11 -05:00
|
|
|
return dns.DomainMatchingType_Keyword
|
2021-09-07 04:13:58 -04:00
|
|
|
case routercommon.Domain_Regex:
|
2019-02-10 13:04:11 -05:00
|
|
|
return dns.DomainMatchingType_Regex
|
|
|
|
default:
|
|
|
|
panic("unknown domain type")
|
|
|
|
}
|
|
|
|
}
|
2021-09-04 16:54:21 -04:00
|
|
|
|
2021-09-04 16:44:10 -04:00
|
|
|
func (c *NameServerConfig) BuildV5(ctx context.Context) (*dns.NameServer, error) {
|
|
|
|
c.cfgctx = ctx
|
|
|
|
return c.Build()
|
|
|
|
}
|
2019-02-10 13:04:11 -05:00
|
|
|
|
|
|
|
func (c *NameServerConfig) Build() (*dns.NameServer, error) {
|
2021-05-03 22:15:11 -04:00
|
|
|
cfgctx := c.cfgctx
|
|
|
|
|
2019-02-10 13:04:11 -05:00
|
|
|
if c.Address == nil {
|
|
|
|
return nil, newError("NameServer address is not specified.")
|
|
|
|
}
|
|
|
|
|
|
|
|
var domains []*dns.NameServer_PriorityDomain
|
2020-08-15 09:22:32 -04:00
|
|
|
var originalRules []*dns.NameServer_OriginalRule
|
2019-02-10 13:04:11 -05:00
|
|
|
|
2020-08-15 09:22:32 -04:00
|
|
|
for _, rule := range c.Domains {
|
2021-05-03 22:15:11 -04:00
|
|
|
parsedDomain, err := rule2.ParseDomainRule(cfgctx, rule)
|
2019-02-10 13:04:11 -05:00
|
|
|
if err != nil {
|
2020-08-15 09:22:32 -04:00
|
|
|
return nil, newError("invalid domain rule: ", rule).Base(err)
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, pd := range parsedDomain {
|
|
|
|
domains = append(domains, &dns.NameServer_PriorityDomain{
|
|
|
|
Type: toDomainMatchingType(pd.Type),
|
|
|
|
Domain: pd.Value,
|
|
|
|
})
|
|
|
|
}
|
2020-08-15 09:22:32 -04:00
|
|
|
originalRules = append(originalRules, &dns.NameServer_OriginalRule{
|
|
|
|
Rule: rule,
|
|
|
|
Size: uint32(len(parsedDomain)),
|
|
|
|
})
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
|
|
|
|
2021-05-03 22:15:11 -04:00
|
|
|
geoipList, err := rule2.ToCidrList(cfgctx, c.ExpectIPs)
|
2019-11-18 10:46:56 -05:00
|
|
|
if err != nil {
|
2020-12-17 19:36:34 -05:00
|
|
|
return nil, newError("invalid IP rule: ", c.ExpectIPs).Base(err)
|
2019-11-18 10:46:56 -05:00
|
|
|
}
|
|
|
|
|
2022-12-14 21:38:28 -05:00
|
|
|
var fakeDNS *fakedns.FakeDnsPoolMulti
|
|
|
|
if c.FakeDNS.FakeDNSConfig != nil {
|
|
|
|
fake, err := c.FakeDNS.FakeDNSConfig.Build()
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("failed to build fakedns").Base(err)
|
|
|
|
}
|
|
|
|
fakeDNS = fake
|
|
|
|
}
|
|
|
|
|
2020-12-11 18:15:29 -05:00
|
|
|
var myClientIP []byte
|
|
|
|
if c.ClientIP != nil {
|
|
|
|
if !c.ClientIP.Family().IsIP() {
|
|
|
|
return nil, newError("not an IP address:", c.ClientIP.String())
|
|
|
|
}
|
|
|
|
myClientIP = []byte(c.ClientIP.IP())
|
|
|
|
}
|
|
|
|
|
2022-11-29 19:43:39 -05:00
|
|
|
queryStrategy := new(dns.QueryStrategy)
|
|
|
|
switch strings.ToLower(c.QueryStrategy) {
|
|
|
|
case "useip", "use_ip", "use-ip":
|
|
|
|
*queryStrategy = dns.QueryStrategy_USE_IP
|
|
|
|
case "useip4", "useipv4", "use_ip4", "use_ipv4", "use_ip_v4", "use-ip4", "use-ipv4", "use-ip-v4":
|
|
|
|
*queryStrategy = dns.QueryStrategy_USE_IP4
|
|
|
|
case "useip6", "useipv6", "use_ip6", "use_ipv6", "use_ip_v6", "use-ip6", "use-ipv6", "use-ip-v6":
|
|
|
|
*queryStrategy = dns.QueryStrategy_USE_IP6
|
|
|
|
default:
|
|
|
|
queryStrategy = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
cacheStrategy := new(dns.CacheStrategy)
|
|
|
|
switch strings.ToLower(c.CacheStrategy) {
|
|
|
|
case "enabled":
|
|
|
|
*cacheStrategy = dns.CacheStrategy_CacheEnabled
|
|
|
|
case "disabled":
|
|
|
|
*cacheStrategy = dns.CacheStrategy_CacheDisabled
|
|
|
|
default:
|
|
|
|
cacheStrategy = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
fallbackStrategy := new(dns.FallbackStrategy)
|
|
|
|
switch strings.ToLower(c.FallbackStrategy) {
|
|
|
|
case "enabled":
|
|
|
|
*fallbackStrategy = dns.FallbackStrategy_Enabled
|
|
|
|
case "disabled":
|
|
|
|
*fallbackStrategy = dns.FallbackStrategy_Disabled
|
|
|
|
case "disabledifanymatch", "disabled_if_any_match", "disabled-if-any-match":
|
|
|
|
*fallbackStrategy = dns.FallbackStrategy_DisabledIfAnyMatch
|
|
|
|
default:
|
|
|
|
fallbackStrategy = nil
|
|
|
|
}
|
|
|
|
|
2019-02-10 13:04:11 -05:00
|
|
|
return &dns.NameServer{
|
|
|
|
Address: &net.Endpoint{
|
|
|
|
Network: net.Network_UDP,
|
|
|
|
Address: c.Address.Build(),
|
|
|
|
Port: uint32(c.Port),
|
|
|
|
},
|
2020-12-11 18:15:29 -05:00
|
|
|
ClientIp: myClientIP,
|
2022-11-29 19:43:39 -05:00
|
|
|
Tag: c.Tag,
|
2021-04-08 22:35:26 -04:00
|
|
|
SkipFallback: c.SkipFallback,
|
2022-11-29 19:43:39 -05:00
|
|
|
QueryStrategy: queryStrategy,
|
|
|
|
CacheStrategy: cacheStrategy,
|
|
|
|
FallbackStrategy: fallbackStrategy,
|
2019-02-10 13:04:11 -05:00
|
|
|
PrioritizedDomain: domains,
|
2019-11-18 10:46:56 -05:00
|
|
|
Geoip: geoipList,
|
2020-08-15 09:22:32 -04:00
|
|
|
OriginalRules: originalRules,
|
2022-12-14 21:38:28 -05:00
|
|
|
FakeDns: fakeDNS,
|
2019-02-10 13:04:11 -05:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-09-07 04:13:58 -04:00
|
|
|
var typeMap = map[routercommon.Domain_Type]dns.DomainMatchingType{
|
|
|
|
routercommon.Domain_Full: dns.DomainMatchingType_Full,
|
|
|
|
routercommon.Domain_RootDomain: dns.DomainMatchingType_Subdomain,
|
|
|
|
routercommon.Domain_Plain: dns.DomainMatchingType_Keyword,
|
|
|
|
routercommon.Domain_Regex: dns.DomainMatchingType_Regex,
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
|
|
|
|
2020-10-11 07:22:46 -04:00
|
|
|
// DNSConfig is a JSON serializable object for dns.Config.
|
2021-11-27 04:16:41 -05:00
|
|
|
type DNSConfig struct { // nolint: revive
|
2021-09-12 22:35:34 -04:00
|
|
|
Servers []*NameServerConfig `json:"servers"`
|
|
|
|
Hosts map[string]*HostAddress `json:"hosts"`
|
2022-12-14 21:38:28 -05:00
|
|
|
FakeDNS *FakeDNSConfig `json:"fakedns"`
|
2022-11-30 01:34:24 -05:00
|
|
|
DomainMatcher string `json:"domainMatcher"`
|
2021-09-12 22:35:34 -04:00
|
|
|
ClientIP *cfgcommon.Address `json:"clientIp"`
|
|
|
|
Tag string `json:"tag"`
|
|
|
|
QueryStrategy string `json:"queryStrategy"`
|
2022-11-29 19:43:39 -05:00
|
|
|
CacheStrategy string `json:"cacheStrategy"`
|
|
|
|
FallbackStrategy string `json:"fallbackStrategy"`
|
2021-09-12 22:35:34 -04:00
|
|
|
DisableCache bool `json:"disableCache"`
|
|
|
|
DisableFallback bool `json:"disableFallback"`
|
|
|
|
DisableFallbackIfMatch bool `json:"disableFallbackIfMatch"`
|
2021-10-28 06:34:19 -04:00
|
|
|
cfgctx context.Context
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
|
|
|
|
2021-04-10 09:56:08 -04:00
|
|
|
type HostAddress struct {
|
2021-05-03 22:15:11 -04:00
|
|
|
addr *cfgcommon.Address
|
|
|
|
addrs []*cfgcommon.Address
|
2021-04-10 09:56:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements encoding/json.Unmarshaler.UnmarshalJSON
|
|
|
|
func (h *HostAddress) UnmarshalJSON(data []byte) error {
|
2021-05-03 22:15:11 -04:00
|
|
|
addr := new(cfgcommon.Address)
|
|
|
|
var addrs []*cfgcommon.Address
|
2021-04-10 09:56:08 -04:00
|
|
|
switch {
|
|
|
|
case json.Unmarshal(data, &addr) == nil:
|
|
|
|
h.addr = addr
|
|
|
|
case json.Unmarshal(data, &addrs) == nil:
|
|
|
|
h.addrs = addrs
|
|
|
|
default:
|
|
|
|
return newError("invalid address")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-07 02:59:57 -04:00
|
|
|
func getHostMapping(ha *HostAddress) *dns.HostMapping {
|
2021-04-10 09:56:08 -04:00
|
|
|
if ha.addr != nil {
|
|
|
|
if ha.addr.Family().IsDomain() {
|
2021-09-07 02:59:57 -04:00
|
|
|
return &dns.HostMapping{
|
2021-04-10 09:56:08 -04:00
|
|
|
ProxiedDomain: ha.addr.Domain(),
|
|
|
|
}
|
|
|
|
}
|
2021-09-07 02:59:57 -04:00
|
|
|
return &dns.HostMapping{
|
2021-04-10 09:56:08 -04:00
|
|
|
Ip: [][]byte{ha.addr.IP()},
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
2020-11-21 16:05:01 -05:00
|
|
|
}
|
2021-04-10 09:56:08 -04:00
|
|
|
|
|
|
|
ips := make([][]byte, 0, len(ha.addrs))
|
|
|
|
for _, addr := range ha.addrs {
|
|
|
|
if addr.Family().IsDomain() {
|
2021-09-07 02:59:57 -04:00
|
|
|
return &dns.HostMapping{
|
2021-04-10 09:56:08 -04:00
|
|
|
ProxiedDomain: addr.Domain(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ips = append(ips, []byte(addr.IP()))
|
|
|
|
}
|
2021-09-07 02:59:57 -04:00
|
|
|
return &dns.HostMapping{
|
2021-04-10 09:56:08 -04:00
|
|
|
Ip: ips,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-04 16:54:21 -04:00
|
|
|
func (c *DNSConfig) BuildV5(ctx context.Context) (*dns.Config, error) {
|
|
|
|
c.cfgctx = ctx
|
|
|
|
return c.Build()
|
|
|
|
}
|
|
|
|
|
2019-02-10 13:04:11 -05:00
|
|
|
// Build implements Buildable
|
2020-10-11 07:22:46 -04:00
|
|
|
func (c *DNSConfig) Build() (*dns.Config, error) {
|
2021-09-04 16:54:21 -04:00
|
|
|
if c.cfgctx == nil {
|
|
|
|
c.cfgctx = cfgcommon.NewConfigureLoadingContext(context.Background())
|
|
|
|
|
|
|
|
geoloadername := platform.NewEnvFlag("v2ray.conf.geoloader").GetValue(func() string {
|
|
|
|
return "standard"
|
|
|
|
})
|
|
|
|
|
|
|
|
if loader, err := geodata.GetGeoDataLoader(geoloadername); err == nil {
|
|
|
|
cfgcommon.SetGeoDataLoader(c.cfgctx, loader)
|
|
|
|
} else {
|
|
|
|
return nil, newError("unable to create geo data loader ").Base(err)
|
|
|
|
}
|
2021-05-03 22:15:11 -04:00
|
|
|
}
|
|
|
|
|
2021-09-04 16:54:21 -04:00
|
|
|
cfgEnv := cfgcommon.GetConfigureLoadingEnvironment(c.cfgctx)
|
2021-05-03 22:15:11 -04:00
|
|
|
geoLoader := cfgEnv.GetGeoLoader()
|
|
|
|
|
2019-02-10 13:04:11 -05:00
|
|
|
config := &dns.Config{
|
2021-09-12 22:35:34 -04:00
|
|
|
Tag: c.Tag,
|
|
|
|
DisableCache: c.DisableCache,
|
|
|
|
DisableFallback: c.DisableFallback,
|
|
|
|
DisableFallbackIfMatch: c.DisableFallbackIfMatch,
|
2022-11-30 01:34:24 -05:00
|
|
|
DomainMatcher: c.DomainMatcher,
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.ClientIP != nil {
|
|
|
|
if !c.ClientIP.Family().IsIP() {
|
|
|
|
return nil, newError("not an IP address:", c.ClientIP.String())
|
|
|
|
}
|
|
|
|
config.ClientIp = []byte(c.ClientIP.IP())
|
|
|
|
}
|
|
|
|
|
2021-03-19 03:55:18 -04:00
|
|
|
config.QueryStrategy = dns.QueryStrategy_USE_IP
|
|
|
|
switch strings.ToLower(c.QueryStrategy) {
|
|
|
|
case "useip", "use_ip", "use-ip":
|
|
|
|
config.QueryStrategy = dns.QueryStrategy_USE_IP
|
|
|
|
case "useip4", "useipv4", "use_ip4", "use_ipv4", "use_ip_v4", "use-ip4", "use-ipv4", "use-ip-v4":
|
|
|
|
config.QueryStrategy = dns.QueryStrategy_USE_IP4
|
|
|
|
case "useip6", "useipv6", "use_ip6", "use_ipv6", "use_ip_v6", "use-ip6", "use-ipv6", "use-ip-v6":
|
|
|
|
config.QueryStrategy = dns.QueryStrategy_USE_IP6
|
|
|
|
}
|
|
|
|
|
2022-11-29 19:43:39 -05:00
|
|
|
config.CacheStrategy = dns.CacheStrategy_CacheEnabled
|
|
|
|
switch strings.ToLower(c.CacheStrategy) {
|
|
|
|
case "enabled":
|
|
|
|
config.CacheStrategy = dns.CacheStrategy_CacheEnabled
|
|
|
|
case "disabled":
|
|
|
|
config.CacheStrategy = dns.CacheStrategy_CacheDisabled
|
|
|
|
}
|
|
|
|
|
|
|
|
config.FallbackStrategy = dns.FallbackStrategy_Enabled
|
|
|
|
switch strings.ToLower(c.FallbackStrategy) {
|
|
|
|
case "enabled":
|
|
|
|
config.FallbackStrategy = dns.FallbackStrategy_Enabled
|
|
|
|
case "disabled":
|
|
|
|
config.FallbackStrategy = dns.FallbackStrategy_Disabled
|
|
|
|
case "disabledifanymatch", "disabled_if_any_match", "disabled-if-any-match":
|
|
|
|
config.FallbackStrategy = dns.FallbackStrategy_DisabledIfAnyMatch
|
|
|
|
}
|
|
|
|
|
2019-02-10 13:04:11 -05:00
|
|
|
for _, server := range c.Servers {
|
2021-09-04 16:54:21 -04:00
|
|
|
server.cfgctx = c.cfgctx
|
2019-02-10 13:04:11 -05:00
|
|
|
ns, err := server.Build()
|
|
|
|
if err != nil {
|
2020-12-17 19:36:34 -05:00
|
|
|
return nil, newError("failed to build nameserver").Base(err)
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
|
|
|
config.NameServer = append(config.NameServer, ns)
|
|
|
|
}
|
|
|
|
|
2021-04-10 09:56:08 -04:00
|
|
|
if c.Hosts != nil {
|
2021-09-07 02:59:57 -04:00
|
|
|
mappings := make([]*dns.HostMapping, 0, 20)
|
2021-04-11 00:46:02 -04:00
|
|
|
|
|
|
|
domains := make([]string, 0, len(c.Hosts))
|
|
|
|
for domain := range c.Hosts {
|
|
|
|
domains = append(domains, domain)
|
|
|
|
}
|
|
|
|
sort.Strings(domains)
|
|
|
|
|
|
|
|
for _, domain := range domains {
|
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(domain, "domain:"):
|
|
|
|
domainName := domain[7:]
|
|
|
|
if len(domainName) == 0 {
|
|
|
|
return nil, newError("empty domain type of rule: ", domain)
|
|
|
|
}
|
|
|
|
mapping := getHostMapping(c.Hosts[domain])
|
|
|
|
mapping.Type = dns.DomainMatchingType_Subdomain
|
|
|
|
mapping.Domain = domainName
|
|
|
|
mappings = append(mappings, mapping)
|
|
|
|
|
|
|
|
case strings.HasPrefix(domain, "geosite:"):
|
|
|
|
listName := domain[8:]
|
|
|
|
if len(listName) == 0 {
|
|
|
|
return nil, newError("empty geosite rule: ", domain)
|
|
|
|
}
|
2021-05-04 19:28:09 -04:00
|
|
|
geositeList, err := geoLoader.LoadGeoSite(listName)
|
2021-04-11 00:46:02 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, newError("failed to load geosite: ", listName).Base(err)
|
|
|
|
}
|
|
|
|
for _, d := range geositeList {
|
|
|
|
mapping := getHostMapping(c.Hosts[domain])
|
|
|
|
mapping.Type = typeMap[d.Type]
|
|
|
|
mapping.Domain = d.Value
|
|
|
|
mappings = append(mappings, mapping)
|
|
|
|
}
|
|
|
|
|
|
|
|
case strings.HasPrefix(domain, "regexp:"):
|
|
|
|
regexpVal := domain[7:]
|
|
|
|
if len(regexpVal) == 0 {
|
|
|
|
return nil, newError("empty regexp type of rule: ", domain)
|
|
|
|
}
|
|
|
|
mapping := getHostMapping(c.Hosts[domain])
|
|
|
|
mapping.Type = dns.DomainMatchingType_Regex
|
|
|
|
mapping.Domain = regexpVal
|
|
|
|
mappings = append(mappings, mapping)
|
|
|
|
|
|
|
|
case strings.HasPrefix(domain, "keyword:"):
|
|
|
|
keywordVal := domain[8:]
|
|
|
|
if len(keywordVal) == 0 {
|
|
|
|
return nil, newError("empty keyword type of rule: ", domain)
|
|
|
|
}
|
|
|
|
mapping := getHostMapping(c.Hosts[domain])
|
|
|
|
mapping.Type = dns.DomainMatchingType_Keyword
|
|
|
|
mapping.Domain = keywordVal
|
|
|
|
mappings = append(mappings, mapping)
|
|
|
|
|
|
|
|
case strings.HasPrefix(domain, "full:"):
|
|
|
|
fullVal := domain[5:]
|
|
|
|
if len(fullVal) == 0 {
|
|
|
|
return nil, newError("empty full domain type of rule: ", domain)
|
|
|
|
}
|
|
|
|
mapping := getHostMapping(c.Hosts[domain])
|
|
|
|
mapping.Type = dns.DomainMatchingType_Full
|
|
|
|
mapping.Domain = fullVal
|
|
|
|
mappings = append(mappings, mapping)
|
|
|
|
|
|
|
|
case strings.HasPrefix(domain, "dotless:"):
|
|
|
|
mapping := getHostMapping(c.Hosts[domain])
|
|
|
|
mapping.Type = dns.DomainMatchingType_Regex
|
|
|
|
switch substr := domain[8:]; {
|
|
|
|
case substr == "":
|
|
|
|
mapping.Domain = "^[^.]*$"
|
|
|
|
case !strings.Contains(substr, "."):
|
|
|
|
mapping.Domain = "^[^.]*" + substr + "[^.]*$"
|
|
|
|
default:
|
|
|
|
return nil, newError("substr in dotless rule should not contain a dot: ", substr)
|
|
|
|
}
|
|
|
|
mappings = append(mappings, mapping)
|
|
|
|
|
|
|
|
case strings.HasPrefix(domain, "ext:"):
|
|
|
|
kv := strings.Split(domain[4:], ":")
|
|
|
|
if len(kv) != 2 {
|
|
|
|
return nil, newError("invalid external resource: ", domain)
|
|
|
|
}
|
|
|
|
filename := kv[0]
|
|
|
|
list := kv[1]
|
2021-05-04 19:28:09 -04:00
|
|
|
geositeList, err := geoLoader.LoadGeoSiteWithAttr(filename, list)
|
2021-04-11 00:46:02 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, newError("failed to load domain list: ", list, " from ", filename).Base(err)
|
|
|
|
}
|
|
|
|
for _, d := range geositeList {
|
|
|
|
mapping := getHostMapping(c.Hosts[domain])
|
|
|
|
mapping.Type = typeMap[d.Type]
|
|
|
|
mapping.Domain = d.Value
|
|
|
|
mappings = append(mappings, mapping)
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
mapping := getHostMapping(c.Hosts[domain])
|
|
|
|
mapping.Type = dns.DomainMatchingType_Full
|
|
|
|
mapping.Domain = domain
|
|
|
|
mappings = append(mappings, mapping)
|
|
|
|
}
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
2021-04-11 00:46:02 -04:00
|
|
|
|
|
|
|
config.StaticHosts = append(config.StaticHosts, mappings...)
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
|
|
|
|
2022-12-14 21:38:28 -05:00
|
|
|
if c.FakeDNS != nil {
|
|
|
|
fakeDNS, err := c.FakeDNS.Build()
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("failed to build fakedns").Base(err)
|
|
|
|
}
|
|
|
|
config.FakeDns = fakeDNS
|
|
|
|
}
|
|
|
|
|
2019-02-10 13:04:11 -05:00
|
|
|
return config, nil
|
|
|
|
}
|