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

mtglib: promote obfuscation out of internal

Per discussion on #494, this allows external packages (e.g. the upcoming
mtglib/dcprobe for the doctor RPC probe) to reuse the obfuscated2
transport without an internal wrapper.

No public-API change beyond the import path. The only exported names
(Obfuscator, its two methods, and the Secret field) were already
exported within the package.
pull/495/head
Alexey Dolotov 1 день назад
Родитель
Сommit
ead06e0e16

+ 2
- 2
.mise.toml Просмотреть файл

58
 
58
 
59
 [tasks."test:fuzz:client-handshake"]
59
 [tasks."test:fuzz:client-handshake"]
60
 description = "Run fuzzy test for ClientHandshake"
60
 description = "Run fuzzy test for ClientHandshake"
61
-run = "go test -v {{ vars.fuzzflags }} -fuzz=FuzzClientServerHandshake ./mtglib/internal/obfuscation"
61
+run = "go test -v {{ vars.fuzzflags }} -fuzz=FuzzClientServerHandshake ./mtglib/obfuscation"
62
 
62
 
63
 [tasks."test:fuzz:server-handshake-frame"]
63
 [tasks."test:fuzz:server-handshake-frame"]
64
 description = "Run fuzzy test for GenerateHandshakeFrame"
64
 description = "Run fuzzy test for GenerateHandshakeFrame"
65
-run = "go test -v {{ vars.fuzzflags }} -fuzz=FuzzGenerateHandshakeFrame ./mtglib/internal/obfuscation"
65
+run = "go test -v {{ vars.fuzzflags }} -fuzz=FuzzGenerateHandshakeFrame ./mtglib/obfuscation"
66
 
66
 
67
 [tasks.static]
67
 [tasks.static]
68
 description = "Build static binary"
68
 description = "Build static binary"

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

3
 import (
3
 import (
4
 	"fmt"
4
 	"fmt"
5
 
5
 
6
-	"github.com/9seconds/mtg/v2/mtglib/internal/obfuscation"
6
+	"github.com/9seconds/mtg/v2/mtglib/obfuscation"
7
 )
7
 )
8
 
8
 
9
 type Addr struct {
9
 type Addr struct {

mtglib/internal/obfuscation/conn.go → mtglib/obfuscation/conn.go Просмотреть файл


mtglib/internal/obfuscation/conn_test.go → mtglib/obfuscation/conn_test.go Просмотреть файл


mtglib/internal/obfuscation/handshake_frame.go → mtglib/obfuscation/handshake_frame.go Просмотреть файл


mtglib/internal/obfuscation/handshake_frame_fuzz_test.go → mtglib/obfuscation/handshake_frame_fuzz_test.go Просмотреть файл


mtglib/internal/obfuscation/handshake_frame_test.go → mtglib/obfuscation/handshake_frame_test.go Просмотреть файл


mtglib/internal/obfuscation/init_test.go → mtglib/obfuscation/init_test.go Просмотреть файл


mtglib/internal/obfuscation/obfuscator.go → mtglib/obfuscation/obfuscator.go Просмотреть файл

13
 	"github.com/9seconds/mtg/v2/essentials"
13
 	"github.com/9seconds/mtg/v2/essentials"
14
 )
14
 )
15
 
15
 
16
+// Obfuscator implements the obfuscated2 handshake
17
+// (https://core.telegram.org/mtproto/mtproto-transports#transport-obfuscation).
18
+// Set Secret to the MTProxy secret for key-mixed handshakes; leave nil for
19
+// direct DC connections.
16
 type Obfuscator struct {
20
 type Obfuscator struct {
17
 	Secret []byte
21
 	Secret []byte
18
 }
22
 }
19
 
23
 
24
+// ReadHandshake reads the 64-byte obfuscated2 client handshake from r,
25
+// validates it, and returns the DC the client requested along with a
26
+// transparent en/decrypting wrapper over r.
20
 func (o Obfuscator) ReadHandshake(r essentials.Conn) (int, essentials.Conn, error) {
27
 func (o Obfuscator) ReadHandshake(r essentials.Conn) (int, essentials.Conn, error) {
21
 	frame := handshakeFrame{}
28
 	frame := handshakeFrame{}
22
 
29
 
46
 	return frame.dc(), cn, nil
53
 	return frame.dc(), cn, nil
47
 }
54
 }
48
 
55
 
56
+// SendHandshake writes a fresh 64-byte obfuscated2 handshake for the given
57
+// DC to w and returns a transparent en/decrypting wrapper over w.
49
 func (o Obfuscator) SendHandshake(w essentials.Conn, dc int) (essentials.Conn, error) {
58
 func (o Obfuscator) SendHandshake(w essentials.Conn, dc int) (essentials.Conn, error) {
50
 	frame := generateHandshake(dc)
59
 	frame := generateHandshake(dc)
51
 	copyFrame := frame
60
 	copyFrame := frame

mtglib/internal/obfuscation/obfuscator_fuzz_test.go → mtglib/obfuscation/obfuscator_fuzz_test.go Просмотреть файл

6
 
6
 
7
 	"github.com/9seconds/mtg/v2/internal/testlib"
7
 	"github.com/9seconds/mtg/v2/internal/testlib"
8
 	"github.com/9seconds/mtg/v2/mtglib"
8
 	"github.com/9seconds/mtg/v2/mtglib"
9
-	"github.com/9seconds/mtg/v2/mtglib/internal/obfuscation"
9
+	"github.com/9seconds/mtg/v2/mtglib/obfuscation"
10
 	"github.com/stretchr/testify/assert"
10
 	"github.com/stretchr/testify/assert"
11
 	"github.com/stretchr/testify/mock"
11
 	"github.com/stretchr/testify/mock"
12
 )
12
 )

mtglib/internal/obfuscation/obfuscator_test.go → mtglib/obfuscation/obfuscator_test.go Просмотреть файл

6
 
6
 
7
 	"github.com/9seconds/mtg/v2/internal/testlib"
7
 	"github.com/9seconds/mtg/v2/internal/testlib"
8
 	"github.com/9seconds/mtg/v2/mtglib"
8
 	"github.com/9seconds/mtg/v2/mtglib"
9
-	"github.com/9seconds/mtg/v2/mtglib/internal/obfuscation"
9
+	"github.com/9seconds/mtg/v2/mtglib/obfuscation"
10
 	"github.com/stretchr/testify/assert"
10
 	"github.com/stretchr/testify/assert"
11
 	"github.com/stretchr/testify/mock"
11
 	"github.com/stretchr/testify/mock"
12
 	"github.com/stretchr/testify/require"
12
 	"github.com/stretchr/testify/require"

mtglib/internal/obfuscation/testdata/client-handshake-snapshot-4529d55776e2d427.json → mtglib/obfuscation/testdata/client-handshake-snapshot-4529d55776e2d427.json Просмотреть файл


mtglib/internal/obfuscation/testdata/client-handshake-snapshot-585c944d672f60a2.json → mtglib/obfuscation/testdata/client-handshake-snapshot-585c944d672f60a2.json Просмотреть файл


+ 1
- 1
mtglib/proxy.go Просмотреть файл

12
 	"github.com/9seconds/mtg/v2/essentials"
12
 	"github.com/9seconds/mtg/v2/essentials"
13
 	"github.com/9seconds/mtg/v2/mtglib/internal/dc"
13
 	"github.com/9seconds/mtg/v2/mtglib/internal/dc"
14
 	"github.com/9seconds/mtg/v2/mtglib/internal/doppel"
14
 	"github.com/9seconds/mtg/v2/mtglib/internal/doppel"
15
-	"github.com/9seconds/mtg/v2/mtglib/internal/obfuscation"
15
+	"github.com/9seconds/mtg/v2/mtglib/obfuscation"
16
 	"github.com/9seconds/mtg/v2/mtglib/internal/relay"
16
 	"github.com/9seconds/mtg/v2/mtglib/internal/relay"
17
 	"github.com/9seconds/mtg/v2/mtglib/internal/tls"
17
 	"github.com/9seconds/mtg/v2/mtglib/internal/tls"
18
 	"github.com/9seconds/mtg/v2/mtglib/internal/tls/fake"
18
 	"github.com/9seconds/mtg/v2/mtglib/internal/tls/fake"

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