refactor upstream proxy handling

This commit is contained in:
Alexander Bersenev
2019-09-20 16:06:59 +05:00
parent 79eaabdd23
commit f2fbaa923b

View File

@@ -154,12 +154,6 @@ def init_config():
conf_dict.setdefault("SOCKS5_HOST", None) conf_dict.setdefault("SOCKS5_HOST", None)
conf_dict.setdefault("SOCKS5_PORT", None) conf_dict.setdefault("SOCKS5_PORT", None)
# apply socks settings in place
if conf_dict.setdefault("SOCKS5_HOST") and conf_dict.setdefault("SOCKS5_PORT"):
import socks
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, config.SOCKS5_HOST, config.SOCKS5_PORT)
socket.socket = socks.socksocket
# 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", {})
@@ -235,6 +229,19 @@ def init_config():
config = type("config", (dict,), conf_dict)(conf_dict) config = type("config", (dict,), conf_dict)(conf_dict)
def apply_upstream_proxy_settings():
# apply socks settings in place
if config.SOCKS5_HOST and config.SOCKS5_PORT:
import socks
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, config.SOCKS5_HOST, config.SOCKS5_PORT)
if not hasattr(socket, "origsocket"):
socket.origsocket = socket.socket
socket.socket = socks.socksocket
elif hasattr(socket, "origsocket"):
socket.socket = socket.origsocket
del socket.origsocket
def try_use_cryptography_module(): def try_use_cryptography_module():
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend from cryptography.hazmat.backends import default_backend
@@ -1981,6 +1988,7 @@ def setup_signals():
if hasattr(signal, 'SIGUSR2'): if hasattr(signal, 'SIGUSR2'):
def reload_signal(signum, frame): def reload_signal(signum, frame):
init_config() init_config()
apply_upstream_proxy_settings()
print("Config reloaded", flush=True, file=sys.stderr) print("Config reloaded", flush=True, file=sys.stderr)
print_tg_info() print_tg_info()
@@ -2115,6 +2123,7 @@ def main():
if __name__ == "__main__": if __name__ == "__main__":
init_config() init_config()
apply_upstream_proxy_settings()
init_ip_info() init_ip_info()
print_tg_info() print_tg_info()
main() main()