-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
server.go
42 lines (35 loc) · 898 Bytes
/
server.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
package obfuscated2
import (
"encoding/binary"
"io"
)
// Metadata represents metadata received from header.
type Metadata struct {
Protocol [4]byte
DC uint16
}
// Accept creates new io.ReadWriter for server-side deobfuscation.
func Accept(conn io.ReadWriter, secret []byte) (io.ReadWriter, Metadata, error) {
var (
buf = make([]byte, 64)
meta Metadata
)
if _, err := io.ReadFull(conn, buf); err != nil {
return nil, meta, err
}
var k keys
if err := k.createStreams(buf, secret); err != nil {
return nil, meta, err
}
// Swap to match client's streams.
k.encrypt, k.decrypt = k.decrypt, k.encrypt
var decrypted [64]byte
k.decrypt.XORKeyStream(decrypted[:], buf)
copy(meta.Protocol[:], decrypted[56:60])
meta.DC = binary.LittleEndian.Uint16(decrypted[60:62])
return &Obfuscated2{
rand: nil, // Used only in Handshake.
conn: conn,
keys: k,
}, meta, nil
}