-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheader.go
58 lines (48 loc) · 1.27 KB
/
header.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
package gospp
import "net"
// Decode will try to decode SPP Header from a bytes array, and returns new buffer with no SPP message in it.
func Decode(buf []byte) (hdr *Header, newBuf []byte, err error) {
if len(buf) < sppMsgLength {
err = ErrBufferShort
return
}
var h rawHeader = buf[:sppMsgLength]
if !h.IsMagicMatch() {
err = ErrNoMagic
return
}
hdr = h.ToStruct()
if len(buf) > sppMsgLength {
newBuf = buf[sppMsgLength:]
}
return
}
// rawHeader is used as a storage and decoder
type rawHeader []byte
func (h rawHeader) IsMagicMatch() bool {
return h[0] == sppMagic[0] && h[1] == sppMagic[1]
}
func (h rawHeader) GetClientAddr() net.IP {
ip := make(net.IP, net.IPv6len)
copy(ip, h[clientAddrOffset:proxyAddrOffset])
return ip
}
func (h rawHeader) GetProxyAddr() net.IP {
ip := make(net.IP, net.IPv6len)
copy(ip, h[proxyAddrOffset:clientPortOffset])
return ip
}
func (h rawHeader) GetClientPort() uint16 {
return byteOrder.Uint16(h[clientPortOffset:proxyPortOffset])
}
func (h rawHeader) GetProxyPort() uint16 {
return byteOrder.Uint16(h[proxyPortOffset:sppMsgLength])
}
func (h rawHeader) ToStruct() *Header {
return &Header{
ClientAddr: h.GetClientAddr(),
ProxyAddr: h.GetProxyAddr(),
ClientPort: h.GetClientPort(),
ProxyPort: h.GetProxyPort(),
}
}