1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 01:57:12 -05:00

Fix: DNS hosts proxied domain priority (#886)

This commit is contained in:
Loyalsoldier 2021-04-11 11:52:12 +08:00 committed by GitHub
parent bada0e48b4
commit 47bbb5a3de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 46 additions and 9 deletions

View File

@ -456,8 +456,7 @@ type Config_HostMapping struct {
Domain string `protobuf:"bytes,2,opt,name=domain,proto3" json:"domain,omitempty"`
Ip [][]byte `protobuf:"bytes,3,rep,name=ip,proto3" json:"ip,omitempty"`
// ProxiedDomain indicates the mapped domain has the same IP address on this
// domain. V2Ray will use this domain for IP queries. This field is only
// effective if ip is empty.
// domain. V2Ray will use this domain for IP queries.
ProxiedDomain string `protobuf:"bytes,4,opt,name=proxied_domain,json=proxiedDomain,proto3" json:"proxied_domain,omitempty"`
}

View File

@ -67,8 +67,7 @@ message Config {
repeated bytes ip = 3;
// ProxiedDomain indicates the mapped domain has the same IP address on this
// domain. V2Ray will use this domain for IP queries. This field is only
// effective if ip is empty.
// domain. V2Ray will use this domain for IP queries.
string proxied_domain = 4;
}

View File

@ -214,7 +214,7 @@ func (s *DNS) lookupIPInternal(domain string, option dns.IPOption) ([]net.IP, er
newError("domain replaced: ", domain, " -> ", addrs[0].Domain()).WriteToLog()
domain = addrs[0].Domain()
default: // Successfully found ip records in static host
newError("returning ", len(addrs), " IPs for domain ", domain, " -> ", addrs).WriteToLog()
newError("returning ", len(addrs), " IP(s) for domain ", domain, " -> ", addrs).WriteToLog()
return toNetIP(addrs)
}

View File

@ -49,6 +49,8 @@ func NewStaticHosts(hosts []*Config_HostMapping, legacy map[string]*net.IPOrDoma
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)
@ -57,10 +59,6 @@ func NewStaticHosts(hosts []*Config_HostMapping, legacy map[string]*net.IPOrDoma
}
ips = append(ips, addr)
}
case len(mapping.ProxiedDomain) > 0:
ips = append(ips, net.DomainAddress(mapping.ProxiedDomain))
default:
return nil, newError("neither IP address nor proxied domain specified for domain: ", mapping.Domain).AtWarning()
}
@ -94,6 +92,7 @@ func (h *StaticHosts) lookup(domain string, option dns.IPOption, maxDepth int) [
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()
if maxDepth > 0 {
unwrapped := h.lookup(addrs[0].Domain(), option, maxDepth-1)
if unwrapped != nil {

View File

@ -20,6 +20,20 @@ func TestStaticHosts(t *testing.T) {
{1, 1, 1, 1},
},
},
{
Type: DomainMatchingType_Full,
Domain: "proxy.v2fly.org",
Ip: [][]byte{
{1, 2, 3, 4},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
},
ProxiedDomain: "another-proxy.v2fly.org",
},
{
Type: DomainMatchingType_Full,
Domain: "proxy2.v2fly.org",
ProxiedDomain: "proxy.v2fly.org",
},
{
Type: DomainMatchingType_Subdomain,
Domain: "v2ray.cn",
@ -53,6 +67,32 @@ func TestStaticHosts(t *testing.T) {
}
}
{
domain := hosts.Lookup("proxy.v2fly.org", dns.IPOption{
IPv4Enable: true,
IPv6Enable: false,
})
if len(domain) != 1 {
t.Error("expect 1 domain, but got ", len(domain))
}
if diff := cmp.Diff(domain[0].Domain(), "another-proxy.v2fly.org"); diff != "" {
t.Error(diff)
}
}
{
domain := hosts.Lookup("proxy2.v2fly.org", dns.IPOption{
IPv4Enable: true,
IPv6Enable: false,
})
if len(domain) != 1 {
t.Error("expect 1 domain, but got ", len(domain))
}
if diff := cmp.Diff(domain[0].Domain(), "another-proxy.v2fly.org"); diff != "" {
t.Error(diff)
}
}
{
ips := hosts.Lookup("www.v2ray.cn", dns.IPOption{
IPv4Enable: true,