Debug direct connection

This commit is contained in:
9seconds
2018-07-07 20:45:10 +03:00
parent f82ff1f6fe
commit cd63483503
6 changed files with 32 additions and 17 deletions
+1 -1
View File
@@ -36,8 +36,8 @@ func DirectInit(ctx context.Context, cancel context.CancelFunc, socket net.Conn,
connOpts.ConnectionProto = mtproto.ConnectionProtocolAny connOpts.ConnectionProto = mtproto.ConnectionProtocolAny
connOpts.ClientAddr = conn.RemoteAddr() connOpts.ClientAddr = conn.RemoteAddr()
conn = wrappers.NewCtx(ctx, cancel, conn)
conn = wrappers.NewStreamCipher(conn, obfs2.Encryptor, obfs2.Decryptor) conn = wrappers.NewStreamCipher(conn, obfs2.Encryptor, obfs2.Decryptor)
conn = wrappers.NewCtx(ctx, cancel, conn)
return conn, connOpts, nil return conn, connOpts, nil
} }
+2
View File
@@ -117,8 +117,10 @@ func main() {
var server *proxy.Proxy var server *proxy.Proxy
if len(conf.AdTag) == 0 { if len(conf.AdTag) == 0 {
zap.S().Infow("Use direct connection to Telegram")
server = proxy.NewProxyDirect(conf) server = proxy.NewProxyDirect(conf)
} else { } else {
zap.S().Infow("Use middle proxy connection to Telegram")
server = proxy.NewProxyMiddle(conf) server = proxy.NewProxyMiddle(conf)
} }
+8 -2
View File
@@ -9,7 +9,6 @@ import (
"github.com/juju/errors" "github.com/juju/errors"
"github.com/9seconds/mtg/mtproto" "github.com/9seconds/mtg/mtproto"
"github.com/9seconds/mtg/utils"
) )
// [frameOffsetFirst:frameOffsetKey:frameOffsetIV:frameOffsetMagic:frameOffsetDC:frameOffsetEnd] // [frameOffsetFirst:frameOffsetKey:frameOffsetIV:frameOffsetMagic:frameOffsetDC:frameOffsetEnd]
@@ -68,7 +67,14 @@ func (f Frame) ConnectionType() (mtproto.ConnectionType, error) {
// Invert inverts frame for extracting encryption keys. Pkease check that link: // Invert inverts frame for extracting encryption keys. Pkease check that link:
// https://blog.susanka.eu/how-telegram-obfuscates-its-mtproto-traffic/ // https://blog.susanka.eu/how-telegram-obfuscates-its-mtproto-traffic/
func (f Frame) Invert() Frame { func (f Frame) Invert() Frame {
return Frame(utils.ReverseBytes([]byte(f))) reversed := make(Frame, FrameLen)
copy(reversed, f)
for i := 0; i < frameLenKey+frameLenIV; i++ {
reversed[frameOffsetFirst+i] = f[frameOffsetIV-1-i]
}
return reversed
} }
// ExtractFrame extracts exact obfuscated2 handshake frame from given reader. // ExtractFrame extracts exact obfuscated2 handshake frame from given reader.
+4 -6
View File
@@ -21,25 +21,23 @@ func NewProxyDirect(conf *config.Config) *Proxy {
return &Proxy{ return &Proxy{
conf: conf, conf: conf,
acceptCallback: func(ctx context.Context, cancel context.CancelFunc, clientSocket net.Conn, acceptCallback: func(ctx context.Context, cancel context.CancelFunc, clientSocket net.Conn,
connID string, wait *sync.WaitGroup, conf *config.Config) error { connID string, wait *sync.WaitGroup, conf *config.Config) (io.Closer, io.Closer, error) {
client, opts, err := client.DirectInit(ctx, cancel, clientSocket, connID, conf) client, opts, err := client.DirectInit(ctx, cancel, clientSocket, connID, conf)
if err != nil { if err != nil {
return errors.Annotate(err, "Cannot initialize client connection") return nil, nil, errors.Annotate(err, "Cannot initialize client connection")
} }
defer client.Close()
server, err := directTelegramStream(ctx, cancel, opts, connID, tg) server, err := directTelegramStream(ctx, cancel, opts, connID, tg)
if err != nil { if err != nil {
return errors.Annotate(err, "Cannot initialize telegram connection") return client, nil, errors.Annotate(err, "Cannot initialize telegram connection")
} }
defer server.Close()
wait.Add(2) wait.Add(2)
go directPipe(client, server, wait) go directPipe(client, server, wait)
go directPipe(server, client, wait) go directPipe(server, client, wait)
return nil return client, server, nil
}, },
} }
} }
+5 -6
View File
@@ -2,6 +2,7 @@ package proxy
import ( import (
"context" "context"
"io"
"net" "net"
"sync" "sync"
@@ -20,25 +21,23 @@ func NewProxyMiddle(conf *config.Config) *Proxy {
return &Proxy{ return &Proxy{
conf: conf, conf: conf,
acceptCallback: func(ctx context.Context, cancel context.CancelFunc, clientSocket net.Conn, acceptCallback: func(ctx context.Context, cancel context.CancelFunc, clientSocket net.Conn,
connID string, wait *sync.WaitGroup, conf *config.Config) error { connID string, wait *sync.WaitGroup, conf *config.Config) (io.Closer, io.Closer, error) {
client, opts, err := client.MiddleInit(ctx, cancel, clientSocket, connID, conf) client, opts, err := client.MiddleInit(ctx, cancel, clientSocket, connID, conf)
if err != nil { if err != nil {
return errors.Annotate(err, "Cannot initialize client connection") return nil, nil, errors.Annotate(err, "Cannot initialize client connection")
} }
defer client.Close()
server, err := middleTelegramStream(ctx, cancel, opts, connID, tg) server, err := middleTelegramStream(ctx, cancel, opts, connID, tg)
if err != nil { if err != nil {
return errors.Annotate(err, "Cannot initialize telegram connection") return client, nil, errors.Annotate(err, "Cannot initialize telegram connection")
} }
defer server.Close()
wait.Add(2) wait.Add(2)
go middlePipe(client, server, wait, &opts.ReadHacks) go middlePipe(client, server, wait, &opts.ReadHacks)
go middlePipe(server, client, wait, &opts.WriteHacks) go middlePipe(server, client, wait, &opts.WriteHacks)
return nil return client, server, nil
}, },
} }
} }
+12 -2
View File
@@ -2,6 +2,7 @@ package proxy
import ( import (
"context" "context"
"io"
"net" "net"
"sync" "sync"
@@ -12,7 +13,7 @@ import (
"github.com/9seconds/mtg/config" "github.com/9seconds/mtg/config"
) )
type proxyAcceptCallback func(context.Context, context.CancelFunc, net.Conn, string, *sync.WaitGroup, *config.Config) error type proxyAcceptCallback func(context.Context, context.CancelFunc, net.Conn, string, *sync.WaitGroup, *config.Config) (io.Closer, io.Closer, error)
type Proxy struct { type Proxy struct {
conf *config.Config conf *config.Config
@@ -51,7 +52,16 @@ func (p *Proxy) accept(conn net.Conn) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
wait := &sync.WaitGroup{} wait := &sync.WaitGroup{}
if err := p.acceptCallback(ctx, cancel, conn, connID, wait, p.conf); err != nil { client, server, err := p.acceptCallback(ctx, cancel, conn, connID, wait, p.conf)
defer func() {
if client != nil {
client.Close()
}
if server != nil {
server.Close()
}
}()
if err != nil {
log.Errorw("Cannot initialize connection", "error", err) log.Errorw("Cannot initialize connection", "error", err)
cancel() cancel()
} }