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

[tor-commits] [bridgedb/master] Rewrite/refactor descriptor generator code



commit 686c3c026e292623ca1f528ecec2d7a70505e7d8
Author: Matthew Finkel <Matthew.Finkel@xxxxxxxxx>
Date:   Fri Aug 16 02:16:41 2013 +0000

    Rewrite/refactor descriptor generator code
    
    It should be better this way.
---
 generatedescriptors.py |  131 ++++++++++++++++++++++++++++--------------------
 1 file changed, 77 insertions(+), 54 deletions(-)

diff --git a/generatedescriptors.py b/generatedescriptors.py
index d507401..6f64ca0 100644
--- a/generatedescriptors.py
+++ b/generatedescriptors.py
@@ -8,6 +8,10 @@ from datetime import datetime
 import binascii
 
 
+def usage():
+    print "syntax: generatedescriptors.py <count>\n"\
+          "    count: number of descriptors to generate\n"
+
 def randomIP():
     return randomIP4()
 
@@ -18,8 +22,7 @@ def randomPort():
     return random.randint(1,65535)
 
 def gettimestamp():
-    ts = time.strftime("%Y-%m-%d %H:%M:%S")
-    return "opt published %s\n" % ts
+    return time.strftime("%Y-%m-%d %H:%M:%S")
 
 def getHexString(size):
     s = ""
@@ -27,58 +30,78 @@ def getHexString(size):
         s+= random.choice("ABCDEF0123456789") 
     return s
 
+def generateDesc():
 
-baseDesc = "router Unnamed %s %s 0 9030\n"\
-"opt fingerprint %s\n"\
-"opt @purpose bridge\n"
-
-baseStatus = "r %s %s %s %s %s %d %d\n"\
-"s Running Stable\n"
-
-baseExtraInfo = "extra-info %s %s\n"\
-"transport %s %s:%d\n"
-
-ei = ""
-df = ""
-sf = ""
-for i in xrange(500):
+    baseDesc = "router Unnamed %s %s 0 %s\n"\
+               "opt fingerprint %s\n"\
+               "opt @purpose bridge\n"\
+               "opt published %s\n"\
+               "router-signature\n"
     fp = "DEAD BEEF F00F DEAD BEEF F00F " + \
-        getHexString(4) + " " + getHexString(4) + " " + \
-        getHexString(4) + " " + getHexString(4)
+         getHexString(4) + " " + getHexString(4) + " " + \
+         getHexString(4) + " " + getHexString(4)
+    ip = randomIP()
+    orport = randomPort()
+    dirport = randomPort()
     ID = binascii.a2b_hex(fp.replace(" ", ""))
-
-    sf += "".join(baseStatus % ("namedontmattah", binascii.b2a_base64(ID)[:-2],
-            "randomstring", time.strftime("%Y-%m-%d %H:%M:%S"), randomIP(),
-             randomPort(), randomPort()))
-
-    df += "".join(baseDesc % (randomIP(), randomPort(), fp))
-    df += gettimestamp()
-    df += "router-signature\n"
-
-    if i % 4 == 0:
-        ei += "".join(baseExtraInfo % ("namedontmattah", fp.replace(" ", ""),
-                                       random.choice(["obfs2", "obfs3", "obfs2"]),
-                                       randomIP(), randomPort()))
-
-ei += "router-signature\n"
-
-try:
-    f = open("networkstatus-bridges", 'w')
-    f.write(sf)
-    f.close()
-except:
-    print "Failed to open status file"
-
-try:
-    f = open("bridge-descriptors", 'w')
-    f.write(df)
-    f.close()
-except:
-    print "Failed to open df file"
-
-try:
-    f = open("extra-infos", 'w')
-    f.write(ei)
-    f.close()
-except:
-    print "Failed to open ei file"
+    df =  baseDesc % (ip, orport, dirport, fp, gettimestamp())
+    return (df, (ID, ip, orport, dirport))
+
+def generateStatus(info, ID=None, ip=None, orport=None, dirport=None):
+    baseStatus = "r %s %s %s %s %s %d %d\n"\
+                 "s Running Stable\n"
+
+    if info and len(info) == 4:
+        ID = info[0]
+        ip = info[1]
+        orport = info[2]
+        dirport = info[3]
+    return "".join(baseStatus % ("namedontmattah", binascii.b2a_base64(ID)[:-2],
+           "randomstring", gettimestamp(), ip,
+            orport, dirport))
+
+def generateExtraInfo(fp, ip=None):
+    baseExtraInfo = "extra-info %s %s\n"\
+                    "transport %s %s:%d\n"\
+                    "router-signature\n"
+    if not ip:
+        ip = randomIP()
+    return "".join(baseExtraInfo % ("namedontmattah", fp,
+                                    random.choice(["obfs2", "obfs3", "obfs2"]),
+                                    ip, randomPort()))
+if __name__ == "__main__":
+    if len(sys.argv) != 2:
+        usage()
+        sys.exit(0)
+
+    df = ''
+    sf = ''
+    ei = ''
+    count = int(sys.argv[1])
+    for i in xrange(count):
+        desc, info = generateDesc()
+        df += desc
+
+        sf += generateStatus(info)
+	ei += generateExtraInfo(binascii.b2a_hex(info[0]))
+        
+    try:
+        f = open("networkstatus-bridges", 'w')
+        f.write(sf)
+        f.close()
+    except:
+        print "Failed to open or write to status file"
+
+    try:
+        f = open("bridge-descriptors", 'w')
+        f.write(df)
+        f.close()
+    except:
+        print "Failed to open or write to descriptor file"
+
+    try:
+        f = open("extra-infos", 'w')
+        f.write(ei)
+        f.close()
+    except:
+        print "Failed to open or write to extra-info file"



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits