[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[tor-commits] [ooni-probe/master] Move startTor and startSniffing methods into Director
commit 5f589c687d42b7f24049081df9e0f64967e1c8ff
Author: aagbsn <aagbsn@xxxxxxxx>
Date: Wed Jan 16 20:34:30 2013 +0000
Move startTor and startSniffing methods into Director
Removes unused imports in runner and adds exceptions to errors
---
ooni/director.py | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
ooni/errors.py | 5 ++
ooni/runner.py | 111 +-----------------------------------------------------
3 files changed, 116 insertions(+), 110 deletions(-)
diff --git a/ooni/director.py b/ooni/director.py
index 80cf22c..b3dfba3 100644
--- a/ooni/director.py
+++ b/ooni/director.py
@@ -1,7 +1,16 @@
+import sys
+from ooni import config
from ooni.managers import ReportEntryManager, MeasurementManager
from ooni.reporter import Report
-from ooni.utils import log
+from ooni.utils import log, checkForRoot, NotRootError
+from ooni.utils.net import randomFreePort
from ooni.nettest import NetTest
+from ooni.errors import UnableToStartTor
+
+from txtorcon import TorConfig
+from txtorcon import TorState, launch_tor
+
+from twisted.internet import defer, reactor
class Director(object):
"""
@@ -161,3 +170,102 @@ class Director(object):
net_test.done.addBoth(self.netTestDone, net_test)
return net_test.done
+ def startSniffing(self):
+ """ Start sniffing with Scapy. Exits if required privileges (root) are not
+ available.
+ """
+ from ooni.utils.txscapy import ScapyFactory, ScapySniffer
+ try:
+ checkForRoot()
+ except NotRootError:
+ print "[!] Includepcap options requires root priviledges to run"
+ print " you should run ooniprobe as root or disable the options in ooniprobe.conf"
+ sys.exit(1)
+
+ print "Starting sniffer"
+ config.scapyFactory = ScapyFactory(config.advanced.interface)
+
+ if os.path.exists(config.reports.pcap):
+ print "Report PCAP already exists with filename %s" % config.reports.pcap
+ print "Renaming files with such name..."
+ pushFilenameStack(config.reports.pcap)
+
+ sniffer = ScapySniffer(config.reports.pcap)
+ config.scapyFactory.registerProtocol(sniffer)
+
+
+ def startTor(self):
+ """ Starts Tor
+ Launches a Tor with :param: socks_port :param: control_port
+ :param: tor_binary set in ooniprobe.conf
+ """
+ @defer.inlineCallbacks
+ def state_complete(state):
+ config.tor_state = state
+ log.msg("Successfully bootstrapped Tor")
+ log.debug("We now have the following circuits: ")
+ for circuit in state.circuits.values():
+ log.debug(" * %s" % circuit)
+
+ socks_port = yield state.protocol.get_conf("SocksPort")
+ control_port = yield state.protocol.get_conf("ControlPort")
+ client_ip = yield state.protocol.get_info("address")
+
+ config.tor.socks_port = int(socks_port.values()[0])
+ config.tor.control_port = int(control_port.values()[0])
+
+ config.probe_ip = client_ip.values()[0]
+
+ log.debug("Obtained our IP address from a Tor Relay %s" % config.privacy.client_ip)
+
+ def setup_failed(failure):
+ log.exception(failure)
+ raise UnableToStartTor
+
+ def setup_complete(proto):
+ """
+ Called when we read from stdout that Tor has reached 100%.
+ """
+ log.debug("Building a TorState")
+ state = TorState(proto.tor_protocol)
+ state.post_bootstrap.addCallback(state_complete)
+ state.post_bootstrap.addErrback(setup_failed)
+ return state.post_bootstrap
+
+ def updates(prog, tag, summary):
+ log.debug("%d%%: %s" % (prog, summary))
+
+ tor_config = TorConfig()
+ if config.tor.control_port:
+ tor_config.ControlPort = config.tor.control_port
+ else:
+ control_port = int(randomFreePort())
+ tor_config.ControlPort = control_port
+ config.tor.control_port = control_port
+
+ if config.tor.socks_port:
+ tor_config.SocksPort = config.tor.socks_port
+ else:
+ socks_port = int(randomFreePort())
+ tor_config.SocksPort = socks_port
+ config.tor.socks_port = socks_port
+
+ if config.tor.data_dir:
+ data_dir = os.path.expanduser(config.tor.data_dir)
+
+ if not os.path.exists(data_dir):
+ log.msg("%s does not exist. Creating it." % data_dir)
+ os.makedirs(data_dir)
+ tor_config.DataDirectory = data_dir
+
+ tor_config.save()
+
+ log.debug("Setting control port as %s" % tor_config.ControlPort)
+ log.debug("Setting SOCKS port as %s" % tor_config.SocksPort)
+
+ d = launch_tor(tor_config, reactor,
+ tor_binary=config.advanced.tor_binary,
+ progress_updates=updates)
+ d.addCallback(setup_complete)
+ d.addErrback(setup_failed)
+ return d
diff --git a/ooni/errors.py b/ooni/errors.py
index cd4a136..6bb2144 100644
--- a/ooni/errors.py
+++ b/ooni/errors.py
@@ -114,3 +114,8 @@ def failureToString(failure):
return string
+class DirectorException(Exception):
+ pass
+
+class UnableToStartTor(DirectorException):
+ pass
diff --git a/ooni/runner.py b/ooni/runner.py
index d56dc26..bf51068 100644
--- a/ooni/runner.py
+++ b/ooni/runner.py
@@ -1,5 +1,4 @@
import os
-import sys
import time
import random
@@ -8,18 +7,14 @@ import yaml
from twisted.internet import defer
from twisted.internet import reactor
-from txtorcon import TorConfig
-from txtorcon import TorState, launch_tor
-
from ooni import config
from ooni.reporter import OONIBReporter, YAMLReporter, OONIBReportError
from ooni.inputunit import InputUnitFactory
-from ooni.utils import log, checkForRoot, pushFilenameStack
-from ooni.utils import NotRootError, Storage
-from ooni.utils.net import randomFreePort
+from ooni.utils import log
+from ooni.utils import Storage
class InvalidResumeFile(Exception):
pass
@@ -230,108 +225,6 @@ def runTestCases(test_cases, options, cmd_line_options):
log.exception("Problem in running test")
yaml_reporter.finish()
-class UnableToStartTor(Exception):
- pass
-
-def startTor():
- """ Starts Tor
- Launches a Tor with :param: socks_port :param: control_port
- :param: tor_binary set in ooniprobe.conf
- """
- @defer.inlineCallbacks
- def state_complete(state):
- config.tor_state = state
- log.msg("Successfully bootstrapped Tor")
- log.debug("We now have the following circuits: ")
- for circuit in state.circuits.values():
- log.debug(" * %s" % circuit)
-
- socks_port = yield state.protocol.get_conf("SocksPort")
- control_port = yield state.protocol.get_conf("ControlPort")
- client_ip = yield state.protocol.get_info("address")
-
- config.tor.socks_port = int(socks_port.values()[0])
- config.tor.control_port = int(control_port.values()[0])
-
- config.probe_ip = client_ip.values()[0]
-
- log.debug("Obtained our IP address from a Tor Relay %s" % config.privacy.client_ip)
-
- def setup_failed(failure):
- log.exception(failure)
- raise UnableToStartTor
-
- def setup_complete(proto):
- """
- Called when we read from stdout that Tor has reached 100%.
- """
- log.debug("Building a TorState")
- state = TorState(proto.tor_protocol)
- state.post_bootstrap.addCallback(state_complete)
- state.post_bootstrap.addErrback(setup_failed)
- return state.post_bootstrap
-
- def updates(prog, tag, summary):
- log.debug("%d%%: %s" % (prog, summary))
-
- tor_config = TorConfig()
- if config.tor.control_port:
- tor_config.ControlPort = config.tor.control_port
- else:
- control_port = int(randomFreePort())
- tor_config.ControlPort = control_port
- config.tor.control_port = control_port
-
- if config.tor.socks_port:
- tor_config.SocksPort = config.tor.socks_port
- else:
- socks_port = int(randomFreePort())
- tor_config.SocksPort = socks_port
- config.tor.socks_port = socks_port
-
- if config.tor.data_dir:
- data_dir = os.path.expanduser(config.tor.data_dir)
-
- if not os.path.exists(data_dir):
- log.msg("%s does not exist. Creating it." % data_dir)
- os.makedirs(data_dir)
- tor_config.DataDirectory = data_dir
-
- tor_config.save()
-
- log.debug("Setting control port as %s" % tor_config.ControlPort)
- log.debug("Setting SOCKS port as %s" % tor_config.SocksPort)
-
- d = launch_tor(tor_config, reactor,
- tor_binary=config.advanced.tor_binary,
- progress_updates=updates)
- d.addCallback(setup_complete)
- d.addErrback(setup_failed)
- return d
-
-def startSniffing():
- """ Start sniffing with Scapy. Exits if required privileges (root) are not
- available.
- """
- from ooni.utils.txscapy import ScapyFactory, ScapySniffer
- try:
- checkForRoot()
- except NotRootError:
- print "[!] Includepcap options requires root priviledges to run"
- print " you should run ooniprobe as root or disable the options in ooniprobe.conf"
- sys.exit(1)
-
- print "Starting sniffer"
- config.scapyFactory = ScapyFactory(config.advanced.interface)
-
- if os.path.exists(config.reports.pcap):
- print "Report PCAP already exists with filename %s" % config.reports.pcap
- print "Renaming files with such name..."
- pushFilenameStack(config.reports.pcap)
-
- sniffer = ScapySniffer(config.reports.pcap)
- config.scapyFactory.registerProtocol(sniffer)
-
def loadTest(cmd_line_options):
"""
Takes care of parsing test command line arguments and loading their
_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits