From 331b6a208ff4393e7188fad05b437abbd86845e8 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Sun, 8 Jul 2018 16:59:51 +0200 Subject: [PATCH] Implement HTTP mode with hardcoded values --- telethon/network/connection/__init__.py | 1 + telethon/network/connection/http.py | 29 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 telethon/network/connection/http.py diff --git a/telethon/network/connection/__init__.py b/telethon/network/connection/__init__.py index 0c7a07d0..262aaa3a 100644 --- a/telethon/network/connection/__init__.py +++ b/telethon/network/connection/__init__.py @@ -2,3 +2,4 @@ from .tcpfull import ConnectionTcpFull from .tcpabridged import ConnectionTcpAbridged from .tcpobfuscated import ConnectionTcpObfuscated from .tcpintermediate import ConnectionTcpIntermediate +from .http import ConnectionHttp diff --git a/telethon/network/connection/http.py b/telethon/network/connection/http.py new file mode 100644 index 00000000..7e2a07f6 --- /dev/null +++ b/telethon/network/connection/http.py @@ -0,0 +1,29 @@ +from .tcpfull import ConnectionTcpFull + + +class ConnectionHttp(ConnectionTcpFull): + async def recv(self): + while True: + line = await self._read_line() + if line.lower().startswith(b'content-length: '): + await self.read(2) + length = int(line[16:-2]) + return await self.read(length) + + async def _read_line(self): + newline = ord('\n') + line = await self.read(1) + while line[-1] != newline: + line += await self.read(1) + return line + + async def send(self, message): + await self.write( + 'POST /api HTTP/1.1\r\n' + 'Host: 149.154.167.91:80\r\n' + 'Content-Type: application/x-www-form-urlencoded\r\n' + 'Connection: keep-alive\r\n' + 'Keep-Alive: timeout=100000, max=10000000\r\n' + 'Content-Length: {}\r\n\r\n'.format(len(message)) + .encode('ascii') + message + )