-
Notifications
You must be signed in to change notification settings - Fork 0
/
address.go
201 lines (168 loc) · 4.46 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package net
import (
"net"
"v2ray.com/core/common/log"
"v2ray.com/core/common/predicate"
)
var (
LocalHostIP = IPAddress([]byte{127, 0, 0, 1})
AnyIP = IPAddress([]byte{0, 0, 0, 0})
)
type AddressFamily int
const (
AddressFamilyIPv4 = AddressFamily(0)
AddressFamilyIPv6 = AddressFamily(1)
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
Family() AddressFamily
String() string // String representation of this Address
Equals(Address) bool
}
// ParseAddress parses a string into an Address. The return value will be an IPAddress when
// the string is in the form of IPv4 or IPv6 address, or a DomainAddress otherwise.
func ParseAddress(addr string) Address {
ip := net.ParseIP(addr)
if ip != nil {
return IPAddress(ip)
}
return DomainAddress(addr)
}
// IPAddress creates an Address with given IP.
func IPAddress(ip []byte) Address {
switch len(ip) {
case net.IPv4len:
var addr ipv4Address = [4]byte{ip[0], ip[1], ip[2], ip[3]}
return &addr
case net.IPv6len:
if predicate.BytesAll(ip[0:10], 0) && predicate.BytesAll(ip[10:12], 0xff) {
return IPAddress(ip[12:16])
}
var addr ipv6Address = [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],
}
return &addr
default:
log.Error("Invalid IP format: ", ip)
return nil
}
}
// DomainAddress creates an Address with given domain.
func DomainAddress(domain string) Address {
var addr domainAddress = domainAddress(domain)
return &addr
}
type ipv4Address [4]byte
func (addr *ipv4Address) IP() net.IP {
return net.IP(addr[:])
}
func (addr *ipv4Address) Domain() string {
panic("Calling Domain() on an IPv4Address.")
}
func (addr *ipv4Address) Family() AddressFamily {
return AddressFamilyIPv4
}
func (this *ipv4Address) String() string {
return this.IP().String()
}
func (this *ipv4Address) Equals(another Address) bool {
anotherIPv4, ok := another.(*ipv4Address)
if !ok {
return false
}
return this[0] == anotherIPv4[0] &&
this[1] == anotherIPv4[1] &&
this[2] == anotherIPv4[2] &&
this[3] == anotherIPv4[3]
}
type ipv6Address [16]byte
func (addr *ipv6Address) IP() net.IP {
return net.IP(addr[:])
}
func (addr *ipv6Address) Domain() string {
panic("Calling Domain() on an IPv6Address.")
}
func (this *ipv6Address) Family() AddressFamily {
return AddressFamilyIPv6
}
func (this *ipv6Address) String() string {
return "[" + this.IP().String() + "]"
}
func (this *ipv6Address) Equals(another Address) bool {
anotherIPv6, ok := another.(*ipv6Address)
if !ok {
return false
}
return this[0] == anotherIPv6[0] &&
this[1] == anotherIPv6[1] &&
this[2] == anotherIPv6[2] &&
this[3] == anotherIPv6[3] &&
this[4] == anotherIPv6[4] &&
this[5] == anotherIPv6[5] &&
this[6] == anotherIPv6[6] &&
this[7] == anotherIPv6[7] &&
this[8] == anotherIPv6[8] &&
this[9] == anotherIPv6[9] &&
this[10] == anotherIPv6[10] &&
this[11] == anotherIPv6[11] &&
this[12] == anotherIPv6[12] &&
this[13] == anotherIPv6[13] &&
this[14] == anotherIPv6[14] &&
this[15] == anotherIPv6[15]
}
type domainAddress string
func (addr *domainAddress) IP() net.IP {
panic("Calling IP() on a DomainAddress.")
}
func (addr *domainAddress) Domain() string {
return string(*addr)
}
func (addr *domainAddress) Family() AddressFamily {
return AddressFamilyDomain
}
func (this *domainAddress) String() string {
return this.Domain()
}
func (this *domainAddress) Equals(another Address) bool {
anotherDomain, ok := another.(*domainAddress)
if !ok {
return false
}
return this.Domain() == anotherDomain.Domain()
}
func (this *IPOrDomain) AsAddress() Address {
if this == nil {
return nil
}
switch addr := this.Address.(type) {
case *IPOrDomain_Ip:
return IPAddress(addr.Ip)
case *IPOrDomain_Domain:
return DomainAddress(addr.Domain)
}
panic("Common|Net: Invalid address.")
}