mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-12-18 13:14:36 +03:00
Some checks failed
Build and Release / build (amd64, openbsd, ) (push) Has been cancelled
Build and Release / build (amd64, windows, ) (push) Has been cancelled
Build and Release / build (arm, 5, linux) (push) Has been cancelled
Build and Release / build (arm, 6, linux) (push) Has been cancelled
Build and Release / build (arm, 7, freebsd) (push) Has been cancelled
Build and Release / build (arm, 7, linux) (push) Has been cancelled
Build and Release / build (arm, 7, openbsd) (push) Has been cancelled
Build and Release / build (arm, 7, windows) (push) Has been cancelled
Build and Release / build (arm64, android) (push) Has been cancelled
Build and Release / build (arm64, darwin) (push) Has been cancelled
Build and Release / build (arm64, freebsd) (push) Has been cancelled
Build and Release / build (arm64, linux) (push) Has been cancelled
Build and Release / build (arm64, openbsd) (push) Has been cancelled
Build and Release / build (arm64, windows) (push) Has been cancelled
Build and Release / build (loong64, linux) (push) Has been cancelled
Build and Release / build (mips, linux) (push) Has been cancelled
Build and Release / build (mips64, linux) (push) Has been cancelled
Build and Release / build (mips64le, linux) (push) Has been cancelled
Build and Release / build (mipsle, linux) (push) Has been cancelled
Build and Release / build (ppc64, linux) (push) Has been cancelled
Build and Release / build (ppc64le, linux) (push) Has been cancelled
Build and Release / build (riscv64, linux) (push) Has been cancelled
Build and Release / build (s390x, linux) (push) Has been cancelled
Build docker image / build-image (push) Has been cancelled
Build and Release for Windows 7 / build (win7-32, 386, windows) (push) Has been cancelled
Build and Release for Windows 7 / build (win7-64, amd64, windows) (push) Has been cancelled
Build and Release / build (386, freebsd, ) (push) Has been cancelled
Build and Release / build (386, linux, ) (push) Has been cancelled
Build and Release / build (386, openbsd, ) (push) Has been cancelled
Build and Release / build (386, windows, ) (push) Has been cancelled
Build and Release / build (amd64, android, android-amd64) (push) Has been cancelled
Build and Release / build (amd64, darwin, ) (push) Has been cancelled
Build and Release / build (amd64, freebsd, ) (push) Has been cancelled
Build and Release / build (amd64, linux, ) (push) Has been cancelled
Scheduled assets update / geodat (push) Has been cancelled
Test / test (macos-latest) (push) Has been cancelled
Test / test (ubuntu-latest) (push) Has been cancelled
Test / test (windows-latest) (push) Has been cancelled
96 lines
2.7 KiB
Go
96 lines
2.7 KiB
Go
package dns
|
|
|
|
import (
|
|
"context"
|
|
"github.com/xtls/xray-core/common/errors"
|
|
"github.com/xtls/xray-core/common/net"
|
|
"github.com/xtls/xray-core/common/strmatcher"
|
|
"github.com/xtls/xray-core/features/dns"
|
|
)
|
|
|
|
// StaticHosts represents static domain-ip mapping in DNS server.
|
|
type StaticHosts struct {
|
|
ips [][]net.Address
|
|
matchers *strmatcher.MatcherGroup
|
|
}
|
|
|
|
// NewStaticHosts creates a new StaticHosts instance.
|
|
func NewStaticHosts(hosts []*Config_HostMapping) (*StaticHosts, error) {
|
|
g := new(strmatcher.MatcherGroup)
|
|
sh := &StaticHosts{
|
|
ips: make([][]net.Address, len(hosts)+16),
|
|
matchers: g,
|
|
}
|
|
|
|
for _, mapping := range hosts {
|
|
matcher, err := toStrMatcher(mapping.Type, mapping.Domain)
|
|
if err != nil {
|
|
return nil, errors.New("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))
|
|
case len(mapping.Ip) > 0:
|
|
for _, ip := range mapping.Ip {
|
|
addr := net.IPAddress(ip)
|
|
if addr == nil {
|
|
return nil, errors.New("invalid IP address in static hosts: ", ip).AtWarning()
|
|
}
|
|
ips = append(ips, addr)
|
|
}
|
|
}
|
|
|
|
sh.ips[id] = ips
|
|
}
|
|
|
|
return sh, nil
|
|
}
|
|
|
|
func filterIP(ips []net.Address, option dns.IPOption) []net.Address {
|
|
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
|
|
}
|
|
|
|
func (h *StaticHosts) lookupInternal(domain string) []net.Address {
|
|
ips := make([]net.Address, 0)
|
|
found := false
|
|
for _, id := range h.matchers.Match(domain) {
|
|
ips = append(ips, h.ips[id]...)
|
|
found = true
|
|
}
|
|
if !found {
|
|
return nil
|
|
}
|
|
return ips
|
|
}
|
|
|
|
func (h *StaticHosts) lookup(domain string, option dns.IPOption, maxDepth int) []net.Address {
|
|
switch addrs := h.lookupInternal(domain); {
|
|
case len(addrs) == 0: // Not recorded in static hosts, return nil
|
|
return addrs
|
|
case len(addrs) == 1 && addrs[0].Family().IsDomain(): // Try to unwrap domain
|
|
errors.LogDebug(context.Background(), "found replaced domain: ", domain, " -> ", addrs[0].Domain(), ". Try to unwrap it")
|
|
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.
|
|
func (h *StaticHosts) Lookup(domain string, option dns.IPOption) []net.Address {
|
|
return h.lookup(domain, option, 5)
|
|
}
|