1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-10 18:00:43 +00:00

support domain to domain mapping in static host

This commit is contained in:
Darien Raymond 2019-01-30 21:04:29 +01:00
parent bb5a959876
commit ffb3793b26
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
7 changed files with 180 additions and 57 deletions

View File

@ -232,12 +232,15 @@ func (m *Config) GetTag() string {
}
type Config_HostMapping struct {
Type DomainMatchingType `protobuf:"varint,1,opt,name=type,proto3,enum=v2ray.core.app.dns.DomainMatchingType" json:"type,omitempty"`
Domain string `protobuf:"bytes,2,opt,name=domain,proto3" json:"domain,omitempty"`
Ip [][]byte `protobuf:"bytes,3,rep,name=ip,proto3" json:"ip,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
Type DomainMatchingType `protobuf:"varint,1,opt,name=type,proto3,enum=v2ray.core.app.dns.DomainMatchingType" json:"type,omitempty"`
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.
ProxiedDomain string `protobuf:"bytes,4,opt,name=proxied_domain,json=proxiedDomain,proto3" json:"proxied_domain,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *Config_HostMapping) Reset() { *m = Config_HostMapping{} }
@ -286,6 +289,13 @@ func (m *Config_HostMapping) GetIp() [][]byte {
return nil
}
func (m *Config_HostMapping) GetProxiedDomain() string {
if m != nil {
return m.ProxiedDomain
}
return ""
}
func init() {
proto.RegisterEnum("v2ray.core.app.dns.DomainMatchingType", DomainMatchingType_name, DomainMatchingType_value)
proto.RegisterType((*NameServer)(nil), "v2ray.core.app.dns.NameServer")
@ -300,39 +310,40 @@ func init() {
}
var fileDescriptor_ed5695198e3def8f = []byte{
// 530 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x53, 0xdb, 0x6e, 0xd3, 0x30,
0x18, 0x26, 0x49, 0xdb, 0xad, 0x7f, 0xc6, 0x54, 0x7c, 0x31, 0x45, 0x45, 0x82, 0x32, 0xc4, 0xa8,
0x40, 0x38, 0x52, 0x40, 0x02, 0x76, 0x33, 0xb1, 0xad, 0x88, 0x0a, 0x0d, 0x2a, 0x0f, 0x71, 0x01,
0x48, 0x95, 0x97, 0x98, 0xce, 0xa2, 0xb1, 0x8d, 0xed, 0x16, 0xc2, 0x2b, 0xf0, 0x08, 0xbc, 0x01,
0x4f, 0x89, 0x6a, 0x77, 0xb4, 0xb0, 0x0e, 0xb8, 0xe1, 0xce, 0x87, 0xef, 0x94, 0xef, 0x77, 0xe0,
0xe6, 0x34, 0xd3, 0xb4, 0xc2, 0xb9, 0x2c, 0xd3, 0x5c, 0x6a, 0x96, 0x52, 0xa5, 0xd2, 0x42, 0x98,
0x34, 0x97, 0xe2, 0x3d, 0x1f, 0x61, 0xa5, 0xa5, 0x95, 0x08, 0x9d, 0x81, 0x34, 0xc3, 0x54, 0x29,
0x5c, 0x08, 0xd3, 0xbe, 0xfd, 0x1b, 0x31, 0x97, 0x65, 0x29, 0x45, 0x2a, 0x98, 0x4d, 0x69, 0x51,
0x68, 0x66, 0x8c, 0x27, 0xb7, 0xef, 0x5e, 0x0c, 0x2c, 0x98, 0xb1, 0x5c, 0x50, 0xcb, 0xa5, 0xf0,
0xe0, 0xed, 0xaf, 0x21, 0xc0, 0x0b, 0x5a, 0xb2, 0x63, 0xa6, 0xa7, 0x4c, 0xa3, 0xc7, 0xb0, 0x36,
0x17, 0x4b, 0x82, 0x4e, 0xd0, 0x8d, 0xb3, 0xeb, 0x78, 0x29, 0x8a, 0x57, 0xc2, 0x82, 0x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0x26, 0x3b, 0x7f, 0x31, 0x39, 0xa2, 0x4a, 0x71, 0x31,
0x22, 0xb1, 0xe7, 0x7a, 0x9f, 0x16, 0x44, 0x96, 0x8e, 0x92, 0x86, 0x2b, 0x74, 0xb6, 0x6c, 0xbf,
0x05, 0x58, 0x44, 0x9a, 0xdd, 0x7f, 0x60, 0x95, 0x1b, 0x57, 0x93, 0xcc, 0x96, 0xe8, 0x21, 0xd4,
0xa7, 0x74, 0x3c, 0x61, 0x6e, 0x08, 0x71, 0x76, 0xe3, 0x82, 0x72, 0xfb, 0x83, 0x97, 0x7a, 0xfe,
0x30, 0x3c, 0x7e, 0x37, 0x7c, 0x14, 0xb4, 0x3f, 0x42, 0xbc, 0x14, 0xe5, 0x7f, 0xbc, 0x06, 0xb4,
0x09, 0xa1, 0xab, 0x2c, 0xea, 0x6e, 0x90, 0x90, 0xab, 0x3b, 0x3d, 0x40, 0xe7, 0x35, 0xd0, 0x3a,
0xd4, 0x9e, 0x4e, 0xc6, 0xe3, 0xd6, 0x25, 0x74, 0x19, 0x9a, 0xc7, 0x93, 0x13, 0x4f, 0x6e, 0x05,
0x28, 0x86, 0xb5, 0xe7, 0xac, 0xfa, 0x24, 0x75, 0xd1, 0x0a, 0x51, 0x13, 0xea, 0x84, 0x8d, 0xd8,
0xe7, 0x56, 0xb4, 0xff, 0x00, 0xb6, 0x72, 0x59, 0xae, 0x48, 0x38, 0x08, 0xde, 0x44, 0x85, 0x30,
0xdf, 0x43, 0xf4, 0x3a, 0x23, 0xb4, 0xc2, 0x07, 0xb3, 0xbb, 0x27, 0x4a, 0xe1, 0x43, 0x61, 0x4e,
0x1a, 0xee, 0x7f, 0xbd, 0xff, 0x23, 0x00, 0x00, 0xff, 0xff, 0x83, 0x2b, 0x1c, 0x4c, 0x40, 0x04,
0x00, 0x00,
// 552 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x53, 0x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x47, 0x51, 0xcd,
0x06, 0x3a, 0xfb, 0x6c, 0xbf, 0x07, 0x58, 0x58, 0x9a, 0xdd, 0x7f, 0x62, 0xa5, 0x1d, 0x57, 0x9d,
0xcc, 0x3e, 0xd1, 0x63, 0xa8, 0x4e, 0xe9, 0x78, 0xc2, 0xec, 0x10, 0xc2, 0xe4, 0xf6, 0x25, 0xe1,
0xf6, 0x07, 0xaf, 0xf5, 0x7c, 0x31, 0x1c, 0x7e, 0xd7, 0x7f, 0xe2, 0xb5, 0xbf, 0x79, 0x10, 0x2e,
0x79, 0xb9, 0x8a, 0x75, 0x40, 0x4d, 0xf0, 0x6d, 0x66, 0x41, 0xb7, 0x41, 0x7c, 0xae, 0xd0, 0x36,
0x34, 0x95, 0x96, 0x67, 0x7c, 0xb1, 0xde, 0x15, 0x8b, 0xdf, 0x98, 0x57, 0x9d, 0xc0, 0xbd, 0x1e,
0xa0, 0x8b, 0x52, 0x68, 0x1d, 0x2a, 0xcf, 0x27, 0xe3, 0x71, 0xeb, 0x1a, 0xda, 0x80, 0xfa, 0xf1,
0xe4, 0xc4, 0x75, 0x68, 0x79, 0x28, 0x84, 0xb5, 0x97, 0xac, 0xfc, 0x2c, 0x75, 0xd6, 0xf2, 0x51,
0x1d, 0xaa, 0x84, 0x8d, 0xd8, 0x59, 0x2b, 0xd8, 0x7f, 0x04, 0x9b, 0xa9, 0xcc, 0x57, 0x3c, 0x64,
0xe0, 0xbd, 0x0b, 0x32, 0x51, 0x7c, 0xf7, 0xd1, 0xdb, 0x84, 0xd0, 0x12, 0x1f, 0xcc, 0xee, 0x9e,
0x29, 0x85, 0x0f, 0x45, 0x71, 0x52, 0xb3, 0xff, 0xf5, 0xc3, 0x9f, 0x01, 0x00, 0x00, 0xff, 0xff,
0x15, 0xed, 0x7b, 0x41, 0x68, 0x04, 0x00, 0x00,
}

View File

@ -45,7 +45,12 @@ message Config {
message HostMapping {
DomainMatchingType type = 1;
string domain = 2;
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.
string proxied_domain = 4;
}
repeated HostMapping static_hosts = 4;

View File

@ -63,25 +63,32 @@ func NewStaticHosts(hosts []*Config_HostMapping, legacy map[string]*net.IPOrDoma
return nil, newError("failed to create domain matcher").Base(err)
}
id := g.Add(matcher)
ips := make([]net.Address, 0, len(mapping.Ip))
for _, ip := range mapping.Ip {
addr := net.IPAddress(ip)
if addr == nil {
return nil, newError("invalid IP address in static hosts: ", ip).AtWarning()
ips := make([]net.Address, 0, len(mapping.Ip)+1)
if 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)
}
ips = append(ips, addr)
} else if len(mapping.ProxiedDomain) > 0 {
ips = append(ips, net.DomainAddress(mapping.ProxiedDomain))
} else {
return nil, newError("neither IP address nor proxied domain specified for domain: ", mapping.Domain).AtWarning()
}
sh.ips[id] = ips
}
return sh, nil
}
func filterIP(ips []net.Address, option IPOption) []net.IP {
filtered := make([]net.IP, 0, len(ips))
func filterIP(ips []net.Address, option 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.IP())
filtered = append(filtered, ip)
}
}
if len(filtered) == 0 {
@ -91,10 +98,14 @@ func filterIP(ips []net.Address, option IPOption) []net.IP {
}
// LookupIP returns IP address for the given domain, if exists in this StaticHosts.
func (h *StaticHosts) LookupIP(domain string, option IPOption) []net.IP {
func (h *StaticHosts) LookupIP(domain string, option IPOption) []net.Address {
id := h.matchers.Match(domain)
if id == 0 {
return nil
}
return filterIP(h.ips[id], option)
ips := h.ips[id]
if len(ips) == 1 && ips[0].Family().IsDomain() {
return ips
}
return filterIP(ips, option)
}

View File

@ -38,7 +38,7 @@ func TestStaticHosts(t *testing.T) {
if len(ips) != 1 {
t.Error("expect 1 IP, but got ", len(ips))
}
if diff := cmp.Diff([]byte(ips[0]), []byte{1, 1, 1, 1}); diff != "" {
if diff := cmp.Diff([]byte(ips[0].IP()), []byte{1, 1, 1, 1}); diff != "" {
t.Error(diff)
}
}
@ -51,7 +51,7 @@ func TestStaticHosts(t *testing.T) {
if len(ips) != 1 {
t.Error("expect 1 IP, but got ", len(ips))
}
if diff := cmp.Diff([]byte(ips[0]), []byte{2, 2, 2, 2}); diff != "" {
if diff := cmp.Diff([]byte(ips[0].IP()), []byte{2, 2, 2, 2}); diff != "" {
t.Error(diff)
}
}

View File

@ -155,9 +155,40 @@ func (s *Server) LookupIPv6(domain string) ([]net.IP, error) {
})
}
func (s *Server) lookupStatic(domain string, option IPOption, depth int32) []net.Address {
ips := s.hosts.LookupIP(domain, option)
if ips == nil {
return nil
}
if ips[0].Family().IsDomain() && depth < 5 {
if newIPs := s.lookupStatic(ips[0].Domain(), option, depth+1); newIPs != nil {
return newIPs
}
}
return ips
}
func toNetIP(ips []net.Address) []net.IP {
if len(ips) == 0 {
return nil
}
netips := make([]net.IP, 0, len(ips))
for _, ip := range ips {
netips = append(netips, ip.IP())
}
return netips
}
func (s *Server) lookupIPInternal(domain string, option IPOption) ([]net.IP, error) {
if ip := s.hosts.LookupIP(domain, option); len(ip) > 0 {
return ip, nil
ips := s.lookupStatic(domain, option, 0)
if ips != nil && ips[0].Family().IsIP() {
return toNetIP(ips), nil
}
if ips != nil && ips[0].Family().IsDomain() {
newdomain := ips[0].Domain()
newError("domain replaced: ", domain, " -> ", newdomain).WriteToLog()
domain = newdomain
}
var lastErr error

View File

@ -339,3 +339,68 @@ func TestUDPServerIPv6(t *testing.T) {
}
}
}
func TestStaticHostDomain(t *testing.T) {
port := udp.PickPort()
dnsServer := dns.Server{
Addr: "127.0.0.1:" + port.String(),
Net: "udp",
Handler: &staticHandler{},
UDPSize: 1200,
}
go dnsServer.ListenAndServe()
time.Sleep(time.Second)
config := &core.Config{
App: []*serial.TypedMessage{
serial.ToTypedMessage(&Config{
NameServers: []*net.Endpoint{
{
Network: net.Network_UDP,
Address: &net.IPOrDomain{
Address: &net.IPOrDomain_Ip{
Ip: []byte{127, 0, 0, 1},
},
},
Port: uint32(port),
},
},
StaticHosts: []*Config_HostMapping{
{
Type: DomainMatchingType_Full,
Domain: "example.com",
ProxiedDomain: "google.com",
},
},
}),
serial.ToTypedMessage(&dispatcher.Config{}),
serial.ToTypedMessage(&proxyman.OutboundConfig{}),
serial.ToTypedMessage(&policy.Config{}),
},
Outbound: []*core.OutboundHandlerConfig{
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
},
},
}
v, err := core.New(config)
common.Must(err)
client := v.GetFeature(feature_dns.ClientType()).(feature_dns.Client)
{
ips, err := client.LookupIP("example.com")
if err != nil {
t.Fatal("unexpected error: ", err)
}
if r := cmp.Diff(ips, []net.IP{{8, 8, 8, 8}}); r != "" {
t.Fatal(r)
}
}
dnsServer.Shutdown()
}

View File

@ -324,7 +324,7 @@ func (s *ClassicNameServer) findIPsForDomain(domain string, option IPOption) []n
ips = append(ips, rec.IP)
}
}
return filterIP(ips, option)
return toNetIP(filterIP(ips, option))
}
return nil
}