diff options
Diffstat (limited to 'contrib/tor-resolve.py')
-rwxr-xr-x | contrib/tor-resolve.py | 75 |
1 files changed, 24 insertions, 51 deletions
diff --git a/contrib/tor-resolve.py b/contrib/tor-resolve.py index 919bc876cc..dd44255bc1 100755 --- a/contrib/tor-resolve.py +++ b/contrib/tor-resolve.py @@ -32,16 +32,15 @@ def socks5Hello(): def socks5ParseHello(response): if response != "\x05\x00": raise ValueError("Bizarre socks5 response") -def socks5ResolveRequest(hostname, atype=0x03, command=0xF0): +def socks5ResolveRequest(hostname): version = 5 + command = 0xF0 rsv = 0 port = 0 - reqheader = struct.pack("!BBBB",version, command, rsv, atype) - if atype == 0x03: - reqheader += struct.pack("!B", len(hostname)) + atype = 0x03 + reqheader = struct.pack("!BBBBB",version, command, rsv, atype, len(hostname)) portstr = struct.pack("!H",port) return "%s%s%s"%(reqheader,hostname,portstr) - def socks5ParseResponse(r): if len(r)<8: return None @@ -50,30 +49,18 @@ def socks5ParseResponse(r): assert rsv==0 if reply != 0x00: return "ERROR",reply - assert atype in (0x01,0x03,0x04) - if atype != 0x03: - expected_len = 4 + ({1:4,4:16}[atype]) + 2 - if len(r) < expected_len: - return None - elif len(r) > expected_len: - raise ValueError("Overlong socks5 reply!") - addr = r[4:-2] - if atype == 0x01: - return "%d.%d.%d.%d"%tuple(map(ord,addr)) - else: - # not really the right way to format IPv6 - return "IPv6: %s"%(":".join([hex(ord(c)) for c in addr])) + assert atype in (0x01,0x04) + expected_len = 4 + ({1:4,4:16}[atype]) + 2 + if len(r) < expected_len: + return None + elif len(r) > expected_len: + raise ValueError("Overlong socks5 reply!") + addr = r[4:-2] + if atype == 0x01: + return "%d.%d.%d.%d"%tuple(map(ord,addr)) else: - hlen, = struct.unpack("!B", r[4]) - expected_len = 5 + hlen + 2 - if len(r) < expected_len: - return None - return r[5:-2] - -def socks5ResolvePTRRequest(hostname): - return socks5ResolveRequest(socket.inet_aton(hostname), - atype=1, command = 0xF1) - + # not really the right way to format IPv6 + return "IPv6: %s"%(":".join([hex(ord(c)) for c in addr])) def parseHostAndPort(h): host, port = "localhost", 9050 @@ -93,23 +80,20 @@ def parseHostAndPort(h): return host, port -def resolve(hostname, sockshost, socksport, socksver=4, reverse=0): +def resolve(hostname, sockshost, socksport, socksver=4): assert socksver in (4,5) if socksver == 4: fmt = socks4AResolveRequest parse = socks4AParseResponse - elif not reverse: - fmt = socks5ResolveRequest - parse = socks5ParseResponse else: - fmt = socks5ResolvePTRRequest + fmt = socks5ResolveRequest parse = socks5ParseResponse - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((sockshost,socksport)) if socksver == 5: s.send(socks5Hello()) socks5ParseHello(s.recv(2)) + print len(fmt(hostname)), len(hostname) s.send(fmt(hostname)) answer = s.recv(6) result = parse(answer) @@ -130,25 +114,14 @@ if __name__ == '__main__': print "Syntax: resolve.py [-4|-5] hostname [sockshost:socksport]" sys.exit(0) socksver = 4 - reverse = 0 - while sys.argv[1][0] == '-': - if sys.argv[1] in ("-4", "-5"): - socksver = int(sys.argv[1][1]) - del sys.argv[1] - elif sys.argv[1] == '-x': - reverse = 1 - del sys.argv[1] - elif sys.argv[1] == '--': - break - - if len(sys.argv) >= 4: - print "Syntax: resolve.py [-x] [-4|-5] hostname [sockshost:socksport]" + if sys.argv[1] in ("-4", "-5"): + socksver = int(sys.argv[1][1]) + del sys.argv[1] + if len(sys.argv) == 4: + print "Syntax: resolve.py [-4|-5] hostname [sockshost:socksport]" sys.exit(0) if len(sys.argv) == 3: sh,sp = parseHostAndPort(sys.argv[2]) else: sh,sp = parseHostAndPort("") - - if reverse and socksver == 4: - socksver = 5 - resolve(sys.argv[1], sh, sp, socksver, reverse) + resolve(sys.argv[1], sh, sp, socksver) |