Skip to content

Commit 0145692

Browse files
committed
fix pypi dependencies + small howto
1 parent 274ffad commit 0145692

File tree

10 files changed

+58
-32
lines changed

10 files changed

+58
-32
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,13 @@
11
# aMTProxy
22
Asynchronous Python MTProto proxy server [beta]
3+
4+
# Installation
5+
To get the latest build use `pip install amtproxy`
6+
7+
# Usage
8+
9+
`from amtproxy.server import start_server`\
10+
`start_server(port=1234, secret='32-digit hex value')`
11+
12+
# TBD
13+
- [ ] `dd` secret prefix

amtproxy/config.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
telegram_ips = [ "149.154.175.50", "149.154.167.51", "149.154.175.100", "149.154.167.91", "149.154.171.5"]
1+
telegram_ips = ["149.154.175.50", "149.154.167.51", "149.154.175.100", "149.154.167.91", "149.154.171.5"]
22
mtproto_footprint = b'\xef\xef\xef\xef'
33
obf_footprint = b'\xdd\xdd\xdd\xdd'
4-
secret = '1234567890abc1234567890abcef1234'
5-

amtproxy/dispatcher.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
from amtproxy.encryption.crypto import Encryptor, Decryptor
2-
from amtproxy.config import secret, telegram_ips, mtproto_footprint, obf_footprint
3-
from amtproxy.exceptions import WrongServerID
4-
from amtproxy.protocols.telegram import TelegramProtocol
1+
from .encryption.crypto import Encryptor, Decryptor
2+
from .config import telegram_ips, mtproto_footprint, obf_footprint
3+
from .exceptions import WrongSecret
4+
from .protocols.telegram import TelegramProtocol
55

66

77
class Dispatcher:
8-
def __init__(self, loop, real_protocol):
8+
def __init__(self, loop, real_protocol, secret):
99
self.loop = loop
10+
self.secret = secret
1011
self.real_protocol = real_protocol
1112
self.telegram_protocol = None
1213
self.telegram_transport = None
@@ -15,14 +16,20 @@ def __init__(self, loop, real_protocol):
1516

1617
async def handle(self, raw_data: bytes):
1718
if not self.encryptor or not self.decryptor:
18-
self.decryptor = Decryptor(raw_data, secret)
19-
self.encryptor = Encryptor(raw_data, secret)
19+
self.decryptor = Decryptor(raw_data, self.secret)
20+
self.encryptor = Encryptor(raw_data, self.secret)
2021
self.decoded_data = await self.decryptor(raw_data)
2122
server_id = int.from_bytes(self.decoded_data[60:62], 'little') - 1
2223
if server_id < 0 or server_id > len(telegram_ips):
23-
raise WrongServerID(f'Got id {server_id}')
24+
raise WrongSecret(f'Got id {server_id}')
2425
self.telegram_server_ip = telegram_ips[server_id]
25-
assert self.decoded_data[56:60] == mtproto_footprint or self.decoded_data[56:60] == obf_footprint, self.decoded_data[56:60]
26+
if self.decoded_data[56:60] == mtproto_footprint:
27+
pass
28+
elif self.decoded_data[56:60] == obf_footprint:
29+
# TODO dd prefix (padding)
30+
pass
31+
else:
32+
raise Exception('Unknown Protocol')
2633
if not self.telegram_protocol or not self.telegram_transport:
2734
self.telegram_transport, self.telegram_protocol = await self.loop.create_connection(lambda: TelegramProtocol(self.loop, self),
2835
self.telegram_server_ip, 443)

amtproxy/encryption/crypto.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from hashlib import sha256
22

3-
from amtproxy.encryption.aes import AES_CTR
3+
from .aes import AES_CTR
44

55

66
class DecryptionKey:

amtproxy/exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ class ProxyBaseException(Exception):
44
pass
55

66

7-
class WrongServerID(ProxyBaseException):
7+
class WrongSecret(ProxyBaseException):
88
pass

amtproxy/protocols/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +0,0 @@
1-
from .real import RealProtocol
2-
from .telegram import TelegramProtocol

amtproxy/protocols/real.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,35 @@
11
import asyncio
2-
from amtproxy.dispatcher import Dispatcher
2+
from ..dispatcher import Dispatcher
33

44

55
class RealProtocol(asyncio.Protocol):
6-
def __init__(self, loop, *args, **kwargs):
6+
transport: asyncio.transports.BaseTransport
7+
task: asyncio.Task = None
8+
9+
def __init__(self, loop, secret, *args, **kwargs):
710
super().__init__(*args, **kwargs)
811
self.loop = loop
9-
self.dispatcher = Dispatcher(loop, self)
12+
self.dispatcher = Dispatcher(loop, self, secret)
1013

1114
def connection_made(self, transport):
1215
self.transport = transport
1316

17+
1418
def data_received(self, data):
15-
task = asyncio.Task(self.async_handler(data), loop=self.loop)
19+
self.task = asyncio.Task(self.async_handler(data), loop=self.loop)
1620

1721

1822
@asyncio.coroutine
1923
def async_handler(self, data):
2024
return (yield from self.dispatcher.handle(data))
2125

2226
def connection_lost(self, exc) -> None:
27+
if self.task:
28+
self.task.cancel()
2329
try:
2430
self.dispatcher.telegram_transport.close()
2531
except AttributeError:
2632
pass
2733

2834
def close_connection(self):
29-
self.transport.close()
35+
self.transport.close()

amtproxy/protocols/telegram.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import asyncio
2+
from asyncio.transports import BaseTransport
23

34

45
class TelegramProtocol(asyncio.Protocol):
5-
6+
transport: BaseTransport
7+
task: asyncio.Task = None
8+
69
def __init__(self, loop, dispatcher, *args, **kwargs):
710
super().__init__(*args, **kwargs)
811
self.loop = loop
@@ -12,11 +15,13 @@ def connection_made(self, transport):
1215
self.transport = transport
1316

1417
def data_received(self, data):
15-
task = asyncio.Task(self.async_handler(data), loop=self.loop)
18+
self.task = asyncio.Task(self.async_handler(data), loop=self.loop)
1619

1720
@asyncio.coroutine
1821
def async_handler(self, data):
1922
return (yield from self.dispatcher.reverse_handle(data))
2023

2124
def connection_lost(self, exc) -> None:
22-
self.dispatcher.real_protocol.close_connection()
25+
if self.task:
26+
self.task.cancel()
27+
self.dispatcher.real_protocol.close_connection()

amtproxy/server.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
import asyncio
2-
from amtproxy.protocols import RealProtocol
32

3+
from .protocols.real import RealProtocol
44

5-
async def main(port=8080):
6-
loop = asyncio.get_running_loop()
75

6+
async def main(port=8080, secret='1234567890abc1234567890abcef1234'):
7+
loop = asyncio.get_running_loop()
8+
print(f'MTProxy is starting on port {port}\nWith secret {secret}')
89
server = await loop.create_server(
9-
lambda: RealProtocol(loop),
10+
lambda: RealProtocol(loop, secret),
1011
'0.0.0.0', port)
1112

1213
async with server:
1314
await server.serve_forever()
1415

1516

16-
def start(port=None):
17-
asyncio.run(main(port))
17+
def start_server(port=None, secret=None):
18+
args = {'port': port, 'secret': secret}
19+
asyncio.run(main(**{k: v for k, v in args.items() if v}))
1820

1921

2022
if __name__ == '__main__':
2123
asyncio.run(main())
22-

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
setuptools.setup(
1313
name="aMTProxy",
14-
version="0.1.5",
14+
version="0.2",
1515
author="Yury (Yurzs)",
1616
author_email="dev@best-service.online",
1717
description="Async python MTProxy server",

0 commit comments

Comments
 (0)