forked from v2fly/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
address.go
155 lines (124 loc) · 3.25 KB
/
address.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package net
import (
"net"
"strconv"
"github.com/v2ray/v2ray-core/common/errors"
"github.com/v2ray/v2ray-core/common/log"
)
// 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
Port() uint16 // Port of this Address
PortBytes() []byte // Port in bytes, network byte order
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
}
// IPAddress creates an Address with given IP and port.
func IPAddress(ip []byte, port uint16) Address {
switch len(ip) {
case net.IPv4len:
return IPv4Address{
PortAddress: PortAddress{port: port},
ip: [4]byte{ip[0], ip[1], ip[2], ip[3]},
}
case net.IPv6len:
return IPv6Address{
PortAddress: PortAddress{port: port},
ip: [16]byte{
ip[0], ip[1], ip[2], ip[3],
ip[4], ip[5], ip[6], ip[7],
ip[8], ip[9], ip[10], ip[11],
ip[12], ip[13], ip[14], ip[15],
},
}
default:
log.Error(errors.NewIPFormatError(ip).Error())
return nil
}
}
// DomainAddress creates an Address with given domain and port.
func DomainAddress(domain string, port uint16) Address {
return DomainAddressImpl{
domain: domain,
PortAddress: PortAddress{port: port},
}
}
type PortAddress struct {
port uint16
}
func (addr PortAddress) Port() uint16 {
return addr.port
}
func (addr PortAddress) PortBytes() []byte {
return []byte{byte(addr.port >> 8), byte(addr.port)}
}
type IPv4Address struct {
PortAddress
ip [4]byte
}
func (addr IPv4Address) IP() net.IP {
return net.IP(addr.ip[:])
}
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) String() string {
return addr.IP().String() + ":" + strconv.Itoa(int(addr.PortAddress.port))
}
type IPv6Address struct {
PortAddress
ip [16]byte
}
func (addr IPv6Address) IP() net.IP {
return net.IP(addr.ip[:])
}
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 (addr IPv6Address) String() string {
return "[" + addr.IP().String() + "]:" + strconv.Itoa(int(addr.PortAddress.port))
}
type DomainAddressImpl struct {
PortAddress
domain string
}
func (addr DomainAddressImpl) IP() net.IP {
panic("Calling IP() on a DomainAddress.")
}
func (addr DomainAddressImpl) Domain() string {
return addr.domain
}
func (addr DomainAddressImpl) IsIPv4() bool {
return false
}
func (addr DomainAddressImpl) IsIPv6() bool {
return false
}
func (addr DomainAddressImpl) IsDomain() bool {
return true
}
func (addr DomainAddressImpl) String() string {
return addr.domain + ":" + strconv.Itoa(int(addr.PortAddress.port))
}