1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-21 17:46:58 -05:00

simplify IPOrDomain construction

This commit is contained in:
Darien Raymond 2016-12-14 23:04:02 +01:00
parent f47c21c422
commit 3b5a791ae8
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
5 changed files with 27 additions and 35 deletions

View File

@ -10,6 +10,7 @@ import (
var (
LocalHostIP = IPAddress([]byte{127, 0, 0, 1})
AnyIP = IPAddress([]byte{0, 0, 0, 0})
LocalHostDomain = DomainAddress("localhost")
)
type AddressFamily int
@ -156,3 +157,22 @@ func (v *IPOrDomain) AsAddress() Address {
}
panic("Common|Net: Invalid address.")
}
func NewIPOrDomain(addr Address) *IPOrDomain {
switch addr.Family() {
case AddressFamilyDomain:
return &IPOrDomain{
Address: &IPOrDomain_Domain{
Domain: addr.Domain(),
},
}
case AddressFamilyIPv4, AddressFamilyIPv6:
return &IPOrDomain{
Address: &IPOrDomain_Ip{
Ip: addr.IP(),
},
}
default:
panic("Unknown Address type.")
}
}

View File

@ -54,11 +54,7 @@ func TestDokodemoTCP(t *testing.T) {
port := v2net.Port(dice.Roll(20000) + 10000)
dokodemo := NewDokodemoDoor(&Config{
Address: &v2net.IPOrDomain{
Address: &v2net.IPOrDomain_Ip{
Ip: v2net.LocalHostIP.IP(),
},
},
Address: v2net.NewIPOrDomain(v2net.LocalHostIP),
Port: uint32(tcpServer.Port),
NetworkList: v2net.Network_TCP.AsList(),
Timeout: 600,
@ -128,11 +124,7 @@ func TestDokodemoUDP(t *testing.T) {
port := v2net.Port(dice.Roll(20000) + 10000)
dokodemo := NewDokodemoDoor(&Config{
Address: &v2net.IPOrDomain{
Address: &v2net.IPOrDomain_Ip{
Ip: v2net.LocalHostIP.IP(),
},
},
Address: v2net.NewIPOrDomain(v2net.LocalHostIP),
Port: uint32(udpServer.Port),
NetworkList: v2net.Network_UDP.AsList(),
Timeout: 600,

View File

@ -70,11 +70,7 @@ func TestIPResolution(t *testing.T) {
space.BindApp(router.APP_ID, r)
dnsServer := dns.NewCacheServer(space, &dns.Config{
Hosts: map[string]*v2net.IPOrDomain{
"v2ray.com": {
Address: &v2net.IPOrDomain_Ip{
Ip: []byte{127, 0, 0, 1},
},
},
"v2ray.com": v2net.NewIPOrDomain(v2net.LocalHostIP),
},
})
space.BindApp(dns.APP_ID, dnsServer)

View File

@ -51,19 +51,7 @@ func (v *Address) UnmarshalJSON(data []byte) error {
}
func (v *Address) Build() *v2net.IPOrDomain {
if v.Family().IsDomain() {
return &v2net.IPOrDomain{
Address: &v2net.IPOrDomain_Domain{
Domain: v.Domain(),
},
}
}
return &v2net.IPOrDomain{
Address: &v2net.IPOrDomain_Ip{
Ip: []byte(v.IP()),
},
}
return v2net.NewIPOrDomain(v.Address)
}
type Network string

View File

@ -63,11 +63,7 @@ func NewPoint(pConfig *Config) (*Point, error) {
dnsServer := dns.NewCacheServer(space, &dns.Config{
NameServers: []*v2net.Endpoint{
{
Address: &v2net.IPOrDomain{
Address: &v2net.IPOrDomain_Domain{
Domain: "localhost",
},
},
Address: v2net.NewIPOrDomain(v2net.LocalHostDomain),
},
},
})