diff --git a/app/dns/config_json.go b/app/dns/config_json.go index bd866598..d1f6528f 100644 --- a/app/dns/config_json.go +++ b/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() diff --git a/app/dns/server.go b/app/dns/server.go index f34fa83b..1398d311 100644 --- a/app/dns/server.go +++ b/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) diff --git a/app/router/rules/condition.go b/app/router/rules/condition.go index 36fa1e21..40cec2fd 100644 --- a/app/router/rules/condition.go +++ b/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()) diff --git a/app/router/rules/router.go b/app/router/rules/router.go index 30623897..786dd042 100644 --- a/app/router/rules/router.go +++ b/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 { diff --git a/common/net/address.go b/common/net/address.go index 70569c37..490538f3 100644 --- a/common/net/address.go +++ b/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 { diff --git a/common/net/address_json_test.go b/common/net/address_json_test.go index 1344b3ef..9ccd68ce 100644 --- a/common/net/address_json_test.go +++ b/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") } diff --git a/proxy/freedom/freedom.go b/proxy/freedom/freedom.go index 9870b90d..089639fe 100644 --- a/proxy/freedom/freedom.go +++ b/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 { diff --git a/proxy/shadowsocks/server.go b/proxy/shadowsocks/server.go index 2593a6b5..d453397b 100644 --- a/proxy/shadowsocks/server.go +++ b/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())) } diff --git a/proxy/socks/protocol/udp.go b/proxy/socks/protocol/udp.go index b68dc473..77912c92 100644 --- a/proxy/socks/protocol/udp.go +++ b/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()) diff --git a/proxy/socks/server.go b/proxy/socks/server.go index 733dcbd1..2ed187e2 100644 --- a/proxy/socks/server.go +++ b/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()) } diff --git a/proxy/vmess/encoding/client.go b/proxy/vmess/encoding/client.go index f9875e73..aa35689a 100644 --- a/proxy/vmess/encoding/client.go +++ b/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()...) } diff --git a/shell/point/config_json.go b/shell/point/config_json.go index 254f2b26..7fe95e78 100644 --- a/shell/point/config_json.go +++ b/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 diff --git a/testing/assert/address.go b/testing/assert/address.go index 53b8afa3..5f1058a3 100644 --- a/testing/assert/address.go +++ b/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") } } diff --git a/testing/scenarios/socks5_helper.go b/testing/scenarios/socks5_helper.go index 9991c506..bc713b72 100644 --- a/testing/scenarios/socks5_helper.go +++ b/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())...) diff --git a/transport/internet/dialer.go b/transport/internet/dialer.go index ad4e4b24..47432e68 100644 --- a/transport/internet/dialer.go +++ b/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)