Parcourir la source

Debug direct connection

tags/0.9
9seconds il y a 7 ans
Parent
révision
cd63483503
6 fichiers modifiés avec 32 ajouts et 17 suppressions
  1. 1
    1
      client/direct.go
  2. 2
    0
      main.go
  3. 8
    2
      obfuscated2/frame.go
  4. 4
    6
      proxy/direct.go
  5. 5
    6
      proxy/middle.go
  6. 12
    2
      proxy/proxy.go

+ 1
- 1
client/direct.go Voir le fichier

@@ -36,8 +36,8 @@ func DirectInit(ctx context.Context, cancel context.CancelFunc, socket net.Conn,
36 36
 	connOpts.ConnectionProto = mtproto.ConnectionProtocolAny
37 37
 	connOpts.ClientAddr = conn.RemoteAddr()
38 38
 
39
-	conn = wrappers.NewCtx(ctx, cancel, conn)
40 39
 	conn = wrappers.NewStreamCipher(conn, obfs2.Encryptor, obfs2.Decryptor)
40
+	conn = wrappers.NewCtx(ctx, cancel, conn)
41 41
 
42 42
 	return conn, connOpts, nil
43 43
 }

+ 2
- 0
main.go Voir le fichier

@@ -117,8 +117,10 @@ func main() {
117 117
 
118 118
 	var server *proxy.Proxy
119 119
 	if len(conf.AdTag) == 0 {
120
+		zap.S().Infow("Use direct connection to Telegram")
120 121
 		server = proxy.NewProxyDirect(conf)
121 122
 	} else {
123
+		zap.S().Infow("Use middle proxy connection to Telegram")
122 124
 		server = proxy.NewProxyMiddle(conf)
123 125
 	}
124 126
 

+ 8
- 2
obfuscated2/frame.go Voir le fichier

@@ -9,7 +9,6 @@ import (
9 9
 	"github.com/juju/errors"
10 10
 
11 11
 	"github.com/9seconds/mtg/mtproto"
12
-	"github.com/9seconds/mtg/utils"
13 12
 )
14 13
 
15 14
 // [frameOffsetFirst:frameOffsetKey:frameOffsetIV:frameOffsetMagic:frameOffsetDC:frameOffsetEnd]
@@ -68,7 +67,14 @@ func (f Frame) ConnectionType() (mtproto.ConnectionType, error) {
68 67
 // Invert inverts frame for extracting encryption keys. Pkease check that link:
69 68
 // https://blog.susanka.eu/how-telegram-obfuscates-its-mtproto-traffic/
70 69
 func (f Frame) Invert() Frame {
71
-	return Frame(utils.ReverseBytes([]byte(f)))
70
+	reversed := make(Frame, FrameLen)
71
+	copy(reversed, f)
72
+
73
+	for i := 0; i < frameLenKey+frameLenIV; i++ {
74
+		reversed[frameOffsetFirst+i] = f[frameOffsetIV-1-i]
75
+	}
76
+
77
+	return reversed
72 78
 }
73 79
 
74 80
 // ExtractFrame extracts exact obfuscated2 handshake frame from given reader.

+ 4
- 6
proxy/direct.go Voir le fichier

@@ -21,25 +21,23 @@ func NewProxyDirect(conf *config.Config) *Proxy {
21 21
 	return &Proxy{
22 22
 		conf: conf,
23 23
 		acceptCallback: func(ctx context.Context, cancel context.CancelFunc, clientSocket net.Conn,
24
-			connID string, wait *sync.WaitGroup, conf *config.Config) error {
24
+			connID string, wait *sync.WaitGroup, conf *config.Config) (io.Closer, io.Closer, error) {
25 25
 			client, opts, err := client.DirectInit(ctx, cancel, clientSocket, connID, conf)
26 26
 			if err != nil {
27
-				return errors.Annotate(err, "Cannot initialize client connection")
27
+				return nil, nil, errors.Annotate(err, "Cannot initialize client connection")
28 28
 			}
29
-			defer client.Close()
30 29
 
31 30
 			server, err := directTelegramStream(ctx, cancel, opts, connID, tg)
32 31
 			if err != nil {
33
-				return errors.Annotate(err, "Cannot initialize telegram connection")
32
+				return client, nil, errors.Annotate(err, "Cannot initialize telegram connection")
34 33
 			}
35
-			defer server.Close()
36 34
 
37 35
 			wait.Add(2)
38 36
 
39 37
 			go directPipe(client, server, wait)
40 38
 			go directPipe(server, client, wait)
41 39
 
42
-			return nil
40
+			return client, server, nil
43 41
 		},
44 42
 	}
45 43
 }

+ 5
- 6
proxy/middle.go Voir le fichier

@@ -2,6 +2,7 @@ package proxy
2 2
 
3 3
 import (
4 4
 	"context"
5
+	"io"
5 6
 	"net"
6 7
 	"sync"
7 8
 
@@ -20,25 +21,23 @@ func NewProxyMiddle(conf *config.Config) *Proxy {
20 21
 	return &Proxy{
21 22
 		conf: conf,
22 23
 		acceptCallback: func(ctx context.Context, cancel context.CancelFunc, clientSocket net.Conn,
23
-			connID string, wait *sync.WaitGroup, conf *config.Config) error {
24
+			connID string, wait *sync.WaitGroup, conf *config.Config) (io.Closer, io.Closer, error) {
24 25
 			client, opts, err := client.MiddleInit(ctx, cancel, clientSocket, connID, conf)
25 26
 			if err != nil {
26
-				return errors.Annotate(err, "Cannot initialize client connection")
27
+				return nil, nil, errors.Annotate(err, "Cannot initialize client connection")
27 28
 			}
28
-			defer client.Close()
29 29
 
30 30
 			server, err := middleTelegramStream(ctx, cancel, opts, connID, tg)
31 31
 			if err != nil {
32
-				return errors.Annotate(err, "Cannot initialize telegram connection")
32
+				return client, nil, errors.Annotate(err, "Cannot initialize telegram connection")
33 33
 			}
34
-			defer server.Close()
35 34
 
36 35
 			wait.Add(2)
37 36
 
38 37
 			go middlePipe(client, server, wait, &opts.ReadHacks)
39 38
 			go middlePipe(server, client, wait, &opts.WriteHacks)
40 39
 
41
-			return nil
40
+			return client, server, nil
42 41
 		},
43 42
 	}
44 43
 }

+ 12
- 2
proxy/proxy.go Voir le fichier

@@ -2,6 +2,7 @@ package proxy
2 2
 
3 3
 import (
4 4
 	"context"
5
+	"io"
5 6
 	"net"
6 7
 	"sync"
7 8
 
@@ -12,7 +13,7 @@ import (
12 13
 	"github.com/9seconds/mtg/config"
13 14
 )
14 15
 
15
-type proxyAcceptCallback func(context.Context, context.CancelFunc, net.Conn, string, *sync.WaitGroup, *config.Config) error
16
+type proxyAcceptCallback func(context.Context, context.CancelFunc, net.Conn, string, *sync.WaitGroup, *config.Config) (io.Closer, io.Closer, error)
16 17
 
17 18
 type Proxy struct {
18 19
 	conf           *config.Config
@@ -51,7 +52,16 @@ func (p *Proxy) accept(conn net.Conn) {
51 52
 	ctx, cancel := context.WithCancel(context.Background())
52 53
 	wait := &sync.WaitGroup{}
53 54
 
54
-	if err := p.acceptCallback(ctx, cancel, conn, connID, wait, p.conf); err != nil {
55
+	client, server, err := p.acceptCallback(ctx, cancel, conn, connID, wait, p.conf)
56
+	defer func() {
57
+		if client != nil {
58
+			client.Close()
59
+		}
60
+		if server != nil {
61
+			server.Close()
62
+		}
63
+	}()
64
+	if err != nil {
55 65
 		log.Errorw("Cannot initialize connection", "error", err)
56 66
 		cancel()
57 67
 	}

Chargement…
Annuler
Enregistrer