Parcourir la source

Introduce pools to obfuscated2

tags/v2.0.0-rc1
9seconds il y a 5 ans
Parent
révision
3a44bcb854

+ 20
- 7
mtglib/internal/obfuscated2/client_handshake.go Voir le fichier

@@ -5,12 +5,20 @@ import (
5 5
 	"crypto/subtle"
6 6
 	"encoding/hex"
7 7
 	"fmt"
8
+	"io"
8 9
 )
9 10
 
10 11
 // Connection Type secure. We support only fake tls.
11 12
 var clientHandshakeMagic = []byte{0xdd, 0xdd, 0xdd, 0xdd}
12 13
 
13
-func ClientHandshake(secret []byte, handshakeFrame *HandhakeFrame) (int16, cipher.Stream, cipher.Stream, error) {
14
+func ClientHandshake(secret []byte, reader io.Reader) (int16, cipher.Stream, cipher.Stream, error) {
15
+	handshakeFrame := acquireHandshakeFrame()
16
+	defer releaseHandshakeFrame(handshakeFrame)
17
+
18
+	if _, err := io.ReadFull(reader, handshakeFrame.data[:]); err != nil {
19
+		return 0, nil, nil, fmt.Errorf("cannot read frame: %w", err)
20
+	}
21
+
14 22
 	decHasher := acquireSha256Hasher()
15 23
 	defer releaseSha256Hasher(decHasher)
16 24
 
@@ -21,17 +29,22 @@ func ClientHandshake(secret []byte, handshakeFrame *HandhakeFrame) (int16, ciphe
21 29
 	encHasher := acquireSha256Hasher()
22 30
 	defer releaseSha256Hasher(encHasher)
23 31
 
24
-	invertedFrame := handshakeFrame.invert()
32
+	invertedFrame := acquireHandshakeFrame()
33
+	defer releaseHandshakeFrame(invertedFrame)
34
+
35
+	for i, v := range handshakeFrame.data {
36
+		invertedFrame.data[handshakeFrameLen-1-i] = v
37
+	}
38
+
25 39
 	encHasher.Write(invertedFrame.key()) // nolint: errcheck
26
-	encHasher.Write(secret)               // nolint: errcheck
40
+	encHasher.Write(secret)              // nolint: errcheck
27 41
 	encryptor := makeAesCtr(encHasher.Sum(nil), invertedFrame.iv())
28 42
 
29
-	decryptedFrame := HandhakeFrame{}
30
-	decryptor.XORKeyStream(decryptedFrame.data[:], handshakeFrame.data[:])
43
+	decryptor.XORKeyStream(handshakeFrame.data[:], handshakeFrame.data[:])
31 44
 
32
-	if magic := decryptedFrame.magic(); subtle.ConstantTimeCompare(clientHandshakeMagic, magic) != 1 {
45
+	if magic := handshakeFrame.magic(); subtle.ConstantTimeCompare(clientHandshakeMagic, magic) != 1 {
33 46
 		return 0, nil, nil, fmt.Errorf("unsupported connection type: %s", hex.EncodeToString(magic))
34 47
 	}
35 48
 
36
-	return decryptedFrame.dc(), encryptor, decryptor, nil
49
+	return handshakeFrame.dc(), encryptor, decryptor, nil
37 50
 }

+ 10
- 38
mtglib/internal/obfuscated2/frame.go Voir le fichier

@@ -1,10 +1,6 @@
1 1
 package obfuscated2
2 2
 
3
-import (
4
-	"encoding/binary"
5
-	"fmt"
6
-	"io"
7
-)
3
+import "encoding/binary"
8 4
 
9 5
 const (
10 6
 	handshakeFrameLen = 64
@@ -32,48 +28,24 @@ const (
32 28
 //    - 4 bytes of 'magic' - this has some settings like a connection type
33 29
 //    - 2 bytes of 'DC'. DC is little endian int16
34 30
 //    - 2 bytes of noise
35
-type HandhakeFrame struct {
31
+type handshakeFrame struct {
36 32
 	data [handshakeFrameLen]byte
37 33
 }
38 34
 
39
-func (f *HandhakeFrame) Fingerprint() []byte {
40
-	return f.data[handshakeFrameOffsetStart:handshakeFrameOffsetEnd]
41
-}
42
-
43
-func (f *HandhakeFrame) dc() int16 {
44
-	data := f.data[handshakeFrameOffsetDC:handshakeFrameOffsetEnd]
35
+func (h *handshakeFrame) dc() int16 {
36
+	data := h.data[handshakeFrameOffsetDC:handshakeFrameOffsetEnd]
45 37
 
46 38
 	return int16(binary.LittleEndian.Uint16(data))
47 39
 }
48 40
 
49
-func (f *HandhakeFrame) key() []byte {
50
-	return f.data[handshakeFrameLenKey:handshakeFrameOffsetIV]
41
+func (h *handshakeFrame) key() []byte {
42
+	return h.data[handshakeFrameLenKey:handshakeFrameOffsetIV]
51 43
 }
52 44
 
53
-func (f *HandhakeFrame) iv() []byte {
54
-	return f.data[handshakeFrameOffsetIV:handshakeFrameOffsetMagic]
45
+func (h *handshakeFrame) iv() []byte {
46
+	return h.data[handshakeFrameOffsetIV:handshakeFrameOffsetMagic]
55 47
 }
56 48
 
57
-func (f *HandhakeFrame) magic() []byte {
58
-	return f.data[handshakeFrameOffsetMagic:handshakeFrameOffsetDC]
59
-}
60
-
61
-func (f *HandhakeFrame) invert() *HandhakeFrame {
62
-	newFrame := &HandhakeFrame{}
63
-
64
-	for i, v := range f.data {
65
-		newFrame.data[handshakeFrameLen-1-i] = v
66
-	}
67
-
68
-	return newFrame
69
-}
70
-
71
-func ReadHandshakeFrame(reader io.Reader) (*HandhakeFrame, error) {
72
-	frame := &HandhakeFrame{}
73
-
74
-	if _, err := io.ReadFull(reader, frame.data[:]); err != nil {
75
-		return nil, fmt.Errorf("cannot read frame data: %w", err)
76
-	}
77
-
78
-	return frame, nil
49
+func (h *handshakeFrame) magic() []byte {
50
+	return h.data[handshakeFrameOffsetMagic:handshakeFrameOffsetDC]
79 51
 }

+ 20
- 5
mtglib/internal/obfuscated2/pools.go Voir le fichier

@@ -6,11 +6,18 @@ import (
6 6
 	"sync"
7 7
 )
8 8
 
9
-var sha256HasherPool = sync.Pool{
10
-	New: func() interface{} {
11
-		return sha256.New()
12
-	},
13
-}
9
+var (
10
+	sha256HasherPool = sync.Pool{
11
+		New: func() interface{} {
12
+			return sha256.New()
13
+		},
14
+	}
15
+	handshakeFramePool = sync.Pool{
16
+		New: func() interface{} {
17
+			return &handshakeFrame{}
18
+		},
19
+	}
20
+)
14 21
 
15 22
 func acquireSha256Hasher() hash.Hash {
16 23
 	return sha256HasherPool.Get().(hash.Hash)
@@ -20,3 +27,11 @@ func releaseSha256Hasher(h hash.Hash) {
20 27
 	h.Reset()
21 28
 	sha256HasherPool.Put(h)
22 29
 }
30
+
31
+func acquireHandshakeFrame() *handshakeFrame {
32
+	return handshakeFramePool.Get().(*handshakeFrame)
33
+}
34
+
35
+func releaseHandshakeFrame(h *handshakeFrame) {
36
+	handshakeFramePool.Put(h)
37
+}

+ 1
- 6
mtglib/proxy.go Voir le fichier

@@ -95,12 +95,7 @@ func (p *Proxy) Shutdown() {
95 95
 }
96 96
 
97 97
 func (p *Proxy) doObfuscated2Handshake(ctx *streamContext) error {
98
-	handshakeFrame, err := obfuscated2.ReadHandshakeFrame(ctx.clientConn)
99
-	if err != nil {
100
-		return fmt.Errorf("cannot read handshake frame: %w", err)
101
-	}
102
-
103
-	dc, encryptor, decryptor, err := obfuscated2.ClientHandshake(p.secret.Key[:], handshakeFrame)
98
+	dc, encryptor, decryptor, err := obfuscated2.ClientHandshake(p.secret.Key[:], ctx.clientConn)
104 99
 	if err != nil {
105 100
 		return fmt.Errorf("cannot process client handshake: %w", err)
106 101
 	}

Chargement…
Annuler
Enregistrer