Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to connect via MTProxy. #344

Merged
merged 4 commits into from
Aug 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 21 additions & 12 deletions mautrix_telegram/abstract_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
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
import platform
import time

from telethon.network import (
ConnectionTcpMTProxyRandomizedIntermediate, ConnectionTcpFull, Connection)
from telethon.tl.patched import MessageService, Message
from telethon.tl.types import (
Channel, ChannelForbidden, Chat, ChatForbidden, MessageActionChannelMigrateFrom, PeerUser,
Expand Down Expand Up @@ -86,21 +88,27 @@ def connected(self) -> bool:
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":
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}")
Expand All @@ -119,6 +127,7 @@ def _init_client(self) -> None:
device = config["telegram.device_info.device_model"]
sysversion = config["telegram.device_info.system_version"]
appversion = config["telegram.device_info.app_version"]
connection, proxy = self._proxy_settings

self.client = MautrixTelegramClient(
session=self.session,
Expand All @@ -135,8 +144,8 @@ def _init_client(self) -> None:
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
Expand Down