Rework for simplier proxy

This commit is contained in:
9seconds
2018-07-08 08:04:29 +03:00
parent cd63483503
commit a0ba89b105
12 changed files with 154 additions and 195 deletions
+2 -2
View File
@@ -43,7 +43,7 @@ func (t *DirectTelegram) Dial(connID string, connOpts *mtproto.ConnectionOpts) (
return t.baseTelegram.dial(dc-1, connID, connOpts.ConnectionProto)
}
func (t *DirectTelegram) Init(connOpts *mtproto.ConnectionOpts, conn wrappers.WrapStreamReadWriteCloser) (wrappers.WrapStreamReadWriteCloser, error) {
func (t *DirectTelegram) Init(connOpts *mtproto.ConnectionOpts, conn wrappers.WrapStreamReadWriteCloser) (wrappers.Wrap, error) {
obfs2, frame := obfuscated2.MakeTelegramObfuscated2Frame(connOpts)
if _, err := conn.Write(frame); err != nil {
@@ -55,7 +55,7 @@ func (t *DirectTelegram) Init(connOpts *mtproto.ConnectionOpts, conn wrappers.Wr
// NewDirectTelegram returns Telegram instance which connects directly
// to Telegram bypassing middleproxies.
func NewDirectTelegram(conf *config.Config) *DirectTelegram {
func NewDirectTelegram(conf *config.Config) Telegram {
return &DirectTelegram{baseTelegram{
dialer: tgDialer{
Dialer: net.Dialer{Timeout: telegramDialTimeout},
+26 -26
View File
@@ -19,32 +19,7 @@ type MiddleTelegram struct {
conf *config.Config
}
func NewMiddleTelegram(conf *config.Config) *MiddleTelegram {
tg := &MiddleTelegram{
middleTelegramCaller: middleTelegramCaller{
baseTelegram: baseTelegram{
dialer: tgDialer{
Dialer: net.Dialer{Timeout: telegramDialTimeout},
conf: conf,
},
},
httpClient: &http.Client{
Timeout: middleTelegramHTTPClientTimeout,
},
dialerMutex: &sync.RWMutex{},
},
conf: conf,
}
if err := tg.update(); err != nil {
panic(err)
}
go tg.autoUpdate()
return tg
}
func (t *MiddleTelegram) Init(connOpts *mtproto.ConnectionOpts, conn wrappers.WrapStreamReadWriteCloser) (wrappers.WrapPacketReadWriteCloser, error) {
func (t *MiddleTelegram) Init(connOpts *mtproto.ConnectionOpts, conn wrappers.WrapStreamReadWriteCloser) (wrappers.Wrap, error) {
rpcNonceConn := wrappers.NewMTProtoFrame(conn, rpc.SeqNoNonce)
rpcNonceReq, err := t.sendRPCNonceRequest(rpcNonceConn)
@@ -125,3 +100,28 @@ func (t *MiddleTelegram) receiveRPCHandshakeResponse(conn wrappers.WrapPacketRea
return rpcHandshakeResp, nil
}
func NewMiddleTelegram(conf *config.Config) Telegram {
tg := &MiddleTelegram{
middleTelegramCaller: middleTelegramCaller{
baseTelegram: baseTelegram{
dialer: tgDialer{
Dialer: net.Dialer{Timeout: telegramDialTimeout},
conf: conf,
},
},
httpClient: &http.Client{
Timeout: middleTelegramHTTPClientTimeout,
},
dialerMutex: &sync.RWMutex{},
},
conf: conf,
}
if err := tg.update(); err != nil {
panic(err)
}
go tg.autoUpdate()
return tg
}
+5
View File
@@ -9,6 +9,11 @@ import (
"github.com/9seconds/mtg/wrappers"
)
type Telegram interface {
Dial(string, *mtproto.ConnectionOpts) (wrappers.WrapStreamReadWriteCloser, error)
Init(*mtproto.ConnectionOpts, wrappers.WrapStreamReadWriteCloser) (wrappers.Wrap, error)
}
type baseTelegram struct {
dialer tgDialer