mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 22:24:02 +03:00
Proxy is working in a simple mode now
This commit is contained in:
@@ -23,16 +23,15 @@ func (c *clientHandhakeFrame) decryptor(secret []byte) cipher.Stream {
|
||||
}
|
||||
|
||||
func (c *clientHandhakeFrame) encryptor(secret []byte) cipher.Stream {
|
||||
arr := clientHandhakeFrame{}
|
||||
invertByteSlices(arr.data[:], c.data[:])
|
||||
invertedHandshake := c.invert()
|
||||
|
||||
hasher := acquireSha256Hasher()
|
||||
defer releaseSha256Hasher(hasher)
|
||||
|
||||
hasher.Write(arr.key()) // nolint: errcheck
|
||||
hasher.Write(secret) // nolint: errcheck
|
||||
hasher.Write(invertedHandshake.key()) // nolint: errcheck
|
||||
hasher.Write(secret) // nolint: errcheck
|
||||
|
||||
return makeAesCtr(hasher.Sum(nil), arr.iv())
|
||||
return makeAesCtr(hasher.Sum(nil), invertedHandshake.iv())
|
||||
}
|
||||
|
||||
func ClientHandshake(secret []byte, reader io.Reader) (int, cipher.Stream, cipher.Stream, error) {
|
||||
|
||||
@@ -60,3 +60,13 @@ func (h *handshakeFrame) iv() []byte {
|
||||
func (h *handshakeFrame) connectionType() []byte {
|
||||
return h.data[handshakeFrameOffsetConnectionType:handshakeFrameOffsetDC]
|
||||
}
|
||||
|
||||
func (h *handshakeFrame) invert() handshakeFrame {
|
||||
copyFrame := *h
|
||||
|
||||
for i := 0; i < handshakeFrameLenKey+handshakeFrameLenIV; i++ {
|
||||
copyFrame.data[handshakeFrameOffsetKey+i] = h.data[handshakeFrameOffsetConnectionType-1-i]
|
||||
}
|
||||
|
||||
return copyFrame
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"crypto/rand"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"net"
|
||||
"io"
|
||||
)
|
||||
|
||||
type serverHandshakeFrame struct {
|
||||
@@ -13,17 +13,16 @@ type serverHandshakeFrame struct {
|
||||
}
|
||||
|
||||
func (s *serverHandshakeFrame) decryptor() cipher.Stream {
|
||||
return makeAesCtr(s.key(), s.iv())
|
||||
invertedHandshake := s.invert()
|
||||
|
||||
return makeAesCtr(invertedHandshake.key(), invertedHandshake.iv())
|
||||
}
|
||||
|
||||
func (s *serverHandshakeFrame) encryptor() cipher.Stream {
|
||||
arr := serverHandshakeFrame{}
|
||||
invertByteSlices(arr.data[:], s.data[:])
|
||||
|
||||
return makeAesCtr(arr.key(), arr.iv())
|
||||
return makeAesCtr(s.key(), s.iv())
|
||||
}
|
||||
|
||||
func ServerHandshake(conn net.Conn) (cipher.Stream, cipher.Stream, error) {
|
||||
func ServerHandshake(writer io.Writer) (cipher.Stream, cipher.Stream, error) {
|
||||
handshake := generateServerHanshakeFrame()
|
||||
copyHandshake := handshake
|
||||
encryptor := handshake.encryptor()
|
||||
@@ -33,7 +32,7 @@ func ServerHandshake(conn net.Conn) (cipher.Stream, cipher.Stream, error) {
|
||||
copy(handshake.key(), copyHandshake.key())
|
||||
copy(handshake.iv(), copyHandshake.iv())
|
||||
|
||||
if _, err := conn.Write(handshake.data[:]); err != nil {
|
||||
if _, err := writer.Write(handshake.data[:]); err != nil {
|
||||
return nil, nil, fmt.Errorf("cannot send a handshake frame to telegram: %w", err)
|
||||
}
|
||||
|
||||
@@ -48,16 +47,16 @@ func generateServerHanshakeFrame() serverHandshakeFrame {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if frame.data[0] == 0xef {
|
||||
if frame.data[0] == 0xef { // nolint: gomnd // taken from tg sources
|
||||
continue
|
||||
}
|
||||
|
||||
switch binary.LittleEndian.Uint32(frame.data[:4]) {
|
||||
case 0x44414548, 0x54534f50, 0x20544547, 0x4954504f, 0xeeeeeeee:
|
||||
case 0x44414548, 0x54534f50, 0x20544547, 0x4954504f, 0xeeeeeeee: // nolint: gomnd // taken from tg sources
|
||||
continue
|
||||
}
|
||||
|
||||
if (frame.data[4] | frame.data[5] | frame.data[6] | frame.data[7]) == 0 {
|
||||
if frame.data[4]|frame.data[5]|frame.data[6]|frame.data[7] == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
@@ -13,11 +13,3 @@ func makeAesCtr(key, iv []byte) cipher.Stream {
|
||||
|
||||
return cipher.NewCTR(block, iv)
|
||||
}
|
||||
|
||||
func invertByteSlices(dst, src []byte) {
|
||||
lenDst := len(dst) - 1
|
||||
|
||||
for i, v := range src {
|
||||
dst[lenDst-i] = v
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user