Bläddra i källkod

Debug direct connection

tags/0.9
9seconds 8 år sedan
förälder
incheckning
cd63483503
6 ändrade filer med 32 tillägg och 17 borttagningar
  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 Visa fil

36
 	connOpts.ConnectionProto = mtproto.ConnectionProtocolAny
36
 	connOpts.ConnectionProto = mtproto.ConnectionProtocolAny
37
 	connOpts.ClientAddr = conn.RemoteAddr()
37
 	connOpts.ClientAddr = conn.RemoteAddr()
38
 
38
 
39
-	conn = wrappers.NewCtx(ctx, cancel, conn)
40
 	conn = wrappers.NewStreamCipher(conn, obfs2.Encryptor, obfs2.Decryptor)
39
 	conn = wrappers.NewStreamCipher(conn, obfs2.Encryptor, obfs2.Decryptor)
40
+	conn = wrappers.NewCtx(ctx, cancel, conn)
41
 
41
 
42
 	return conn, connOpts, nil
42
 	return conn, connOpts, nil
43
 }
43
 }

+ 2
- 0
main.go Visa fil

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

+ 8
- 2
obfuscated2/frame.go Visa fil

9
 	"github.com/juju/errors"
9
 	"github.com/juju/errors"
10
 
10
 
11
 	"github.com/9seconds/mtg/mtproto"
11
 	"github.com/9seconds/mtg/mtproto"
12
-	"github.com/9seconds/mtg/utils"
13
 )
12
 )
14
 
13
 
15
 // [frameOffsetFirst:frameOffsetKey:frameOffsetIV:frameOffsetMagic:frameOffsetDC:frameOffsetEnd]
14
 // [frameOffsetFirst:frameOffsetKey:frameOffsetIV:frameOffsetMagic:frameOffsetDC:frameOffsetEnd]
68
 // Invert inverts frame for extracting encryption keys. Pkease check that link:
67
 // Invert inverts frame for extracting encryption keys. Pkease check that link:
69
 // https://blog.susanka.eu/how-telegram-obfuscates-its-mtproto-traffic/
68
 // https://blog.susanka.eu/how-telegram-obfuscates-its-mtproto-traffic/
70
 func (f Frame) Invert() Frame {
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
 // ExtractFrame extracts exact obfuscated2 handshake frame from given reader.
80
 // ExtractFrame extracts exact obfuscated2 handshake frame from given reader.

+ 4
- 6
proxy/direct.go Visa fil

21
 	return &Proxy{
21
 	return &Proxy{
22
 		conf: conf,
22
 		conf: conf,
23
 		acceptCallback: func(ctx context.Context, cancel context.CancelFunc, clientSocket net.Conn,
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
 			client, opts, err := client.DirectInit(ctx, cancel, clientSocket, connID, conf)
25
 			client, opts, err := client.DirectInit(ctx, cancel, clientSocket, connID, conf)
26
 			if err != nil {
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
 			server, err := directTelegramStream(ctx, cancel, opts, connID, tg)
30
 			server, err := directTelegramStream(ctx, cancel, opts, connID, tg)
32
 			if err != nil {
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
 			wait.Add(2)
35
 			wait.Add(2)
38
 
36
 
39
 			go directPipe(client, server, wait)
37
 			go directPipe(client, server, wait)
40
 			go directPipe(server, client, wait)
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 Visa fil

2
 
2
 
3
 import (
3
 import (
4
 	"context"
4
 	"context"
5
+	"io"
5
 	"net"
6
 	"net"
6
 	"sync"
7
 	"sync"
7
 
8
 
20
 	return &Proxy{
21
 	return &Proxy{
21
 		conf: conf,
22
 		conf: conf,
22
 		acceptCallback: func(ctx context.Context, cancel context.CancelFunc, clientSocket net.Conn,
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
 			client, opts, err := client.MiddleInit(ctx, cancel, clientSocket, connID, conf)
25
 			client, opts, err := client.MiddleInit(ctx, cancel, clientSocket, connID, conf)
25
 			if err != nil {
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
 			server, err := middleTelegramStream(ctx, cancel, opts, connID, tg)
30
 			server, err := middleTelegramStream(ctx, cancel, opts, connID, tg)
31
 			if err != nil {
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
 			wait.Add(2)
35
 			wait.Add(2)
37
 
36
 
38
 			go middlePipe(client, server, wait, &opts.ReadHacks)
37
 			go middlePipe(client, server, wait, &opts.ReadHacks)
39
 			go middlePipe(server, client, wait, &opts.WriteHacks)
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 Visa fil

2
 
2
 
3
 import (
3
 import (
4
 	"context"
4
 	"context"
5
+	"io"
5
 	"net"
6
 	"net"
6
 	"sync"
7
 	"sync"
7
 
8
 
12
 	"github.com/9seconds/mtg/config"
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
 type Proxy struct {
18
 type Proxy struct {
18
 	conf           *config.Config
19
 	conf           *config.Config
51
 	ctx, cancel := context.WithCancel(context.Background())
52
 	ctx, cancel := context.WithCancel(context.Background())
52
 	wait := &sync.WaitGroup{}
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
 		log.Errorw("Cannot initialize connection", "error", err)
65
 		log.Errorw("Cannot initialize connection", "error", err)
56
 		cancel()
66
 		cancel()
57
 	}
67
 	}

Laddar…
Avbryt
Spara