Browse Source

fix usability for both IPv4 and v6 in static dns mapping

Darien Raymond 6 years ago
parent
commit
c27050ad90
2 changed files with 26 additions and 0 deletions
  1. 5 0
      app/dns/hosts.go
  2. 21 0
      app/dns/hosts_test.go

+ 5 - 0
app/dns/hosts.go

@@ -80,6 +80,11 @@ func NewStaticHosts(hosts []*Config_HostMapping, legacy map[string]*net.IPOrDoma
 			return nil, newError("neither IP address nor proxied domain specified for domain: ", mapping.Domain).AtWarning()
 			return nil, newError("neither IP address nor proxied domain specified for domain: ", mapping.Domain).AtWarning()
 		}
 		}
 
 
+		// Special handling for localhost IPv6. This is a dirty workaround as JSON config supports only single IP mapping.
+		if len(ips) == 1 && ips[0] == net.LocalHostIP {
+			ips = append(ips, net.LocalHostIPv6)
+		}
+
 		sh.ips[id] = ips
 		sh.ips[id] = ips
 	}
 	}
 
 

+ 21 - 0
app/dns/hosts_test.go

@@ -7,6 +7,7 @@ import (
 
 
 	. "v2ray.com/core/app/dns"
 	. "v2ray.com/core/app/dns"
 	"v2ray.com/core/common"
 	"v2ray.com/core/common"
+	"v2ray.com/core/common/net"
 )
 )
 
 
 func TestStaticHosts(t *testing.T) {
 func TestStaticHosts(t *testing.T) {
@@ -25,6 +26,13 @@ func TestStaticHosts(t *testing.T) {
 				{2, 2, 2, 2},
 				{2, 2, 2, 2},
 			},
 			},
 		},
 		},
+		{
+			Type:   DomainMatchingType_Subdomain,
+			Domain: "baidu.com",
+			Ip: [][]byte{
+				{127, 0, 0, 1},
+			},
+		},
 	}
 	}
 
 
 	hosts, err := NewStaticHosts(pb, nil)
 	hosts, err := NewStaticHosts(pb, nil)
@@ -55,4 +63,17 @@ func TestStaticHosts(t *testing.T) {
 			t.Error(diff)
 			t.Error(diff)
 		}
 		}
 	}
 	}
+
+	{
+		ips := hosts.LookupIP("baidu.com", IPOption{
+			IPv4Enable: false,
+			IPv6Enable: true,
+		})
+		if len(ips) != 1 {
+			t.Error("expect 1 IP, but got ", len(ips))
+		}
+		if diff := cmp.Diff([]byte(ips[0].IP()), []byte(net.LocalHostIPv6.IP())); diff != "" {
+			t.Error(diff)
+		}
+	}
 }
 }