forked from v2ray/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdestination.go
61 lines (52 loc) · 1.43 KB
/
destination.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
package net
import (
"net"
)
// Destination represents a network destination including address and protocol (tcp / udp).
type Destination struct {
Network Network
Address Address
Port Port
}
func DestinationFromAddr(addr net.Addr) Destination {
switch addr := addr.(type) {
case *net.TCPAddr:
return TCPDestination(IPAddress(addr.IP), Port(addr.Port))
case *net.UDPAddr:
return UDPDestination(IPAddress(addr.IP), Port(addr.Port))
default:
panic("Unknown address type.")
}
}
// TCPDestination creates a TCP destination with given address
func TCPDestination(address Address, port Port) Destination {
return Destination{
Network: Network_TCP,
Address: address,
Port: port,
}
}
// UDPDestination creates a UDP destination with given address
func UDPDestination(address Address, port Port) Destination {
return Destination{
Network: Network_UDP,
Address: address,
Port: port,
}
}
func (this Destination) NetAddr() string {
return this.Address.String() + ":" + this.Port.String()
}
func (this Destination) String() string {
return this.Network.UrlPrefix() + ":" + this.NetAddr()
}
func (this Destination) Equals(another Destination) bool {
return this.Network == another.Network && this.Port == another.Port && this.Address.Equals(another.Address)
}
func (this *Endpoint) AsDestination() Destination {
return Destination{
Network: this.Network,
Address: this.Address.AsAddress(),
Port: Port(this.Port),
}
}