This repository has been archived by the owner on May 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathconn_raw.go
498 lines (445 loc) · 13.8 KB
/
conn_raw.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
package tapdance
import (
"bytes"
"encoding/binary"
"encoding/hex"
"errors"
"io"
"net"
"strconv"
"strings"
"sync"
"time"
"github.com/golang/protobuf/proto"
"github.com/refraction-networking/utls"
)
// Simply establishes TLS and TapDance connection.
// Both reader and writer flows shall have this underlying raw connection.
// Knows about but doesn't keep track of timeout and upload limit
type tdRawConn struct {
tcpConn closeWriterConn
tlsConn *tls.UConn
flowId uint64
sessionId uint64
strIdSuffix string
customDialer func(string, string) (net.Conn, error)
decoySpec TLSDecoySpec
establishedAt time.Time
pinDecoySpec bool // don't ever change decoy (still changeable from outside)
remoteConnId []byte
stationPubkey []byte
failedDecoys []string
initialMsg StationToClient
tagType tdTagType
UploadLimit int // used only in POST-based tags
closed chan struct{}
closeOnce sync.Once
}
// TODO: move to assets?
func (ds *TLSDecoySpec) GetIpv4AddrStr() string {
if ds.Ipv4Addr != nil {
ip := make(net.IP, 4)
binary.BigEndian.PutUint32(ip, ds.GetIpv4Addr())
// TODO: what checks need to be done, and what's guaranteed?
ipv4Str := ip.To4().String() + ":443"
return ipv4Str
} else {
return ""
}
}
func makeTdRaw(handshakeType tdTagType,
stationPubkey []byte,
remoteConnId []byte) *tdRawConn {
tdRaw := &tdRawConn{tagType: handshakeType,
stationPubkey: stationPubkey,
remoteConnId: remoteConnId}
tdRaw.flowId = 0
tdRaw.closed = make(chan struct{})
return tdRaw
}
func (tdRaw *tdRawConn) Redial() error {
tdRaw.flowId += 1
return tdRaw.dial(true)
}
func (tdRaw *tdRawConn) Dial() error {
return tdRaw.dial(false)
}
func (tdRaw *tdRawConn) dial(reconnect bool) error {
var connection_attempts int
var err error
/*
// Randomize tdConn.maxSend to avoid heuristics
tdConn.maxSend = getRandInt(sendLimitMin, sendLimitMax)
tdConn.maxSend -= transitionMsgSize // reserve space for transition msg
tdConn.maxSend -= 2 // reserve 2 bytes for transition msg header
*/
var expectedTransition S2C_Transition
if reconnect {
connection_attempts = 2
expectedTransition = S2C_Transition_S2C_CONFIRM_RECONNECT
tdRaw.tlsConn.Close()
} else {
connection_attempts = 6
expectedTransition = S2C_Transition_S2C_SESSION_INIT
}
for i := 0; i < connection_attempts; i++ {
if tdRaw.IsClosed() {
return errors.New("Closed")
}
// sleep to prevent overwhelming decoy servers
if waitTime := sleepBeforeConnect(i); waitTime != nil {
select {
case <-waitTime:
case <-tdRaw.closed:
return errors.New("Closed")
}
}
if tdRaw.pinDecoySpec {
if tdRaw.decoySpec.Ipv4Addr == nil {
return errors.New("decoySpec is pinned, but empty!")
}
} else {
if !reconnect {
tdRaw.decoySpec = Assets().GetDecoy()
if tdRaw.decoySpec.GetIpv4AddrStr() == "" {
return errors.New("tdConn.decoyAddr is empty!")
}
}
}
err = tdRaw.tryDialOnce(expectedTransition)
if err == nil {
return err
} else {
tdRaw.failedDecoys = append(tdRaw.failedDecoys,
tdRaw.decoySpec.GetHostname()+" "+tdRaw.decoySpec.GetIpv4AddrStr())
}
}
return err
}
func (tdRaw *tdRawConn) tryDialOnce(expectedTransition S2C_Transition) (err error) {
Logger().Infoln(tdRaw.idStr() + " Attempting to connect to decoy " +
tdRaw.decoySpec.GetHostname() + " (" + tdRaw.decoySpec.GetIpv4AddrStr() + ")")
err = tdRaw.establishTLStoDecoy()
if err != nil {
Logger().Errorf(tdRaw.idStr() + " establishTLStoDecoy(" +
tdRaw.decoySpec.GetHostname() + "," + tdRaw.decoySpec.GetIpv4AddrStr() +
") failed with " + err.Error())
return err
} else {
Logger().Infof(tdRaw.idStr() + " Connected to decoy " +
tdRaw.decoySpec.GetHostname() + " (" + tdRaw.decoySpec.GetIpv4AddrStr() + ")")
}
if tdRaw.IsClosed() {
// if connection was closed externally while in establishTLStoDecoy()
tdRaw.tlsConn.Close()
return errors.New("Closed")
}
// Check if cipher is supported
cipherIsSupported := func(id uint16) bool {
for _, c := range TDSupportedCiphers {
if c == id {
return true
}
}
return false
}
if !cipherIsSupported(tdRaw.tlsConn.ConnectionState().CipherSuite) {
Logger().Errorf("%s decoy %s offered unsupported cipher %d\n Client ciphers: %#v\n",
tdRaw.idStr(), tdRaw.decoySpec.GetHostname(),
tdRaw.tlsConn.ConnectionState().CipherSuite,
tdRaw.tlsConn.HandshakeState.Hello.CipherSuites)
err = errors.New("Unsupported cipher.")
tdRaw.tlsConn.Close()
return err
}
tdRaw.tlsConn.SetDeadline(time.Now().Add(deadlineConnectTDStation * time.Second))
var tdRequest string
tdRequest, err = tdRaw.prepareTDRequest(tdRaw.tagType)
if err != nil {
Logger().Errorf(tdRaw.idStr() +
" Preparation of initial TD request failed with " + err.Error())
tdRaw.tlsConn.Close()
return
}
tdRaw.establishedAt = time.Now() // TODO: recheck how ClientConf's timeout is calculated and move, if needed
Logger().Infoln(tdRaw.idStr() + " Attempting to connect to TapDance Station" +
" with connection ID: " + hex.EncodeToString(tdRaw.remoteConnId[:]) + ", method: " +
tdRaw.tagType.Str())
_, err = tdRaw.tlsConn.Write([]byte(tdRequest))
if err != nil {
Logger().Errorf(tdRaw.idStr() +
" Could not send initial TD request, error: " + err.Error())
tdRaw.tlsConn.Close()
return
}
switch tdRaw.tagType {
case tagHttpGetIncomplete:
tdRaw.initialMsg, err = tdRaw.readProto()
if err != nil {
str_err := err.Error()
if strings.Contains(str_err, ": i/o timeout") || // client timed out
err.Error() == "EOF" {
// decoy timed out
/*
TODO: to fix https://github.com/SergeyFrolov/gotapdance/issues/38
Generate ascii-range message with same length as tdRequest
Make sure it ends with "/r/n/r/n", send it and forget(don't close!)
On the wire this will look like spurious retransmission
*/
err = errors.New("TapDance station didn't pick up the request: " + str_err)
Logger().Errorf(tdRaw.idStr() + " " + err.Error())
} else {
// any other error will be fatal
Logger().Errorf(tdRaw.idStr() +
" fatal error reading from TapDance station: " +
err.Error())
return
}
tdRaw.tlsConn.Close()
return
}
if tdRaw.initialMsg.GetStateTransition() != expectedTransition {
err = errors.New("Init error: state transition mismatch!" +
" Received: " + tdRaw.initialMsg.GetStateTransition().String() +
" Expected: " + expectedTransition.String())
// this exceptional error implies that station has lost state, thus is fatal
return err
} else {
Logger().Infoln(tdRaw.idStr() + " Successfully connected to TapDance Station")
}
case tagHttpPostIncomplete:
// don't wait for response
default:
panic("Unsupported td handshake type:" + tdRaw.tagType.Str())
}
// TapDance should NOT have a timeout, timeouts have to be handled by client and server
tdRaw.tlsConn.SetDeadline(time.Time{}) // unsets timeout
/*
if !reconnect && len(tdConn.failedDecoys) > 0 {
tdConn.writeListFailedDecoys()
}*/
return nil
}
func (tdRaw *tdRawConn) establishTLStoDecoy() (err error) {
var dialConn net.Conn
if tdRaw.customDialer != nil {
dialConn, err = tdRaw.customDialer("tcp", tdRaw.decoySpec.GetIpv4AddrStr())
if err != nil {
return err
}
} else {
dialConn, err = net.DialTimeout("tcp", tdRaw.decoySpec.GetIpv4AddrStr(),
deadlineTCPtoDecoy*time.Second)
if err != nil {
return err
}
}
config := tls.Config{ServerName: tdRaw.decoySpec.GetHostname()}
if config.ServerName == "" {
// if SNI is unset -- try IP
config.ServerName, _, err = net.SplitHostPort(tdRaw.decoySpec.GetIpv4AddrStr())
if err != nil {
dialConn.Close()
return
}
Logger().Infoln(tdRaw.idStr() + ": SNI was nil. Setting it to" +
config.ServerName)
}
tdRaw.tlsConn = tls.UClient(dialConn, &config, tls.HelloRandomizedNoALPN)
err = tdRaw.tlsConn.BuildHandshakeState()
if err != nil {
dialConn.Close()
return
}
tdRaw.tlsConn.HandshakeState.Hello.CipherSuites =
forceSupportedCiphersFirst(tdRaw.tlsConn.HandshakeState.Hello.CipherSuites)
err = tdRaw.tlsConn.MarshalClientHello()
if err != nil {
dialConn.Close()
return
}
err = tdRaw.tlsConn.Handshake()
if err != nil {
dialConn.Close()
return
}
closeWriter, ok := dialConn.(closeWriterConn)
if !ok {
return errors.New("dialConn is not a closeWriter")
}
tdRaw.tcpConn = closeWriter
return
}
func (tdRaw *tdRawConn) Close() error {
var err error
tdRaw.closeOnce.Do(func() {
close(tdRaw.closed)
if tdRaw.tlsConn != nil {
err = tdRaw.tlsConn.Close()
}
})
return err
}
type closeWriterConn interface {
net.Conn
CloseWrite() error
}
func (tdRaw *tdRawConn) closeWrite() error {
return tdRaw.tcpConn.CloseWrite()
}
func (tdRaw *tdRawConn) prepareTDRequest(handshakeType tdTagType) (string, error) {
// Generate initial TapDance request
buf := new(bytes.Buffer) // What we have to encrypt with the shared secret using AES
master_key := tdRaw.tlsConn.HandshakeState.MasterSecret
// write flags
flags := tdFlagUseTIL
if tdRaw.tagType == tagHttpPostIncomplete {
flags |= tdFlagUploadOnly
}
if err := binary.Write(buf, binary.BigEndian, flags); err != nil {
return "", err
}
buf.Write(master_key[:])
buf.Write(tdRaw.tlsConn.HandshakeState.ServerHello.Random)
buf.Write(tdRaw.tlsConn.HandshakeState.Hello.Random)
buf.Write(tdRaw.remoteConnId[:]) // connection id for persistence
tag, err := obfuscateTag(buf.Bytes(), tdRaw.stationPubkey) // What we encode into the ciphertext
if err != nil {
return "", err
}
return tdRaw.genHTTP1Tag(tag)
}
// mutates tdRaw: sets tdRaw.UploadLimit
func (tdRaw *tdRawConn) genHTTP1Tag(tag []byte) (string, error) {
var httpTag string
switch tdRaw.tagType {
// for complete copy http generator of golang
case tagHttpGetComplete:
fallthrough
case tagHttpGetIncomplete:
tdRaw.UploadLimit = int(tdRaw.decoySpec.GetTcpwin()) - getRandInt(1, 1045)
httpTag = `GET / HTTP/1.1
Host: ` + tdRaw.decoySpec.GetHostname() + `
User-Agent: TapDance/1.2 (+https://tapdance.team/info)
Accept-Encoding: None
X-Ignore: ` + getRandPadding(7, 612, 10)
httpTag = strings.Replace(httpTag, "\n", "\r\n", -1)
case tagHttpPostIncomplete:
ContentLength := getRandInt(900000, 1045000)
tdRaw.UploadLimit = ContentLength - 1
httpTag = `POST / HTTP/1.1
Accept-Encoding: None
Host: ` + tdRaw.decoySpec.GetHostname() + `
User-Agent: TapDance/1.2 (+https://tapdance.team/info)
X-Padding: ` + getRandPadding(1, 461, 10) + `
Content-Type: application/zip; boundary=----WebKitFormBoundaryaym16ehT29q60rUx
Content-Length: ` + strconv.Itoa(ContentLength) + `
----WebKitFormBoundaryaym16ehT29q60rUx
Content-Disposition: form-data; name=\"td.zip\"
`
httpTag = strings.Replace(httpTag, "\n", "\r\n", -1)
}
keystreamOffset := len(httpTag)
keystreamSize := (len(tag)/3+1)*4 + keystreamOffset // we can't use first 2 bits of every byte
whole_keystream, err := tdRaw.tlsConn.GetOutKeystream(keystreamSize)
if err != nil {
return httpTag, err
}
keystreamAtTag := whole_keystream[keystreamOffset:]
httpTag += reverseEncrypt(tag, keystreamAtTag)
if tdRaw.tagType == tagHttpGetComplete {
httpTag += "\r\n\r\n"
}
Logger().Debugf("Generated HTTP TAG:\n%s\n", httpTag)
return httpTag, nil
}
func (tdRaw *tdRawConn) idStr() string {
return "[Session " + strconv.FormatUint(tdRaw.sessionId, 10) + ", " +
"Flow " + strconv.FormatUint(tdRaw.flowId, 10) + tdRaw.strIdSuffix + "]"
}
// Simply reads and returns protobuf
// Returns error if it's not a protobuf
func (tdRaw *tdRawConn) readProto() (msg StationToClient, err error) {
var readBytes int
var readBytesTotal uint32 // both header and body
headerSize := uint32(2)
var msgLen uint32 // just the body(e.g. raw data or protobuf)
var outerProtoMsgType MsgType
headerBuffer := make([]byte, 6) // TODO: allocate once at higher level?
for readBytesTotal < headerSize {
readBytes, err = tdRaw.tlsConn.Read(headerBuffer[readBytesTotal:headerSize])
readBytesTotal += uint32(readBytes)
if err != nil {
return
}
}
// Get TIL
typeLen := Uint16toInt16(binary.BigEndian.Uint16(headerBuffer[0:2]))
if typeLen < 0 {
outerProtoMsgType = msg_raw_data
msgLen = uint32(-typeLen)
} else if typeLen > 0 {
outerProtoMsgType = msg_protobuf
msgLen = uint32(typeLen)
} else {
// protobuf with size over 32KB, not fitting into 2-byte TL
outerProtoMsgType = msg_protobuf
headerSize += 4
for readBytesTotal < headerSize {
readBytes, err = tdRaw.tlsConn.Read(headerBuffer[readBytesTotal:headerSize])
readBytesTotal += uint32(readBytes)
if err == io.EOF && readBytesTotal == headerSize {
break
}
if err != nil {
return
}
}
msgLen = binary.BigEndian.Uint32(headerBuffer[2:6])
}
if outerProtoMsgType == msg_raw_data {
err = errors.New("Received data message in uninitialized flow")
return
}
totalBytesToRead := headerSize + msgLen
read_buffer := make([]byte, msgLen)
// Get the message itself
for readBytesTotal < totalBytesToRead {
readBytes, err = tdRaw.tlsConn.Read(read_buffer[readBytesTotal-headerSize : msgLen])
readBytesTotal += uint32(readBytes)
if err != nil {
return
}
}
err = proto.Unmarshal(read_buffer[:], &msg)
if err != nil {
return
}
Logger().Debugln(tdRaw.idStr() + " INIT: received protobuf: " + msg.String())
return
}
// Generates padding and stuff
// Currently guaranteed to be less than 1024 bytes long
func (tdRaw *tdRawConn) writeTransition(transition C2S_Transition) (n int, err error) {
currGen := Assets().GetGeneration()
msg := ClientToStation{Padding: []byte(getRandPadding(150, 900, 10)),
DecoyListGeneration: &currGen,
StateTransition: &transition,
UploadSync: new(uint64)} // TODO: remove
msgBytes, err := proto.Marshal(&msg)
if err != nil {
return
}
Logger().Infoln(tdRaw.idStr()+" sending transition: ", msg.String())
b := getMsgWithHeader(msg_protobuf, msgBytes)
n, err = tdRaw.tlsConn.Write(b)
return
}
func (tdRaw *tdRawConn) IsClosed() bool {
select {
case <-tdRaw.closed:
return true
default:
return false
}
}