| 1 | // +build !confonly |
| 2 | |
| 3 | package dnsscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/ |
| 4 | |
| 5 | import ( |
| 6 | "v2ray.com/core/commonscip-go gomod v2ray.com/core . `v2ray.com/core/common`/" |
| 7 | "v2ray.com/core/common/netscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/" |
| 8 | "v2ray.com/core/common/strmatcherscip-go gomod v2ray.com/core . `v2ray.com/core/common/strmatcher`/" |
| 9 | "v2ray.com/core/featuresscip-go gomod v2ray.com/core . `v2ray.com/core/features`/" |
| 10 | ) |
| 11 | |
| 12 | // StaticHosts represents static domain-ip mapping in DNS server. |
| 13 | type StaticHostsscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/StaticHosts# struct { |
| 14 | ipsscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/StaticHosts#ips. [][]netscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/.Addressscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/Address# |
| 15 | matchersscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/StaticHosts#matchers. *strmatcherscip-go gomod v2ray.com/core . `v2ray.com/core/common/strmatcher`/.MatcherGroupscip-go gomod v2ray.com/core . `v2ray.com/core/common/strmatcher`/MatcherGroup# |
| 16 | } |
| 17 | |
| 18 | // NewStaticHosts creates a new StaticHosts instance. |
| 19 | func NewStaticHostsscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/NewStaticHosts().(hosts []*Config_HostMappingscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Config_HostMapping#, legacy map[string]*netscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/.IPOrDomainscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/IPOrDomain#) (*StaticHostsscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/StaticHosts#, error) { |
| 20 | g := new(strmatcherscip-go gomod v2ray.com/core . `v2ray.com/core/common/strmatcher`/.MatcherGroupscip-go gomod v2ray.com/core . `v2ray.com/core/common/strmatcher`/MatcherGroup#) |
| 21 | sh := &StaticHostsscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/StaticHosts#{ |
| 22 | ipsscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/StaticHosts#ips.: make([][]netscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/.Addressscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/Address#, len(hosts)+len(legacy)+16), |
| 23 | matchersscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/StaticHosts#matchers.: g, |
| 24 | } |
| 25 | |
| 26 | if legacy != nil { |
| 27 | featuresscip-go gomod v2ray.com/core . `v2ray.com/core/features`/.PrintDeprecatedFeatureWarningscip-go gomod v2ray.com/core . `v2ray.com/core/features`/PrintDeprecatedFeatureWarning().("simple host mapping") |
| 28 | |
| 29 | for domain, ip := range legacy { |
| 30 | matcher, err := strmatcherscip-go gomod v2ray.com/core . `v2ray.com/core/common/strmatcher`/.Fullscip-go gomod v2ray.com/core . `v2ray.com/core/common/strmatcher`/Full..Newscip-go gomod v2ray.com/core . `v2ray.com/core/common/strmatcher`/Type#New().(domain) |
| 31 | commonscip-go gomod v2ray.com/core . `v2ray.com/core/common`/.Mustscip-go gomod v2ray.com/core . `v2ray.com/core/common`/Must().(err) |
| 32 | id := g.Addscip-go gomod v2ray.com/core . `v2ray.com/core/common/strmatcher`/MatcherGroup#Add().(matcher) |
| 33 | |
| 34 | address := ip.AsAddressscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/IPOrDomain#AsAddress().() |
| 35 | if address.Familyscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/Address#Family.().IsDomainscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/AddressFamily#IsDomain().() { |
| 36 | return nil, newErrorscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/newError().("invalid domain address in static hosts: ", address.Domainscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/Address#Domain.()).AtWarningscip-go gomod v2ray.com/core . `v2ray.com/core/common/errors`/Error#AtWarning().() |
| 37 | } |
| 38 | |
| 39 | sh.ipsscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/StaticHosts#ips.[id] = []netscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/.Addressscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/Address#{address} |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | for _, mapping := range hosts { |
| 44 | matcher, err := toStrMatcherscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/toStrMatcher().(mapping.Typescip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Config_HostMapping#Type., mapping.Domainscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Config_HostMapping#Domain.) |
| 45 | if err != nil { |
| 46 | return nil, newErrorscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/newError().("failed to create domain matcher").Basescip-go gomod v2ray.com/core . `v2ray.com/core/common/errors`/Error#Base().(err) |
| 47 | } |
| 48 | id := g.Addscip-go gomod v2ray.com/core . `v2ray.com/core/common/strmatcher`/MatcherGroup#Add().(matcher) |
| 49 | ips := make([]netscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/.Addressscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/Address#, 0, len(mapping.Ipscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Config_HostMapping#Ip.)+1) |
| 50 | switch { |
| 51 | case len(mapping.Ipscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Config_HostMapping#Ip.) > 0: |
| 52 | for _, ip := range mapping.Ipscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Config_HostMapping#Ip. { |
| 53 | addr := netscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/.IPAddressscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/IPAddress().(ip) |
| 54 | if addr == nil { |
| 55 | return nil, newErrorscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/newError().("invalid IP address in static hosts: ", ip).AtWarningscip-go gomod v2ray.com/core . `v2ray.com/core/common/errors`/Error#AtWarning().() |
| 56 | } |
| 57 | ips = append(ips, addr) |
| 58 | } |
| 59 | |
| 60 | case len(mapping.ProxiedDomainscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Config_HostMapping#ProxiedDomain.) > 0: |
| 61 | ips = append(ips, netscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/.DomainAddressscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/DomainAddress().(mapping.ProxiedDomainscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Config_HostMapping#ProxiedDomain.)) |
| 62 | |
| 63 | default: |
| 64 | return nil, newErrorscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/newError().("neither IP address nor proxied domain specified for domain: ", mapping.Domainscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/Config_HostMapping#Domain.).AtWarningscip-go gomod v2ray.com/core . `v2ray.com/core/common/errors`/Error#AtWarning().() |
| 65 | } |
| 66 | |
| 67 | // Special handling for localhost IPv6. This is a dirty workaround as JSON config supports only single IP mapping. |
| 68 | if len(ips) == 1 && ips[0] == netscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/.LocalHostIPscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/LocalHostIP. { |
| 69 | ips = append(ips, netscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/.LocalHostIPv6scip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/LocalHostIPv6.) |
| 70 | } |
| 71 | |
| 72 | sh.ipsscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/StaticHosts#ips.[id] = ips |
| 73 | } |
| 74 | |
| 75 | return sh, nil |
| 76 | } |
| 77 | |
| 78 | func filterIPscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/filterIP().(ips []netscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/.Addressscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/Address#, option IPOptionscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/IPOption#) []netscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/.Addressscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/Address# { |
| 79 | filtered := make([]netscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/.Addressscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/Address#, 0, len(ips)) |
| 80 | for _, ip := range ips { |
| 81 | if (ip.Familyscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/Address#Family.().IsIPv4scip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/AddressFamily#IsIPv4().() && option.IPv4Enablescip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/IPOption#IPv4Enable.) || (ip.Familyscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/Address#Family.().IsIPv6scip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/AddressFamily#IsIPv6().() && option.IPv6Enablescip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/IPOption#IPv6Enable.) { |
| 82 | filtered = append(filtered, ip) |
| 83 | } |
| 84 | } |
| 85 | return filtered |
| 86 | } |
| 87 | |
| 88 | func (h *StaticHostsscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/StaticHosts#) lookupInternalscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/StaticHosts#lookupInternal().(domain string) []netscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/.Addressscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/Address# { |
| 89 | var ips []netscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/.Addressscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/Address# |
| 90 | for _, id := range h.matchersscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/StaticHosts#matchers..Matchscip-go gomod v2ray.com/core . `v2ray.com/core/common/strmatcher`/MatcherGroup#Match().(domain) { |
| 91 | ips = append(ips, h.ipsscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/StaticHosts#ips.[id]...) |
| 92 | } |
| 93 | return ips |
| 94 | } |
| 95 | |
| 96 | func (h *StaticHostsscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/StaticHosts#) lookupscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/StaticHosts#lookup().(domain string, option IPOptionscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/IPOption#, maxDepth int) []netscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/.Addressscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/Address# { |
| 97 | switch addrs := h.lookupInternalscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/StaticHosts#lookupInternal().(domain); { |
| 98 | case len(addrs) == 0: // Not recorded in static hosts, return nil |
| 99 | return nil |
| 100 | case len(addrs) == 1 && addrs[0].Familyscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/Address#Family.().IsDomainscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/AddressFamily#IsDomain().(): // Try to unwrap domain |
| 101 | if maxDepth > 0 { |
| 102 | unwrapped := h.lookupscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/StaticHosts#lookup().(addrs[0].Domainscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/Address#Domain.(), option, maxDepth-1) |
| 103 | if unwrapped != nil { |
| 104 | return unwrapped |
| 105 | } |
| 106 | } |
| 107 | return addrs |
| 108 | default: // IP record found, return a non-nil IP array |
| 109 | return filterIPscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/filterIP().(addrs, option) |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // Lookup returns IP addresses or proxied domain for the given domain, if exists in this StaticHosts. |
| 114 | func (h *StaticHostsscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/StaticHosts#) Lookupscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/StaticHosts#Lookup().(domain string, option IPOptionscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/IPOption#) []netscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/.Addressscip-go gomod v2ray.com/core . `v2ray.com/core/common/net`/Address# { |
| 115 | return h.lookupscip-go gomod v2ray.com/core . `v2ray.com/core/app/dns`/StaticHosts#lookup().(domain, option, 5) |
| 116 | } |