mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-09-01 04:44:02 +03:00
wip
This commit is contained in:
+16
-22
@@ -1,7 +1,6 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net"
|
||||
@@ -28,25 +27,8 @@ func (b *baseTelegram) Secret() []byte {
|
||||
return b.secret
|
||||
}
|
||||
|
||||
func (b *baseTelegram) dialToAddress(ctx context.Context,
|
||||
cancel context.CancelFunc,
|
||||
addr string) (wrappers.StreamReadWriteCloser, error) {
|
||||
conn, err := b.dialer.Dial("tcp", addr)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("dial has failed: %w", err)
|
||||
}
|
||||
|
||||
if err := utils.InitTCP(conn); err != nil {
|
||||
return nil, fmt.Errorf("cannot initialize tcp socket: %w", err)
|
||||
}
|
||||
|
||||
return wrappers.NewTelegramConn(ctx, cancel, conn), nil
|
||||
}
|
||||
|
||||
func (b *baseTelegram) dial(ctx context.Context,
|
||||
cancel context.CancelFunc,
|
||||
dc conntypes.DC,
|
||||
protocol conntypes.ConnectionProtocol) (wrappers.StreamReadWriteCloser, error) {
|
||||
func (b *baseTelegram) dial(dc conntypes.DC,
|
||||
protocol conntypes.ConnectionProtocol) (conntypes.StreamReadWriteCloser, error) {
|
||||
addr := ""
|
||||
|
||||
switch protocol {
|
||||
@@ -56,7 +38,16 @@ func (b *baseTelegram) dial(ctx context.Context,
|
||||
addr = b.chooseAddress(b.v6Addresses, dc, b.V6DefaultDC)
|
||||
}
|
||||
|
||||
return b.dialToAddress(ctx, cancel, addr)
|
||||
conn, err := b.dialer.Dial("tcp", addr)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("dial has failed: %w", err)
|
||||
}
|
||||
|
||||
if err := utils.InitTCP(conn); err != nil {
|
||||
return nil, fmt.Errorf("cannot initialize tcp socket: %w", err)
|
||||
}
|
||||
|
||||
return wrappers.NewTelegramConn(conn), nil
|
||||
}
|
||||
|
||||
func (b *baseTelegram) chooseAddress(addresses map[conntypes.DC][]string,
|
||||
@@ -66,7 +57,10 @@ func (b *baseTelegram) chooseAddress(addresses map[conntypes.DC][]string,
|
||||
addrs, _ = addresses[defaultDC]
|
||||
}
|
||||
|
||||
if len(addrs) > 0 {
|
||||
switch {
|
||||
case len(addrs) == 1:
|
||||
return addrs[0]
|
||||
case len(addrs) > 1:
|
||||
return addrs[rand.Intn(len(addrs))]
|
||||
}
|
||||
|
||||
|
||||
+11
-19
@@ -1,15 +1,11 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
|
||||
"github.com/9seconds/mtg/conntypes"
|
||||
"github.com/9seconds/mtg/wrappers"
|
||||
)
|
||||
|
||||
var Direct = newDirectTelegram()
|
||||
|
||||
const (
|
||||
directV4DefaultIdx conntypes.DC = 1
|
||||
directV6DefaultIdx conntypes.DC = 1
|
||||
@@ -36,10 +32,8 @@ type directTelegram struct {
|
||||
baseTelegram
|
||||
}
|
||||
|
||||
func (d *directTelegram) Dial(ctx context.Context,
|
||||
cancel context.CancelFunc,
|
||||
dc conntypes.DC,
|
||||
protocol conntypes.ConnectionProtocol) (wrappers.StreamReadWriteCloser, error) {
|
||||
func (d *directTelegram) Dial(dc conntypes.DC,
|
||||
protocol conntypes.ConnectionProtocol) (conntypes.StreamReadWriteCloser, error) {
|
||||
switch {
|
||||
case dc < 0:
|
||||
dc = -dc
|
||||
@@ -47,17 +41,15 @@ func (d *directTelegram) Dial(ctx context.Context,
|
||||
dc = conntypes.DCDefaultIdx
|
||||
}
|
||||
|
||||
return d.baseTelegram.dial(ctx, cancel, dc-1, protocol)
|
||||
return d.baseTelegram.dial(dc-1, protocol)
|
||||
}
|
||||
|
||||
func newDirectTelegram() Telegram {
|
||||
return &directTelegram{
|
||||
baseTelegram: baseTelegram{
|
||||
dialer: net.Dialer{Timeout: telegramDialTimeout},
|
||||
v4DefaultDC: directV4DefaultIdx,
|
||||
V6DefaultDC: directV6DefaultIdx,
|
||||
v4Addresses: directV4Addresses,
|
||||
v6Addresses: directV6Addresses,
|
||||
},
|
||||
}
|
||||
var Direct = &directTelegram{
|
||||
baseTelegram: baseTelegram{
|
||||
dialer: net.Dialer{Timeout: telegramDialTimeout},
|
||||
v4DefaultDC: directV4DefaultIdx,
|
||||
V6DefaultDC: directV6DefaultIdx,
|
||||
v4Addresses: directV4Addresses,
|
||||
v6Addresses: directV6Addresses,
|
||||
},
|
||||
}
|
||||
|
||||
+2
-10
@@ -1,16 +1,8 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/9seconds/mtg/conntypes"
|
||||
"github.com/9seconds/mtg/wrappers"
|
||||
)
|
||||
import "github.com/9seconds/mtg/conntypes"
|
||||
|
||||
type Telegram interface {
|
||||
Dial(context.Context,
|
||||
context.CancelFunc,
|
||||
conntypes.DC,
|
||||
conntypes.ConnectionProtocol) (wrappers.StreamReadWriteCloser, error)
|
||||
Dial(conntypes.DC, conntypes.ConnectionProtocol) (conntypes.StreamReadWriteCloser, error)
|
||||
Secret() []byte
|
||||
}
|
||||
|
||||
+3
-7
@@ -1,7 +1,6 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"sync"
|
||||
@@ -11,7 +10,6 @@ import (
|
||||
|
||||
"github.com/9seconds/mtg/conntypes"
|
||||
"github.com/9seconds/mtg/telegram/api"
|
||||
"github.com/9seconds/mtg/wrappers"
|
||||
)
|
||||
|
||||
const middleTelegramBackgroundUpdateEvery = time.Hour
|
||||
@@ -67,10 +65,8 @@ func (m *middleTelegram) backgroundUpdate() {
|
||||
}
|
||||
}
|
||||
|
||||
func (m *middleTelegram) Dial(ctx context.Context,
|
||||
cancel context.CancelFunc,
|
||||
dc conntypes.DC,
|
||||
protocol conntypes.ConnectionProtocol) (wrappers.StreamReadWriteCloser, error) {
|
||||
func (m *middleTelegram) Dial(dc conntypes.DC,
|
||||
protocol conntypes.ConnectionProtocol) (conntypes.StreamReadWriteCloser, error) {
|
||||
if dc == 0 {
|
||||
dc = conntypes.DCDefaultIdx
|
||||
}
|
||||
@@ -78,7 +74,7 @@ func (m *middleTelegram) Dial(ctx context.Context,
|
||||
m.mutex.RLock()
|
||||
defer m.mutex.RUnlock()
|
||||
|
||||
return m.baseTelegram.dial(ctx, cancel, dc, protocol)
|
||||
return m.baseTelegram.dial(dc, protocol)
|
||||
}
|
||||
|
||||
func MiddleInit() {
|
||||
|
||||
Reference in New Issue
Block a user