forked from openrightsgroup/OrgProbe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprobe.py
333 lines (284 loc) · 11.4 KB
/
probe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import re
import sys
import json
import time
import logging
import requests
import hashlib
import contextlib
from api import RegisterProbeRequest, PrepareProbeRequest, StatusIPRequest, \
ConfigRequest
from httpqueue import HTTPQueue
from signing import RequestSigner
from category import Categorizor
class SelfTestError(Exception):
pass
class OrgProbe(object):
DEFAULT_USERAGENT = 'OrgProbe/0.9.4 (+http://www.blocked.org.uk)'
def __init__(self, config):
self.config = config
# initialize configuration
# set up in .run()
self.probe = None
self.signer = None
self.headers = {}
self.read_size = 8192 # size of body to read
# set up in .configure()
self.apiconfig = None
self.isp = None
self.ip = None
self.categorizor = None
self.blocktype = None
# set up in .setup_queue()
self.hb = None
pass
def register(self, opts):
logging.warning("Untested code")
return
req = PrepareProbeRequest(opts['--secret'], email=opts['--email'])
code, data = req.execute()
print code, data
if data['success'] is not True:
logging.error("Unable to prepare probe: %s", data)
return
probe_uuid = hashlib.md5(
opts['--seed'] + '-' + data['probe_hmac']).hexdigest()
req2 = RegisterProbeRequest(opts['--secret'], email=opts['--email'],
probe_seed=opts['--seed'],
probe_uuid=probe_uuid
)
code2, data2 = req2.execute()
print code2, data2
if data2['success'] is not True:
logging.error("Unable to prepare probe: %s", data2)
return
self.config.add_section(opts['--seed'])
self.config.set(opts['--seed'], 'uuid', probe_uuid)
self.config.set(opts['--seed'], 'secret', data2['secret'])
def configure(self):
self.get_api_config()
self.get_ip_status()
self.run_selftest()
self.setup_queue()
def get_api_config(self):
if self.probe.get('api_config_file'):
with open(self.probe['api_config_file']) as fp:
self.apiconfig = json.load(fp)
logging.info("Loaded config: %s from %s",
self.apiconfig['version'],
self.probe['api_config_file'])
return
req = ConfigRequest(None, self.probe.get('config_version', 'latest'))
code, data = req.execute()
if code == 200:
logging.info("Loaded config: %s", data['version'])
self.apiconfig = data
print >> sys.stderr, data
elif code == 304:
pass
else:
logging.error("Error downloading config: %s, %s", code,
data['error'])
def get_ip_status(self):
try:
args = (self.probe['public_ip'],)
except KeyError:
args = []
req = StatusIPRequest(self.signer, *args,
probe_uuid=self.probe['uuid'])
code, data = req.execute()
logging.info("Status: %s, %s", code, data)
if 'network' in self.probe:
self.isp = self.probe['network']
logging.debug("Overriding network to: %s", self.isp)
else:
self.isp = data['isp']
self.ip = data['ip']
for rule in self.apiconfig['rules']:
if rule['isp'] == self.isp:
self.rules = rule['match']
if 'category' in rule:
logging.info("Creating Categorizor with rule: %s",
rule['category'])
self.categorizor = Categorizor(rule['category'])
if 'blocktype' in rule:
logging.info("Adding blocktype array: %s",
rule['blocktype'])
self.blocktype = rule['blocktype']
break
else:
logging.error("No rules found for ISP: %s", self.isp)
if self.probe.get('skip_rules', 'False') == 'True':
self.rules = []
else:
sys.exit(1)
logging.info("Got rules: %s", self.rules)
def setup_queue(self):
if not self.config.has_section('amqp'):
logging.info("Using HTTP Queue")
self.queue = HTTPQueue(self.probe['uuid'], self.signer, self.isp)
else:
from amqpqueue import AMQPQueue
opts = dict(self.config.items('amqp'))
logging.info("Setting up AMQP with options: %s", opts)
lifetime = int(self.probe['lifetime']) if 'lifetime' in \
self.probe else None
self.queue = AMQPQueue(opts,
self.isp.lower().replace(' ', '_'),
self.probe.get('queue', 'org'),
self.signer,
lifetime
)
if 'heartbeat' in self.probe:
from heartbeat import Heartbeat
self.hb = Heartbeat(self.queue.conn,
int(self.probe['heartbeat']),
self.probe['uuid']
)
self.hb.start_thread()
def match_rule(self, req, body, rule):
if rule.startswith('re:'):
ruletype, field, pattern = rule.split(':', 2)
if field == 'url':
value = req.url
flags = 0
if field == 'body':
value = body
flags = re.M
match = re.search(pattern, value, flags)
if match is not None:
return True
return False
return None
def test_response(self, req):
category = ''
if self.read_size > 0:
if req.headers['content-type'].lower().startswith('text'):
body = req.iter_content(self.read_size).next()
else:
# we're not downloading images
body = ''
else:
body = req.content
logging.info("Read body length: %s", len(body))
for rulenum, rule in enumerate(self.rules):
if self.match_rule(req, body, rule) is True:
logging.info("Matched rule: %s; blocked", rule)
if self.categorizor:
category = self.categorizor.categorize(req.url)
return (
'blocked',
req.history[-1].status_code if hasattr(req,
'history') and len(
req.history) > 0 else req.status_code,
category,
self.blocktype[rulenum] if self.blocktype else None
)
logging.info("Status: OK")
return 'ok', req.status_code, None, None
def test_url(self, url):
logging.info("Testing URL: %s", url)
try:
with contextlib.closing(requests.get(
url,
headers=self.headers,
timeout=int(self.probe.get('timeout', 5)),
verify=self.verify_ssl,
stream=True if self.read_size > 0 else False
)) as req:
try:
return self.test_response(req)
except Exception, v:
logging.error("Response test error: %s", v)
raise
except (requests.exceptions.SSLError,), v:
logging.warn("SSL Error: %s", v)
return 'sslerror', -1, None, None
except (requests.exceptions.Timeout,), v:
logging.warn("Connection timeout: %s", v)
return 'timeout', -1, None, None
except Exception, v:
logging.warn("Connection error: %s", v)
try:
# look for dns failure in exception message
# requests lib turns nested exceptions into strings
if 'Name or service not known' in v.args[0].message:
logging.info("DNS resolution failed(1)")
return 'dnserror', -1, None, None
except:
pass
try:
# look for dns failure in exception message
if 'Name or service not known' in v.args[0][1].strerror:
logging.info("DNS resolution failed(2)")
return 'dnserror', -1, None, None
except:
pass
return 'error', -1, None, None
def test_and_report_url(self, url, urlhash=None):
result, code, category, blocktype = self.test_url(url)
logging.info("Logging result with ORG: %s, %s", result, code)
report = {
'network_name': self.isp,
'ip_network': self.ip,
'url': url,
'http_status': code,
'status': result,
'probe_uuid': self.probe['uuid'],
'config': self.apiconfig['version'],
'category': category or '',
'blocktype': blocktype or '',
}
self.queue.send(report, urlhash)
def run_selftest(self):
if self.probe.get('selftest', 'True') == 'False':
return
for url in self.apiconfig['self-test']['must-allow']:
if self.test_url(url)[0] != 'ok':
raise SelfTestError
for url in self.apiconfig['self-test']['must-block']:
if self.test_url(url)[0] != 'blocked':
raise SelfTestError
def delay(self, multiplier=1):
# only httpqueue requires sleep intervals at the moment
if isinstance(self.queue, HTTPQueue):
time.sleep(self.config.getint('global', 'interval') * multiplier)
def run_test(self, data):
if data is None:
self.delay(5)
return
if 'url' in data:
self.test_and_report_url(data['url'], data['hash'])
elif 'urls' in data:
for url in data['urls']:
self.test_and_report_url(url['url'], data['hash'])
self.delay()
def run(self, args):
if len(args) > 0:
self.probename = args[0]
else:
try:
self.probename = [x for x in self.config.sections() if
x not in ('amqp', 'api', 'global')][0]
except IndexError:
logging.error("No probe identity configuration found")
return 1
logging.info("Using probe: %s", self.probename)
self.probe = dict([(x, self.config.get(self.probename, x))
for x in self.config.options(self.probename)
])
self.signer = RequestSigner(self.probe['secret'])
self.headers = {
'User-Agent': self.probe.get('useragent', self.DEFAULT_USERAGENT),
}
if 'read_size' in self.probe:
self.read_size = int(self.probe['read_size'])
if 'verify_ssl' in self.probe:
self.verify_ssl = (self.probe['verify_ssl'] == 'true')
self.configure()
try:
self.queue.drive(self.run_test)
logging.info("Exiting cleanly")
finally:
if self.hb is not None:
self.hb.stop_thread()