forked from txthinking/brook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encrypt.go
170 lines (156 loc) · 3.82 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
166
167
168
169
170
// Copyright (c) 2016-present Cloud <[email protected]>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of version 3 of the GNU General Public
// License as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package brook
import (
"bytes"
"encoding/binary"
"errors"
"io"
"net"
"strconv"
"time"
"github.com/txthinking/socks5"
"github.com/txthinking/x"
)
// IncrementNonce loves your compute to use Little 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 := x.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 = x.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(x.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 := x.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 = x.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 x.HkdfSha256RandomSalt(p, []byte{0x62, 0x72, 0x6f, 0x6f, 0x6b}, 12)
}
// GetKey.
func GetKey(p, n []byte) ([]byte, error) {
return x.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 = x.AESGCMEncrypt(b, k, n)
if err != nil {
return nil, err
}
b = append(n, b...)
return b, nil
}
// Decrypt data.
func Decrypt(p, b []byte) (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 := x.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(x.Random(1, 60*10)) * time.Second)
err = errors.New("Expired request")
return
}
bb = bb[10:]
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
}