mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 15:34:01 +03:00
Simplify internals
This commit is contained in:
+8
-8
@@ -14,7 +14,6 @@ import (
|
||||
"github.com/9seconds/mtg/obfuscated2"
|
||||
"github.com/9seconds/mtg/proxy"
|
||||
"github.com/9seconds/mtg/stats"
|
||||
"github.com/9seconds/mtg/telegram"
|
||||
"github.com/9seconds/mtg/utils"
|
||||
)
|
||||
|
||||
@@ -79,14 +78,15 @@ func Proxy() error {
|
||||
app := &proxy.Proxy{
|
||||
Logger: zap.S().Named("proxy"),
|
||||
Context: ctx,
|
||||
ClientProtocolMaker: obfuscated2.MakeClientProtocol,
|
||||
TelegramProtocolMaker: obfuscated2.MakeTelegramProtocol,
|
||||
}
|
||||
if len(config.C.AdTag) == 0 {
|
||||
app.TelegramProtocolMaker = obfuscated2.MakeTelegramProtocol
|
||||
app.TelegramDialer = telegram.NewDirectTelegram()
|
||||
}
|
||||
if config.C.SecretMode != config.SecretModeTLS {
|
||||
app.ClientProtocolMaker = obfuscated2.MakeClientProtocol
|
||||
}
|
||||
// if len(config.C.AdTag) == 0 {
|
||||
// app.TelegramProtocolMaker = obfuscated2.MakeTelegramProtocol
|
||||
// }
|
||||
// if config.C.SecretMode != config.SecretModeTLS {
|
||||
// app.ClientProtocolMaker = obfuscated2.MakeClientProtocol
|
||||
// }
|
||||
|
||||
app.Serve(proxyListener)
|
||||
|
||||
|
||||
@@ -20,7 +20,21 @@ import (
|
||||
const clientProtocolHandshakeTimeout = 10 * time.Second
|
||||
|
||||
type ClientProtocol struct {
|
||||
protocol.BaseProtocol
|
||||
connectionType conntypes.ConnectionType
|
||||
connectionProtocol conntypes.ConnectionProtocol
|
||||
dc conntypes.DC
|
||||
}
|
||||
|
||||
func (c *ClientProtocol) ConnectionType() conntypes.ConnectionType {
|
||||
return c.connectionType
|
||||
}
|
||||
|
||||
func (c *ClientProtocol) ConnectionProtocol() conntypes.ConnectionProtocol {
|
||||
return c.connectionProtocol
|
||||
}
|
||||
|
||||
func (c *ClientProtocol) DC() conntypes.DC {
|
||||
return c.dc
|
||||
}
|
||||
|
||||
func (c *ClientProtocol) Handshake(socket wrappers.StreamReadWriteCloser) (wrappers.StreamReadWriteCloser, error) {
|
||||
@@ -46,23 +60,23 @@ func (c *ClientProtocol) Handshake(socket wrappers.StreamReadWriteCloser) (wrapp
|
||||
magic := decryptedFrame.Magic()
|
||||
switch {
|
||||
case bytes.Equal(magic, conntypes.ConnectionTagAbridged):
|
||||
c.ConnectionType = conntypes.ConnectionTypeAbridged
|
||||
c.connectionType = conntypes.ConnectionTypeAbridged
|
||||
case bytes.Equal(magic, conntypes.ConnectionTagIntermediate):
|
||||
c.ConnectionType = conntypes.ConnectionTypeIntermediate
|
||||
c.connectionType = conntypes.ConnectionTypeIntermediate
|
||||
case bytes.Equal(magic, conntypes.ConnectionTagSecure):
|
||||
c.ConnectionType = conntypes.ConnectionTypeSecure
|
||||
c.connectionType = conntypes.ConnectionTypeSecure
|
||||
default:
|
||||
return nil, errors.New("Unknown connection type")
|
||||
}
|
||||
|
||||
c.ConnectionProtocol = conntypes.ConnectionProtocolIPv4
|
||||
c.connectionProtocol = conntypes.ConnectionProtocolIPv4
|
||||
if socket.LocalAddr().IP.To4() == nil {
|
||||
c.ConnectionProtocol = conntypes.ConnectionProtocolIPv6
|
||||
c.connectionProtocol = conntypes.ConnectionProtocolIPv6
|
||||
}
|
||||
|
||||
buf := bytes.NewReader(decryptedFrame.DC())
|
||||
if err := binary.Read(buf, binary.LittleEndian, &c.DC); err != nil {
|
||||
c.DC = conntypes.DCDefaultIdx
|
||||
if err := binary.Read(buf, binary.LittleEndian, &c.dc); err != nil {
|
||||
c.dc = conntypes.DCDefaultIdx
|
||||
}
|
||||
|
||||
antiReplayKey := decryptedFrame.Unique()
|
||||
|
||||
@@ -10,17 +10,13 @@ import (
|
||||
"github.com/9seconds/mtg/wrappers"
|
||||
)
|
||||
|
||||
type TelegramProtocol struct {
|
||||
protocol.BaseProtocol
|
||||
|
||||
dialer telegram.Telegram
|
||||
}
|
||||
type TelegramProtocol struct{}
|
||||
|
||||
func (t *TelegramProtocol) Handshake(req *protocol.TelegramRequest) (wrappers.Wrap, error) {
|
||||
socket, err := t.dialer.Dial(req.Ctx,
|
||||
socket, err := telegram.Direct.Dial(req.Ctx,
|
||||
req.Cancel,
|
||||
req.ClientProtocol.GetDC(),
|
||||
req.ClientProtocol.GetConnectionProtocol())
|
||||
req.ClientProtocol.DC(),
|
||||
req.ClientProtocol.ConnectionProtocol())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot dial to telegram: %w", err)
|
||||
}
|
||||
@@ -43,10 +39,8 @@ func (t *TelegramProtocol) Handshake(req *protocol.TelegramRequest) (wrappers.Wr
|
||||
return wrappers.NewObfuscated2(socket, encryptor, decryptor), nil
|
||||
}
|
||||
|
||||
func MakeTelegramProtocol(dialer telegram.Telegram) protocol.TelegramProtocol {
|
||||
return &TelegramProtocol{
|
||||
dialer: dialer,
|
||||
}
|
||||
func MakeTelegramProtocol() protocol.TelegramProtocol {
|
||||
return &TelegramProtocol{}
|
||||
}
|
||||
|
||||
func generateFrame(cp protocol.ClientProtocol) (fm Frame) {
|
||||
@@ -70,7 +64,7 @@ func generateFrame(cp protocol.ClientProtocol) (fm Frame) {
|
||||
continue
|
||||
}
|
||||
|
||||
copy(fm.Magic(), cp.GetConnectionType().Tag())
|
||||
copy(fm.Magic(), cp.ConnectionType().Tag())
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
package protocol
|
||||
|
||||
import "github.com/9seconds/mtg/conntypes"
|
||||
|
||||
type BaseProtocol struct {
|
||||
ConnectionType conntypes.ConnectionType
|
||||
ConnectionProtocol conntypes.ConnectionProtocol
|
||||
DC conntypes.DC
|
||||
}
|
||||
|
||||
func (b *BaseProtocol) GetConnectionType() conntypes.ConnectionType {
|
||||
return b.ConnectionType
|
||||
}
|
||||
|
||||
func (b *BaseProtocol) GetConnectionProtocol() conntypes.ConnectionProtocol {
|
||||
return b.ConnectionProtocol
|
||||
}
|
||||
|
||||
func (b *BaseProtocol) GetDC() conntypes.DC {
|
||||
return b.DC
|
||||
}
|
||||
@@ -2,21 +2,19 @@ package protocol
|
||||
|
||||
import (
|
||||
"github.com/9seconds/mtg/conntypes"
|
||||
"github.com/9seconds/mtg/telegram"
|
||||
"github.com/9seconds/mtg/wrappers"
|
||||
)
|
||||
|
||||
type ClientProtocol interface {
|
||||
Handshake(wrappers.StreamReadWriteCloser) (wrappers.StreamReadWriteCloser, error)
|
||||
GetConnectionType() conntypes.ConnectionType
|
||||
GetConnectionProtocol() conntypes.ConnectionProtocol
|
||||
GetDC() conntypes.DC
|
||||
ConnectionType() conntypes.ConnectionType
|
||||
ConnectionProtocol() conntypes.ConnectionProtocol
|
||||
DC() conntypes.DC
|
||||
}
|
||||
|
||||
type ClientProtocolMaker func() ClientProtocol
|
||||
|
||||
type TelegramProtocol interface {
|
||||
Handshake(*TelegramRequest) (wrappers.Wrap, error)
|
||||
}
|
||||
|
||||
type TelegramProtocolMaker func(telegram.Telegram) TelegramProtocol
|
||||
type ClientProtocolMaker func() ClientProtocol
|
||||
type TelegramProtocolMaker func() TelegramProtocol
|
||||
|
||||
+3
-5
@@ -12,7 +12,6 @@ import (
|
||||
"github.com/9seconds/mtg/conntypes"
|
||||
"github.com/9seconds/mtg/protocol"
|
||||
"github.com/9seconds/mtg/stats"
|
||||
"github.com/9seconds/mtg/telegram"
|
||||
"github.com/9seconds/mtg/utils"
|
||||
"github.com/9seconds/mtg/wrappers"
|
||||
)
|
||||
@@ -24,7 +23,6 @@ type Proxy struct {
|
||||
Context context.Context
|
||||
ClientProtocolMaker protocol.ClientProtocolMaker
|
||||
TelegramProtocolMaker protocol.TelegramProtocolMaker
|
||||
TelegramDialer telegram.Telegram
|
||||
}
|
||||
|
||||
func (p *Proxy) Serve(listener net.Listener) {
|
||||
@@ -77,8 +75,8 @@ func (p *Proxy) accept(conn net.Conn) {
|
||||
}
|
||||
defer wrappedConn.Close()
|
||||
|
||||
stats.S.ClientConnected(clientProtocol.GetConnectionType(), wrappedConn.RemoteAddr())
|
||||
defer stats.S.ClientDisconnected(clientProtocol.GetConnectionType(), wrappedConn.RemoteAddr())
|
||||
stats.S.ClientConnected(clientProtocol.ConnectionType(), wrappedConn.RemoteAddr())
|
||||
defer stats.S.ClientDisconnected(clientProtocol.ConnectionType(), wrappedConn.RemoteAddr())
|
||||
logger.Infow("Client connected", "addr", conn.RemoteAddr())
|
||||
|
||||
req := &protocol.TelegramRequest{
|
||||
@@ -100,7 +98,7 @@ func (p *Proxy) accept(conn net.Conn) {
|
||||
}
|
||||
|
||||
func (p *Proxy) acceptDirectConnection(request *protocol.TelegramRequest) error {
|
||||
telegramProtocol := p.TelegramProtocolMaker(p.TelegramDialer)
|
||||
telegramProtocol := p.TelegramProtocolMaker()
|
||||
telegramConnRaw, err := telegramProtocol.Handshake(request)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
+3
-1
@@ -8,6 +8,8 @@ import (
|
||||
"github.com/9seconds/mtg/wrappers"
|
||||
)
|
||||
|
||||
var Direct = newDirectTelegram()
|
||||
|
||||
const (
|
||||
directV4DefaultIdx conntypes.DC = 1
|
||||
directV6DefaultIdx conntypes.DC = 1
|
||||
@@ -48,7 +50,7 @@ func (d *directTelegram) Dial(ctx context.Context,
|
||||
return d.baseTelegram.dial(ctx, cancel, dc-1, protocol)
|
||||
}
|
||||
|
||||
func NewDirectTelegram() Telegram {
|
||||
func newDirectTelegram() Telegram {
|
||||
return &directTelegram{
|
||||
baseTelegram: baseTelegram{
|
||||
dialer: net.Dialer{Timeout: telegramDialTimeout},
|
||||
|
||||
@@ -16,6 +16,8 @@ import (
|
||||
|
||||
const middleTelegramBackgroundUpdateEvery = time.Hour
|
||||
|
||||
var Middle = NewMiddleTelegram()
|
||||
|
||||
type middleTelegram struct {
|
||||
baseTelegram
|
||||
|
||||
|
||||
Reference in New Issue
Block a user