mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 20:54:02 +03:00
Refactored all the things!
This commit is contained in:
+5
-2
@@ -30,11 +30,14 @@ func (t *tgDialer) dial(addr string) (net.Conn, error) {
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
func (t *tgDialer) dialRWC(addr, sock string) (wrappers.ReadWriteCloserWithAddr, error) {
|
||||
func (t *tgDialer) dialRWC(addr, connID string) (wrappers.WrapStreamReadWriteCloser, error) {
|
||||
conn, err := t.dial(addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return wrappers.NewTimeoutRWC(conn, sock, t.conf.PublicIPv4, t.conf.PublicIPv6), nil
|
||||
tgConn := wrappers.NewConn(conn, connID, wrappers.ConnPurposeTelegram,
|
||||
t.conf.PublicIPv4, t.conf.PublicIPv6)
|
||||
|
||||
return tgConn, nil
|
||||
}
|
||||
|
||||
+8
-9
@@ -4,7 +4,6 @@ import (
|
||||
"net"
|
||||
|
||||
"github.com/juju/errors"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/9seconds/mtg/config"
|
||||
"github.com/9seconds/mtg/mtproto"
|
||||
@@ -29,11 +28,11 @@ var (
|
||||
}
|
||||
)
|
||||
|
||||
type directTelegram struct {
|
||||
type DirectTelegram struct {
|
||||
baseTelegram
|
||||
}
|
||||
|
||||
func (t *directTelegram) Dial(sock string, connOpts *mtproto.ConnectionOpts) (wrappers.ReadWriteCloserWithAddr, error) {
|
||||
func (t *DirectTelegram) Dial(connID string, connOpts *mtproto.ConnectionOpts) (wrappers.WrapStreamReadWriteCloser, error) {
|
||||
dc := connOpts.DC
|
||||
if dc < 0 {
|
||||
dc = -dc
|
||||
@@ -41,23 +40,23 @@ func (t *directTelegram) Dial(sock string, connOpts *mtproto.ConnectionOpts) (wr
|
||||
dc = 1
|
||||
}
|
||||
|
||||
return t.baseTelegram.dial(dc-1, sock, connOpts.ConnectionProto)
|
||||
return t.baseTelegram.dial(dc-1, connID, connOpts.ConnectionProto)
|
||||
}
|
||||
|
||||
func (t *directTelegram) Init(connOpts *mtproto.ConnectionOpts, conn wrappers.ReadWriteCloserWithAddr) (wrappers.ReadWriteCloserWithAddr, error) {
|
||||
func (t *DirectTelegram) Init(connOpts *mtproto.ConnectionOpts, conn wrappers.WrapStreamReadWriteCloser) (wrappers.WrapStreamReadWriteCloser, error) {
|
||||
obfs2, frame := obfuscated2.MakeTelegramObfuscated2Frame(connOpts)
|
||||
|
||||
if n, err := conn.Write(frame); err != nil || n != obfuscated2.FrameLen {
|
||||
if _, err := conn.Write(frame); err != nil {
|
||||
return nil, errors.Annotate(err, "Cannot write hadnshake frame")
|
||||
}
|
||||
|
||||
return wrappers.NewStreamCipherRWC(conn, obfs2.Encryptor, obfs2.Decryptor), nil
|
||||
return wrappers.NewStreamCipher(conn, obfs2.Encryptor, obfs2.Decryptor), nil
|
||||
}
|
||||
|
||||
// NewDirectTelegram returns Telegram instance which connects directly
|
||||
// to Telegram bypassing middleproxies.
|
||||
func NewDirectTelegram(conf *config.Config, _ *zap.SugaredLogger) Telegram {
|
||||
return &directTelegram{baseTelegram{
|
||||
func NewDirectTelegram(conf *config.Config) *DirectTelegram {
|
||||
return &DirectTelegram{baseTelegram{
|
||||
dialer: tgDialer{
|
||||
Dialer: net.Dialer{Timeout: telegramDialTimeout},
|
||||
conf: conf,
|
||||
|
||||
+18
-26
@@ -1,29 +1,26 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
"github.com/juju/errors"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/9seconds/mtg/config"
|
||||
"github.com/9seconds/mtg/mtproto"
|
||||
"github.com/9seconds/mtg/mtproto/rpc"
|
||||
mtwrappers "github.com/9seconds/mtg/mtproto/wrappers"
|
||||
"github.com/9seconds/mtg/wrappers"
|
||||
)
|
||||
|
||||
type middleTelegram struct {
|
||||
type MiddleTelegram struct {
|
||||
middleTelegramCaller
|
||||
|
||||
conf *config.Config
|
||||
}
|
||||
|
||||
func NewMiddleTelegram(conf *config.Config, logger *zap.SugaredLogger) Telegram {
|
||||
tg := &middleTelegram{
|
||||
func NewMiddleTelegram(conf *config.Config) *MiddleTelegram {
|
||||
tg := &MiddleTelegram{
|
||||
middleTelegramCaller: middleTelegramCaller{
|
||||
baseTelegram: baseTelegram{
|
||||
dialer: tgDialer{
|
||||
@@ -31,7 +28,6 @@ func NewMiddleTelegram(conf *config.Config, logger *zap.SugaredLogger) Telegram
|
||||
conf: conf,
|
||||
},
|
||||
},
|
||||
logger: logger,
|
||||
httpClient: &http.Client{
|
||||
Timeout: middleTelegramHTTPClientTimeout,
|
||||
},
|
||||
@@ -48,8 +44,8 @@ func NewMiddleTelegram(conf *config.Config, logger *zap.SugaredLogger) Telegram
|
||||
return tg
|
||||
}
|
||||
|
||||
func (t *middleTelegram) Init(connOpts *mtproto.ConnectionOpts, conn wrappers.ReadWriteCloserWithAddr) (wrappers.ReadWriteCloserWithAddr, error) {
|
||||
rpcNonceConn := mtwrappers.NewFrameRWC(conn, rpc.SeqNoNonce)
|
||||
func (t *MiddleTelegram) Init(connOpts *mtproto.ConnectionOpts, conn wrappers.WrapStreamReadWriteCloser) (wrappers.WrapPacketReadWriteCloser, error) {
|
||||
rpcNonceConn := wrappers.NewMTProtoFrame(conn, rpc.SeqNoNonce)
|
||||
|
||||
rpcNonceReq, err := t.sendRPCNonceRequest(rpcNonceConn)
|
||||
if err != nil {
|
||||
@@ -60,22 +56,22 @@ func (t *middleTelegram) Init(connOpts *mtproto.ConnectionOpts, conn wrappers.Re
|
||||
return nil, err
|
||||
}
|
||||
|
||||
secureConn := mtwrappers.NewMiddleProxyCipherRWC(conn, rpcNonceReq, rpcNonceResp, t.proxySecret)
|
||||
secureConn = mtwrappers.NewFrameRWC(secureConn, rpc.SeqNoHandshake)
|
||||
secureConn := wrappers.NewMiddleProxyCipher(conn, rpcNonceReq, rpcNonceResp, t.proxySecret)
|
||||
frameConn := wrappers.NewMTProtoFrame(secureConn, rpc.SeqNoHandshake)
|
||||
|
||||
rpcHandshakeReq, err := t.sendRPCHandshakeRequest(secureConn)
|
||||
rpcHandshakeReq, err := t.sendRPCHandshakeRequest(frameConn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, err = t.receiveRPCHandshakeResponse(secureConn, rpcHandshakeReq)
|
||||
_, err = t.receiveRPCHandshakeResponse(frameConn, rpcHandshakeReq)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return mtwrappers.NewProxyRequestRWC(secureConn, connOpts, t.conf.AdTag)
|
||||
return wrappers.NewMTProtoProxy(frameConn, connOpts, t.conf.AdTag)
|
||||
}
|
||||
|
||||
func (t *middleTelegram) sendRPCNonceRequest(conn io.Writer) (*rpc.NonceRequest, error) {
|
||||
func (t *MiddleTelegram) sendRPCNonceRequest(conn wrappers.WrapPacketWriter) (*rpc.NonceRequest, error) {
|
||||
rpcNonceReq, err := rpc.NewNonceRequest(t.proxySecret)
|
||||
if err != nil {
|
||||
return nil, errors.Annotate(err, "Cannot create RPC nonce request")
|
||||
@@ -87,15 +83,13 @@ func (t *middleTelegram) sendRPCNonceRequest(conn io.Writer) (*rpc.NonceRequest,
|
||||
return rpcNonceReq, nil
|
||||
}
|
||||
|
||||
func (t *middleTelegram) receiveRPCNonceResponse(conn io.Reader, req *rpc.NonceRequest) (*rpc.NonceResponse, error) {
|
||||
var ans [128]byte
|
||||
|
||||
n, err := conn.Read(ans[:])
|
||||
func (t *MiddleTelegram) receiveRPCNonceResponse(conn wrappers.WrapPacketReader, req *rpc.NonceRequest) (*rpc.NonceResponse, error) {
|
||||
packet, err := conn.Read()
|
||||
if err != nil {
|
||||
return nil, errors.Annotate(err, "Cannot read RPC nonce response")
|
||||
}
|
||||
|
||||
rpcNonceResp, err := rpc.NewNonceResponse(ans[:n])
|
||||
rpcNonceResp, err := rpc.NewNonceResponse(packet)
|
||||
if err != nil {
|
||||
return nil, errors.Annotate(err, "Cannot initialize RPC nonce response")
|
||||
}
|
||||
@@ -106,7 +100,7 @@ func (t *middleTelegram) receiveRPCNonceResponse(conn io.Reader, req *rpc.NonceR
|
||||
return rpcNonceResp, nil
|
||||
}
|
||||
|
||||
func (t *middleTelegram) sendRPCHandshakeRequest(conn io.Writer) (*rpc.HandshakeRequest, error) {
|
||||
func (t *MiddleTelegram) sendRPCHandshakeRequest(conn wrappers.WrapPacketWriter) (*rpc.HandshakeRequest, error) {
|
||||
req := rpc.NewHandshakeRequest()
|
||||
if _, err := conn.Write(req.Bytes()); err != nil {
|
||||
return nil, errors.Annotate(err, "Cannot send RPC handshake request")
|
||||
@@ -115,15 +109,13 @@ func (t *middleTelegram) sendRPCHandshakeRequest(conn io.Writer) (*rpc.Handshake
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func (t *middleTelegram) receiveRPCHandshakeResponse(conn io.Reader, req *rpc.HandshakeRequest) (*rpc.HandshakeResponse, error) {
|
||||
var ans [128]byte
|
||||
|
||||
n, err := conn.Read(ans[:])
|
||||
func (t *MiddleTelegram) receiveRPCHandshakeResponse(conn wrappers.WrapPacketReader, req *rpc.HandshakeRequest) (*rpc.HandshakeResponse, error) {
|
||||
packet, err := conn.Read()
|
||||
if err != nil {
|
||||
return nil, errors.Annotate(err, "Cannot read RPC handshake response")
|
||||
}
|
||||
|
||||
rpcHandshakeResp, err := rpc.NewHandshakeResponse(ans[:n])
|
||||
rpcHandshakeResp, err := rpc.NewHandshakeResponse(packet)
|
||||
if err != nil {
|
||||
return nil, errors.Annotate(err, "Cannot initialize RPC handshake response")
|
||||
}
|
||||
|
||||
@@ -35,11 +35,10 @@ type middleTelegramCaller struct {
|
||||
|
||||
proxySecret []byte
|
||||
dialerMutex *sync.RWMutex
|
||||
logger *zap.SugaredLogger
|
||||
httpClient *http.Client
|
||||
}
|
||||
|
||||
func (t *middleTelegramCaller) Dial(sock string, connOpts *mtproto.ConnectionOpts) (wrappers.ReadWriteCloserWithAddr, error) {
|
||||
func (t *middleTelegramCaller) Dial(connID string, connOpts *mtproto.ConnectionOpts) (wrappers.WrapStreamReadWriteCloser, error) {
|
||||
dc := connOpts.DC
|
||||
if dc == 0 {
|
||||
dc = 1
|
||||
@@ -47,13 +46,13 @@ func (t *middleTelegramCaller) Dial(sock string, connOpts *mtproto.ConnectionOpt
|
||||
t.dialerMutex.RLock()
|
||||
defer t.dialerMutex.RUnlock()
|
||||
|
||||
return t.baseTelegram.dial(dc, sock, connOpts.ConnectionProto)
|
||||
return t.baseTelegram.dial(dc, connID, connOpts.ConnectionProto)
|
||||
}
|
||||
|
||||
func (t *middleTelegramCaller) autoUpdate() {
|
||||
for range time.Tick(middleTelegramAutoUpdateInterval) {
|
||||
if err := t.update(); err != nil {
|
||||
t.logger.Warnw("Cannot update from Telegram", "error", err)
|
||||
zap.S().Warnw("Cannot update from Telegram", "error", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -80,7 +79,7 @@ func (t *middleTelegramCaller) update() error {
|
||||
t.v6Addresses = v6Addresses
|
||||
t.dialerMutex.Unlock()
|
||||
|
||||
t.logger.Infow("Telegram middle proxy data has been updated")
|
||||
zap.S().Infow("Telegram middle proxy data has been updated")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
+2
-10
@@ -9,14 +9,6 @@ import (
|
||||
"github.com/9seconds/mtg/wrappers"
|
||||
)
|
||||
|
||||
// Telegram defines an interface to connect to Telegram. This
|
||||
// encapsulates logic of working with middleproxies or direct
|
||||
// connections.
|
||||
type Telegram interface {
|
||||
Dial(string, *mtproto.ConnectionOpts) (wrappers.ReadWriteCloserWithAddr, error)
|
||||
Init(*mtproto.ConnectionOpts, wrappers.ReadWriteCloserWithAddr) (wrappers.ReadWriteCloserWithAddr, error)
|
||||
}
|
||||
|
||||
type baseTelegram struct {
|
||||
dialer tgDialer
|
||||
|
||||
@@ -24,7 +16,7 @@ type baseTelegram struct {
|
||||
v6Addresses map[int16][]string
|
||||
}
|
||||
|
||||
func (b *baseTelegram) dial(dcIdx int16, sock string, proto mtproto.ConnectionProtocol) (wrappers.ReadWriteCloserWithAddr, error) {
|
||||
func (b *baseTelegram) dial(dcIdx int16, connID string, proto mtproto.ConnectionProtocol) (wrappers.WrapStreamReadWriteCloser, error) {
|
||||
addrs := make([]string, 2)
|
||||
|
||||
if proto&mtproto.ConnectionProtocolIPv6 != 0 {
|
||||
@@ -39,7 +31,7 @@ func (b *baseTelegram) dial(dcIdx int16, sock string, proto mtproto.ConnectionPr
|
||||
}
|
||||
|
||||
for _, addr := range addrs {
|
||||
if conn, err := b.dialer.dialRWC(addr, sock); err == nil {
|
||||
if conn, err := b.dialer.dialRWC(addr, connID); err == nil {
|
||||
return conn, err
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user