mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-09-01 00:14:02 +03:00
Move telegram to separate module
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/juju/errors"
|
||||
|
||||
"github.com/9seconds/mtg/config"
|
||||
"github.com/9seconds/mtg/wrappers"
|
||||
)
|
||||
|
||||
const telegramKeepAlive = 30 * time.Second
|
||||
|
||||
type tgDialer struct {
|
||||
net.Dialer
|
||||
|
||||
conf *config.Config
|
||||
}
|
||||
|
||||
func (t *tgDialer) dial(addr string) (net.Conn, error) {
|
||||
connRaw, err := t.Dialer.Dial("tcp", addr)
|
||||
if err != nil {
|
||||
return nil, errors.Annotate(err, "Cannot connect to Telegram")
|
||||
}
|
||||
conn := connRaw.(*net.TCPConn)
|
||||
|
||||
if err = conn.SetKeepAlive(true); err != nil {
|
||||
return nil, errors.Annotate(err, "Cannot establish keepalive connection")
|
||||
}
|
||||
if err = conn.SetKeepAlivePeriod(telegramKeepAlive); err != nil {
|
||||
return nil, errors.Annotate(err, "Cannot set keepalive timeout")
|
||||
}
|
||||
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
func (t *tgDialer) dialRWC(addr string) (io.ReadWriteCloser, error) {
|
||||
conn, err := t.dial(addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return wrappers.NewTimeoutRWC(conn, t.conf.TimeoutRead, t.conf.TimeoutWrite), nil
|
||||
}
|
||||
|
||||
func newDialer(conf *config.Config) *tgDialer {
|
||||
return &tgDialer{
|
||||
Dialer: net.Dialer{Timeout: conf.TimeoutRead},
|
||||
conf: conf,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"github.com/juju/errors"
|
||||
|
||||
"github.com/9seconds/mtg/config"
|
||||
"github.com/9seconds/mtg/obfuscated2"
|
||||
"github.com/9seconds/mtg/wrappers"
|
||||
)
|
||||
|
||||
var (
|
||||
directV4Addresses = map[int16][]string{
|
||||
0: []string{"149.154.175.50:443"},
|
||||
1: []string{"149.154.167.51:443"},
|
||||
2: []string{"149.154.175.100:443"},
|
||||
3: []string{"149.154.167.91:443"},
|
||||
4: []string{"149.154.171.5:443"},
|
||||
}
|
||||
directV6Addresses = map[int16][]string{
|
||||
0: []string{"[2001:b28:f23d:f001::a]:443"},
|
||||
1: []string{"[2001:67c:04e8:f002::a]:443"},
|
||||
2: []string{"[2001:b28:f23d:f003::a]:443"},
|
||||
3: []string{"[2001:67c:04e8:f004::a]:443"},
|
||||
4: []string{"[2001:b28:f23f:f005::a]:443"},
|
||||
}
|
||||
)
|
||||
|
||||
type directTelegram struct {
|
||||
baseTelegram
|
||||
}
|
||||
|
||||
func (t *directTelegram) Dial(dcIdx int16) (io.ReadWriteCloser, error) {
|
||||
if dcIdx < 0 {
|
||||
dcIdx = -dcIdx
|
||||
} else if dcIdx == 0 {
|
||||
dcIdx = 1
|
||||
}
|
||||
|
||||
return t.baseTelegram.Dial(dcIdx - 1)
|
||||
}
|
||||
|
||||
func (t *directTelegram) Init(conn io.ReadWriteCloser) (io.ReadWriteCloser, error) {
|
||||
obfs2, frame := obfuscated2.MakeTelegramObfuscated2Frame()
|
||||
if n, err := conn.Write(frame); err != nil || n != len(frame) {
|
||||
return nil, errors.Annotate(err, "Cannot write hadnshake frame")
|
||||
}
|
||||
|
||||
return wrappers.NewStreamCipherRWC(conn, obfs2.Encryptor, obfs2.Decryptor), nil
|
||||
}
|
||||
|
||||
func NewDirectTelegram(conf *config.Config) Telegram {
|
||||
return &directTelegram{baseTelegram{
|
||||
dialer: newDialer(conf),
|
||||
v4Addresses: directV4Addresses,
|
||||
v6Addresses: directV6Addresses,
|
||||
}}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"io"
|
||||
"math/rand"
|
||||
|
||||
"github.com/juju/errors"
|
||||
)
|
||||
|
||||
type Telegram interface {
|
||||
Dial(int16) (io.ReadWriteCloser, error)
|
||||
Init(io.ReadWriteCloser) (io.ReadWriteCloser, error)
|
||||
}
|
||||
|
||||
type baseTelegram struct {
|
||||
dialer *tgDialer
|
||||
|
||||
v4Addresses map[int16][]string
|
||||
v6Addresses map[int16][]string
|
||||
}
|
||||
|
||||
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))])
|
||||
}
|
||||
if addr, ok := b.v4Addresses[dcIdx]; ok && len(addr) > 0 {
|
||||
addrs = append(addrs, addr[rand.Intn(len(addr))])
|
||||
}
|
||||
|
||||
for _, addr := range addrs {
|
||||
if conn, err := b.dialer.dialRWC(addr); err == nil {
|
||||
return conn, err
|
||||
}
|
||||
}
|
||||
|
||||
return nil, errors.New("Cannot connect to Telegram")
|
||||
}
|
||||
Reference in New Issue
Block a user