mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-12-02 16:43:32 +01:00
Fix Python resolve script so we have an easy way to test SOCKS5 resolves.
svn:r3852
This commit is contained in:
parent
7521f2d58a
commit
ba578f0ed6
@ -17,7 +17,6 @@ def socks4AResolveRequest(hostname):
|
|||||||
def socks4AParseResponse(response):
|
def socks4AParseResponse(response):
|
||||||
RESPONSE_LEN = 8
|
RESPONSE_LEN = 8
|
||||||
if len(response) < RESPONSE_LEN:
|
if len(response) < RESPONSE_LEN:
|
||||||
print "return none", len(response)
|
|
||||||
return None
|
return None
|
||||||
assert len(response) >= RESPONSE_LEN
|
assert len(response) >= RESPONSE_LEN
|
||||||
version,status,port = struct.unpack("!BBH",response[:4])
|
version,status,port = struct.unpack("!BBH",response[:4])
|
||||||
@ -39,16 +38,17 @@ def socks5ResolveRequest(hostname):
|
|||||||
rsv = 0
|
rsv = 0
|
||||||
port = 0
|
port = 0
|
||||||
atype = 0x03
|
atype = 0x03
|
||||||
reqheader = struct.pack("!BBBB",version, command, rsv, atype)
|
reqheader = struct.pack("!BBBBB",version, command, rsv, atype, len(hostname))
|
||||||
portstr = struct.pack("!H",port)
|
portstr = struct.pack("!H",port)
|
||||||
return "%s%s\0%s"%(reqheader,hostname,port)
|
return "%s%s%s"%(reqheader,hostname,portstr)
|
||||||
def socks5ParseResponse(r):
|
def socks5ParseResponse(r):
|
||||||
if len(r)<8: return None
|
if len(r)<8:
|
||||||
|
return None
|
||||||
version, reply, rsv, atype = struct.unpack("!BBBB",r[:4])
|
version, reply, rsv, atype = struct.unpack("!BBBB",r[:4])
|
||||||
assert version==5
|
assert version==5
|
||||||
assert rsv==0
|
assert rsv==0
|
||||||
if reply != 0x00:
|
if reply != 0x00:
|
||||||
return "ERROR"
|
return "ERROR",reply
|
||||||
assert atype in (0x01,0x04)
|
assert atype in (0x01,0x04)
|
||||||
expected_len = 4 + ({1:4,4:16}[atype]) + 2
|
expected_len = 4 + ({1:4,4:16}[atype]) + 2
|
||||||
if len(r) < expected_len:
|
if len(r) < expected_len:
|
||||||
@ -92,27 +92,28 @@ def resolve(hostname, sockshost, socksport, socksver=4):
|
|||||||
s.connect((sockshost,socksport))
|
s.connect((sockshost,socksport))
|
||||||
if socksver == 5:
|
if socksver == 5:
|
||||||
s.send(socks5Hello())
|
s.send(socks5Hello())
|
||||||
socksParseHello(s.recv(2))
|
socks5ParseHello(s.recv(2))
|
||||||
|
print len(fmt(hostname)), len(hostname)
|
||||||
s.send(fmt(hostname))
|
s.send(fmt(hostname))
|
||||||
answer = s.recv(8)
|
answer = s.recv(6)
|
||||||
result = parse(answer)
|
result = parse(answer)
|
||||||
while result is None:
|
while result is None:
|
||||||
more = s.recv(1)
|
more = s.recv(1)
|
||||||
if not more:
|
if not more:
|
||||||
print "Connection closed; dying."
|
|
||||||
return None
|
return None
|
||||||
answer += more
|
answer += more
|
||||||
result = parse(answer)
|
result = parse(answer)
|
||||||
print "Got answer",result
|
print "Got answer",result
|
||||||
m = s.recv(1)
|
m = s.recv(1)
|
||||||
if m:
|
if m:
|
||||||
print "Got extra data too! Ick."
|
print "Got extra data too: %r"%m
|
||||||
return result
|
return result
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
if len(sys.argv) not in (2,3,4):
|
if len(sys.argv) not in (2,3,4):
|
||||||
print "Syntax: resolve.py [-4|-5] hostname [sockshost:socksport]"
|
print "Syntax: resolve.py [-4|-5] hostname [sockshost:socksport]"
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
socksver = 4
|
||||||
if sys.argv[1] in ("-4", "-5"):
|
if sys.argv[1] in ("-4", "-5"):
|
||||||
socksver = int(sys.argv[1][1])
|
socksver = int(sys.argv[1][1])
|
||||||
del sys.argv[1]
|
del sys.argv[1]
|
||||||
@ -123,4 +124,4 @@ if __name__ == '__main__':
|
|||||||
sh,sp = parseHostAndPort(sys.argv[2])
|
sh,sp = parseHostAndPort(sys.argv[2])
|
||||||
else:
|
else:
|
||||||
sh,sp = parseHostAndPort("")
|
sh,sp = parseHostAndPort("")
|
||||||
resolve(sys.argv[1], sh, sp)
|
resolve(sys.argv[1], sh, sp, socksver)
|
||||||
|
Loading…
Reference in New Issue
Block a user