-
Notifications
You must be signed in to change notification settings - Fork 10
/
packet.go
63 lines (58 loc) · 1.21 KB
/
packet.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
package shadowsocks
import (
"bytes"
"context"
"net"
)
type ListenPacket interface {
ListenPacket(ctx context.Context, network, address string) (net.PacketConn, error)
}
func decryptPacket(c ConnCipher, p BytesPool, dist, src []byte) (n int, addr net.Addr, err error) {
i, err := c.Decrypt(dist, src)
if err != nil {
return 0, nil, err
}
buf := bytes.NewBuffer(dist[:i])
a, err := readAddress(buf)
if err != nil {
return 0, nil, err
}
i = copy(dist, buf.Bytes())
return i, a, nil
}
func encryptPacket(c ConnCipher, p BytesPool, dist, src []byte, addr net.Addr) (n int, err error) {
a, err := parseAddress(addr.String())
if err != nil {
return 0, err
}
buf := getBytes(p)
defer putBytes(p, buf)
b := bytes.NewBuffer(buf[:0])
err = writeAddress(b, a)
if err != nil {
return 0, err
}
b.Write(src)
i, err := c.Encrypt(dist, b.Bytes())
if err != nil {
return 0, err
}
return i, nil
}
func toUDPAddr(addr net.Addr) (net.Addr, error) {
switch a := addr.(type) {
case *net.UDPAddr:
return addr, nil
case *address:
return &net.UDPAddr{
IP: a.IP,
Port: a.Port,
}, nil
default:
a, err := net.ResolveUDPAddr("udp", addr.String())
if err != nil {
return nil, err
}
return a, nil
}
}