[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] Fix Python resolve script so we have an easy way to test SO...



Update of /home/or/cvsroot/tor/contrib
In directory moria.mit.edu:/tmp/cvs-serv9109/contrib

Modified Files:
	tor-resolve.py 
Log Message:
Fix Python resolve script so we have an easy way to test SOCKS5 resolves.

Index: tor-resolve.py
===================================================================
RCS file: /home/or/cvsroot/tor/contrib/tor-resolve.py,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- tor-resolve.py	9 Nov 2004 09:27:34 -0000	1.2
+++ tor-resolve.py	24 Mar 2005 01:13:27 -0000	1.3
@@ -17,7 +17,6 @@
 def socks4AParseResponse(response):
     RESPONSE_LEN = 8
     if len(response) < RESPONSE_LEN:
-        print "return none", len(response)
         return None
     assert len(response) >= RESPONSE_LEN
     version,status,port = struct.unpack("!BBH",response[:4])
@@ -39,16 +38,17 @@
     rsv = 0
     port = 0
     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)
-    return "%s%s\0%s"%(reqheader,hostname,port)
+    return "%s%s%s"%(reqheader,hostname,portstr)
 def socks5ParseResponse(r):
-    if len(r)<8: return None
+    if len(r)<8:
+        return None
     version, reply, rsv, atype = struct.unpack("!BBBB",r[:4])
     assert version==5
     assert rsv==0
     if reply != 0x00:
-        return "ERROR"
+        return "ERROR",reply
     assert atype in (0x01,0x04)
     expected_len = 4 + ({1:4,4:16}[atype]) + 2
     if len(r) < expected_len:
@@ -92,27 +92,28 @@
     s.connect((sockshost,socksport))
     if socksver == 5:
         s.send(socks5Hello())
-        socksParseHello(s.recv(2))
+        socks5ParseHello(s.recv(2))
+    print len(fmt(hostname)), len(hostname)
     s.send(fmt(hostname))
-    answer = s.recv(8)
+    answer = s.recv(6)
     result = parse(answer)
     while result is None:
         more = s.recv(1)
         if not more:
-            print "Connection closed; dying."
             return None
         answer += more
         result = parse(answer)
     print "Got answer",result
     m = s.recv(1)
     if m:
-        print "Got extra data too! Ick."
+        print "Got extra data too: %r"%m
     return result
 
 if __name__ == '__main__':
     if len(sys.argv) not in (2,3,4):
         print "Syntax: resolve.py [-4|-5] hostname [sockshost:socksport]"
         sys.exit(0)
+    socksver = 4
     if sys.argv[1] in ("-4", "-5"):
         socksver = int(sys.argv[1][1])
         del sys.argv[1]
@@ -123,4 +124,4 @@
         sh,sp = parseHostAndPort(sys.argv[2])
     else:
         sh,sp = parseHostAndPort("")
-    resolve(sys.argv[1], sh, sp)
+    resolve(sys.argv[1], sh, sp, socksver)