1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-02 15:36:41 -05:00

introduce address family in v2net

This commit is contained in:
v2ray 2016-08-14 18:14:12 +02:00
parent a9d583b92f
commit 4419f1e3d6
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
15 changed files with 73 additions and 78 deletions

View File

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

View File

@ -42,7 +42,7 @@ func NewCacheServer(space app.Space, config *Config) *CacheServer {
dispatcher := space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher) dispatcher := space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher)
for idx, ns := range config.NameServers { 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{} server.servers[idx] = &LocalNameServer{}
} else { } else {
server.servers[idx] = NewUDPNameServer(ns, dispatcher) server.servers[idx] = NewUDPNameServer(ns, dispatcher)

View File

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

View File

@ -64,7 +64,7 @@ func (this *Router) takeDetourWithoutCache(dest v2net.Destination) (string, erro
return rule.Tag, nil 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) log.Info("Router: Looking up IP for ", dest)
ipDests := this.ResolveIP(dest) ipDests := this.ResolveIP(dest)
if ipDests != nil { if ipDests != nil {

View File

@ -20,15 +20,33 @@ const (
AddressFamilyDomain = AddressFamily(2) 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 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. // address, not both. This interface doesn't resolve IP address for a given domain.
type Address interface { type Address interface {
IP() net.IP // IP of this Address IP() net.IP // IP of this Address
Domain() string // Domain of this Address Domain() string // Domain of this Address
Family() AddressFamily
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
String() string // String representation of this Address String() string // String representation of this Address
Equals(Address) bool Equals(Address) bool
@ -83,16 +101,8 @@ func (addr *ipv4Address) Domain() string {
panic("Calling Domain() on an IPv4Address.") panic("Calling Domain() on an IPv4Address.")
} }
func (addr *ipv4Address) IsIPv4() bool { func (addr *ipv4Address) Family() AddressFamily {
return true return AddressFamilyIPv4
}
func (addr *ipv4Address) IsIPv6() bool {
return false
}
func (addr *ipv4Address) IsDomain() bool {
return false
} }
func (this *ipv4Address) String() string { func (this *ipv4Address) String() string {
@ -120,16 +130,8 @@ func (addr *ipv6Address) Domain() string {
panic("Calling Domain() on an IPv6Address.") panic("Calling Domain() on an IPv6Address.")
} }
func (addr *ipv6Address) IsIPv4() bool { func (this *ipv6Address) Family() AddressFamily {
return false return AddressFamilyIPv6
}
func (addr *ipv6Address) IsIPv6() bool {
return true
}
func (addr *ipv6Address) IsDomain() bool {
return false
} }
func (this *ipv6Address) String() string { func (this *ipv6Address) String() string {
@ -169,16 +171,8 @@ func (addr *domainAddress) Domain() string {
return string(*addr) return string(*addr)
} }
func (addr *domainAddress) IsIPv4() bool { func (addr *domainAddress) Family() AddressFamily {
return false return AddressFamilyDomain
}
func (addr *domainAddress) IsIPv6() bool {
return false
}
func (addr *domainAddress) IsDomain() bool {
return true
} }
func (this *domainAddress) String() string { func (this *domainAddress) String() string {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -8,6 +8,7 @@ import (
"github.com/v2ray/v2ray-core/common/crypto" "github.com/v2ray/v2ray-core/common/crypto"
"github.com/v2ray/v2ray-core/common/log" "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/common/protocol"
"github.com/v2ray/v2ray-core/proxy/vmess" "github.com/v2ray/v2ray-core/proxy/vmess"
"github.com/v2ray/v2ray-core/transport" "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 = append(buffer, this.responseHeader, byte(header.Option), byte(0), byte(0), byte(header.Command))
buffer = header.Port.Bytes(buffer) buffer = header.Port.Bytes(buffer)
switch { switch header.Address.Family() {
case header.Address.IsIPv4(): case v2net.AddressFamilyIPv4:
buffer = append(buffer, AddrTypeIPv4) buffer = append(buffer, AddrTypeIPv4)
buffer = append(buffer, header.Address.IP()...) buffer = append(buffer, header.Address.IP()...)
case header.Address.IsIPv6(): case v2net.AddressFamilyIPv6:
buffer = append(buffer, AddrTypeIPv6) buffer = append(buffer, AddrTypeIPv6)
buffer = append(buffer, header.Address.IP()...) 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, AddrTypeDomain, byte(len(header.Address.Domain())))
buffer = append(buffer, header.Address.Domain()...) buffer = append(buffer, header.Address.Domain()...)
} }

View File

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

View File

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

View File

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

View File

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