-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaddress.go
56 lines (44 loc) · 868 Bytes
/
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
package socks5
import (
"encoding/binary"
"fmt"
"net"
"strconv"
)
type address struct {
Type byte
IP net.IP
Port port
Domain []byte
DomainLen byte
}
func (a address) String() string {
if a.IP != nil {
host := a.IP.String()
port := a.Port.String()
return net.JoinHostPort(host, port)
}
return fmt.Sprintf("%s:%s", a.Domain, a.Port)
}
func (a address) getDomainOrIP() string {
if a.IP != nil {
return a.IP.String()
}
return string(a.Domain)
}
type port []byte
func (p port) String() string {
return fmt.Sprintf("%d", binary.BigEndian.Uint16(p))
}
func (p *port) fromAddress(address net.Addr) {
_, port, err := net.SplitHostPort(address.String())
if err != nil {
return
}
i, err := strconv.ParseInt(port, 10, 64)
if err != nil {
return
}
*p = make([]byte, 2)
binary.BigEndian.PutUint16(*p, uint16(i))
}