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:
parent
f47c21c422
commit
3b5a791ae8
@ -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.")
|
||||
}
|
||||
}
|
||||
|
@ -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,
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
|
6
v2ray.go
6
v2ray.go
@ -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),
|
||||
},
|
||||
},
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user