2019-02-10 13:04:11 -05:00
|
|
|
package conf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
2021-02-16 15:31:50 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v4/app/dns"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/app/router"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/net"
|
2019-02-10 13:04:11 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type NameServerConfig struct {
|
2019-11-20 12:09:23 -05:00
|
|
|
Address *Address
|
2020-12-11 18:15:29 -05:00
|
|
|
ClientIP *Address
|
2019-11-20 12:09:23 -05:00
|
|
|
Port uint16
|
|
|
|
Domains []string
|
|
|
|
ExpectIPs StringList
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *NameServerConfig) UnmarshalJSON(data []byte) error {
|
|
|
|
var address Address
|
|
|
|
if err := json.Unmarshal(data, &address); err == nil {
|
|
|
|
c.Address = &address
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var advanced struct {
|
2019-11-20 12:09:23 -05:00
|
|
|
Address *Address `json:"address"`
|
2020-12-11 18:15:29 -05:00
|
|
|
ClientIP *Address `json:"clientIp"`
|
2019-11-20 12:09:23 -05:00
|
|
|
Port uint16 `json:"port"`
|
|
|
|
Domains []string `json:"domains"`
|
|
|
|
ExpectIPs StringList `json:"expectIps"`
|
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
|
|
|
|
c.Domains = advanced.Domains
|
2019-11-20 12:09:23 -05:00
|
|
|
c.ExpectIPs = advanced.ExpectIPs
|
2019-02-10 13:04:11 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return newError("failed to parse name server: ", string(data))
|
|
|
|
}
|
|
|
|
|
|
|
|
func toDomainMatchingType(t router.Domain_Type) dns.DomainMatchingType {
|
|
|
|
switch t {
|
|
|
|
case router.Domain_Domain:
|
|
|
|
return dns.DomainMatchingType_Subdomain
|
|
|
|
case router.Domain_Full:
|
|
|
|
return dns.DomainMatchingType_Full
|
|
|
|
case router.Domain_Plain:
|
|
|
|
return dns.DomainMatchingType_Keyword
|
|
|
|
case router.Domain_Regex:
|
|
|
|
return dns.DomainMatchingType_Regex
|
|
|
|
default:
|
|
|
|
panic("unknown domain type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *NameServerConfig) Build() (*dns.NameServer, error) {
|
|
|
|
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 {
|
|
|
|
parsedDomain, err := parseDomainRule(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
|
|
|
}
|
|
|
|
|
2019-11-20 12:09:23 -05:00
|
|
|
geoipList, err := toCidrList(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
|
|
|
}
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
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,
|
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,
|
2019-02-10 13:04:11 -05:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var typeMap = map[router.Domain_Type]dns.DomainMatchingType{
|
|
|
|
router.Domain_Full: dns.DomainMatchingType_Full,
|
|
|
|
router.Domain_Domain: dns.DomainMatchingType_Subdomain,
|
|
|
|
router.Domain_Plain: dns.DomainMatchingType_Keyword,
|
|
|
|
router.Domain_Regex: dns.DomainMatchingType_Regex,
|
|
|
|
}
|
|
|
|
|
2020-10-11 07:22:46 -04:00
|
|
|
// DNSConfig is a JSON serializable object for dns.Config.
|
|
|
|
type DNSConfig struct {
|
2021-03-19 03:55:18 -04:00
|
|
|
Servers []*NameServerConfig `json:"servers"`
|
|
|
|
Hosts map[string]*Address `json:"hosts"`
|
|
|
|
ClientIP *Address `json:"clientIp"`
|
|
|
|
Tag string `json:"tag"`
|
|
|
|
QueryStrategy string `json:"queryStrategy"`
|
|
|
|
DisableCache bool `json:"disableCache"`
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func getHostMapping(addr *Address) *dns.Config_HostMapping {
|
|
|
|
if addr.Family().IsIP() {
|
|
|
|
return &dns.Config_HostMapping{
|
|
|
|
Ip: [][]byte{[]byte(addr.IP())},
|
|
|
|
}
|
2020-11-21 16:05:01 -05:00
|
|
|
}
|
|
|
|
return &dns.Config_HostMapping{
|
|
|
|
ProxiedDomain: addr.Domain(),
|
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) {
|
2019-02-10 13:04:11 -05:00
|
|
|
config := &dns.Config{
|
2021-02-24 04:12:44 -05:00
|
|
|
Tag: c.Tag,
|
2021-02-24 03:03:50 -05:00
|
|
|
DisableCache: c.DisableCache,
|
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
|
|
|
|
}
|
|
|
|
|
2019-02-10 13:04:11 -05:00
|
|
|
for _, server := range c.Servers {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Hosts != nil && len(c.Hosts) > 0 {
|
|
|
|
domains := make([]string, 0, len(c.Hosts))
|
|
|
|
for domain := range c.Hosts {
|
|
|
|
domains = append(domains, domain)
|
|
|
|
}
|
|
|
|
sort.Strings(domains)
|
2020-10-11 07:22:46 -04:00
|
|
|
|
2019-02-10 13:04:11 -05:00
|
|
|
for _, domain := range domains {
|
|
|
|
addr := c.Hosts[domain]
|
|
|
|
var mappings []*dns.Config_HostMapping
|
2020-10-11 07:22:46 -04:00
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(domain, "domain:"):
|
2020-12-17 19:36:34 -05:00
|
|
|
domainName := domain[7:]
|
|
|
|
if len(domainName) == 0 {
|
|
|
|
return nil, newError("empty domain type of rule: ", domain)
|
|
|
|
}
|
2019-02-10 13:04:11 -05:00
|
|
|
mapping := getHostMapping(addr)
|
|
|
|
mapping.Type = dns.DomainMatchingType_Subdomain
|
2020-12-17 19:36:34 -05:00
|
|
|
mapping.Domain = domainName
|
2019-02-10 13:04:11 -05:00
|
|
|
mappings = append(mappings, mapping)
|
2020-10-11 07:22:46 -04:00
|
|
|
|
|
|
|
case strings.HasPrefix(domain, "geosite:"):
|
2020-12-17 19:36:34 -05:00
|
|
|
listName := domain[8:]
|
|
|
|
if len(listName) == 0 {
|
|
|
|
return nil, newError("empty geosite rule: ", domain)
|
|
|
|
}
|
|
|
|
domains, err := loadGeositeWithAttr("geosite.dat", listName)
|
2019-02-10 13:04:11 -05:00
|
|
|
if err != nil {
|
2020-12-17 19:36:34 -05:00
|
|
|
return nil, newError("failed to load geosite: ", listName).Base(err)
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
|
|
|
for _, d := range domains {
|
|
|
|
mapping := getHostMapping(addr)
|
|
|
|
mapping.Type = typeMap[d.Type]
|
|
|
|
mapping.Domain = d.Value
|
|
|
|
mappings = append(mappings, mapping)
|
|
|
|
}
|
2020-10-11 07:22:46 -04:00
|
|
|
|
|
|
|
case strings.HasPrefix(domain, "regexp:"):
|
2020-12-17 19:36:34 -05:00
|
|
|
regexpVal := domain[7:]
|
|
|
|
if len(regexpVal) == 0 {
|
|
|
|
return nil, newError("empty regexp type of rule: ", domain)
|
|
|
|
}
|
2019-02-26 15:04:00 -05:00
|
|
|
mapping := getHostMapping(addr)
|
|
|
|
mapping.Type = dns.DomainMatchingType_Regex
|
2020-12-17 19:36:34 -05:00
|
|
|
mapping.Domain = regexpVal
|
2019-02-26 15:04:00 -05:00
|
|
|
mappings = append(mappings, mapping)
|
2020-10-11 07:22:46 -04:00
|
|
|
|
|
|
|
case strings.HasPrefix(domain, "keyword:"):
|
2020-12-17 19:36:34 -05:00
|
|
|
keywordVal := domain[8:]
|
|
|
|
if len(keywordVal) == 0 {
|
|
|
|
return nil, newError("empty keyword type of rule: ", domain)
|
|
|
|
}
|
2019-02-26 15:04:00 -05:00
|
|
|
mapping := getHostMapping(addr)
|
|
|
|
mapping.Type = dns.DomainMatchingType_Keyword
|
2020-12-17 19:36:34 -05:00
|
|
|
mapping.Domain = keywordVal
|
2019-02-26 15:32:07 -05:00
|
|
|
mappings = append(mappings, mapping)
|
2020-10-11 07:22:46 -04:00
|
|
|
|
|
|
|
case strings.HasPrefix(domain, "full:"):
|
2020-12-17 19:36:34 -05:00
|
|
|
fullVal := domain[5:]
|
|
|
|
if len(fullVal) == 0 {
|
|
|
|
return nil, newError("empty full domain type of rule: ", domain)
|
|
|
|
}
|
2019-02-26 15:32:07 -05:00
|
|
|
mapping := getHostMapping(addr)
|
|
|
|
mapping.Type = dns.DomainMatchingType_Full
|
2020-12-17 19:36:34 -05:00
|
|
|
mapping.Domain = fullVal
|
2020-08-15 11:42:04 -04:00
|
|
|
mappings = append(mappings, mapping)
|
2020-10-11 07:22:46 -04:00
|
|
|
|
|
|
|
case strings.HasPrefix(domain, "dotless:"):
|
2020-08-15 11:42:04 -04:00
|
|
|
mapping := getHostMapping(addr)
|
|
|
|
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)
|
|
|
|
}
|
2019-02-26 15:04:00 -05:00
|
|
|
mappings = append(mappings, mapping)
|
2020-10-11 07:22:46 -04:00
|
|
|
|
|
|
|
case strings.HasPrefix(domain, "ext:"):
|
2019-11-02 03:49:29 -04:00
|
|
|
kv := strings.Split(domain[4:], ":")
|
|
|
|
if len(kv) != 2 {
|
|
|
|
return nil, newError("invalid external resource: ", domain)
|
|
|
|
}
|
|
|
|
filename := kv[0]
|
2020-12-17 19:36:34 -05:00
|
|
|
list := kv[1]
|
|
|
|
domains, err := loadGeositeWithAttr(filename, list)
|
2019-11-02 03:49:29 -04:00
|
|
|
if err != nil {
|
2020-12-17 19:36:34 -05:00
|
|
|
return nil, newError("failed to load domain list: ", list, " from ", filename).Base(err)
|
2019-11-02 03:49:29 -04:00
|
|
|
}
|
|
|
|
for _, d := range domains {
|
|
|
|
mapping := getHostMapping(addr)
|
|
|
|
mapping.Type = typeMap[d.Type]
|
|
|
|
mapping.Domain = d.Value
|
|
|
|
mappings = append(mappings, mapping)
|
|
|
|
}
|
2020-10-11 07:22:46 -04:00
|
|
|
|
|
|
|
default:
|
2019-02-10 13:04:11 -05:00
|
|
|
mapping := getHostMapping(addr)
|
|
|
|
mapping.Type = dns.DomainMatchingType_Full
|
|
|
|
mapping.Domain = domain
|
|
|
|
mappings = append(mappings, mapping)
|
|
|
|
}
|
|
|
|
|
|
|
|
config.StaticHosts = append(config.StaticHosts, mappings...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|