create socks4_resolve()

moves socks4 resolution out of socket_connect block
This commit is contained in:
Mickaël Serneels
2019-04-13 15:56:49 +02:00
parent bad4d25bcf
commit 7b9f8b2e00

View File

@@ -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 = [