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