From 7b9f8b2e002528f1d9e0646e09486aa542609d9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Serneels?= Date: Sat, 13 Apr 2019 15:56:49 +0200 Subject: [PATCH] create socks4_resolve() moves socks4 resolution out of socket_connect block --- proxywatchd.py | 54 ++++++++++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/proxywatchd.py b/proxywatchd.py index e8ebc0c..3d9ea85 100644 --- a/proxywatchd.py +++ b/proxywatchd.py @@ -19,6 +19,29 @@ def try_div(a, b): if b != 0: return a/float(b) return 0 +def socks4_resolve(srvname, server_port): + srv = srvname + if srv in cached_dns: + srv = cached_dns[srvname] + if config.watchd.debug: + _log("using cached ip (%s) for %s (proxy: %s)"%(srv, srvname, self.proxy), "debug") + else: + dns_fail = False + try: + af, sa = rocksock.resolve(rocksock.RocksockHostinfo(srvname, server_port), want_v4=True) + if sa is not None: + cached_dns[srvname] = sa[0] + srv = sa[0] + else: dns_fail = True + except rocksock.RocksockException as e: + assert(e.get_errortype() == rocksock.RS_ET_GAI) + dns_fail = True + if dns_fail: + fail_inc = 0 + _log("could not resolve connection target %s"%srvname, "ERROR") + return False + return srv + class WorkerJob(): def __init__(self, proxy, proto, failcount, success_count, total_duration): @@ -39,30 +62,13 @@ class WorkerJob(): for proto in protos: torhost = random.choice(config.torhosts) - srv = srvname - if proto == 'socks4': - # socks4 (without 4a) requires a raw ip address - # rocksock automatically resolves if needed, but it's more - # efficient to cache the result. - if srv in cached_dns: - srv = cached_dns[srvname] - if config.watchd.debug: - _log("using cached ip (%s) for %s (proxy: %s)"%(srv, srvname, self.proxy), "debug") - else: - dns_fail = False - try: - af, sa = rocksock.resolve(rocksock.RocksockHostinfo(srvname, server_port), want_v4=True) - if sa is not None: - cached_dns[srvname] = sa[0] - srv = sa[0] - else: dns_fail = True - except rocksock.RocksockException as e: - assert(e.get_errortype() == rocksock.RS_ET_GAI) - dns_fail = True - if dns_fail: - fail_inc = 0 - _log("could not resolve connection target %s"%srvname, "ERROR") - break + # socks4 (without 4a) requires a raw ip address + # rocksock automatically resolves if needed, but it's more + # efficient to cache the result. + if proto == 'socks4': srv = socks4_resolve(srvname, server_port) + else: srv = srvname + ## skip socks4 failed resolution + if not srv: continue duration = time.time() proxies = [