1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-21 17:46:58 -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 {
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()

View File

@ -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)

View File

@ -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())

View File

@ -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 {

View File

@ -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 {

View File

@ -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")
}

View File

@ -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 {

View File

@ -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()))
}

View File

@ -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())

View File

@ -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())
}

View File

@ -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()...)
}

View File

@ -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

View File

@ -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")
}
}

View File

@ -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())...)

View File

@ -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)