forked from txthinking/brook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encrypt.go
165 lines (152 loc) · 3.42 KB
/
encrypt.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
package brook
import (
"bytes"
"encoding/binary"
"errors"
"io"
"net"
"strconv"
"time"
"github.com/txthinking/ant"
"github.com/txthinking/brook/plugin"
"github.com/txthinking/socks5"
)
// IncrementNonce loves your compute to use Litter Endian
func IncrementNonce(n []byte) []byte {
i := int(binary.LittleEndian.Uint16(n))
i += 1
n = make([]byte, 12)
binary.LittleEndian.PutUint16(n, uint16(i))
return n
}
// ReadFrom
func ReadFrom(c *net.TCPConn, k, n []byte, hasTime bool) ([]byte, []byte, error) {
b := make([]byte, 18)
if _, err := io.ReadFull(c, b); err != nil {
return nil, nil, err
}
n = IncrementNonce(n)
d, err := ant.AESGCMDecrypt(b, k, n)
if err != nil {
return nil, nil, err
}
l := int(binary.BigEndian.Uint16(d))
b = make([]byte, l)
if _, err := io.ReadFull(c, b); err != nil {
return nil, nil, err
}
n = IncrementNonce(n)
d, err = ant.AESGCMDecrypt(b, k, n)
if err != nil {
return nil, nil, err
}
if hasTime {
i, err := strconv.Atoi(string(d[0:10]))
if err != nil {
return nil, nil, err
}
if time.Now().Unix()-int64(i) > 90 {
time.Sleep(time.Duration(ant.Random(1, 60*10)) * time.Second)
return nil, nil, errors.New("Expired request")
}
d = d[10:]
}
return d, n, nil
}
// WriteTo
func WriteTo(c *net.TCPConn, d, k, n []byte, needTime bool) ([]byte, error) {
if needTime {
d = append(bytes.NewBufferString(strconv.Itoa(int(time.Now().Unix()))).Bytes(), d...)
}
i := len(d) + 16
bb := make([]byte, 2)
binary.BigEndian.PutUint16(bb, uint16(i))
n = IncrementNonce(n)
b, err := ant.AESGCMEncrypt(bb, k, n)
if err != nil {
return nil, err
}
if _, err := c.Write(b); err != nil {
return nil, err
}
n = IncrementNonce(n)
b, err = ant.AESGCMEncrypt(d, k, n)
if err != nil {
return nil, err
}
if _, err := c.Write(b); err != nil {
return nil, err
}
return n, nil
}
// PrepareKey
func PrepareKey(p []byte) ([]byte, []byte, error) {
return ant.HkdfSha256RandomSalt(p, []byte{0x62, 0x72, 0x6f, 0x6f, 0x6b}, 12)
}
// GetKey
func GetKey(p, n []byte) ([]byte, error) {
return ant.HkdfSha256WithSalt(p, n, []byte{0x62, 0x72, 0x6f, 0x6f, 0x6b})
}
// Encrypt data
func Encrypt(p, b []byte) ([]byte, error) {
b = append(bytes.NewBufferString(strconv.Itoa(int(time.Now().Unix()))).Bytes(), b...)
k, n, err := PrepareKey(p)
if err != nil {
return nil, err
}
b, err = ant.AESGCMEncrypt(b, k, n)
if err != nil {
return nil, err
}
b = append(n, b...)
return b, nil
}
// Decrypt data
func Decrypt(p, b []byte, token plugin.TokenChecker) (a byte, addr, port, data []byte, err error) {
err = errors.New("Data length error")
if len(b) <= 12+16 {
return
}
k, err := GetKey(p, b[0:12])
bb, err := ant.AESGCMDecrypt(b[12:], k, b[0:12])
if err != nil {
return
}
i, err := strconv.Atoi(string(bb[0:10]))
if err != nil {
return
}
if time.Now().Unix()-int64(i) > 90 {
time.Sleep(time.Duration(ant.Random(1, 60*10)) * time.Second)
err = errors.New("Expired request")
return
}
bb = bb[10:]
if token != nil {
l := int(binary.BigEndian.Uint16(bb[0:2]))
t := bb[2 : l+2]
if err = token.Check(t); err != nil {
return
}
bb = b[l+2:]
}
a = bb[0]
if a == socks5.ATYPIPv4 {
addr = bb[1:5]
port = bb[5:7]
data = bb[7:]
} else if a == socks5.ATYPIPv6 {
addr = bb[1:17]
port = bb[17:19]
data = bb[19:]
} else if a == socks5.ATYPDomain {
l := int(bb[1])
addr = bb[1 : 1+l]
port = bb[1+l : 1+l+2]
data = bb[1+l+2:]
} else {
return
}
err = nil
return
}