Browse Source

Fix: DNS hosts proxied domain priority (#886)

Loyalsoldier 4 years ago
parent
commit
47bbb5a3de
5 changed files with 46 additions and 9 deletions
  1. 1 2
      app/dns/config.pb.go
  2. 1 2
      app/dns/config.proto
  3. 1 1
      app/dns/dns.go
  4. 3 4
      app/dns/hosts.go
  5. 40 0
      app/dns/hosts_test.go

+ 1 - 2
app/dns/config.pb.go

@@ -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"`
 }
 

+ 1 - 2
app/dns/config.proto

@@ -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;
   }
 

+ 1 - 1
app/dns/dns.go

@@ -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)
 	}
 

+ 3 - 4
app/dns/hosts.go

@@ -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 {

+ 40 - 0
app/dns/hosts_test.go

@@ -21,6 +21,20 @@ func TestStaticHosts(t *testing.T) {
 			},
 		},
 		{
+			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",
 			Ip: [][]byte{
@@ -54,6 +68,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,
 			IPv6Enable: true,