-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpack.go
282 lines (260 loc) · 5.83 KB
/
pack.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Read / write softether packs
package main
import (
"crypto/sha1"
"encoding/binary"
"errors"
"io"
"net"
"strings"
"golang.org/x/crypto/cryptobyte"
)
type valueType uint32
const (
valueInt valueType = iota
valueData
valueString
valueUniString
valueInt64
)
const (
maxPackSize = 10 * 1024
)
type packElement struct {
valType valueType
values []any
}
type pack struct {
elements map[string]packElement
}
func (p *pack) marshal() ([]byte, error) {
var cb cryptobyte.Builder
num := len(p.elements)
cb.AddUint32(uint32(num))
for key, element := range p.elements {
b := []byte(key)
cb.AddUint32(uint32(len(b)) + 1)
cb.AddBytes(b)
cb.AddUint32(uint32(element.valType))
cb.AddUint32(uint32(len(element.values)))
for _, v := range element.values {
switch element.valType {
case valueInt:
cb.AddUint32(v.(uint32))
case valueInt64:
cb.AddUint64(v.(uint64))
case valueData:
cb.AddUint32LengthPrefixed(func(b *cryptobyte.Builder) {
b.AddBytes(v.([]byte))
})
case valueString, valueUniString:
cb.AddUint32LengthPrefixed(func(b *cryptobyte.Builder) {
b.AddBytes([]byte(v.(string)))
})
}
}
}
return cb.Bytes()
}
func (p *pack) unmarshal(data []byte) bool {
cb := cryptobyte.String(data)
var num uint32
if !cb.ReadUint32(&num) {
return false
}
p.elements = make(map[string]packElement)
for i := 0; i < int(num); i++ {
var n uint32
if !cb.ReadUint32(&n) || n <= 1 || n > 64 {
return false
}
var b []byte
if !cb.ReadBytes(&b, int(n-1)) {
return false
}
name := strings.ToLower(string(b))
if !cb.ReadUint32(&n) {
return false
}
element := packElement{valType: valueType(n)}
if !cb.ReadUint32(&n) {
return false
}
for j := 0; j < int(n); j++ {
switch element.valType {
case valueInt:
var v uint32
if !cb.ReadUint32(&v) {
return false
}
element.values = append(element.values, v)
case valueInt64:
var v uint64
if !cb.ReadUint64(&v) {
return false
}
element.values = append(element.values, v)
case valueData:
var n uint32
if !cb.ReadUint32(&n) {
return false
}
var v []byte
if !cb.ReadBytes(&v, int(n)) {
return false
}
element.values = append(element.values, v)
case valueString, valueUniString:
var n uint32
if !cb.ReadUint32(&n) {
return false
}
var v []byte
if !cb.ReadBytes(&v, int(n)) {
return false
}
element.values = append(element.values, string(v))
}
}
p.elements[name] = element
}
return true
}
func recvPack(conn io.Reader, hash bool) (pack, error) {
b := make([]byte, 4)
if _, err := io.ReadFull(conn, b); err != nil {
return pack{}, err
}
n := binary.BigEndian.Uint32(b)
if n > maxPackSize {
return pack{}, errors.New("pack size too large")
}
b = make([]byte, n)
if _, err := io.ReadFull(conn, b); err != nil {
return pack{}, err
}
if hash {
var h [20]byte
if _, err := io.ReadFull(conn, h[:]); err != nil {
return pack{}, err
}
if sha1.Sum(b) != h {
return pack{}, errors.New("invalid checksum")
}
}
var p pack
if p.unmarshal(b) {
return p, nil
}
return pack{}, errors.New("invalid pack")
}
func (p *pack) send(conn io.Writer, hash bool) (int, error) {
var cb cryptobyte.Builder
data, err := p.marshal()
if err != nil {
return 0, err
}
cb.AddUint32LengthPrefixed(func(b *cryptobyte.Builder) {
b.AddBytes(data)
})
b, err := cb.Bytes()
if err != nil {
return 0, err
}
if hash {
h := sha1.Sum(data)
b = append(b, h[:]...)
}
return conn.Write(b)
}
func (p *pack) getInt(key string) (uint32, bool) {
k := strings.ToLower(key)
e, ok := p.elements[k]
if !ok || e.valType != valueInt || len(e.values) == 0 {
return 0, false
}
v, ok := e.values[0].(uint32)
if !ok {
return 0, false
}
return v, true
}
func (p *pack) getInt64(key string) (uint64, bool) {
k := strings.ToLower(key)
e, ok := p.elements[k]
if !ok || e.valType != valueInt64 || len(e.values) == 0 {
return 0, false
}
v, ok := e.values[0].(uint64)
if !ok {
return 0, false
}
return v, true
}
func (p *pack) getData(key string) ([]byte, bool) {
k := strings.ToLower(key)
e, ok := p.elements[k]
if !ok || e.valType != valueData || len(e.values) == 0 {
return nil, false
}
v, ok := e.values[0].([]byte)
if !ok {
return nil, false
}
return v, true
}
func (p *pack) getString(key string, lowercase bool) (string, bool) {
k := strings.ToLower(key)
e, ok := p.elements[k]
if !ok || e.valType != valueString || len(e.values) == 0 {
return "", false
}
v, ok := e.values[0].(string)
if !ok {
return "", false
}
if lowercase {
v = strings.ToLower(v)
}
return v, true
}
func newPackElementInt(n uint32) packElement {
e := packElement{valType: valueInt}
e.values = append(e.values, n)
return e
}
func newPackElementInt64(n uint64) packElement {
e := packElement{valType: valueInt64}
e.values = append(e.values, n)
return e
}
func newPackElementData(b []byte) packElement {
e := packElement{valType: valueData}
e.values = append(e.values, b)
return e
}
func newPackElementString(s string) packElement {
e := packElement{valType: valueString}
e.values = append(e.values, s)
return e
}
func (p *pack) addIP(field string, ip net.IP) {
if ip4 := ip.To4(); ip4 != nil {
p.elements[field+"@ipv6_bool"] = newPackElementInt(0)
p.elements[field+"@ipv6_array"] = newPackElementData(net.IPv6zero)
p.elements[field+"@ipv6_scope_id"] = newPackElementInt(0)
p.elements[field] = newPackElementInt(ip4ToUInt32(ip4))
} else {
p.elements[field+"@ipv6_bool"] = newPackElementInt(1)
p.elements[field+"@ipv6_array"] = newPackElementData(ip)
p.elements[field+"@ipv6_scope_id"] = newPackElementInt(0)
p.elements[field] = newPackElementInt(0)
}
}
// Convert IPv4 address to uint32 in little endian
func ip4ToUInt32(ip net.IP) uint32 {
if len(ip) != net.IPv4len {
return 0
}
return binary.LittleEndian.Uint32(ip)
}