Commit | Line | Data |
---|---|---|
b0cfbfce | 1 | # -*- python -*- |
0256fc10 IJ |
2 | # |
3 | # Hippotat - Asinine IP Over HTTP program | |
4 | # hippotatlib/__init__.py - common library code | |
5 | # | |
6 | # Copyright 2017 Ian Jackson | |
7 | # | |
f85d143f | 8 | # GPLv3+ |
0256fc10 | 9 | # |
f85d143f IJ |
10 | # This program is free software: you can redistribute it and/or modify |
11 | # it under the terms of the GNU General Public License as published by | |
12 | # the Free Software Foundation, either version 3 of the License, or | |
13 | # (at your option) any later version. | |
0256fc10 | 14 | # |
f85d143f IJ |
15 | # This program is distributed in the hope that it will be useful, |
16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | # GNU General Public License for more details. | |
19 | # | |
20 | # You should have received a copy of the GNU General Public License | |
21 | # along with this program, in the file GPLv3. If not, | |
22 | # see <http://www.gnu.org/licenses/>. | |
23 | ||
b0cfbfce | 24 | |
37ab4cdc IJ |
25 | import signal |
26 | signal.signal(signal.SIGINT, signal.SIG_DFL) | |
27 | ||
1321ad5f | 28 | import sys |
cae50358 | 29 | import os |
1321ad5f | 30 | |
b83d422a IJ |
31 | from zope.interface import implementer |
32 | ||
040ff511 IJ |
33 | import twisted |
34 | from twisted.internet import reactor | |
1d023c89 | 35 | import twisted.internet.endpoints |
8c3b6620 IJ |
36 | import twisted.logger |
37 | from twisted.logger import LogLevel | |
38 | import twisted.python.constants | |
39 | from twisted.python.constants import NamedConstant | |
b0cfbfce IJ |
40 | |
41 | import ipaddress | |
42 | from ipaddress import AddressValueError | |
43 | ||
ae7c7784 | 44 | from optparse import OptionParser |
5510890e | 45 | import configparser |
ae7c7784 IJ |
46 | from configparser import ConfigParser |
47 | from configparser import NoOptionError | |
48 | ||
c13ee6e6 IJ |
49 | from functools import partial |
50 | ||
ae7c7784 | 51 | import collections |
84e763c7 | 52 | import time |
ef041033 IJ |
53 | import hmac |
54 | import hashlib | |
55 | import base64 | |
8c3b6620 | 56 | import codecs |
eedc8b30 | 57 | import traceback |
ae7c7784 | 58 | |
1321ad5f IJ |
59 | import re as regexp |
60 | ||
5a37bac8 | 61 | import hippotatlib.slip as slip |
1321ad5f | 62 | |
d579a048 | 63 | class DBG(twisted.python.constants.Names): |
380ed56c | 64 | INIT = NamedConstant() |
cae50358 | 65 | CONFIG = NamedConstant() |
d579a048 | 66 | ROUTE = NamedConstant() |
b68c0739 | 67 | DROP = NamedConstant() |
4a780703 | 68 | OWNSOURCE = NamedConstant() |
d579a048 IJ |
69 | FLOW = NamedConstant() |
70 | HTTP = NamedConstant() | |
380ed56c | 71 | TWISTED = NamedConstant() |
d579a048 | 72 | QUEUE = NamedConstant() |
380ed56c | 73 | HTTP_CTRL = NamedConstant() |
d579a048 | 74 | QUEUE_CTRL = NamedConstant() |
297b3ebf | 75 | HTTP_FULL = NamedConstant() |
0accf0d3 | 76 | CTRL_DUMP = NamedConstant() |
380ed56c | 77 | SLIP_FULL = NamedConstant() |
9acb0eca | 78 | DATA_COMPLETE = NamedConstant() |
d579a048 | 79 | |
b68c0739 | 80 | _hex_codec = codecs.getencoder('hex_codec') |
8c3b6620 | 81 | |
b83d422a IJ |
82 | #---------- logging ---------- |
83 | ||
84 | org_stderr = sys.stderr | |
85 | ||
8c3b6620 IJ |
86 | log = twisted.logger.Logger() |
87 | ||
2e68eb10 IJ |
88 | debug_set = set() |
89 | debug_def_detail = DBG.HTTP | |
3e35fc99 | 90 | |
8c3b6620 | 91 | def log_debug(dflag, msg, idof=None, d=None): |
3e35fc99 | 92 | if dflag not in debug_set: return |
e8fcf3b7 | 93 | #print('---------------->',repr((dflag, msg, idof, d)), file=sys.stderr) |
8c3b6620 | 94 | if idof is not None: |
e8ed0029 | 95 | msg = '[%#x] %s' % (id(idof), msg) |
8c3b6620 | 96 | if d is not None: |
9acb0eca IJ |
97 | trunc = '' |
98 | if not DBG.DATA_COMPLETE in debug_set: | |
99 | if len(d) > 64: | |
100 | d = d[0:64] | |
101 | trunc = '...' | |
b68c0739 | 102 | d = _hex_codec(d)[0].decode('ascii') |
9acb0eca | 103 | msg += ' ' + d + trunc |
8c3b6620 IJ |
104 | log.info('{dflag} {msgcore}', dflag=dflag, msgcore=msg) |
105 | ||
80e963a1 IJ |
106 | def logevent_is_boringtwisted(event): |
107 | try: | |
108 | if event.get('log_level') != LogLevel.info: | |
109 | return False | |
110 | dflag = event.get('dflag') | |
111 | if dflag is False : return False | |
112 | if dflag in debug_set: return False | |
113 | if dflag is None and DBG.TWISTED in debug_set: return False | |
114 | return True | |
115 | except Exception: | |
02a201e1 IJ |
116 | print('EXCEPTION (IN BORINGTWISTED CHECK)', |
117 | traceback.format_exc(), file=org_stderr) | |
80e963a1 IJ |
118 | return False |
119 | ||
b83d422a IJ |
120 | @implementer(twisted.logger.ILogFilterPredicate) |
121 | class LogNotBoringTwisted: | |
122 | def __call__(self, event): | |
80e963a1 IJ |
123 | return ( |
124 | twisted.logger.PredicateResult.no | |
125 | if logevent_is_boringtwisted(event) else | |
126 | twisted.logger.PredicateResult.yes | |
127 | ) | |
b83d422a IJ |
128 | |
129 | #---------- default config ---------- | |
130 | ||
ca732796 | 131 | defcfg = ''' |
71f9ddb6 | 132 | [COMMON] |
9e445690 IJ |
133 | max_batch_down = 65536 |
134 | max_queue_time = 10 | |
135 | target_requests_outstanding = 3 | |
136 | http_timeout = 30 | |
137 | http_timeout_grace = 5 | |
138 | max_requests_outstanding = 6 | |
139 | max_batch_up = 4000 | |
140 | http_retry = 5 | |
c7fb640e | 141 | port = 80 |
8d374606 | 142 | vroutes = '' |
d72f8360 IJ |
143 | ifname_client = hippo%%d |
144 | ifname_server = shippo%%d | |
ef041033 | 145 | max_clock_skew = 300 |
ca732796 IJ |
146 | |
147 | #[server] or [<client>] overrides | |
d72f8360 | 148 | ipif = userv root ipif %(local)s,%(peer)s,%(mtu)s,slip,%(ifname)s %(rnets)s |
ca732796 | 149 | |
9e445690 | 150 | # relating to virtual network |
ca732796 | 151 | mtu = 1500 |
ca732796 | 152 | |
9e445690 | 153 | # addrs = 127.0.0.1 ::1 |
9e445690 IJ |
154 | # url |
155 | ||
156 | # relating to virtual network | |
8d374606 IJ |
157 | vvnetwork = 172.24.230.192 |
158 | # vnetwork = <prefix>/<len> | |
c57e18a7 | 159 | # vaddr = <ipaddr> |
8d374606 | 160 | # vrelay = <ipaddr> |
9e445690 | 161 | |
ca732796 IJ |
162 | |
163 | # [<client-ip4-or-ipv6-address>] | |
dce21e00 | 164 | # secret = <secret> # used by both, must match |
ca732796 | 165 | |
c7fb640e | 166 | [LIMIT] |
9e445690 IJ |
167 | max_batch_down = 262144 |
168 | max_queue_time = 121 | |
169 | http_timeout = 121 | |
170 | target_requests_outstanding = 10 | |
ca732796 IJ |
171 | ''' |
172 | ||
87a7c0c7 | 173 | # these need to be defined here so that they can be imported by import * |
cae50358 | 174 | cfg = ConfigParser(strict=False) |
ae7c7784 IJ |
175 | optparser = OptionParser() |
176 | ||
e4006ac4 | 177 | _mimetrans = bytes.maketrans(b'-'+slip.esc, slip.esc+b'-') |
7b07f0b5 IJ |
178 | def mime_translate(s): |
179 | # SLIP-encoded packets cannot contain ESC ESC. | |
180 | # Swap `-' and ESC. The result cannot contain `--' | |
181 | return s.translate(_mimetrans) | |
182 | ||
87a7c0c7 | 183 | class ConfigResults: |
c7fb640e IJ |
184 | def __init__(self): |
185 | pass | |
87a7c0c7 IJ |
186 | def __repr__(self): |
187 | return 'ConfigResults('+repr(self.__dict__)+')' | |
188 | ||
a8827d59 | 189 | def log_discard(packet, iface, saddr, daddr, why): |
b68c0739 | 190 | log_debug(DBG.DROP, |
a8827d59 | 191 | 'discarded packet [%s] %s -> %s: %s' % (iface, saddr, daddr, why), |
b68c0739 | 192 | d=packet) |
1321ad5f | 193 | |
b0cfbfce IJ |
194 | #---------- packet parsing ---------- |
195 | ||
196 | def packet_addrs(packet): | |
197 | version = packet[0] >> 4 | |
198 | if version == 4: | |
199 | addrlen = 4 | |
200 | saddroff = 3*4 | |
201 | factory = ipaddress.IPv4Address | |
202 | elif version == 6: | |
203 | addrlen = 16 | |
204 | saddroff = 2*4 | |
205 | factory = ipaddress.IPv6Address | |
206 | else: | |
207 | raise ValueError('unsupported IP version %d' % version) | |
208 | saddr = factory(packet[ saddroff : saddroff + addrlen ]) | |
209 | daddr = factory(packet[ saddroff + addrlen : saddroff + addrlen*2 ]) | |
210 | return (saddr, daddr) | |
211 | ||
212 | #---------- address handling ---------- | |
213 | ||
214 | def ipaddr(input): | |
215 | try: | |
216 | r = ipaddress.IPv4Address(input) | |
217 | except AddressValueError: | |
218 | r = ipaddress.IPv6Address(input) | |
219 | return r | |
220 | ||
221 | def ipnetwork(input): | |
222 | try: | |
223 | r = ipaddress.IPv4Network(input) | |
224 | except NetworkValueError: | |
225 | r = ipaddress.IPv6Network(input) | |
226 | return r | |
040ff511 IJ |
227 | |
228 | #---------- ipif (SLIP) subprocess ---------- | |
229 | ||
a95cfeb2 | 230 | class SlipStreamDecoder(): |
db6ba584 | 231 | def __init__(self, desc, on_packet): |
040ff511 | 232 | self._buffer = b'' |
a95cfeb2 | 233 | self._on_packet = on_packet |
db6ba584 IJ |
234 | self._desc = desc |
235 | self._log('__init__') | |
236 | ||
237 | def _log(self, msg, **kwargs): | |
3297cac1 | 238 | log_debug(DBG.SLIP_FULL, 'slip %s: %s' % (self._desc, msg), **kwargs) |
a95cfeb2 IJ |
239 | |
240 | def inputdata(self, data): | |
db6ba584 | 241 | self._log('inputdata', d=data) |
7fa9c132 IJ |
242 | data = self._buffer + data |
243 | self._buffer = b'' | |
244 | packets = slip.decode(data, True) | |
040ff511 IJ |
245 | self._buffer = packets.pop() |
246 | for packet in packets: | |
a95cfeb2 | 247 | self._maybe_packet(packet) |
54890d4d | 248 | self._log('bufremain', d=self._buffer) |
a95cfeb2 IJ |
249 | |
250 | def _maybe_packet(self, packet): | |
54890d4d | 251 | self._log('maybepacket', d=packet) |
db6ba584 IJ |
252 | if len(packet): |
253 | self._on_packet(packet) | |
a95cfeb2 | 254 | |
4f991c0c | 255 | def flush(self): |
54890d4d | 256 | self._log('flush') |
7fa9c132 | 257 | data = self._buffer |
a95cfeb2 | 258 | self._buffer = b'' |
7fa9c132 IJ |
259 | packets = slip.decode(data) |
260 | assert(len(packets) == 1) | |
261 | self._maybe_packet(packets[0]) | |
4f991c0c | 262 | |
e4006ac4 | 263 | class _IpifProcessProtocol(twisted.internet.protocol.ProcessProtocol): |
4f991c0c IJ |
264 | def __init__(self, router): |
265 | self._router = router | |
db6ba584 | 266 | self._decoder = SlipStreamDecoder('ipif', self.slip_on_packet) |
a95cfeb2 IJ |
267 | def connectionMade(self): pass |
268 | def outReceived(self, data): | |
269 | self._decoder.inputdata(data) | |
270 | def slip_on_packet(self, packet): | |
4f991c0c IJ |
271 | (saddr, daddr) = packet_addrs(packet) |
272 | if saddr.is_link_local or daddr.is_link_local: | |
a8827d59 | 273 | log_discard(packet, 'ipif', saddr, daddr, 'link-local') |
4f991c0c IJ |
274 | return |
275 | self._router(packet, saddr, daddr) | |
040ff511 IJ |
276 | def processEnded(self, status): |
277 | status.raiseException() | |
278 | ||
279 | def start_ipif(command, router): | |
040ff511 IJ |
280 | ipif = _IpifProcessProtocol(router) |
281 | reactor.spawnProcess(ipif, | |
282 | '/bin/sh',['sh','-xc', command], | |
ff613365 IJ |
283 | childFDs={0:'w', 1:'r', 2:2}, |
284 | env=None) | |
909e0ff3 | 285 | return ipif |
040ff511 | 286 | |
909e0ff3 | 287 | def queue_inbound(ipif, packet): |
15407d80 | 288 | log_debug(DBG.FLOW, "queue_inbound", d=packet) |
040ff511 IJ |
289 | ipif.transport.write(slip.delimiter) |
290 | ipif.transport.write(slip.encode(packet)) | |
291 | ipif.transport.write(slip.delimiter) | |
292 | ||
650a3251 IJ |
293 | #---------- packet queue ---------- |
294 | ||
295 | class PacketQueue(): | |
d579a048 IJ |
296 | def __init__(self, desc, max_queue_time): |
297 | self._desc = desc | |
8718b02c | 298 | assert(desc + '') |
650a3251 IJ |
299 | self._max_queue_time = max_queue_time |
300 | self._pq = collections.deque() # packets | |
301 | ||
b68c0739 | 302 | def _log(self, dflag, msg, **kwargs): |
8c3b6620 | 303 | log_debug(dflag, self._desc+' pq: '+msg, **kwargs) |
d579a048 | 304 | |
650a3251 | 305 | def append(self, packet): |
8c3b6620 | 306 | self._log(DBG.QUEUE, 'append', d=packet) |
650a3251 IJ |
307 | self._pq.append((time.monotonic(), packet)) |
308 | ||
309 | def nonempty(self): | |
8c3b6620 | 310 | self._log(DBG.QUEUE, 'nonempty ?') |
650a3251 IJ |
311 | while True: |
312 | try: (queuetime, packet) = self._pq[0] | |
8c3b6620 IJ |
313 | except IndexError: |
314 | self._log(DBG.QUEUE, 'nonempty ? empty.') | |
315 | return False | |
650a3251 IJ |
316 | |
317 | age = time.monotonic() - queuetime | |
84e763c7 | 318 | if age > self._max_queue_time: |
650a3251 | 319 | # strip old packets off the front |
8c3b6620 | 320 | self._log(DBG.QUEUE, 'dropping (old)', d=packet) |
650a3251 IJ |
321 | self._pq.popleft() |
322 | continue | |
323 | ||
8c3b6620 | 324 | self._log(DBG.QUEUE, 'nonempty ? nonempty.') |
650a3251 IJ |
325 | return True |
326 | ||
7b07f0b5 IJ |
327 | def process(self, sizequery, moredata, max_batch): |
328 | # sizequery() should return size of batch so far | |
329 | # moredata(s) should add s to batch | |
8c3b6620 | 330 | self._log(DBG.QUEUE, 'process...') |
7b07f0b5 IJ |
331 | while True: |
332 | try: (dummy, packet) = self._pq[0] | |
8c3b6620 IJ |
333 | except IndexError: |
334 | self._log(DBG.QUEUE, 'process... empty') | |
335 | break | |
336 | ||
337 | self._log(DBG.QUEUE_CTRL, 'process... packet', d=packet) | |
7b07f0b5 IJ |
338 | |
339 | encoded = slip.encode(packet) | |
340 | sofar = sizequery() | |
341 | ||
8c3b6620 IJ |
342 | self._log(DBG.QUEUE_CTRL, |
343 | 'process... (sofar=%d, max=%d) encoded' % (sofar, max_batch), | |
b68c0739 | 344 | d=encoded) |
8c3b6620 | 345 | |
7b07f0b5 IJ |
346 | if sofar > 0: |
347 | if sofar + len(slip.delimiter) + len(encoded) > max_batch: | |
8c3b6620 | 348 | self._log(DBG.QUEUE_CTRL, 'process... overflow') |
7b07f0b5 IJ |
349 | break |
350 | moredata(slip.delimiter) | |
351 | ||
352 | moredata(encoded) | |
84e763c7 | 353 | self._pq.popleft() |
ae7c7784 IJ |
354 | |
355 | #---------- error handling ---------- | |
356 | ||
b68c0739 IJ |
357 | _crashing = False |
358 | ||
ae7c7784 | 359 | def crash(err): |
b68c0739 IJ |
360 | global _crashing |
361 | _crashing = True | |
e8ed0029 IJ |
362 | print('========== CRASH ==========', err, |
363 | '===========================', file=sys.stderr) | |
ae7c7784 IJ |
364 | try: reactor.stop() |
365 | except twisted.internet.error.ReactorNotRunning: pass | |
366 | ||
367 | def crash_on_defer(defer): | |
368 | defer.addErrback(lambda err: crash(err)) | |
369 | ||
e4006ac4 | 370 | def crash_on_critical(event): |
ae7c7784 IJ |
371 | if event.get('log_level') >= LogLevel.critical: |
372 | crash(twisted.logger.formatEvent(event)) | |
373 | ||
ef041033 IJ |
374 | #---------- authentication tokens ---------- |
375 | ||
376 | _authtoken_digest = hashlib.sha256 | |
377 | ||
378 | def _authtoken_time(): | |
379 | return int(time.time()) | |
380 | ||
381 | def _authtoken_hmac(secret, hextime): | |
382 | return hmac.new(secret, hextime, _authtoken_digest).digest() | |
383 | ||
384 | def authtoken_make(secret): | |
385 | hextime = ('%x' % _authtoken_time()).encode('ascii') | |
386 | mac = _authtoken_hmac(secret, hextime) | |
387 | return hextime + b' ' + base64.b64encode(mac) | |
388 | ||
389 | def authtoken_check(secret, token, maxskew): | |
390 | (hextime, theirmac64) = token.split(b' ') | |
391 | now = _authtoken_time() | |
392 | then = int(hextime, 16) | |
393 | skew = then - now; | |
394 | if (abs(skew) > maxskew): | |
395 | raise ValueError('too much clock skew (client %ds ahead)' % skew) | |
396 | theirmac = base64.b64decode(theirmac64) | |
397 | ourmac = _authtoken_hmac(secret, hextime) | |
398 | if not hmac.compare_digest(theirmac, ourmac): | |
399 | raise ValueError('invalid token (wrong secret?)') | |
400 | pass | |
401 | ||
87a7c0c7 IJ |
402 | #---------- config processing ---------- |
403 | ||
c7fb640e IJ |
404 | def _cfg_process_putatives(): |
405 | servers = { } | |
406 | clients = { } | |
407 | # maps from abstract object to canonical name for cs's | |
87a7c0c7 | 408 | |
c7fb640e IJ |
409 | def putative(cmap, abstract, canoncs): |
410 | try: | |
411 | current_canoncs = cmap[abstract] | |
412 | except KeyError: | |
413 | pass | |
414 | else: | |
415 | assert(current_canoncs == canoncs) | |
416 | cmap[abstract] = canoncs | |
417 | ||
418 | server_pat = r'[-.0-9A-Za-z]+' | |
419 | client_pat = r'[.:0-9a-f]+' | |
420 | server_re = regexp.compile(server_pat) | |
6d5e8381 IJ |
421 | serverclient_re = regexp.compile( |
422 | server_pat + r' ' + '(?:' + client_pat + '|LIMIT)') | |
88487243 | 423 | |
c7fb640e | 424 | for cs in cfg.sections(): |
3a8ed92d IJ |
425 | def dbg(m): |
426 | log_debug_config('putatives: section [%s] %s' % (cs, m)) | |
4652e382 | 427 | |
43dd2ce0 | 428 | def log_ignore(why): |
3a8ed92d | 429 | dbg('X ignore: %s' % (why)) |
43dd2ce0 IJ |
430 | print('warning: ignoring config section [%s] (%s)' % (cs, why), |
431 | file=sys.stderr) | |
432 | ||
71f9ddb6 IJ |
433 | if cs == 'LIMIT' or cs == 'COMMON': |
434 | # plan A "[LIMIT]" or "[COMMON]" | |
3a8ed92d | 435 | dbg('A ignore') |
c7fb640e | 436 | continue |
88487243 | 437 | |
c7fb640e IJ |
438 | try: |
439 | # plan B "[<client>]" part 1 | |
440 | ci = ipaddr(cs) | |
441 | except AddressValueError: | |
88487243 | 442 | |
c7fb640e IJ |
443 | if server_re.fullmatch(cs): |
444 | # plan C "[<servername>]" | |
3a8ed92d | 445 | dbg('C <server>') |
c7fb640e IJ |
446 | putative(servers, cs, cs) |
447 | continue | |
448 | ||
449 | if serverclient_re.fullmatch(cs): | |
450 | # plan D "[<servername> <client>]" part 1 | |
451 | (pss,pcs) = cs.split(' ') | |
452 | ||
8d374606 | 453 | if pcs == 'LIMIT': |
c7fb640e | 454 | # plan E "[<servername> LIMIT]" |
3a8ed92d | 455 | dbg('E <server> LIMIT') |
c7fb640e IJ |
456 | continue |
457 | ||
458 | try: | |
459 | # plan D "[<servername> <client>]" part 2 | |
b8c38e66 | 460 | ci = ipaddr(pcs) |
c7fb640e | 461 | except AddressValueError: |
343c8cf4 | 462 | # plan F branch 1 "[<some thing we do not understand>]" |
43dd2ce0 | 463 | log_ignore('bad-addr') |
c7fb640e IJ |
464 | continue |
465 | ||
466 | else: # no AddressValueError | |
4652e382 | 467 | # plan D "[<servername> <client>]" part 3 |
3a8ed92d | 468 | dbg('D <server> <client>') |
c7fb640e IJ |
469 | putative(clients, ci, pcs) |
470 | putative(servers, pss, pss) | |
471 | continue | |
343c8cf4 IJ |
472 | else: |
473 | # plan F branch 2 "[<some thing we do not understand>]" | |
474 | log_ignore('nomatch '+ repr(serverclient_re)) | |
c7fb640e IJ |
475 | |
476 | else: # no AddressValueError | |
477 | # plan B "[<client>" part 2 | |
3a8ed92d | 478 | dbg('B <client>') |
c7fb640e IJ |
479 | putative(clients, ci, cs) |
480 | continue | |
481 | ||
482 | return (servers, clients) | |
483 | ||
62d13acc | 484 | def cfg_process_general(c, ss): |
300fe4ed | 485 | c.mtu = cfg1getint(ss, 'mtu') |
c7fb640e IJ |
486 | |
487 | def cfg_process_saddrs(c, ss): | |
488 | class ServerAddr(): | |
489 | def __init__(self, port, addrspec): | |
490 | self.port = port | |
491 | # also self.addr | |
492 | try: | |
493 | self.addr = ipaddress.IPv4Address(addrspec) | |
494 | self._endpointfactory = twisted.internet.endpoints.TCP4ServerEndpoint | |
495 | self._inurl = b'%s' | |
496 | except AddressValueError: | |
497 | self.addr = ipaddress.IPv6Address(addrspec) | |
498 | self._endpointfactory = twisted.internet.endpoints.TCP6ServerEndpoint | |
499 | self._inurl = b'[%s]' | |
500 | def make_endpoint(self): | |
3b69fba9 IJ |
501 | return self._endpointfactory(reactor, self.port, |
502 | interface= '%s' % self.addr) | |
c7fb640e IJ |
503 | def url(self): |
504 | url = b'http://' + (self._inurl % str(self.addr).encode('ascii')) | |
505 | if self.port != 80: url += b':%d' % self.port | |
506 | url += b'/' | |
507 | return url | |
3b69fba9 IJ |
508 | def __repr__(self): |
509 | return 'ServerAddr'+repr((self.port,self.addr)) | |
c7fb640e | 510 | |
300fe4ed | 511 | c.port = cfg1getint(ss,'port') |
c7fb640e | 512 | c.saddrs = [ ] |
300fe4ed | 513 | for addrspec in cfg1get(ss, 'addrs').split(): |
c7fb640e IJ |
514 | sa = ServerAddr(c.port, addrspec) |
515 | c.saddrs.append(sa) | |
516 | ||
517 | def cfg_process_vnetwork(c, ss): | |
300fe4ed | 518 | c.vnetwork = ipnetwork(cfg1get(ss,'vnetwork')) |
c7f134ce IJ |
519 | if c.vnetwork.num_addresses < 3 + 2: |
520 | raise ValueError('vnetwork needs at least 2^3 addresses') | |
88487243 | 521 | |
8d374606 | 522 | def cfg_process_vaddr(c, ss): |
88487243 | 523 | try: |
300fe4ed | 524 | c.vaddr = cfg1get(ss,'vaddr') |
88487243 | 525 | except NoOptionError: |
8d374606 | 526 | cfg_process_vnetwork(c, ss) |
c7f134ce | 527 | c.vaddr = next(c.vnetwork.hosts()) |
88487243 | 528 | |
c7fb640e IJ |
529 | def cfg_search_section(key,sections): |
530 | for section in sections: | |
531 | if cfg.has_option(section, key): | |
532 | return section | |
8d374606 | 533 | raise NoOptionError(key, repr(sections)) |
c7fb640e | 534 | |
fa63bd93 IJ |
535 | def cfg_get_raw(*args, **kwargs): |
536 | # for passing to cfg_search | |
537 | return cfg.get(*args, raw=True, **kwargs) | |
538 | ||
c7fb640e IJ |
539 | def cfg_search(getter,key,sections): |
540 | section = cfg_search_section(key,sections) | |
541 | return getter(section, key) | |
542 | ||
71f9ddb6 IJ |
543 | def cfg1get(section,key, getter=cfg.get,**kwargs): |
544 | section = cfg_search_section(key,[section,'COMMON']) | |
545 | return getter(section,key,**kwargs) | |
300fe4ed | 546 | |
71f9ddb6 IJ |
547 | def cfg1getint(section,key, **kwargs): |
548 | return cfg1get(section,key, getter=cfg.getint,**kwargs); | |
300fe4ed | 549 | |
c7fb640e | 550 | def cfg_process_client_limited(cc,ss,sections,key): |
300fe4ed IJ |
551 | val = cfg_search(cfg1getint, key, sections) |
552 | lim = cfg_search(cfg1getint, key, ['%s LIMIT' % ss, 'LIMIT']) | |
c7fb640e IJ |
553 | cc.__dict__[key] = min(val,lim) |
554 | ||
555 | def cfg_process_client_common(cc,ss,cs,ci): | |
dce21e00 | 556 | # returns sections to search in, iff secret is defined, otherwise None |
c7fb640e IJ |
557 | cc.ci = ci |
558 | ||
8d374606 | 559 | sections = ['%s %s' % (ss,cs), |
c7fb640e IJ |
560 | cs, |
561 | ss, | |
71f9ddb6 | 562 | 'COMMON'] |
c7fb640e | 563 | |
dce21e00 | 564 | try: pwsection = cfg_search_section('secret', sections) |
c7fb640e | 565 | except NoOptionError: return None |
88487243 | 566 | |
dce21e00 IJ |
567 | pw = cfg1get(pwsection, 'secret') |
568 | cc.secret = pw.encode('utf-8') | |
88487243 | 569 | |
c7fb640e IJ |
570 | cfg_process_client_limited(cc,ss,sections,'target_requests_outstanding') |
571 | cfg_process_client_limited(cc,ss,sections,'http_timeout') | |
88487243 | 572 | |
c7fb640e IJ |
573 | return sections |
574 | ||
8d374606 | 575 | def cfg_process_ipif(c, sections, varmap): |
c7fb640e IJ |
576 | for d, s in varmap: |
577 | try: v = getattr(c, s) | |
578 | except AttributeError: continue | |
579 | setattr(c, d, v) | |
580 | ||
c7f134ce | 581 | #print('CFGIPIF',repr((varmap, sections, c.__dict__)),file=sys.stderr) |
8d374606 | 582 | |
c7fb640e | 583 | section = cfg_search_section('ipif', sections) |
300fe4ed | 584 | c.ipif_command = cfg1get(section,'ipif', vars=c.__dict__) |
88487243 | 585 | |
ae7c7784 IJ |
586 | #---------- startup ---------- |
587 | ||
8c771381 IJ |
588 | def log_debug_config(m): |
589 | if not DBG.CONFIG in debug_set: return | |
590 | print('DBG.CONFIG:', m) | |
591 | ||
5510890e | 592 | def common_startup(process_cfg): |
c7fb640e IJ |
593 | # calls process_cfg(putative_clients, putative_servers) |
594 | ||
82302bac | 595 | # ConfigParser hates #-comments after values |
c7fb640e | 596 | trailingcomments_re = regexp.compile(r'#.*') |
82302bac | 597 | cfg.read_string(trailingcomments_re.sub('', defcfg)) |
cae50358 IJ |
598 | need_defcfg = True |
599 | ||
600 | def readconfig(pathname, mandatory=True): | |
601 | def log(m, p=pathname): | |
602 | if not DBG.CONFIG in debug_set: return | |
00ea5443 | 603 | log_debug_config('%s: %s' % (m, p)) |
cae50358 IJ |
604 | |
605 | try: | |
606 | files = os.listdir(pathname) | |
607 | ||
608 | except FileNotFoundError: | |
609 | if mandatory: raise | |
610 | log('skipped') | |
611 | return | |
612 | ||
613 | except NotADirectoryError: | |
614 | cfg.read(pathname) | |
615 | log('read file') | |
616 | return | |
617 | ||
618 | # is a directory | |
619 | log('directory') | |
620 | re = regexp.compile('[^-A-Za-z0-9_]') | |
2b13e1cc | 621 | for f in os.listdir(pathname): |
cae50358 IJ |
622 | if re.search(f): continue |
623 | subpath = pathname + '/' + f | |
624 | try: | |
625 | os.stat(subpath) | |
626 | except FileNotFoundError: | |
627 | log('entry skipped', subpath) | |
628 | continue | |
629 | cfg.read(subpath) | |
630 | log('entry read', subpath) | |
631 | ||
632 | def oc_config(od,os, value, op): | |
633 | nonlocal need_defcfg | |
634 | need_defcfg = False | |
635 | readconfig(value) | |
2e68eb10 | 636 | |
26f04eff IJ |
637 | def oc_extra_config(od,os, value, op): |
638 | readconfig(value) | |
639 | ||
7852bfc8 IJ |
640 | def read_defconfig(): |
641 | readconfig('/etc/hippotat/config.d', False) | |
dce21e00 | 642 | readconfig('/etc/hippotat/secrets.d', False) |
7852bfc8 IJ |
643 | readconfig('/etc/hippotat/master.cfg', False) |
644 | ||
26f04eff IJ |
645 | def oc_defconfig(od,os, value, op): |
646 | nonlocal need_defcfg | |
647 | need_defcfg = False | |
648 | read_defconfig(value) | |
649 | ||
9acb0eca IJ |
650 | def dfs_less_detailed(dl): |
651 | return [df for df in DBG.iterconstants() if df <= dl] | |
652 | ||
653 | def ds_default(od,os,dl,op): | |
2e68eb10 | 654 | global debug_set |
ff0fc3fa IJ |
655 | debug_set.clear |
656 | debug_set |= set(dfs_less_detailed(debug_def_detail)) | |
2e68eb10 | 657 | |
9acb0eca | 658 | def ds_select(od,os, spec, op): |
9acb0eca IJ |
659 | for it in spec.split(','): |
660 | ||
9acb0eca IJ |
661 | if it.startswith('-'): |
662 | mutator = debug_set.discard | |
663 | it = it[1:] | |
664 | else: | |
665 | mutator = debug_set.add | |
2cf75145 IJ |
666 | |
667 | if it == '+': | |
668 | dfs = DBG.iterconstants() | |
669 | ||
670 | else: | |
671 | if it.endswith('+'): | |
672 | mapper = dfs_less_detailed | |
673 | it = it[0:len(it)-1] | |
674 | else: | |
675 | mapper = lambda x: [x] | |
676 | ||
677 | try: | |
678 | dfspec = DBG.lookupByName(it) | |
679 | except ValueError: | |
680 | optparser.error('unknown debug flag %s in --debug-select' % it) | |
681 | ||
682 | dfs = mapper(dfspec) | |
683 | ||
684 | for df in dfs: | |
685 | mutator(df) | |
9acb0eca IJ |
686 | |
687 | optparser.add_option('-D', '--debug', | |
2e68eb10 IJ |
688 | nargs=0, |
689 | action='callback', | |
9acb0eca IJ |
690 | help='enable default debug (to stdout)', |
691 | callback= ds_default) | |
692 | ||
693 | optparser.add_option('--debug-select', | |
694 | nargs=1, | |
695 | type='string', | |
2cf75145 | 696 | metavar='[-]DFLAG[+]|[-]+,...', |
9acb0eca | 697 | help= |
2cf75145 IJ |
698 | '''enable (`-': disable) each specified DFLAG; |
699 | `+': do same for all "more interesting" DFLAGSs; | |
700 | just `+': all DFLAGs. | |
701 | DFLAGS: ''' + ' '.join([df.name for df in DBG.iterconstants()]), | |
9acb0eca IJ |
702 | action='callback', |
703 | callback= ds_select) | |
2e68eb10 | 704 | |
cae50358 IJ |
705 | optparser.add_option('-c', '--config', |
706 | nargs=1, | |
707 | type='string', | |
708 | metavar='CONFIGFILE', | |
709 | dest='configfile', | |
710 | action='callback', | |
711 | callback= oc_config) | |
712 | ||
26f04eff IJ |
713 | optparser.add_option('--extra-config', |
714 | nargs=1, | |
715 | type='string', | |
716 | metavar='CONFIGFILE', | |
717 | dest='configfile', | |
718 | action='callback', | |
719 | callback= oc_extra_config) | |
720 | ||
721 | optparser.add_option('--default-config', | |
722 | action='callback', | |
723 | callback= oc_defconfig) | |
724 | ||
f022d67f IJ |
725 | (opts, args) = optparser.parse_args() |
726 | if len(args): optparser.error('no non-option arguments please') | |
2e68eb10 | 727 | |
cae50358 | 728 | if need_defcfg: |
7852bfc8 | 729 | read_defconfig() |
9acb0eca | 730 | |
c7fb640e | 731 | try: |
8d374606 | 732 | (pss, pcs) = _cfg_process_putatives() |
1cc6968f | 733 | process_cfg(opts, pss, pcs) |
5510890e IJ |
734 | except (configparser.Error, ValueError): |
735 | traceback.print_exc(file=sys.stderr) | |
736 | print('\nInvalid configuration, giving up.', file=sys.stderr) | |
737 | sys.exit(12) | |
738 | ||
ff0fc3fa IJ |
739 | |
740 | #print('X', debug_set, file=sys.stderr) | |
2e68eb10 | 741 | |
8c3b6620 | 742 | log_formatter = twisted.logger.formatEventAsClassicLogText |
389236df IJ |
743 | stdout_obs = twisted.logger.FileLogObserver(sys.stdout, log_formatter) |
744 | stderr_obs = twisted.logger.FileLogObserver(sys.stderr, log_formatter) | |
745 | pred = twisted.logger.LogLevelFilterPredicate(LogLevel.error) | |
b83d422a | 746 | stdsomething_obs = twisted.logger.FilteringLogObserver( |
389236df IJ |
747 | stderr_obs, [pred], stdout_obs |
748 | ) | |
ec2c9312 IJ |
749 | global file_log_observer |
750 | file_log_observer = twisted.logger.FilteringLogObserver( | |
b83d422a IJ |
751 | stdsomething_obs, [LogNotBoringTwisted()] |
752 | ) | |
753 | #log_observer = stdsomething_obs | |
8c3b6620 | 754 | twisted.logger.globalLogBeginner.beginLoggingTo( |
ec2c9312 | 755 | [ file_log_observer, crash_on_critical ] |
8c3b6620 | 756 | ) |
ae7c7784 | 757 | |
ae7c7784 | 758 | def common_run(): |
b68c0739 IJ |
759 | log_debug(DBG.INIT, 'entering reactor') |
760 | if not _crashing: reactor.run() | |
2eecd19c | 761 | print('ENDED', file=sys.stderr) |
207f5042 | 762 | sys.exit(16) |