create socks4_resolve()
moves socks4 resolution out of socket_connect block
This commit is contained in:
@@ -19,6 +19,29 @@ def try_div(a, b):
|
|||||||
if b != 0: return a/float(b)
|
if b != 0: return a/float(b)
|
||||||
return 0
|
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():
|
class WorkerJob():
|
||||||
def __init__(self, proxy, proto, failcount, success_count, total_duration):
|
def __init__(self, proxy, proto, failcount, success_count, total_duration):
|
||||||
@@ -39,30 +62,13 @@ class WorkerJob():
|
|||||||
|
|
||||||
for proto in protos:
|
for proto in protos:
|
||||||
torhost = random.choice(config.torhosts)
|
torhost = random.choice(config.torhosts)
|
||||||
srv = srvname
|
# socks4 (without 4a) requires a raw ip address
|
||||||
if proto == 'socks4':
|
# rocksock automatically resolves if needed, but it's more
|
||||||
# socks4 (without 4a) requires a raw ip address
|
# efficient to cache the result.
|
||||||
# rocksock automatically resolves if needed, but it's more
|
if proto == 'socks4': srv = socks4_resolve(srvname, server_port)
|
||||||
# efficient to cache the result.
|
else: srv = srvname
|
||||||
if srv in cached_dns:
|
## skip socks4 failed resolution
|
||||||
srv = cached_dns[srvname]
|
if not srv: continue
|
||||||
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
|
|
||||||
|
|
||||||
duration = time.time()
|
duration = time.time()
|
||||||
proxies = [
|
proxies = [
|
||||||
|
|||||||
Reference in New Issue
Block a user