mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-09-01 00:54:01 +03:00
Support different client connection types
This commit is contained in:
+2
-2
@@ -45,8 +45,8 @@ func (t *tgDialer) dialRWC(addr string) (io.ReadWriteCloser, error) {
|
||||
return wrappers.NewTimeoutRWC(conn, t.conf.TimeoutRead, t.conf.TimeoutWrite), nil
|
||||
}
|
||||
|
||||
func newDialer(conf *config.Config) *tgDialer {
|
||||
return &tgDialer{
|
||||
func newDialer(conf *config.Config) tgDialer {
|
||||
return tgDialer{
|
||||
Dialer: net.Dialer{Timeout: conf.TimeoutRead},
|
||||
conf: conf,
|
||||
}
|
||||
|
||||
+10
-8
@@ -6,6 +6,7 @@ import (
|
||||
"github.com/juju/errors"
|
||||
|
||||
"github.com/9seconds/mtg/config"
|
||||
"github.com/9seconds/mtg/mtproto"
|
||||
"github.com/9seconds/mtg/obfuscated2"
|
||||
"github.com/9seconds/mtg/wrappers"
|
||||
)
|
||||
@@ -31,18 +32,19 @@ type directTelegram struct {
|
||||
baseTelegram
|
||||
}
|
||||
|
||||
func (t *directTelegram) Dial(dcIdx int16) (io.ReadWriteCloser, error) {
|
||||
if dcIdx < 0 {
|
||||
dcIdx = -dcIdx
|
||||
} else if dcIdx == 0 {
|
||||
dcIdx = 1
|
||||
func (t *directTelegram) Dial(connOpts *mtproto.ConnectionOpts) (io.ReadWriteCloser, error) {
|
||||
dc := connOpts.DC
|
||||
if dc < 0 {
|
||||
dc = -dc
|
||||
} else if dc == 0 {
|
||||
dc = 1
|
||||
}
|
||||
|
||||
return t.baseTelegram.Dial(dcIdx - 1)
|
||||
return t.baseTelegram.dial(dc - 1)
|
||||
}
|
||||
|
||||
func (t *directTelegram) Init(conn io.ReadWriteCloser) (io.ReadWriteCloser, error) {
|
||||
obfs2, frame := obfuscated2.MakeTelegramObfuscated2Frame()
|
||||
func (t *directTelegram) Init(connOpts *mtproto.ConnectionOpts, conn io.ReadWriteCloser) (io.ReadWriteCloser, error) {
|
||||
obfs2, frame := obfuscated2.MakeTelegramObfuscated2Frame(connOpts)
|
||||
defer obfuscated2.ReturnFrame(frame)
|
||||
|
||||
if n, err := conn.Write(*frame); err != nil || n != len(*frame) {
|
||||
|
||||
@@ -5,24 +5,26 @@ import (
|
||||
"math/rand"
|
||||
|
||||
"github.com/juju/errors"
|
||||
|
||||
"github.com/9seconds/mtg/mtproto"
|
||||
)
|
||||
|
||||
// Telegram defines an interface to connect to Telegram. This
|
||||
// encapsulates logic of working with middleproxies or direct
|
||||
// connections.
|
||||
type Telegram interface {
|
||||
Dial(int16) (io.ReadWriteCloser, error)
|
||||
Init(io.ReadWriteCloser) (io.ReadWriteCloser, error)
|
||||
Dial(*mtproto.ConnectionOpts) (io.ReadWriteCloser, error)
|
||||
Init(*mtproto.ConnectionOpts, io.ReadWriteCloser) (io.ReadWriteCloser, error)
|
||||
}
|
||||
|
||||
type baseTelegram struct {
|
||||
dialer *tgDialer
|
||||
dialer tgDialer
|
||||
|
||||
v4Addresses map[int16][]string
|
||||
v6Addresses map[int16][]string
|
||||
}
|
||||
|
||||
func (b *baseTelegram) Dial(dcIdx int16) (io.ReadWriteCloser, error) {
|
||||
func (b *baseTelegram) dial(dcIdx int16) (io.ReadWriteCloser, error) {
|
||||
addrs := make([]string, 2)
|
||||
if addr, ok := b.v6Addresses[dcIdx]; ok && len(addr) > 0 {
|
||||
addrs = append(addrs, addr[rand.Intn(len(addr))])
|
||||
|
||||
Reference in New Issue
Block a user