From 351866d9e497450906df55a6b38ffc0730a64e43 Mon Sep 17 00:00:00 2001 From: Sergey Blazhko Date: Thu, 18 Jul 2019 12:33:38 +0300 Subject: [PATCH 1/4] Added option to connect via MTProxy. Proxy secret should be set in proxy.password config parameter. --- example-config.yaml | 2 +- mautrix_telegram/abstract_user.py | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/example-config.yaml b/example-config.yaml index 1c80967e..690d081b 100644 --- a/example-config.yaml +++ b/example-config.yaml @@ -307,7 +307,7 @@ telegram: # Telethon proxy configuration. # You must install PySocks from pip for proxies to work. proxy: - # Allowed types: disabled, socks4, socks5, http + # Allowed types: disabled, socks4, socks5, http, mtproxy type: disabled # Proxy IP address and port. address: 127.0.0.1 diff --git a/mautrix_telegram/abstract_user.py b/mautrix_telegram/abstract_user.py index dd9f3d02..dd3112f4 100644 --- a/mautrix_telegram/abstract_user.py +++ b/mautrix_telegram/abstract_user.py @@ -96,6 +96,8 @@ class AbstractUser(ABC): proxy_type = 2 elif proxy_type == "http": proxy_type = 3 + elif proxy_type == "mtproxy": + proxy_type = 4 return (proxy_type, config["telegram.proxy.address"], config["telegram.proxy.port"], @@ -119,6 +121,12 @@ class AbstractUser(ABC): device = config["telegram.device_info.device_model"] sysversion = config["telegram.device_info.system_version"] appversion = config["telegram.device_info.app_version"] + import telethon + connection = telethon.network.connection.ConnectionTcpFull + proxy = self._proxy_settings + if proxy is not None and proxy[0] == 4: + connection = telethon.network.connection.ConnectionTcpMTProxyRandomizedIntermediate + proxy = (proxy[1], proxy[2], proxy[5]) self.client = MautrixTelegramClient( session=self.session, @@ -135,8 +143,8 @@ class AbstractUser(ABC): retry_delay=config["telegram.connection.retry_delay"], flood_sleep_threshold=config["telegram.connection.flood_sleep_threshold"], request_retries=config["telegram.connection.request_retries"], - - proxy=self._proxy_settings, + connection=connection, + proxy=proxy, loop=self.loop, base_logger=base_logger From e40cd9f6a2aa4e288fa6c255c69abf203f6a60ed Mon Sep 17 00:00:00 2001 From: Randall Lawrence Date: Fri, 19 Jul 2019 14:12:56 +0300 Subject: [PATCH 2/4] Changed connection import --- mautrix_telegram/abstract_user.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mautrix_telegram/abstract_user.py b/mautrix_telegram/abstract_user.py index dd3112f4..86340a98 100644 --- a/mautrix_telegram/abstract_user.py +++ b/mautrix_telegram/abstract_user.py @@ -21,6 +21,7 @@ import logging import platform import time +from telethon.network import ConnectionTcpMTProxyRandomizedIntermediate, ConnectionTcpFull from telethon.tl.patched import MessageService, Message from telethon.tl.types import ( Channel, ChannelForbidden, Chat, ChatForbidden, MessageActionChannelMigrateFrom, PeerUser, @@ -121,11 +122,10 @@ class AbstractUser(ABC): device = config["telegram.device_info.device_model"] sysversion = config["telegram.device_info.system_version"] appversion = config["telegram.device_info.app_version"] - import telethon - connection = telethon.network.connection.ConnectionTcpFull + connection = ConnectionTcpFull proxy = self._proxy_settings if proxy is not None and proxy[0] == 4: - connection = telethon.network.connection.ConnectionTcpMTProxyRandomizedIntermediate + connection = ConnectionTcpMTProxyRandomizedIntermediate proxy = (proxy[1], proxy[2], proxy[5]) self.client = MautrixTelegramClient( From e3a457f84cf45b266d6838880e932ebbce7f14b4 Mon Sep 17 00:00:00 2001 From: Lawrence <41361221+Eramde@users.noreply.github.com> Date: Fri, 19 Jul 2019 14:41:05 +0300 Subject: [PATCH 3/4] Amend: Changed connection import --- mautrix_telegram/abstract_user.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mautrix_telegram/abstract_user.py b/mautrix_telegram/abstract_user.py index 86340a98..1a4c26dc 100644 --- a/mautrix_telegram/abstract_user.py +++ b/mautrix_telegram/abstract_user.py @@ -21,7 +21,8 @@ import logging import platform import time -from telethon.network import ConnectionTcpMTProxyRandomizedIntermediate, ConnectionTcpFull +from telethon.network import ( + ConnectionTcpMTProxyRandomizedIntermediate, ConnectionTcpFull) from telethon.tl.patched import MessageService, Message from telethon.tl.types import ( Channel, ChannelForbidden, Chat, ChatForbidden, MessageActionChannelMigrateFrom, PeerUser, From 01a58ad2ed4d69801b0d7e1ace0998d2c10a1b00 Mon Sep 17 00:00:00 2001 From: Lawrence <41361221+Eramde@users.noreply.github.com> Date: Tue, 6 Aug 2019 14:33:50 +0300 Subject: [PATCH 4/4] Minor change changed _proxy_settings return value(s) --- mautrix_telegram/abstract_user.py | 34 +++++++++++++++---------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/mautrix_telegram/abstract_user.py b/mautrix_telegram/abstract_user.py index 1a4c26dc..2cec021d 100644 --- a/mautrix_telegram/abstract_user.py +++ b/mautrix_telegram/abstract_user.py @@ -14,7 +14,7 @@ # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -from typing import Tuple, Optional, List, Union, Dict, TYPE_CHECKING +from typing import Tuple, Optional, List, Union, Dict, Type, TYPE_CHECKING from abc import ABC, abstractmethod import asyncio import logging @@ -22,7 +22,7 @@ import platform import time from telethon.network import ( - ConnectionTcpMTProxyRandomizedIntermediate, ConnectionTcpFull) + ConnectionTcpMTProxyRandomizedIntermediate, ConnectionTcpFull, Connection) from telethon.tl.patched import MessageService, Message from telethon.tl.types import ( Channel, ChannelForbidden, Chat, ChatForbidden, MessageActionChannelMigrateFrom, PeerUser, @@ -88,23 +88,27 @@ class AbstractUser(ABC): return self.client and self.client.is_connected() @property - def _proxy_settings(self) -> Optional[Tuple[int, str, str, str, str, str]]: + def _proxy_settings(self) -> Tuple[Type[Connection], Optional[tuple]]: proxy_type = config["telegram.proxy.type"].lower() + connection = ConnectionTcpFull + connection_data = (config["telegram.proxy.address"], + config["telegram.proxy.port"], + config["telegram.proxy.rdns"], + config["telegram.proxy.username"], + config["telegram.proxy.password"]) if proxy_type == "disabled": - return None + connection_data = None elif proxy_type == "socks4": - proxy_type = 1 + connection_data = (1,) + connection_data elif proxy_type == "socks5": - proxy_type = 2 + connection_data = (2,) + connection_data elif proxy_type == "http": - proxy_type = 3 + connection_data = (3,) + connection_data elif proxy_type == "mtproxy": - proxy_type = 4 + connection = ConnectionTcpMTProxyRandomizedIntermediate + connection_data = (connection_data[0], connection_data[1], connection_data[4]) - return (proxy_type, - config["telegram.proxy.address"], config["telegram.proxy.port"], - config["telegram.proxy.rdns"], - config["telegram.proxy.username"], config["telegram.proxy.password"]) + return connection, connection_data def _init_client(self) -> None: self.log.debug(f"Initializing client for {self.name}") @@ -123,11 +127,7 @@ class AbstractUser(ABC): device = config["telegram.device_info.device_model"] sysversion = config["telegram.device_info.system_version"] appversion = config["telegram.device_info.app_version"] - connection = ConnectionTcpFull - proxy = self._proxy_settings - if proxy is not None and proxy[0] == 4: - connection = ConnectionTcpMTProxyRandomizedIntermediate - proxy = (proxy[1], proxy[2], proxy[5]) + connection, proxy = self._proxy_settings self.client = MautrixTelegramClient( session=self.session,