[or-cvs] [torflow/master 53/92] Made a snakeinspector config class to hold arguments

mikeperry at torproject.org mikeperry at torproject.org
Sat Aug 21 05:14:00 UTC 2010


Author: John M. Schanck <john at anomos.info>
Date: Sun, 1 Aug 2010 12:08:47 -0400
Subject: Made a snakeinspector config class to hold arguments
Commit: 535f009437009ed2fd31563cdc8ffc48a7fad12e

---
 NetworkScanners/ExitAuthority/snakeinspector.py |  176 ++++++++++++-----------
 1 files changed, 89 insertions(+), 87 deletions(-)

diff --git a/NetworkScanners/ExitAuthority/snakeinspector.py b/NetworkScanners/ExitAuthority/snakeinspector.py
index 8717938..d3fd676 100755
--- a/NetworkScanners/ExitAuthority/snakeinspector.py
+++ b/NetworkScanners/ExitAuthority/snakeinspector.py
@@ -27,7 +27,6 @@ if TorCtl.TorUtil.loglevels[TorCtl.TorUtil.loglevel] > TorCtl.TorUtil.loglevels[
   # Kill stderr (jsdiffer and exception noise) if our loglevel is above INFO
   sys.stderr = file("/dev/null", "w")
 
-
 def usage(argv):
   print "Usage: "+argv[0]+" with 0 or more of the following filters: "
   print "  --dir <datadir>"
@@ -45,68 +44,74 @@ def usage(argv):
   print "  --verbose"
   sys.exit(1)
 
-def getargs(argv):
-  try:
-    opts,args = getopt.getopt(argv[1:],"d:f:e:x:r:vt:p:s:o:n:a:b:Fmc",
-             ["dir=", "file=", "exit=", "reason=", "resultfilter=", "proto=",
-              "verbose", "statuscode=", "sortby=", "noreason=", "after=",
-              "before=", "falsepositives", "email", "confirmed"])
-  except getopt.GetoptError,err:
-    print str(err)
-    usage(argv)
-  # FIXME: make all these repeatable
-  use_dir="./data/"
-  use_file=None
-  node=None
-  reasons=[]
-  noreasons=[]
-  result=2
-  verbose=1
-  proto=None
-  resultfilter=None
-  before = 0xffffffff
-  after = 0
-  sortby="proto"
-  falsepositives=False
-  send_email = False
-  confirmed = False
-  for o,a in opts:
-    if o == '-d' or o == '--dir':
-      use_dir = a
-    elif o == '-e' or o == '--email':
-      send_email = True
-    elif o == '-x' or o == '--exit':
-      node = a
-    elif o == '-f' or o == '--file':
-      use_file = a
-    elif o == '-b' or o == '--before':
-      before = time.mktime(time.strptime(a))
-    elif o == '-a' or o == '--after': 
-      after = time.mktime(time.strptime(a))
-    elif o == '-r' or o == '--reason': 
-      reasons.append(a)
-    elif o == '-r' or o == '--noreason': 
-      noreasons.append(a)
-    elif o == '-v' or o == '--verbose': 
-      verbose += 1
-    elif o == '-t' or o == '--resultfilter':
-      resultfilter = a
-    elif o == '-p' or o == '--proto':
-      proto = a
-    elif o == '-F' or o == '--falsepositives':
-      falsepositives = True
-    elif o == '-c' or o == '--confirmed':
-      confirmed = True
-    elif o == '-s' or o == '--sortby': 
-      if a not in ["proto", "site", "exit", "reason"]:
-        usage(argv)
-      else: sortby = a 
-    elif o == '-s' or o == '--statuscode': 
-      try:
-        result = int(a)
-      except ValueError:
-        result = RESULT_CODES[a]
-  return use_dir,use_file,node,reasons,noreasons,result,verbose,resultfilter,proto,sortby,before,after,falsepositives,send_email,confirmed
+class SIConf(object):
+  def __init__(self, argv=None):
+    # FIXME: make all these repeatable
+    self.use_dir="./data/"
+    self.use_file=None
+    self.node=None
+    self.reasons=[]
+    self.noreasons=[]
+    self.statuscode=2
+    self.verbose=1
+    self.proto=None
+    self.resultfilter=None
+    self.before = 0xffffffff
+    self.after = 0
+    self.sortby="proto"
+    self.falsepositives=False
+    self.send_email = False
+    self.confirmed = False
+    if argv:
+      self.getargs(argv)
+
+  def getargs(self, argv):
+    try:
+      opts,args = getopt.getopt(argv[1:],"d:f:x:r:n:a:b:t:p:o:s:Fmcv",
+               ["dir=", "file=", "exit=", "reason=", "resultfilter=", "proto=",
+                "verbose", "statuscode=", "sortby=",
+                "noreason=", "after=", "before=", "falsepositives", "email",
+                "confirmed"])
+    except getopt.GetoptError,err:
+      print str(err)
+      usage(argv)
+    for o,a in opts:
+      if o == '-d' or o == '--dir':
+        self.use_dir = a
+      elif o == '-f' or o == '--file':
+        self.use_file = a
+      elif o == '-x' or o == '--exit':
+        self.node = a
+      elif o == '-r' or o == '--reason':
+        self.reasons.append(a)
+      elif o == '-n' or o == '--noreason':
+        self.noreasons.append(a)
+      elif o == '-a' or o == '--after':
+        self.after = time.mktime(time.strptime(a))
+      elif o == '-b' or o == '--before':
+        self.before = time.mktime(time.strptime(a))
+      elif o == '-t' or o == '--resultfilter':
+        self.resultfilter = a
+      elif o == '-p' or o == '--proto':
+        self.proto = a
+      elif o == '-F' or o == '--falsepositives':
+        self.falsepositives = True
+      elif o == '-m' or o == '--email':
+        self.send_email = True
+      elif o == '-c' or o == '--confirmed':
+        self.confirmed = True
+      elif o == '-v' or o == '--verbose':
+        self.verbose += 1
+      elif o == '-o' or o == '--sortby':
+        if a not in ["proto", "site", "exit", "reason"]:
+          usage(argv)
+        else:
+          sortby = a
+      elif o == '-s' or o == '--statuscode':
+        try:
+          self.statuscode = int(a)
+        except ValueError:
+          self.statuscode = RESULT_CODES[a]
 
 def send_mail(fro, to, subject, text, server, files=[]):
   assert type(to)==list
@@ -132,43 +137,40 @@ def send_mail(fro, to, subject, text, server, files=[]):
   smtp.sendmail(fro, to, msg.as_string() )
   smtp.close()
 
-
 def main(argv):
   now = time.time()
-  use_dir,use_file,node,reasons,noreasons,result,verbose,resultfilter,proto,sortby,before,after,falsepositives,send_email,confirmed=getargs(argv)
-  dh = DataHandler(use_dir)
+  conf=SIConf(argv)
+  dh = DataHandler(conf.use_dir)
 
-  if use_file:
-    results = [dh.getResult(use_file)]
-  elif node:
-    results = dh.filterByNode(dh.getAll(), node)
+  if conf.use_file:
+    results = [dh.getResult(conf.use_file)]
+  elif conf.node:
+    results = dh.filterByNode(dh.getAll(), conf.node)
   else:
     results = dh.getAll()
 
-  if sortby == "url":
+  if conf.sortby == "url":
     results.sort(lambda x, y: cmp(x.site, y.site))
-  elif sortby == "reason":
+  elif conf.sortby == "reason":
     results.sort(lambda x, y: cmp(x.reason, y.reason))
-  elif sortby == "exit":
+  elif conf.sortby == "exit":
     results.sort(lambda x, y: cmp(x.exit_node, y.exit_node))
 
   by_proto = {}
 
   for r in results:
-    r.verbose = verbose
-    if r.reason in noreasons: continue
-    if reasons and r.reason not in reasons: continue
-    if r.timestamp < after or before < r.timestamp: continue
-    if (falsepositives) ^ r.false_positive: continue
-    if confirmed != r.confirmed: continue
-    if (not result or r.status == result) and \
-       (not proto or r.proto == proto) and \
-       (not resultfilter or r.__class__.__name__ == resultfilter):
-      if send_email:
+    r.verbose = conf.verbose
+    if r.reason in conf.noreasons: continue
+    if conf.reasons and r.reason not in conf.reasons: continue
+    if r.timestamp < conf.after or conf.before < r.timestamp: continue
+    if (conf.falsepositives) ^ r.false_positive: continue
+    if conf.confirmed != r.confirmed: continue
+    if (not conf.statuscode or r.status == conf.statuscode) and \
+       (not conf.proto or r.proto == conf.proto) and \
+       (not conf.resultfilter or r.__class__.__name__ == conf.resultfilter):
+      if conf.send_email:
         if r.timestamp > now - mail_interval - 60:
-          if r.proto not in by_proto:
-            by_proto[r.proto]=[]
-          by_proto[r.proto].append(r)
+          by_proto.setdefault(r.proto, []).append(r)
         continue
       try:
         print r
@@ -180,7 +182,7 @@ def main(argv):
       else:
           print "\n-----------------------------\n"
 
-  if send_email:
+  if conf.send_email:
     for p in by_proto.iterkeys():
       print "Mailing "+str(len(by_proto[p]))+" "+p+" results..."
       subject = p+" scan found "+str(len(by_proto[p]))+" snakes"
-- 
1.7.1




More information about the tor-commits mailing list