Просмотр исходного кода

More correct calculation of dc for obfuscated2 frame

tags/v2.0.0-rc1
9seconds 5 лет назад
Родитель
Сommit
26070d5b3e

+ 1
- 6
mtglib/internal/obfuscated2/handshake_frame.go Просмотреть файл

@@ -1,7 +1,5 @@
1 1
 package obfuscated2
2 2
 
3
-import "encoding/binary"
4
-
5 3
 const (
6 4
 	DefaultDC = 2
7 5
 
@@ -10,14 +8,12 @@ const (
10 8
 	handshakeFrameLenKey            = 32
11 9
 	handshakeFrameLenIV             = 16
12 10
 	handshakeFrameLenConnectionType = 4
13
-	handshakeFrameLenDC             = 2
14 11
 
15 12
 	handshakeFrameOffsetStart          = 8
16 13
 	handshakeFrameOffsetKey            = handshakeFrameOffsetStart
17 14
 	handshakeFrameOffsetIV             = handshakeFrameOffsetKey + handshakeFrameLenKey
18 15
 	handshakeFrameOffsetConnectionType = handshakeFrameOffsetIV + handshakeFrameLenIV
19 16
 	handshakeFrameOffsetDC             = handshakeFrameOffsetConnectionType + handshakeFrameLenConnectionType
20
-	handshakeFrameOffsetEnd            = handshakeFrameOffsetDC + handshakeFrameLenDC
21 17
 )
22 18
 
23 19
 // Connection-Type: Secure. We support only fake tls.
@@ -38,8 +34,7 @@ type handshakeFrame struct {
38 34
 }
39 35
 
40 36
 func (h *handshakeFrame) dc() int {
41
-	data := h.data[handshakeFrameOffsetDC:handshakeFrameOffsetEnd]
42
-	idx := int16(binary.LittleEndian.Uint16(data))
37
+	idx := int16(h.data[handshakeFrameOffsetDC]) | int16(h.data[handshakeFrameOffsetDC+1])<<8 // nolint: gomnd, lll // little endian for int16 is here
43 38
 
44 39
 	switch {
45 40
 	case idx > 0:

+ 27
- 0
mtglib/internal/obfuscated2/handshake_frame_internal_test.go Просмотреть файл

@@ -1,9 +1,12 @@
1 1
 package obfuscated2
2 2
 
3 3
 import (
4
+	"crypto/rand"
4 5
 	"encoding/base64"
6
+	"strconv"
5 7
 	"testing"
6 8
 
9
+	"github.com/stretchr/testify/assert"
7 10
 	"github.com/stretchr/testify/suite"
8 11
 )
9 12
 
@@ -40,6 +43,30 @@ func (suite *HandshakeFrameTestSuite) TestOk() {
40 43
 	suite.EqualValues(2094, inverted.dc())
41 44
 }
42 45
 
46
+func (suite *HandshakeFrameTestSuite) TestDC() {
47
+	testData := map[int16]int{
48
+		1:  1,
49
+		-1: 1,
50
+		0:  DefaultDC,
51
+	}
52
+
53
+	for k, v := range testData {
54
+		incoming := k
55
+		expected := v
56
+
57
+		suite.T().Run(strconv.Itoa(int(incoming)), func(t *testing.T) {
58
+			frame := handshakeFrame{}
59
+
60
+			rand.Read(frame.data[:]) // nolint: errcheck
61
+
62
+			frame.data[handshakeFrameOffsetDC] = byte(incoming)
63
+			frame.data[handshakeFrameOffsetDC+1] = byte(incoming >> 8)
64
+
65
+			assert.Equal(t, expected, frame.dc())
66
+		})
67
+	}
68
+}
69
+
43 70
 func TestHandshakeFrame(t *testing.T) {
44 71
 	t.Parallel()
45 72
 	suite.Run(t, &HandshakeFrameTestSuite{})

Загрузка…
Отмена
Сохранить