Parcourir la source

introduce address family in v2net

v2ray il y a 9 ans
Parent
commit
4419f1e3d6

+ 1 - 1
app/dns/config_json.go

@@ -27,7 +27,7 @@ func (this *Config) UnmarshalJSON(data []byte) error {
 	if jsonConfig.Hosts != nil {
 		this.Hosts = make(map[string]net.IP)
 		for domain, ip := range jsonConfig.Hosts {
-			if ip.Address.IsDomain() {
+			if ip.Address.Family().IsDomain() {
 				return errors.New(ip.Address.String() + " is not an IP.")
 			}
 			this.Hosts[domain] = ip.Address.IP()

+ 1 - 1
app/dns/server.go

@@ -42,7 +42,7 @@ func NewCacheServer(space app.Space, config *Config) *CacheServer {
 
 		dispatcher := space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher)
 		for idx, ns := range config.NameServers {
-			if ns.Address().IsDomain() && ns.Address().Domain() == "localhost" {
+			if ns.Address().Family().IsDomain() && ns.Address().Domain() == "localhost" {
 				server.servers[idx] = &LocalNameServer{}
 			} else {
 				server.servers[idx] = NewUDPNameServer(ns, dispatcher)

+ 4 - 4
app/router/rules/condition.go

@@ -73,7 +73,7 @@ func NewPlainDomainMatcher(pattern string) *PlainDomainMatcher {
 }
 
 func (this *PlainDomainMatcher) Apply(dest v2net.Destination) bool {
-	if !dest.Address().IsDomain() {
+	if !dest.Address().Family().IsDomain() {
 		return false
 	}
 	domain := dest.Address().Domain()
@@ -95,7 +95,7 @@ func NewRegexpDomainMatcher(pattern string) (*RegexpDomainMatcher, error) {
 }
 
 func (this *RegexpDomainMatcher) Apply(dest v2net.Destination) bool {
-	if !dest.Address().IsDomain() {
+	if !dest.Address().Family().IsDomain() {
 		return false
 	}
 	domain := dest.Address().Domain()
@@ -117,7 +117,7 @@ func NewCIDRMatcher(ipnet string) (*CIDRMatcher, error) {
 }
 
 func (this *CIDRMatcher) Apply(dest v2net.Destination) bool {
-	if !dest.Address().IsIPv4() && !dest.Address().IsIPv6() {
+	if !dest.Address().Family().Either(v2net.AddressFamilyIPv4, v2net.AddressFamilyIPv6) {
 		return false
 	}
 	return this.cidr.Contains(dest.Address().IP())
@@ -134,7 +134,7 @@ func NewIPv4Matcher(ipnet *v2net.IPNet) *IPv4Matcher {
 }
 
 func (this *IPv4Matcher) Apply(dest v2net.Destination) bool {
-	if !dest.Address().IsIPv4() {
+	if !dest.Address().Family().Either(v2net.AddressFamilyIPv4) {
 		return false
 	}
 	return this.ipv4net.Contains(dest.Address().IP())

+ 1 - 1
app/router/rules/router.go

@@ -64,7 +64,7 @@ func (this *Router) takeDetourWithoutCache(dest v2net.Destination) (string, erro
 			return rule.Tag, nil
 		}
 	}
-	if this.config.DomainStrategy == UseIPIfNonMatch && dest.Address().IsDomain() {
+	if this.config.DomainStrategy == UseIPIfNonMatch && dest.Address().Family().IsDomain() {
 		log.Info("Router: Looking up IP for ", dest)
 		ipDests := this.ResolveIP(dest)
 		if ipDests != nil {

+ 28 - 34
common/net/address.go

@@ -20,15 +20,33 @@ const (
 	AddressFamilyDomain = AddressFamily(2)
 )
 
+func (this AddressFamily) Either(fs ...AddressFamily) bool {
+	for _, f := range fs {
+		if this == f {
+			return true
+		}
+	}
+	return false
+}
+
+func (this AddressFamily) IsIPv4() bool {
+	return this == AddressFamilyIPv4
+}
+
+func (this AddressFamily) IsIPv6() bool {
+	return this == AddressFamilyIPv6
+}
+
+func (this AddressFamily) IsDomain() bool {
+	return this == AddressFamilyDomain
+}
+
 // Address represents a network address to be communicated with. It may be an IP address or domain
 // address, not both. This interface doesn't resolve IP address for a given domain.
 type Address interface {
 	IP() net.IP     // IP of this Address
 	Domain() string // Domain of this Address
-
-	IsIPv4() bool   // True if this Address is an IPv4 address
-	IsIPv6() bool   // True if this Address is an IPv6 address
-	IsDomain() bool // True if this Address is an domain address
+	Family() AddressFamily
 
 	String() string // String representation of this Address
 	Equals(Address) bool
@@ -83,16 +101,8 @@ func (addr *ipv4Address) Domain() string {
 	panic("Calling Domain() on an IPv4Address.")
 }
 
-func (addr *ipv4Address) IsIPv4() bool {
-	return true
-}
-
-func (addr *ipv4Address) IsIPv6() bool {
-	return false
-}
-
-func (addr *ipv4Address) IsDomain() bool {
-	return false
+func (addr *ipv4Address) Family() AddressFamily {
+	return AddressFamilyIPv4
 }
 
 func (this *ipv4Address) String() string {
@@ -120,16 +130,8 @@ func (addr *ipv6Address) Domain() string {
 	panic("Calling Domain() on an IPv6Address.")
 }
 
-func (addr *ipv6Address) IsIPv4() bool {
-	return false
-}
-
-func (addr *ipv6Address) IsIPv6() bool {
-	return true
-}
-
-func (addr *ipv6Address) IsDomain() bool {
-	return false
+func (this *ipv6Address) Family() AddressFamily {
+	return AddressFamilyIPv6
 }
 
 func (this *ipv6Address) String() string {
@@ -169,16 +171,8 @@ func (addr *domainAddress) Domain() string {
 	return string(*addr)
 }
 
-func (addr *domainAddress) IsIPv4() bool {
-	return false
-}
-
-func (addr *domainAddress) IsIPv6() bool {
-	return false
-}
-
-func (addr *domainAddress) IsDomain() bool {
-	return true
+func (addr *domainAddress) Family() AddressFamily {
+	return AddressFamilyDomain
 }
 
 func (this *domainAddress) String() string {

+ 4 - 4
common/net/address_json_test.go

@@ -18,8 +18,8 @@ func TestIPParsing(t *testing.T) {
 	var address AddressJson
 	err := json.Unmarshal([]byte(rawJson), &address)
 	assert.Error(err).IsNil()
-	assert.Bool(address.Address.IsIPv4()).IsTrue()
-	assert.Bool(address.Address.IsDomain()).IsFalse()
+	assert.Bool(address.Address.Family().Either(AddressFamilyIPv4)).IsTrue()
+	assert.Bool(address.Address.Family().Either(AddressFamilyDomain)).IsFalse()
 	assert.Bool(address.Address.IP().Equal(net.ParseIP("8.8.8.8"))).IsTrue()
 }
 
@@ -30,8 +30,8 @@ func TestDomainParsing(t *testing.T) {
 	var address AddressJson
 	err := json.Unmarshal([]byte(rawJson), &address)
 	assert.Error(err).IsNil()
-	assert.Bool(address.Address.IsIPv4()).IsFalse()
-	assert.Bool(address.Address.IsDomain()).IsTrue()
+	assert.Bool(address.Address.Family().Either(AddressFamilyIPv4)).IsFalse()
+	assert.Bool(address.Address.Family().Either(AddressFamilyDomain)).IsTrue()
 	assert.String(address.Address.Domain()).Equals("v2ray.com")
 }
 

+ 2 - 2
proxy/freedom/freedom.go

@@ -47,7 +47,7 @@ func NewFreedomConnection(config *Config, space app.Space, meta *proxy.OutboundH
 
 // @Private
 func (this *FreedomConnection) ResolveIP(destination v2net.Destination) v2net.Destination {
-	if !destination.Address().IsDomain() {
+	if !destination.Address().Family().IsDomain() {
 		return destination
 	}
 
@@ -76,7 +76,7 @@ func (this *FreedomConnection) Dispatch(destination v2net.Destination, payload *
 	defer ray.OutboundOutput().Close()
 
 	var conn internet.Connection
-	if this.domainStrategy == DomainStrategyUseIP && destination.Address().IsDomain() {
+	if this.domainStrategy == DomainStrategyUseIP && destination.Address().Family().IsDomain() {
 		destination = this.ResolveIP(destination)
 	}
 	err := retry.Timed(5, 100).On(func() error {

+ 4 - 4
proxy/shadowsocks/server.go

@@ -132,14 +132,14 @@ func (this *Server) handlerUDPPayload(payload *alloc.Buffer, source v2net.Destin
 
 		writer := crypto.NewCryptionWriter(stream, response)
 
-		switch {
-		case request.Address.IsIPv4():
+		switch request.Address.Family() {
+		case v2net.AddressFamilyIPv4:
 			writer.Write([]byte{AddrTypeIPv4})
 			writer.Write(request.Address.IP())
-		case request.Address.IsIPv6():
+		case v2net.AddressFamilyIPv6:
 			writer.Write([]byte{AddrTypeIPv6})
 			writer.Write(request.Address.IP())
-		case request.Address.IsDomain():
+		case v2net.AddressFamilyDomain:
 			writer.Write([]byte{AddrTypeDomain, byte(len(request.Address.Domain()))})
 			writer.Write([]byte(request.Address.Domain()))
 		}

+ 4 - 4
proxy/socks/protocol/udp.go

@@ -26,12 +26,12 @@ func (request *Socks5UDPRequest) Destination() v2net.Destination {
 
 func (request *Socks5UDPRequest) Write(buffer *alloc.Buffer) {
 	buffer.AppendBytes(0, 0, request.Fragment)
-	switch {
-	case request.Address.IsIPv4():
+	switch request.Address.Family() {
+	case v2net.AddressFamilyIPv4:
 		buffer.AppendBytes(AddrTypeIPv4).Append(request.Address.IP())
-	case request.Address.IsIPv6():
+	case v2net.AddressFamilyIPv6:
 		buffer.AppendBytes(AddrTypeIPv6).Append(request.Address.IP())
-	case request.Address.IsDomain():
+	case v2net.AddressFamilyDomain:
 		buffer.AppendBytes(AddrTypeDomain, byte(len(request.Address.Domain()))).Append([]byte(request.Address.Domain()))
 	}
 	buffer.AppendUint16(request.Port.Value())

+ 4 - 4
proxy/socks/server.go

@@ -238,12 +238,12 @@ func (this *Server) handleUDP(reader io.Reader, writer *v2io.BufferedWriter) err
 	udpAddr := this.udpAddress
 
 	response.Port = udpAddr.Port()
-	switch {
-	case udpAddr.Address().IsIPv4():
+	switch udpAddr.Address().Family() {
+	case v2net.AddressFamilyIPv4:
 		response.SetIPv4(udpAddr.Address().IP())
-	case udpAddr.Address().IsIPv6():
+	case v2net.AddressFamilyIPv6:
 		response.SetIPv6(udpAddr.Address().IP())
-	case udpAddr.Address().IsDomain():
+	case v2net.AddressFamilyDomain:
 		response.SetDomain(udpAddr.Address().Domain())
 	}
 

+ 5 - 4
proxy/vmess/encoding/client.go

@@ -8,6 +8,7 @@ import (
 
 	"github.com/v2ray/v2ray-core/common/crypto"
 	"github.com/v2ray/v2ray-core/common/log"
+	v2net "github.com/v2ray/v2ray-core/common/net"
 	"github.com/v2ray/v2ray-core/common/protocol"
 	"github.com/v2ray/v2ray-core/proxy/vmess"
 	"github.com/v2ray/v2ray-core/transport"
@@ -62,14 +63,14 @@ func (this *ClientSession) EncodeRequestHeader(header *protocol.RequestHeader, w
 	buffer = append(buffer, this.responseHeader, byte(header.Option), byte(0), byte(0), byte(header.Command))
 	buffer = header.Port.Bytes(buffer)
 
-	switch {
-	case header.Address.IsIPv4():
+	switch header.Address.Family() {
+	case v2net.AddressFamilyIPv4:
 		buffer = append(buffer, AddrTypeIPv4)
 		buffer = append(buffer, header.Address.IP()...)
-	case header.Address.IsIPv6():
+	case v2net.AddressFamilyIPv6:
 		buffer = append(buffer, AddrTypeIPv6)
 		buffer = append(buffer, header.Address.IP()...)
-	case header.Address.IsDomain():
+	case v2net.AddressFamilyDomain:
 		buffer = append(buffer, AddrTypeDomain, byte(len(header.Address.Domain())))
 		buffer = append(buffer, header.Address.Domain()...)
 	}

+ 4 - 4
shell/point/config_json.go

@@ -81,7 +81,7 @@ func (this *InboundConnectionConfig) UnmarshalJSON(data []byte) error {
 	this.Port = v2net.Port(jsonConfig.Port)
 	this.ListenOn = v2net.AnyIP
 	if jsonConfig.Listen != nil {
-		if jsonConfig.Listen.Address.IsDomain() {
+		if jsonConfig.Listen.Address.Family().IsDomain() {
 			return errors.New("Point: Unable to listen on domain address: " + jsonConfig.Listen.Address.Domain())
 		}
 		this.ListenOn = jsonConfig.Listen.Address
@@ -112,7 +112,7 @@ func (this *OutboundConnectionConfig) UnmarshalJSON(data []byte) error {
 
 	if jsonConfig.SendThrough != nil {
 		address := jsonConfig.SendThrough.Address
-		if address.IsDomain() {
+		if address.Family().IsDomain() {
 			return errors.New("Point: Unable to send through: " + address.String())
 		}
 		this.SendThrough = address
@@ -200,7 +200,7 @@ func (this *InboundDetourConfig) UnmarshalJSON(data []byte) error {
 	}
 	this.ListenOn = v2net.AnyIP
 	if jsonConfig.ListenOn != nil {
-		if jsonConfig.ListenOn.Address.IsDomain() {
+		if jsonConfig.ListenOn.Address.Family().IsDomain() {
 			return errors.New("Point: Unable to listen on domain address: " + jsonConfig.ListenOn.Address.Domain())
 		}
 		this.ListenOn = jsonConfig.ListenOn.Address
@@ -241,7 +241,7 @@ func (this *OutboundDetourConfig) UnmarshalJSON(data []byte) error {
 
 	if jsonConfig.SendThrough != nil {
 		address := jsonConfig.SendThrough.Address
-		if address.IsDomain() {
+		if address.Family().IsDomain() {
 			return errors.New("Point: Unable to send through: " + address.String())
 		}
 		this.SendThrough = address

+ 6 - 6
testing/assert/address.go

@@ -44,37 +44,37 @@ func (subject *AddressSubject) EqualsString(another string) {
 }
 
 func (subject *AddressSubject) IsIPv4() {
-	if !subject.value.IsIPv4() {
+	if !subject.value.Family().IsIPv4() {
 		subject.Fail("is", "an IPv4 address")
 	}
 }
 
 func (subject *AddressSubject) IsNotIPv4() {
-	if subject.value.IsIPv4() {
+	if subject.value.Family().IsIPv4() {
 		subject.Fail("is not", "an IPv4 address")
 	}
 }
 
 func (subject *AddressSubject) IsIPv6() {
-	if !subject.value.IsIPv6() {
+	if !subject.value.Family().IsIPv6() {
 		subject.Fail("is", "an IPv6 address")
 	}
 }
 
 func (subject *AddressSubject) IsNotIPv6() {
-	if subject.value.IsIPv6() {
+	if subject.value.Family().IsIPv6() {
 		subject.Fail("is not", "an IPv6 address")
 	}
 }
 
 func (subject *AddressSubject) IsDomain() {
-	if !subject.value.IsDomain() {
+	if !subject.value.Family().IsDomain() {
 		subject.Fail("is", "a domain address")
 	}
 }
 
 func (subject *AddressSubject) IsNotDomain() {
-	if subject.value.IsDomain() {
+	if subject.value.Family().IsDomain() {
 		subject.Fail("is not", "a domain address")
 	}
 }

+ 4 - 4
testing/scenarios/socks5_helper.go

@@ -15,16 +15,16 @@ func socks5AuthMethodRequest(methods ...byte) []byte {
 }
 
 func appendAddress(request []byte, address v2net.Address) []byte {
-	switch {
-	case address.IsIPv4():
+	switch address.Family() {
+	case v2net.AddressFamilyIPv4:
 		request = append(request, byte(0x01))
 		request = append(request, address.IP()...)
 
-	case address.IsIPv6():
+	case v2net.AddressFamilyIPv6:
 		request = append(request, byte(0x04))
 		request = append(request, address.IP()...)
 
-	case address.IsDomain():
+	case v2net.AddressFamilyDomain:
 		request = append(request, byte(0x03), byte(len(address.Domain())))
 		request = append(request, []byte(address.Domain())...)
 

+ 1 - 1
transport/internet/dialer.go

@@ -44,7 +44,7 @@ func Dial(src v2net.Address, dest v2net.Destination, settings *StreamSettings) (
 		}
 
 		config := settings.TLSSettings.GetTLSConfig()
-		if dest.Address().IsDomain() {
+		if dest.Address().Family().IsDomain() {
 			config.ServerName = dest.Address().Domain()
 		}
 		tlsConn := tls.Client(connection, config)