make the socks module optional

This commit is contained in:
Alexander Bersenev
2019-09-20 15:38:18 +05:00
parent 516600a32d
commit 23c7b0d53b
2 changed files with 14 additions and 13 deletions

View File

@@ -20,7 +20,3 @@ SECURE_ONLY = True
# Tag for advertising, obtainable from @MTProxybot # Tag for advertising, obtainable from @MTProxybot
# AD_TAG = "3c09c680b76ee91a4c25ad51f742267d" # AD_TAG = "3c09c680b76ee91a4c25ad51f742267d"
# Use upstream SOCKS5 proxy for connections (f.e. Tor)
# SOCKS5_HOST = "localhost"
# SOCKS5_PORT = 9050

View File

@@ -4,7 +4,6 @@ import asyncio
import socket import socket
import urllib.parse import urllib.parse
import urllib.request import urllib.request
import socks
import collections import collections
import time import time
import datetime import datetime
@@ -121,10 +120,6 @@ def init_config():
# load advanced settings # load advanced settings
# use upstream SOCKS5 proxy
conf_dict.setdefault("SOCKS5_HOST", "localhost")
conf_dict.setdefault("SOCKS5_PORT", 0)
# use middle proxy, necessary to show ad # use middle proxy, necessary to show ad
conf_dict.setdefault("USE_MIDDLE_PROXY", len(conf_dict["AD_TAG"]) == 16) conf_dict.setdefault("USE_MIDDLE_PROXY", len(conf_dict["AD_TAG"]) == 16)
@@ -155,6 +150,10 @@ def init_config():
# the next host's port to forward bad clients # the next host's port to forward bad clients
conf_dict.setdefault("MASK_PORT", 443) conf_dict.setdefault("MASK_PORT", 443)
# use upstream SOCKS5 proxy
conf_dict.setdefault("SOCKS5_HOST", None)
conf_dict.setdefault("SOCKS5_PORT", None)
# user tcp connection limits, the mapping from name to the integer limit # user tcp connection limits, the mapping from name to the integer limit
# one client can create many tcp connections, up to 8 # one client can create many tcp connections, up to 8
conf_dict.setdefault("USER_MAX_TCP_CONNS", {}) conf_dict.setdefault("USER_MAX_TCP_CONNS", {})
@@ -1870,14 +1869,16 @@ async def update_middle_proxy_info():
await asyncio.sleep(config.PROXY_INFO_UPDATE_PERIOD) await asyncio.sleep(config.PROXY_INFO_UPDATE_PERIOD)
def init_socks():
import socks
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, config.SOCKS5_HOST, config.SOCKS5_PORT)
socket.socket = socks.socksocket
def init_ip_info(): def init_ip_info():
global my_ip_info global my_ip_info
global disable_middle_proxy global disable_middle_proxy
if not config.SOCKS5_PORT == 0:
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, config.SOCKS5_HOST, config.SOCKS5_PORT)
socket.socket = socks.socksocket
def get_ip_from_url(url): def get_ip_from_url(url):
TIMEOUT = 5 TIMEOUT = 5
try: try:
@@ -2039,6 +2040,8 @@ def main():
init_stats() init_stats()
init_proxy_start_time() init_proxy_start_time()
init_socks()
if sys.platform == "win32": if sys.platform == "win32":
loop = asyncio.ProactorEventLoop() loop = asyncio.ProactorEventLoop()
asyncio.set_event_loop(loop) asyncio.set_event_loop(loop)
@@ -2114,6 +2117,8 @@ def main():
if __name__ == "__main__": if __name__ == "__main__":
init_config() init_config()
if config.SOCKS5_HOST and config.SOCKS5_PORT:
init_socks()
init_ip_info() init_ip_info()
print_tg_info() print_tg_info()
main() main()