commit ead15c4bf8cfe05e204f94b94d5e9f976e27f9dd Author: Arturo Filastò art@fuffa.org Date: Wed Jan 16 22:38:35 2013 +0100
Validate that the collector command line option --- ooni/errors.py | 1 + ooni/nettest.py | 14 +++++++------- ooni/oonicli.py | 13 +++++++++++++ ooni/reporter.py | 54 +++++++++++++++++++++++++++++++----------------------- 4 files changed, 52 insertions(+), 30 deletions(-)
diff --git a/ooni/errors.py b/ooni/errors.py index 6bb2144..9281c6c 100644 --- a/ooni/errors.py +++ b/ooni/errors.py @@ -118,4 +118,5 @@ class DirectorException(Exception): pass
class UnableToStartTor(DirectorException): +class InvalidOONIBCollectorAddress(Exception): pass diff --git a/ooni/nettest.py b/ooni/nettest.py index 2da1c8f..e2c17f1 100644 --- a/ooni/nettest.py +++ b/ooni/nettest.py @@ -59,13 +59,13 @@ class NetTestLoader(object): client_geodata['countrycode'] = None
test_details = {'start_time': otime.utcTimeNow(), - 'probe_asn': client_geodata['asn'], - 'probe_cc': client_geodata['countrycode'], - 'probe_ip': client_geodata['ip'], - 'test_name': self.testName, - 'test_version': self.testVersion, - 'software_name': 'ooniprobe', - 'software_version': software_version + 'probe_asn': client_geodata['asn'], + 'probe_cc': client_geodata['countrycode'], + 'probe_ip': client_geodata['ip'], + 'test_name': self.testName, + 'test_version': self.testVersion, + 'software_name': 'ooniprobe', + 'software_version': software_version } return test_details
diff --git a/ooni/oonicli.py b/ooni/oonicli.py index 405f8ac..e307d35 100644 --- a/ooni/oonicli.py +++ b/ooni/oonicli.py @@ -9,6 +9,8 @@ from twisted.internet import defer, reactor, task from twisted.python import usage from twisted.python.util import spewer
+from ooni.errors import InvalidOONIBCollectorAddress + from ooni import config from ooni.director import Director from ooni.reporter import YAMLReporter, OONIBReporter @@ -184,6 +186,17 @@ def runWithDirector(): yaml_reporter = YAMLReporter(net_test_loader.testDetails) reporters = [yaml_reporter]
+ if global_options['collector']: + try: + oonib_reporter = OONIBReporter(net_test_loader.testDetails, + global_options['collector']) + reporters.append(oonib_reporter) + except InvalidOONIBCollectorAddress: + log.err("Invalid format for oonib collector address.") + log.msg("Should be in the format http://<collector_address>:<port>") + log.msg("for example: ooniprobe -c httpo://nkvphnp3p6agi5qq.onion") + sys.exit(1) + director = Director(reporters) try: d = director.startNetTest(net_test_loader, net_test_options) diff --git a/ooni/reporter.py b/ooni/reporter.py index 30c4817..237bae7 100644 --- a/ooni/reporter.py +++ b/ooni/reporter.py @@ -25,6 +25,8 @@ except ImportError: log.err("Scapy is not installed.")
+from ooni.errors import InvalidOONIBCollectorAddress + from ooni import otime from ooni.utils import geodata, pushFilenameStack from ooni.utils.net import BodyReceiver, StringProducer, userAgents @@ -234,10 +236,11 @@ class OONIBTestDetailsLookupError(OONIBReportError): pass
class OONIBReporter(OReporter): - collector_address = '' def __init__(self, test_details, collector_address): - self.collector_address = collector_address - self.report_id = None + self.collectorAddress = collector_address + self.validateCollectorAddress() + + self.reportID = None
from ooni.utils.txagentwithsocks import Agent from twisted.internet import reactor @@ -249,6 +252,15 @@ class OONIBReporter(OReporter):
OReporter.__init__(self, test_details)
+ def validateCollectorAddress(self): + """ + Will raise :class:ooni.errors.InvalidOONIBCollectorAddress an exception + if the oonib reporter is not valid. + """ + regexp = re.compile('^(http|httpo)://\w+(:\d+)?$') + if not regexp.match(self.collectorAddress): + raise InvalidOONIBCollectorAddress + @defer.inlineCallbacks def writeReportEntry(self, entry): log.debug("Writing report with OONIB reporter") @@ -256,12 +268,12 @@ class OONIBReporter(OReporter): content += safe_dump(entry) content += '...\n'
- url = self.collector_address + '/report' + url = self.collectorAddress + '/report'
- request = {'report_id': self.report_id, + request = {'report_id': self.reportID, 'content': content}
- log.debug("Updating report with id %s (%s)" % (self.report_id, url)) + log.debug("Updating report with id %s (%s)" % (self.reportID, url)) request_json = json.dumps(request) log.debug("Sending %s" % request_json)
@@ -277,28 +289,24 @@ class OONIBReporter(OReporter): raise OONIBReportUpdateError
@defer.inlineCallbacks - def createReport(self, options): + def createReport(self): """ Creates a report on the oonib collector. """ - url = self.collector_address + '/report' - - test_details['options'] = self.cmd_line_options - - log.debug("Obtained test_details: %s" % test_details) + url = self.collectorAddress + '/report'
content = '---\n' - content += safe_dump(test_details) + content += safe_dump(self.testDetails) content += '...\n'
- test_name = options['name'] - test_version = options['version'] - - request = {'software_name': test_details['software_name'], - 'software_version': test_details['software_version'], - 'probe_asn': test_details['probe_asn'], - 'test_name': test_details['test_name'], - 'test_version': test_details['test_version'], + request = {'software_name': self.testDetails['software_name'], + 'software_version': self.testDetails['software_version'], + 'probe_asn': self.testDetails['probe_asn'], + 'test_name': self.testDetails['test_name'], + 'test_version': self.testDetails['test_version'], + # XXX there is a bunch of redundancy in the arguments getting sent + # to the backend. This may need to get changed in the client and the + # backend. 'content': content }
@@ -335,8 +343,8 @@ class OONIBReporter(OReporter): log.exception(e) raise OONIBReportCreationError
- self.report_id = parsed_response['report_id'] - self.backend_version = parsed_response['backend_version'] + self.reportID = parsed_response['report_id'] + self.backendVersion = parsed_response['backend_version'] log.debug("Created report with id %s" % parsed_response['report_id'])
class ReportClosed(Exception):
tor-commits@lists.torproject.org