mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 14:54:01 +03:00
Simplify internals
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user