Xray-core/app/dns/hosts.go

115 lines
3.2 KiB
Go
Raw Normal View History

2020-11-25 11:01:53 +00:00
package dns
import (
2020-12-04 01:36:16 +00:00
"github.com/xtls/xray-core/common"
2021-03-24 15:01:20 +00:00
"github.com/xtls/xray-core/common/matcher/str"
2020-12-04 01:36:16 +00:00
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/features"
"github.com/xtls/xray-core/features/dns"
2020-11-25 11:01:53 +00:00
)
// StaticHosts represents static domain-ip mapping in DNS server.
type StaticHosts struct {
ips [][]net.Address
2021-03-24 15:01:20 +00:00
matchers *str.MatcherGroup
2020-11-25 11:01:53 +00:00
}
// NewStaticHosts creates a new StaticHosts instance.
func NewStaticHosts(hosts []*Config_HostMapping, legacy map[string]*net.IPOrDomain) (*StaticHosts, error) {
2021-03-24 15:01:20 +00:00
g := new(str.MatcherGroup)
2020-11-25 11:01:53 +00:00
sh := &StaticHosts{
ips: make([][]net.Address, len(hosts)+len(legacy)+16),
matchers: g,
}
if legacy != nil {
features.PrintDeprecatedFeatureWarning("simple host mapping")
for domain, ip := range legacy {
2021-03-24 15:01:20 +00:00
matcher, err := str.Full.New(domain)
2020-11-25 11:01:53 +00:00
common.Must(err)
id := g.Add(matcher)
address := ip.AsAddress()
if address.Family().IsDomain() {
return nil, newError("invalid domain address in static hosts: ", address.Domain()).AtWarning()
}
sh.ips[id] = []net.Address{address}
}
}
for _, mapping := range hosts {
matcher, err := toStrMatcher(mapping.Type, mapping.Domain)
if err != nil {
return nil, newError("failed to create domain matcher").Base(err)
}
id := g.Add(matcher)
ips := make([]net.Address, 0, len(mapping.Ip)+1)
switch {
case len(mapping.ProxiedDomain) > 0:
ips = append(ips, net.DomainAddress(mapping.ProxiedDomain))
2020-11-25 11:01:53 +00:00
case len(mapping.Ip) > 0:
for _, ip := range mapping.Ip {
addr := net.IPAddress(ip)
if addr == nil {
return nil, newError("invalid IP address in static hosts: ", ip).AtWarning()
}
ips = append(ips, addr)
}
default:
return nil, newError("neither IP address nor proxied domain specified for domain: ", mapping.Domain).AtWarning()
}
sh.ips[id] = ips
}
return sh, nil
}
2021-04-10 03:36:58 +00:00
func filterIP(ips []net.Address, option *dns.IPOption) []net.Address {
2020-11-25 11:01:53 +00:00
filtered := make([]net.Address, 0, len(ips))
for _, ip := range ips {
if (ip.Family().IsIPv4() && option.IPv4Enable) || (ip.Family().IsIPv6() && option.IPv6Enable) {
filtered = append(filtered, ip)
}
}
return filtered
}
2021-03-18 15:24:24 +00:00
func (h *StaticHosts) lookupInternal(domain string) []net.Address {
var ips []net.Address
for _, id := range h.matchers.Match(domain) {
2020-11-25 11:01:53 +00:00
ips = append(ips, h.ips[id]...)
}
if len(ips) == 1 && ips[0].Family().IsDomain() {
return ips
}
2021-03-18 15:24:24 +00:00
return ips
}
2021-04-10 03:36:58 +00:00
func (h *StaticHosts) lookup(domain string, option *dns.IPOption, maxDepth int) []net.Address {
2021-03-18 15:24:24 +00:00
switch addrs := h.lookupInternal(domain); {
case len(addrs) == 0: // Not recorded in static hosts, return nil
return nil
case len(addrs) == 1 && addrs[0].Family().IsDomain(): // Try to unwrap domain
newError("found replaced domain: ", domain, " -> ", addrs[0].Domain(), ". Try to unwrap it").AtDebug().WriteToLog()
2021-03-18 15:24:24 +00:00
if maxDepth > 0 {
unwrapped := h.lookup(addrs[0].Domain(), option, maxDepth-1)
if unwrapped != nil {
return unwrapped
}
}
return addrs
default: // IP record found, return a non-nil IP array
return filterIP(addrs, option)
}
}
// Lookup returns IP addresses or proxied domain for the given domain, if exists in this StaticHosts.
2021-04-10 03:36:58 +00:00
func (h *StaticHosts) Lookup(domain string, option *dns.IPOption) []net.Address {
2021-03-18 15:24:24 +00:00
return h.lookup(domain, option, 5)
2020-11-25 11:01:53 +00:00
}