From 15fd394d5433c0df4570174ada2c3324b88b46f4 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Thu, 12 Jul 2018 23:08:08 +0300 Subject: [PATCH] Add proxy config. Fixes #153 --- example-config.yaml | 13 +++++++++++++ mautrix_telegram/abstract_user.py | 24 ++++++++++++++++++++++-- mautrix_telegram/config.py | 6 ++++++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/example-config.yaml b/example-config.yaml index 82042e2d..27c64991 100644 --- a/example-config.yaml +++ b/example-config.yaml @@ -190,6 +190,19 @@ telegram: api_hash: tjyd5yge35lbodk1xwzw2jstp90k55qz # (Optional) Create your own bot at https://t.me/BotFather bot_token: disabled + # Telethon proxy configuration. + # You must install PySocks from pip for proxies to work. + proxy: + # Allowed types: disabled, socks4, socks5, http + type: disabled + # Proxy IP address and port. + address: 127.0.0.1 + port: 1080 + # Whether or not to perform DNS resolving remotely. + rdns: true + # Proxy authentication (optional). + username: "" + password: "" # Python logging configuration. # diff --git a/mautrix_telegram/abstract_user.py b/mautrix_telegram/abstract_user.py index 722b4655..dd1fe03b 100644 --- a/mautrix_telegram/abstract_user.py +++ b/mautrix_telegram/abstract_user.py @@ -50,6 +50,23 @@ class AbstractUser: def connected(self): return self.client and self.client.is_connected() + @property + def _proxy_settings(self): + type = config["telegram.proxy.type"].lower() + if type == "disabled": + return None + elif type == "socks4": + type = 1 + elif type == "socks5": + type = 2 + elif type == "http": + type = 3 + + return (type, + config["telegram.proxy.address"], config["telegram.proxy.port"], + config["telegram.proxy.rdns"], + config["telegram.proxy.username"], config["telegram.proxy.password"]) + def _init_client(self): self.log.debug(f"Initializing client for {self.name}") device = f"{platform.system()} {platform.release()}" @@ -62,7 +79,8 @@ class AbstractUser: app_version=__version__, system_version=sysversion, device_model=device, - timeout=120) + timeout=120, + proxy=self._proxy_settings) self.client.add_event_handler(self._update_catch) async def update(self, update): @@ -95,7 +113,9 @@ class AbstractUser: return self.client and await self.client.is_user_authorized() async def has_full_access(self, allow_bot=False): - return self.puppet_whitelisted and (not self.is_bot or allow_bot) and await self.is_logged_in() + return (self.puppet_whitelisted + and (not self.is_bot or allow_bot) + and await self.is_logged_in()) async def start(self, delete_unless_authenticated=False): if not self.client: diff --git a/mautrix_telegram/config.py b/mautrix_telegram/config.py index 8b90eabd..fb2e14a2 100644 --- a/mautrix_telegram/config.py +++ b/mautrix_telegram/config.py @@ -220,6 +220,12 @@ class Config(DictWithRecursion): copy("telegram.api_id") copy("telegram.api_hash") copy("telegram.bot_token") + copy("telegram.proxy.type") + copy("telegram.proxy.address") + copy("telegram.proxy.port") + copy("telegram.proxy.rdns") + copy("telegram.proxy.username") + copy("telegram.proxy.password") if "appservice.debug" in self and "logging" not in self: level = "DEBUG" if self["appservice.debug"] else "INFO"