0000: 23 21 2f 75 73 72 2f 62 69 6e 2f 65 6e 76 20 70 #!/usr/bin/env p
0010: 79 74 68 6f 6e 33 2e 31 0a 0a 69 6d 70 6f 72 74 ython3.1..import
0020: 20 63 6f 6e 66 69 67 70 61 72 73 65 72 2c 20 6f configparser, o
0030: 70 74 70 61 72 73 65 2c 20 6f 73 2c 20 70 6f 73 ptparse, os, pos
0040: 74 67 72 65 73 71 6c 2e 61 70 69 2c 20 72 65 2c tgresql.api, re,
0050: 20 73 79 73 2c 20 5f 74 68 72 65 61 64 0a 0a 23 sys, _thread..#
0060: 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 wrapper around
0070: 73 79 73 6c 6f 67 2c 20 63 61 6e 20 62 65 20 6d syslog, can be m
0080: 75 74 65 64 0a 63 6c 61 73 73 20 4c 6f 67 67 65 uted.class Logge
0090: 72 3a 0a 09 5f 5f 73 6c 6f 74 73 5f 5f 20 3d 20 r:..__slots__ =
00a0: 66 72 6f 7a 65 6e 73 65 74 28 5b 27 5f 73 79 73 frozenset(['_sys
00b0: 6c 6f 67 27 5d 29 0a 0a 09 64 65 66 20 5f 5f 69 log'])...def __i
00c0: 6e 69 74 5f 5f 28 73 65 6c 66 29 3a 0a 09 09 63 nit__(self):...c
00d0: 6f 6e 66 69 67 2e 73 65 63 74 69 6f 6e 28 27 6c onfig.section('l
00e0: 6f 67 27 29 0a 09 09 69 66 20 63 6f 6e 66 69 67 og')...if config
00f0: 5b 27 73 69 6c 65 6e 74 27 5d 20 3d 3d 20 27 79 ['silent'] == 'y
0100: 65 73 27 3a 0a 09 09 09 73 65 6c 66 2e 5f 73 79 es':....self._sy
0110: 73 6c 6f 67 20 3d 20 4e 6f 6e 65 0a 09 09 65 6c slog = None...el
0120: 73 65 3a 0a 09 09 09 69 6d 70 6f 72 74 20 73 79 se:....import sy
0130: 73 6c 6f 67 0a 09 09 09 73 65 6c 66 2e 5f 73 79 slog....self._sy
0140: 73 6c 6f 67 20 3d 20 73 79 73 6c 6f 67 0a 09 09 slog = syslog...
0150: 09 73 65 6c 66 2e 5f 73 79 73 6c 6f 67 2e 6f 70 .self._syslog.op
0160: 65 6e 6c 6f 67 28 27 73 71 75 69 64 54 61 67 27 enlog('squidTag'
0170: 29 0a 0a 09 64 65 66 20 69 6e 66 6f 28 73 65 6c )...def info(sel
0180: 66 2c 20 6d 65 73 73 61 67 65 29 3a 0a 09 09 69 f, message):...i
0190: 66 20 73 65 6c 66 2e 5f 73 79 73 6c 6f 67 3a 0a f self._syslog:.
01a0: 09 09 09 73 65 6c 66 2e 5f 73 79 73 6c 6f 67 2e ...self._syslog.
01b0: 73 79 73 6c 6f 67 28 73 65 6c 66 2e 5f 73 79 73 syslog(self._sys
01c0: 6c 6f 67 2e 4c 4f 47 5f 49 4e 46 4f 2c 20 6d 65 log.LOG_INFO, me
01d0: 73 73 61 67 65 29 0a 0a 09 64 65 66 20 6e 6f 74 ssage)...def not
01e0: 69 63 65 28 73 65 6c 66 2c 20 6d 65 73 73 61 67 ice(self, messag
01f0: 65 29 3a 0a 09 09 69 66 20 73 65 6c 66 2e 5f 73 e):...if self._s
0200: 79 73 6c 6f 67 3a 0a 09 09 09 73 65 6c 66 2e 5f yslog:....self._
0210: 73 79 73 6c 6f 67 2e 73 79 73 6c 6f 67 28 73 65 syslog.syslog(se
0220: 6c 66 2e 5f 73 79 73 6c 6f 67 2e 4c 4f 47 5f 4e lf._syslog.LOG_N
0230: 4f 54 49 43 45 2c 20 6d 65 73 73 61 67 65 29 0a OTICE, message).
0240: 0a 23 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e .# wrapper aroun
0250: 64 20 64 61 74 61 62 61 73 65 0a 63 6c 61 73 73 d database.class
0260: 20 74 61 67 44 42 3a 0a 09 5f 5f 73 6c 6f 74 73 tagDB:..__slots
0270: 5f 5f 20 3d 20 66 72 6f 7a 65 6e 73 65 74 28 28 __ = frozenset((
0280: 27 5f 63 68 65 63 6b 5f 73 74 6d 74 27 2c 20 27 '_check_stmt', '
0290: 5f 64 62 27 29 29 0a 0a 09 64 65 66 20 5f 5f 69 _db'))...def __i
02a0: 6e 69 74 5f 5f 28 73 65 6c 66 29 3a 0a 09 09 63 nit__(self):...c
02b0: 6f 6e 66 69 67 2e 73 65 63 74 69 6f 6e 28 27 64 onfig.section('d
02c0: 61 74 61 62 61 73 65 27 29 0a 09 09 73 65 6c 66 atabase')...self
02d0: 2e 5f 64 62 20 3d 20 70 6f 73 74 67 72 65 73 71 ._db = postgresq
02e0: 6c 2e 6f 70 65 6e 28 0a 09 09 09 27 70 71 3a 2f l.open(....'pq:/
02f0: 2f 7b 7d 3a 7b 7d 40 7b 7d 2f 7b 7d 27 2e 66 6f /{}:{}@{}/{}'.fo
0300: 72 6d 61 74 28 0a 09 09 09 09 63 6f 6e 66 69 67 rmat(.....config
0310: 5b 27 75 73 65 72 27 5d 2c 0a 09 09 09 09 63 6f ['user'],.....co
0320: 6e 66 69 67 5b 27 70 61 73 73 77 6f 72 64 27 5d nfig['password']
0330: 2c 0a 09 09 09 09 63 6f 6e 66 69 67 5b 27 68 6f ,.....config['ho
0340: 73 74 27 5d 2c 0a 09 09 09 09 63 6f 6e 66 69 67 st'],.....config
0350: 5b 27 64 61 74 61 62 61 73 65 27 5d 2c 0a 09 09 ['database'],...
0360: 29 20 29 0a 09 09 73 65 6c 66 2e 5f 63 68 65 63 ) )...self._chec
0370: 6b 5f 73 74 6d 74 20 3d 20 73 65 6c 66 2e 5f 64 k_stmt = self._d
0380: 62 2e 70 72 65 70 61 72 65 28 22 73 65 6c 65 63 b.prepare("selec
0390: 74 20 72 65 64 69 72 65 63 74 5f 75 72 6c 2c 20 t redirect_url,
03a0: 72 65 67 65 78 70 20 66 72 6f 6d 20 73 69 74 65 regexp from site
03b0: 5f 72 75 6c 65 20 77 68 65 72 65 20 73 69 74 65 _rule where site
03c0: 20 3c 40 20 74 72 69 70 64 6f 6d 61 69 6e 28 24 <@ tripdomain($
03d0: 31 29 20 61 6e 64 20 6e 65 74 6d 61 73 6b 20 3e 1) and netmask >
03e0: 3e 20 24 32 3a 3a 74 65 78 74 3a 3a 69 6e 65 74 > $2::text::inet
03f0: 20 6f 72 64 65 72 20 62 79 20 61 72 72 61 79 5f order by array_
0400: 6c 65 6e 67 74 68 28 73 69 74 65 2c 20 31 29 20 length(site, 1)
0410: 64 65 73 63 22 29 0a 0a 09 64 65 66 20 63 68 65 desc")...def che
0420: 63 6b 28 73 65 6c 66 2c 20 73 69 74 65 2c 20 69 ck(self, site, i
0430: 70 5f 61 64 64 72 65 73 73 29 3a 0a 09 09 72 65 p_address):...re
0440: 74 75 72 6e 28 73 65 6c 66 2e 5f 63 68 65 63 6b turn(self._check
0450: 5f 73 74 6d 74 28 73 69 74 65 2c 20 69 70 5f 61 _stmt(site, ip_a
0460: 64 64 72 65 73 73 29 29 0a 0a 23 20 61 62 73 74 ddress))..# abst
0470: 72 61 63 74 20 63 6c 61 73 73 20 77 69 74 68 20 ract class with
0480: 62 61 73 69 63 20 63 68 65 63 6b 69 6e 67 20 66 basic checking f
0490: 75 6e 63 74 69 6f 6e 61 6c 69 74 79 0a 63 6c 61 unctionality.cla
04a0: 73 73 20 43 68 65 63 6b 65 72 3a 0a 09 5f 5f 73 ss Checker:..__s
04b0: 6c 6f 74 73 5f 5f 20 3d 20 66 72 6f 7a 65 6e 73 lots__ = frozens
04c0: 65 74 28 5b 27 5f 64 62 27 2c 20 27 5f 6c 6f 67 et(['_db', '_log
04d0: 27 5d 29 0a 0a 09 64 65 66 20 5f 5f 69 6e 69 74 '])...def __init
04e0: 5f 5f 28 73 65 6c 66 29 3a 0a 09 09 73 65 6c 66 __(self):...self
04f0: 2e 5f 64 62 20 3d 20 74 61 67 44 42 28 29 0a 09 ._db = tagDB()..
0500: 09 73 65 6c 66 2e 5f 6c 6f 67 20 3d 20 4c 6f 67 .self._log = Log
0510: 67 65 72 28 29 0a 09 09 73 65 6c 66 2e 5f 6c 6f ger()...self._lo
0520: 67 2e 69 6e 66 6f 28 27 73 74 61 72 74 65 64 5c g.info('started\
0530: 6e 27 29 0a 0a 09 64 65 66 20 70 72 6f 63 65 73 n')...def proces
0540: 73 28 73 65 6c 66 2c 20 69 64 2c 20 73 69 74 65 s(self, id, site
0550: 2c 20 69 70 5f 61 64 64 72 65 73 73 2c 20 75 72 , ip_address, ur
0560: 6c 5f 70 61 74 68 2c 20 6c 69 6e 65 20 3d 20 4e l_path, line = N
0570: 6f 6e 65 29 3a 0a 09 09 73 65 6c 66 2e 5f 6c 6f one):...self._lo
0580: 67 2e 69 6e 66 6f 28 27 74 72 79 69 6e 67 20 7b g.info('trying {
0590: 7d 5c 6e 27 2e 66 6f 72 6d 61 74 28 73 69 74 65 }\n'.format(site
05a0: 29 29 0a 09 09 72 65 73 75 6c 74 20 3d 20 73 65 ))...result = se
05b0: 6c 66 2e 5f 64 62 2e 63 68 65 63 6b 28 73 69 74 lf._db.check(sit
05c0: 65 2c 20 69 70 5f 61 64 64 72 65 73 73 29 0a 09 e, ip_address)..
05d0: 09 23 72 65 70 6c 79 20 3d 20 27 7b 7d 3a 2f 2f .#reply = '{}://
05e0: 7b 7d 2f 7b 7d 27 2e 66 6f 72 6d 61 74 28 72 65 {}/{}'.format(re
05f0: 71 5b 34 5d 2c 20 72 65 71 5b 31 5d 2c 20 72 65 q[4], req[1], re
0600: 71 5b 33 5d 29 0a 09 09 72 65 70 6c 79 20 3d 20 q[3])...reply =
0610: 27 2d 27 0a 09 09 66 6f 72 20 72 6f 77 20 69 6e '-'...for row in
0620: 20 72 65 73 75 6c 74 3a 0a 09 09 09 69 66 20 72 result:....if r
0630: 6f 77 20 21 3d 20 4e 6f 6e 65 20 61 6e 64 20 72 ow != None and r
0640: 6f 77 5b 30 5d 20 21 3d 20 4e 6f 6e 65 3a 0a 09 ow[0] != None:..
0650: 09 09 09 69 66 20 72 6f 77 5b 31 5d 20 21 3d 20 ...if row[1] !=
0660: 4e 6f 6e 65 3a 0a 09 09 09 09 09 73 65 6c 66 2e None:......self.
0670: 5f 6c 6f 67 2e 69 6e 66 6f 28 27 74 72 79 69 6e _log.info('tryin
0680: 67 20 72 65 67 65 78 70 20 22 7b 7d 22 20 76 65 g regexp "{}" ve
0690: 72 73 75 73 20 22 7b 7d 22 5c 6e 27 2e 66 6f 72 rsus "{}"\n'.for
06a0: 6d 61 74 28 72 6f 77 5b 31 5d 2c 20 75 72 6c 5f mat(row[1], url_
06b0: 70 61 74 68 29 29 0a 09 09 09 09 09 74 72 79 3a path))......try:
06c0: 0a 09 09 09 09 09 09 69 66 20 72 65 2e 63 6f 6d .......if re.com
06d0: 70 69 6c 65 28 72 6f 77 5b 31 5d 29 2e 6d 61 74 pile(row[1]).mat
06e0: 63 68 28 75 72 6c 5f 70 61 74 68 29 3a 0a 09 09 ch(url_path):...
06f0: 09 09 09 09 09 72 65 70 6c 79 20 3d 20 72 6f 77 .....reply = row
0700: 5b 30 5d 2e 66 6f 72 6d 61 74 28 75 72 6c 5f 70 [0].format(url_p
0710: 61 74 68 29 0a 09 09 09 09 09 09 09 62 72 65 61 ath)........brea
0720: 6b 0a 09 09 09 09 09 09 65 6c 73 65 3a 0a 09 09 k.......else:...
0730: 09 09 09 09 09 63 6f 6e 74 69 6e 75 65 0a 09 09 .....continue...
0740: 09 09 09 65 78 63 65 70 74 3a 0a 09 09 09 09 09 ...except:......
0750: 09 73 65 6c 66 2e 5f 6c 6f 67 2e 69 6e 66 6f 28 .self._log.info(
0760: 22 63 61 6e 27 74 20 63 6f 6d 70 69 6c 65 20 72 "can't compile r
0770: 65 67 65 78 70 22 29 0a 09 09 09 09 65 6c 73 65 egexp").....else
0780: 3a 0a 09 09 09 09 09 72 65 70 6c 79 20 3d 20 72 :......reply = r
0790: 6f 77 5b 30 5d 2e 66 6f 72 6d 61 74 28 75 72 6c ow[0].format(url
07a0: 5f 70 61 74 68 29 0a 09 09 09 09 09 62 72 65 61 _path)......brea
07b0: 6b 0a 09 09 73 65 6c 66 2e 77 72 69 74 65 6c 69 k...self.writeli
07c0: 6e 65 28 27 7b 7d 20 7b 7d 5c 6e 27 2e 66 6f 72 ne('{} {}\n'.for
07d0: 6d 61 74 28 69 64 2c 20 72 65 70 6c 79 29 29 0a mat(id, reply)).
07e0: 0a 09 64 65 66 20 63 68 65 63 6b 28 73 65 6c 66 ..def check(self
07f0: 2c 20 6c 69 6e 65 29 3a 0a 09 09 72 65 71 75 65 , line):...reque
0800: 73 74 20 3d 20 72 65 2e 63 6f 6d 70 69 6c 65 28 st = re.compile(
0810: 27 5e 28 5b 30 2d 39 5d 2b 29 5c 20 28 68 74 74 '^([0-9]+)\ (htt
0820: 70 7c 66 74 70 29 3a 5c 2f 5c 2f 28 5b 2d 5c 77 p|ftp):\/\/([-\w
0830: 2e 3a 5d 2b 29 5c 2f 28 5b 5e 20 5d 2a 29 5c 20 .:]+)\/([^ ]*)\
0840: 28 5b 30 2d 39 2e 5d 2b 29 5c 2f 28 2d 7c 5b 5c ([0-9.]+)\/(-|[\
0850: 77 5c 2e 5d 2b 29 5c 20 28 2d 7c 5c 77 2b 29 5c w\.]+)\ (-|\w+)\
0860: 20 28 2d 7c 47 45 54 7c 48 45 41 44 7c 50 4f 53 (-|GET|HEAD|POS
0870: 54 29 2e 2a 24 27 29 2e 6d 61 74 63 68 28 6c 69 T).*$').match(li
0880: 6e 65 29 0a 09 09 69 66 20 72 65 71 75 65 73 74 ne)...if request
0890: 3a 0a 09 09 09 69 64 20 3d 20 72 65 71 75 65 73 :....id = reques
08a0: 74 2e 67 72 6f 75 70 28 31 29 0a 09 09 09 23 70 t.group(1)....#p
08b0: 72 6f 74 6f 20 3d 20 72 65 71 75 65 73 74 2e 67 roto = request.g
08c0: 72 6f 75 70 28 32 29 0a 09 09 09 73 69 74 65 20 roup(2)....site
08d0: 3d 20 72 65 71 75 65 73 74 2e 67 72 6f 75 70 28 = request.group(
08e0: 33 29 0a 09 09 09 75 72 6c 5f 70 61 74 68 20 3d 3)....url_path =
08f0: 20 72 65 71 75 65 73 74 2e 67 72 6f 75 70 28 34 request.group(4
0900: 29 0a 09 09 09 69 70 5f 61 64 64 72 65 73 73 20 )....ip_address
0910: 3d 20 72 65 71 75 65 73 74 2e 67 72 6f 75 70 28 = request.group(
0920: 35 29 0a 09 09 09 73 65 6c 66 2e 70 72 6f 63 65 5)....self.proce
0930: 73 73 28 69 64 2c 20 73 69 74 65 2c 20 69 70 5f ss(id, site, ip_
0940: 61 64 64 72 65 73 73 2c 20 75 72 6c 5f 70 61 74 address, url_pat
0950: 68 2c 20 6c 69 6e 65 29 0a 09 09 09 72 65 74 75 h, line)....retu
0960: 72 6e 28 54 72 75 65 29 0a 09 09 65 6c 73 65 3a rn(True)...else:
0970: 0a 09 09 09 73 65 6c 66 2e 5f 6c 6f 67 2e 69 6e ....self._log.in
0980: 66 6f 28 27 62 61 64 20 72 65 71 75 65 73 74 5c fo('bad request\
0990: 6e 27 29 0a 09 09 09 73 65 6c 66 2e 77 72 69 74 n')....self.writ
09a0: 65 6c 69 6e 65 28 6c 69 6e 65 29 0a 09 09 09 72 eline(line)....r
09b0: 65 74 75 72 6e 28 46 61 6c 73 65 29 0a 0a 09 64 eturn(False)...d
09c0: 65 66 20 77 72 69 74 65 6c 69 6e 65 28 73 65 6c ef writeline(sel
09d0: 66 2c 20 73 74 72 69 6e 67 29 3a 0a 09 09 73 65 f, string):...se
09e0: 6c 66 2e 5f 6c 6f 67 2e 69 6e 66 6f 28 27 73 65 lf._log.info('se
09f0: 6e 64 69 6e 67 3a 20 27 20 2b 20 73 74 72 69 6e nding: ' + strin
0a00: 67 29 0a 09 09 73 79 73 2e 73 74 64 6f 75 74 2e g)...sys.stdout.
0a10: 77 72 69 74 65 28 73 74 72 69 6e 67 29 0a 09 09 write(string)...
0a20: 73 79 73 2e 73 74 64 6f 75 74 2e 66 6c 75 73 68 sys.stdout.flush
0a30: 28 29 0a 0a 09 64 65 66 20 6c 6f 6f 70 28 73 65 ()...def loop(se
0a40: 6c 66 29 3a 0a 09 09 77 68 69 6c 65 20 54 72 75 lf):...while Tru
0a50: 65 3a 0a 09 09 09 6c 69 6e 65 20 3d 20 73 79 73 e:....line = sys
0a60: 2e 73 74 64 69 6e 2e 72 65 61 64 6c 69 6e 65 28 .stdin.readline(
0a70: 29 0a 09 09 09 69 66 20 6c 65 6e 28 6c 69 6e 65 )....if len(line
0a80: 29 20 3d 3d 20 30 3a 0a 09 09 09 09 62 72 65 61 ) == 0:.....brea
0a90: 6b 0a 09 09 09 73 65 6c 66 2e 63 68 65 63 6b 28 k....self.check(
0aa0: 6c 69 6e 65 29 0a 0a 23 20 74 68 72 65 61 64 65 line)..# threade
0ab0: 64 20 63 68 65 63 6b 69 6e 67 20 66 61 63 69 6c d checking facil
0ac0: 69 74 79 0a 63 6c 61 73 73 20 43 68 65 63 6b 65 ity.class Checke
0ad0: 72 54 68 72 65 61 64 28 43 68 65 63 6b 65 72 29 rThread(Checker)
0ae0: 3a 0a 09 5f 5f 73 6c 6f 74 73 5f 5f 20 3d 20 66 :..__slots__ = f
0af0: 72 6f 7a 65 6e 73 65 74 28 5b 27 5f 6c 6f 63 6b rozenset(['_lock
0b00: 27 2c 20 27 5f 6c 6f 63 6b 5f 65 78 69 74 27 2c ', '_lock_exit',
0b10: 20 27 5f 6c 6f 63 6b 5f 71 75 65 75 65 27 2c 20 '_lock_queue',
0b20: 27 5f 71 75 65 75 65 27 5d 29 0a 0a 09 64 65 66 '_queue'])...def
0b30: 20 5f 5f 69 6e 69 74 5f 5f 28 73 65 6c 66 29 3a __init__(self):
0b40: 0a 09 09 23 20 62 61 73 69 63 20 69 6e 69 74 69 ...# basic initi
0b50: 61 6c 69 73 61 74 69 6f 6e 0a 09 09 43 68 65 63 alisation...Chec
0b60: 6b 65 72 2e 5f 5f 69 6e 69 74 5f 5f 28 73 65 6c ker.__init__(sel
0b70: 66 29 0a 0a 09 09 23 20 53 70 69 6e 20 6c 6f 63 f)....# Spin loc
0b80: 6b 2e 20 4c 6f 6f 70 20 61 63 71 75 69 72 65 73 k. Loop acquires
0b90: 20 69 74 20 6f 6e 20 73 74 61 72 74 20 74 68 65 it on start the
0ba0: 6e 20 72 65 6c 65 61 73 65 73 20 69 74 20 77 68 n releases it wh
0bb0: 65 6e 20 68 6f 6c 64 69 6e 67 20 71 75 65 75 65 en holding queue
0bc0: 0a 09 09 23 20 6c 6f 63 6b 2e 20 54 68 69 73 20 ...# lock. This
0bd0: 77 61 79 20 74 68 65 20 74 68 72 65 61 64 20 70 way the thread p
0be0: 72 6f 63 65 65 64 73 20 77 69 74 68 6f 75 74 20 roceeds without
0bf0: 73 74 6f 70 73 20 77 68 69 6c 65 20 71 75 65 75 stops while queu
0c00: 65 20 68 61 73 20 64 61 74 61 20 61 6e 64 0a 09 e has data and..
0c10: 09 23 20 67 65 74 73 20 73 74 61 6c 6c 65 64 20 .# gets stalled
0c20: 77 68 65 6e 20 6e 6f 20 64 61 74 61 20 70 72 65 when no data pre
0c30: 73 65 6e 74 2e 20 54 68 65 20 6c 6f 63 6b 20 69 sent. The lock i
0c40: 73 20 72 65 6c 65 61 73 65 64 20 62 79 20 71 75 s released by qu
0c50: 65 75 65 20 77 72 69 74 65 72 0a 09 09 23 20 61 eue writer...# a
0c60: 66 74 65 72 20 73 74 6f 72 69 6e 67 20 73 6f 6d fter storing som
0c70: 65 74 68 69 6e 67 20 69 6e 74 6f 20 74 68 65 20 ething into the
0c80: 71 75 65 75 65 0a 09 09 73 65 6c 66 2e 5f 6c 6f queue...self._lo
0c90: 63 6b 20 3d 20 5f 74 68 72 65 61 64 2e 61 6c 6c ck = _thread.all
0ca0: 6f 63 61 74 65 5f 6c 6f 63 6b 28 29 0a 09 09 73 ocate_lock()...s
0cb0: 65 6c 66 2e 5f 6c 6f 63 6b 5f 65 78 69 74 20 3d elf._lock_exit =
0cc0: 20 5f 74 68 72 65 61 64 2e 61 6c 6c 6f 63 61 74 _thread.allocat
0cd0: 65 5f 6c 6f 63 6b 28 29 0a 09 09 73 65 6c 66 2e e_lock()...self.
0ce0: 5f 6c 6f 63 6b 5f 71 75 65 75 65 20 3d 20 5f 74 _lock_queue = _t
0cf0: 68 72 65 61 64 2e 61 6c 6c 6f 63 61 74 65 5f 6c hread.allocate_l
0d00: 6f 63 6b 28 29 0a 09 09 73 65 6c 66 2e 5f 6c 6f ock()...self._lo
0d10: 63 6b 2e 61 63 71 75 69 72 65 28 29 0a 09 09 73 ck.acquire()...s
0d20: 65 6c 66 2e 5f 71 75 65 75 65 20 3d 20 5b 5d 0a elf._queue = [].
0d30: 09 09 5f 74 68 72 65 61 64 2e 73 74 61 72 74 5f .._thread.start_
0d40: 6e 65 77 5f 74 68 72 65 61 64 28 73 65 6c 66 2e new_thread(self.
0d50: 5f 73 74 61 72 74 2c 20 28 29 29 0a 0a 09 64 65 _start, ())...de
0d60: 66 20 5f 73 74 61 72 74 28 73 65 6c 66 29 3a 0a f _start(self):.
0d70: 09 09 77 68 69 6c 65 20 54 72 75 65 3a 0a 09 09 ..while True:...
0d80: 09 73 65 6c 66 2e 5f 6c 6f 63 6b 2e 61 63 71 75 .self._lock.acqu
0d90: 69 72 65 28 29 0a 09 09 09 77 69 74 68 20 73 65 ire()....with se
0da0: 6c 66 2e 5f 6c 6f 63 6b 5f 71 75 65 75 65 3a 0a lf._lock_queue:.
0db0: 09 09 09 09 23 20 79 65 73 20 74 68 69 73 20 73 ....# yes this s
0dc0: 68 6f 75 6c 64 20 62 65 20 77 72 69 74 74 65 6e hould be written
0dd0: 20 74 68 69 73 20 77 61 79 2c 20 61 6e 64 20 79 this way, and y
0de0: 65 73 2c 20 74 68 69 73 20 69 73 20 77 68 79 20 es, this is why
0df0: 49 20 68 61 74 65 20 74 68 72 65 61 64 69 6e 67 I hate threading
0e00: 0a 09 09 09 09 69 66 20 6c 65 6e 28 73 65 6c 66 .....if len(self
0e10: 2e 5f 71 75 65 75 65 29 20 3e 20 31 3a 0a 09 09 ._queue) > 1:...
0e20: 09 09 09 69 66 20 73 65 6c 66 2e 5f 6c 6f 63 6b ...if self._lock
0e30: 2e 6c 6f 63 6b 65 64 28 29 3a 0a 09 09 09 09 09 .locked():......
0e40: 09 73 65 6c 66 2e 5f 6c 6f 63 6b 2e 72 65 6c 65 .self._lock.rele
0e50: 61 73 65 28 29 0a 09 09 09 09 72 65 71 20 3d 20 ase().....req =
0e60: 73 65 6c 66 2e 5f 71 75 65 75 65 2e 70 6f 70 28 self._queue.pop(
0e70: 30 29 0a 09 09 09 43 68 65 63 6b 65 72 2e 70 72 0)....Checker.pr
0e80: 6f 63 65 73 73 28 73 65 6c 66 2c 20 72 65 71 5b ocess(self, req[
0e90: 30 5d 2c 20 72 65 71 5b 31 5d 2c 20 72 65 71 5b 0], req[1], req[
0ea0: 32 5d 2c 20 72 65 71 5b 33 5d 29 0a 09 09 09 77 2], req[3])....w
0eb0: 69 74 68 20 73 65 6c 66 2e 5f 6c 6f 63 6b 5f 71 ith self._lock_q
0ec0: 75 65 75 65 3a 0a 09 09 09 09 69 66 20 6c 65 6e ueue:.....if len
0ed0: 28 73 65 6c 66 2e 5f 71 75 65 75 65 29 20 3d 3d (self._queue) ==
0ee0: 20 30 3a 0a 09 09 09 09 09 69 66 20 73 65 6c 66 0:......if self
0ef0: 2e 5f 6c 6f 63 6b 5f 65 78 69 74 2e 6c 6f 63 6b ._lock_exit.lock
0f00: 65 64 28 29 3a 0a 09 09 09 09 09 09 73 65 6c 66 ed():.......self
0f10: 2e 5f 6c 6f 63 6b 5f 65 78 69 74 2e 72 65 6c 65 ._lock_exit.rele
0f20: 61 73 65 28 29 0a 0a 09 64 65 66 20 70 72 6f 63 ase()...def proc
0f30: 65 73 73 28 73 65 6c 66 2c 20 69 64 2c 20 73 69 ess(self, id, si
0f40: 74 65 2c 20 69 70 5f 61 64 64 72 65 73 73 2c 20 te, ip_address,
0f50: 75 72 6c 5f 70 61 74 68 2c 20 6c 69 6e 65 29 3a url_path, line):
0f60: 0a 09 09 77 69 74 68 20 73 65 6c 66 2e 5f 6c 6f ...with self._lo
0f70: 63 6b 5f 71 75 65 75 65 3a 0a 09 09 09 73 65 6c ck_queue:....sel
0f80: 66 2e 5f 71 75 65 75 65 2e 61 70 70 65 6e 64 28 f._queue.append(
0f90: 28 69 64 2c 20 73 69 74 65 2c 20 69 70 5f 61 64 (id, site, ip_ad
0fa0: 64 72 65 73 73 2c 20 75 72 6c 5f 70 61 74 68 29 dress, url_path)
0fb0: 29 0a 09 09 09 73 65 6c 66 2e 5f 6c 6f 67 2e 69 )....self._log.i
0fc0: 6e 66 6f 28 27 72 65 71 75 65 73 74 20 7b 7d 20 nfo('request {}
0fd0: 71 75 65 75 65 64 20 28 7b 7d 29 5c 6e 27 2e 66 queued ({})\n'.f
0fe0: 6f 72 6d 61 74 28 69 64 2c 20 6c 69 6e 65 29 29 ormat(id, line))
0ff0: 0a 09 09 09 69 66 20 6e 6f 74 20 73 65 6c 66 2e ....if not self.
1000: 5f 6c 6f 63 6b 5f 65 78 69 74 2e 6c 6f 63 6b 65 _lock_exit.locke
1010: 64 28 29 3a 0a 09 09 09 09 73 65 6c 66 2e 5f 6c d():.....self._l
1020: 6f 63 6b 5f 65 78 69 74 2e 61 63 71 75 69 72 65 ock_exit.acquire
1030: 28 29 0a 09 09 09 69 66 20 73 65 6c 66 2e 5f 6c ()....if self._l
1040: 6f 63 6b 2e 6c 6f 63 6b 65 64 28 29 3a 0a 09 09 ock.locked():...
1050: 09 09 73 65 6c 66 2e 5f 6c 6f 63 6b 2e 72 65 6c ..self._lock.rel
1060: 65 61 73 65 28 29 0a 0a 09 64 65 66 20 6c 6f 6f ease()...def loo
1070: 70 28 73 65 6c 66 29 3a 0a 09 09 77 68 69 6c 65 p(self):...while
1080: 20 54 72 75 65 3a 0a 09 09 09 6c 69 6e 65 20 3d True:....line =
1090: 20 73 79 73 2e 73 74 64 69 6e 2e 72 65 61 64 6c sys.stdin.readl
10a0: 69 6e 65 28 29 0a 09 09 09 69 66 20 6c 65 6e 28 ine()....if len(
10b0: 6c 69 6e 65 29 20 3d 3d 20 30 3a 0a 09 09 09 09 line) == 0:.....
10c0: 62 72 65 61 6b 0a 09 09 09 73 65 6c 66 2e 63 68 break....self.ch
10d0: 65 63 6b 28 6c 69 6e 65 29 0a 09 09 73 65 6c 66 eck(line)...self
10e0: 2e 5f 6c 6f 63 6b 5f 65 78 69 74 2e 61 63 71 75 ._lock_exit.acqu
10f0: 69 72 65 28 29 0a 0a 23 20 6b 71 75 65 75 65 20 ire()..# kqueue
1100: 65 6e 61 62 6c 65 64 20 63 6c 61 73 73 20 66 6f enabled class fo
1110: 72 20 42 53 44 27 73 0a 63 6c 61 73 73 20 43 68 r BSD's.class Ch
1120: 65 63 6b 65 72 4b 71 75 65 75 65 28 43 68 65 63 eckerKqueue(Chec
1130: 6b 65 72 29 3a 0a 09 5f 5f 73 6c 6f 74 73 5f 5f ker):..__slots__
1140: 20 3d 20 66 72 6f 7a 65 6e 73 65 74 28 5b 27 5f = frozenset(['_
1150: 6b 71 27 2c 20 27 5f 73 65 6c 65 63 74 27 2c 20 kq', '_select',
1160: 27 5f 71 75 65 75 65 27 5d 29 0a 0a 09 64 65 66 '_queue'])...def
1170: 20 5f 5f 69 6e 69 74 5f 5f 28 73 65 6c 66 29 3a __init__(self):
1180: 0a 09 09 23 20 62 61 73 69 63 20 69 6e 69 74 69 ...# basic initi
1190: 61 6c 69 73 61 74 69 6f 6e 0a 09 09 43 68 65 63 alisation...Chec
11a0: 6b 65 72 2e 5f 5f 69 6e 69 74 5f 5f 28 73 65 6c ker.__init__(sel
11b0: 66 29 0a 0a 09 09 23 20 69 6d 70 6f 72 74 69 6e f)....# importin
11c0: 67 20 73 65 6c 65 63 74 20 6d 6f 64 75 6c 65 0a g select module.
11d0: 09 09 69 6d 70 6f 72 74 20 73 65 6c 65 63 74 0a ..import select.
11e0: 09 09 73 65 6c 66 2e 5f 73 65 6c 65 63 74 20 3d ..self._select =
11f0: 20 73 65 6c 65 63 74 0a 0a 09 09 23 20 6b 72 65 select....# kre
1200: 61 74 69 6e 67 20 6b 71 75 65 75 65 0a 09 09 73 ating kqueue...s
1210: 65 6c 66 2e 5f 6b 71 20 3d 20 73 65 6c 66 2e 5f elf._kq = self._
1220: 73 65 6c 65 63 74 2e 6b 71 75 65 75 65 28 29 0a select.kqueue().
1230: 09 09 61 73 73 65 72 74 20 73 65 6c 66 2e 5f 6b ..assert self._k
1240: 71 2e 66 69 6c 65 6e 6f 28 29 20 21 3d 20 2d 31 q.fileno() != -1
1250: 2c 20 22 46 61 74 61 6c 20 65 72 72 6f 72 3a 20 , "Fatal error:
1260: 63 61 6e 27 74 20 69 6e 69 74 69 61 6c 69 73 65 can't initialise
1270: 20 6b 71 75 65 75 65 2e 22 0a 0a 09 09 23 20 77 kqueue."....# w
1280: 61 74 63 68 69 6e 67 20 73 79 73 2e 73 74 64 69 atching sys.stdi
1290: 6e 20 66 6f 72 20 64 61 74 61 0a 09 09 73 65 6c n for data...sel
12a0: 66 2e 5f 6b 71 2e 63 6f 6e 74 72 6f 6c 28 5b 73 f._kq.control([s
12b0: 65 6c 66 2e 5f 73 65 6c 65 63 74 2e 6b 65 76 65 elf._select.keve
12c0: 6e 74 28 73 79 73 2e 73 74 64 69 6e 2c 20 73 65 nt(sys.stdin, se
12d0: 6c 66 2e 5f 73 65 6c 65 63 74 2e 4b 51 5f 46 49 lf._select.KQ_FI
12e0: 4c 54 45 52 5f 52 45 41 44 2c 20 73 65 6c 66 2e LTER_READ, self.
12f0: 5f 73 65 6c 65 63 74 2e 4b 51 5f 45 56 5f 41 44 _select.KQ_EV_AD
1300: 44 29 5d 2c 20 30 29 0a 0a 09 09 23 20 63 72 65 D)], 0)....# cre
1310: 61 74 69 6e 67 20 64 61 74 61 20 71 75 65 75 65 ating data queue
1320: 0a 09 09 73 65 6c 66 2e 5f 71 75 65 75 65 20 3d ...self._queue =
1330: 20 5b 5d 0a 0a 09 64 65 66 20 6c 6f 6f 70 28 73 []...def loop(s
1340: 65 6c 66 29 3a 0a 09 09 23 20 57 61 69 74 20 66 elf):...# Wait f
1350: 6f 72 20 64 61 74 61 20 62 79 20 64 65 66 61 75 or data by defau
1360: 6c 74 0a 09 09 74 69 6d 65 6f 75 74 20 3d 20 4e lt...timeout = N
1370: 6f 6e 65 0a 09 09 65 6f 66 20 3d 20 46 61 6c 73 one...eof = Fals
1380: 65 0a 09 09 62 75 66 66 65 72 20 3d 20 27 27 0a e...buffer = ''.
1390: 09 09 77 68 69 6c 65 20 54 72 75 65 3a 0a 09 09 ..while True:...
13a0: 09 23 20 63 68 65 63 6b 69 6e 67 20 69 66 20 74 .# checking if t
13b0: 68 65 72 65 20 69 73 20 61 6e 79 20 64 61 74 61 here is any data
13c0: 20 6f 72 20 77 69 74 69 6e 67 20 66 6f 72 20 64 or witing for d
13d0: 61 74 61 20 74 6f 20 61 72 72 69 76 65 0a 09 09 ata to arrive...
13e0: 09 6b 65 76 73 20 3d 20 73 65 6c 66 2e 5f 6b 71 .kevs = self._kq
13f0: 2e 63 6f 6e 74 72 6f 6c 28 4e 6f 6e 65 2c 20 31 .control(None, 1
1400: 2c 20 74 69 6d 65 6f 75 74 29 0a 0a 09 09 09 23 , timeout).....#
1410: 20 64 65 74 65 63 74 20 65 6e 64 20 6f 66 20 73 detect end of s
1420: 74 72 65 61 6d 20 61 6e 64 20 65 78 69 74 20 69 tream and exit i
1430: 66 20 70 6f 73 73 69 62 6c 65 0a 09 09 09 69 66 f possible....if
1440: 20 6c 65 6e 28 6b 65 76 73 29 20 3e 20 30 20 61 len(kevs) > 0 a
1450: 6e 64 20 6b 65 76 73 5b 30 5d 2e 66 6c 61 67 73 nd kevs[0].flags
1460: 20 3e 3e 20 31 35 20 3d 3d 20 31 3a 0a 09 09 09 >> 15 == 1:....
1470: 09 65 6f 66 20 3d 20 54 72 75 65 0a 0a 09 09 09 .eof = True.....
1480: 69 66 20 6c 65 6e 28 6b 65 76 73 29 20 3e 20 30 if len(kevs) > 0
1490: 20 61 6e 64 20 6b 65 76 73 5b 30 5d 2e 66 69 6c and kevs[0].fil
14a0: 74 65 72 20 3d 3d 20 73 65 6c 66 2e 5f 73 65 6c ter == self._sel
14b0: 65 63 74 2e 4b 51 5f 46 49 4c 54 45 52 5f 52 45 ect.KQ_FILTER_RE
14c0: 41 44 20 61 6e 64 20 6b 65 76 73 5b 30 5d 2e 64 AD and kevs[0].d
14d0: 61 74 61 20 3e 20 30 3a 0a 09 09 09 09 23 20 72 ata > 0:.....# r
14e0: 65 61 64 69 6e 67 20 64 61 74 61 20 69 6e 0a 09 eading data in..
14f0: 09 09 09 6e 65 77 5f 62 75 66 66 65 72 20 3d 20 ...new_buffer =
1500: 73 79 73 2e 73 74 64 69 6e 2e 72 65 61 64 28 6b sys.stdin.read(k
1510: 65 76 73 5b 30 5d 2e 64 61 74 61 29 0a 09 09 09 evs[0].data)....
1520: 09 23 20 69 66 20 6e 6f 20 64 61 74 61 20 77 61 .# if no data wa
1530: 73 20 73 65 6e 74 20 2d 20 77 65 20 68 61 76 65 s sent - we have
1540: 20 72 65 61 63 68 65 64 20 65 6e 64 20 6f 66 20 reached end of
1550: 66 69 6c 65 0a 09 09 09 09 69 66 20 6c 65 6e 28 file.....if len(
1560: 6e 65 77 5f 62 75 66 66 65 72 29 20 3d 3d 20 30 new_buffer) == 0
1570: 3a 0a 09 09 09 09 09 65 6f 66 20 3d 20 54 72 75 :......eof = Tru
1580: 65 0a 09 09 09 09 65 6c 73 65 3a 0a 09 09 09 09 e.....else:.....
1590: 09 23 20 61 64 64 69 6e 67 20 63 75 72 72 65 6e .# adding curren
15a0: 74 20 62 75 66 66 65 72 20 74 6f 20 6f 6c 64 20 t buffer to old
15b0: 62 75 66 66 65 72 20 72 65 6d 61 69 6e 73 0a 09 buffer remains..
15c0: 09 09 09 09 62 75 66 66 65 72 20 2b 3d 20 6e 65 ....buffer += ne
15d0: 77 5f 62 75 66 66 65 72 0a 09 09 09 09 09 23 20 w_buffer......#
15e0: 73 70 6c 69 74 74 69 6e 67 20 74 6f 20 6c 69 6e splitting to lin
15f0: 65 73 0a 09 09 09 09 09 6c 69 6e 65 73 20 3d 20 es......lines =
1600: 62 75 66 66 65 72 2e 73 70 6c 69 74 28 27 5c 6e buffer.split('\n
1610: 27 29 0a 09 09 09 09 09 23 20 6c 61 73 74 20 6c ')......# last l
1620: 69 6e 65 20 74 68 61 74 20 77 61 73 20 6e 6f 74 ine that was not
1630: 20 74 65 72 6d 69 6e 61 74 65 20 62 79 20 6e 65 terminate by ne
1640: 77 6c 69 6e 65 20 72 65 74 75 72 6e 73 20 74 6f wline returns to
1650: 20 62 75 66 66 65 72 0a 09 09 09 09 09 62 75 66 buffer......buf
1660: 66 65 72 20 3d 20 6c 69 6e 65 73 5b 2d 31 5d 0a fer = lines[-1].
1670: 09 09 09 09 09 23 20 61 6e 20 6f 6e 6c 79 20 69 .....# an only i
1680: 66 20 74 68 65 72 65 20 77 61 73 20 61 74 20 6c f there was at l
1690: 65 61 73 74 20 6f 6e 65 20 6e 65 77 6c 69 6e 65 east one newline
16a0: 0a 09 09 09 09 09 69 66 20 6c 65 6e 28 6c 69 6e ......if len(lin
16b0: 65 73 29 20 3e 20 31 3a 0a 09 09 09 09 09 09 66 es) > 1:.......f
16c0: 6f 72 20 6c 69 6e 65 20 69 6e 20 6c 69 6e 65 73 or line in lines
16d0: 5b 3a 2d 31 5d 3a 0a 09 09 09 09 09 09 09 23 20 [:-1]:........#
16e0: 61 64 64 20 64 61 74 61 20 74 6f 20 74 68 65 20 add data to the
16f0: 71 75 65 75 65 0a 09 09 09 09 09 09 09 69 66 20 queue........if
1700: 73 65 6c 66 2e 63 68 65 63 6b 28 6c 69 6e 65 20 self.check(line
1710: 2b 20 27 5c 6e 27 29 3a 0a 09 09 09 09 09 09 09 + '\n'):........
1720: 09 23 20 64 6f 6e 27 74 20 77 61 69 74 20 66 6f .# don't wait fo
1730: 72 20 6d 6f 72 65 20 64 61 74 61 2c 20 73 74 61 r more data, sta
1740: 72 74 20 70 72 6f 63 65 73 73 69 6e 67 0a 09 09 rt processing...
1750: 09 09 09 09 09 09 74 69 6d 65 6f 75 74 20 3d 20 ......timeout =
1760: 30 0a 09 09 09 65 6c 73 65 3a 0a 09 09 09 09 69 0....else:.....i
1770: 66 20 6c 65 6e 28 73 65 6c 66 2e 5f 71 75 65 75 f len(self._queu
1780: 65 29 20 3e 20 30 3a 0a 09 09 09 09 09 23 20 67 e) > 0:......# g
1790: 65 74 20 6f 6e 65 20 72 65 71 75 65 73 74 20 61 et one request a
17a0: 6e 64 20 70 72 6f 63 65 73 73 20 69 74 0a 09 09 nd process it...
17b0: 09 09 09 72 65 71 20 3d 20 73 65 6c 66 2e 5f 71 ...req = self._q
17c0: 75 65 75 65 2e 70 6f 70 28 30 29 0a 09 09 09 09 ueue.pop(0).....
17d0: 09 43 68 65 63 6b 65 72 2e 70 72 6f 63 65 73 73 .Checker.process
17e0: 28 73 65 6c 66 2c 20 72 65 71 5b 30 5d 2c 20 72 (self, req[0], r
17f0: 65 71 5b 31 5d 2c 20 72 65 71 5b 32 5d 2c 20 72 eq[1], req[2], r
1800: 65 71 5b 33 5d 29 0a 09 09 09 09 09 69 66 20 6c eq[3])......if l
1810: 65 6e 28 73 65 6c 66 2e 5f 71 75 65 75 65 29 20 en(self._queue)
1820: 3d 3d 20 30 3a 0a 09 09 09 09 09 09 23 20 77 61 == 0:.......# wa
1830: 69 74 20 66 6f 72 20 64 61 74 61 20 2d 20 77 65 it for data - we
1840: 20 68 61 76 65 20 6e 6f 74 68 69 6e 67 20 74 6f have nothing to
1850: 20 70 72 6f 63 65 73 73 0a 09 09 09 09 09 09 74 process.......t
1860: 69 6d 65 6f 75 74 20 3d 20 4e 6f 6e 65 0a 0a 09 imeout = None...
1870: 09 09 23 20 69 66 20 71 75 65 75 65 20 69 73 20 ..# if queue is
1880: 65 6d 70 74 79 20 61 6e 64 20 77 65 20 72 65 61 empty and we rea
1890: 63 68 65 64 20 65 6e 64 20 6f 66 20 73 74 72 65 ched end of stre
18a0: 61 6d 20 2d 20 77 65 20 63 61 6e 20 65 78 69 74 am - we can exit
18b0: 0a 09 09 09 69 66 20 6c 65 6e 28 73 65 6c 66 2e ....if len(self.
18c0: 5f 71 75 65 75 65 29 20 3d 3d 20 30 20 61 6e 64 _queue) == 0 and
18d0: 20 65 6f 66 3a 0a 09 09 09 09 62 72 65 61 6b 0a eof:.....break.
18e0: 0a 09 64 65 66 20 70 72 6f 63 65 73 73 28 73 65 ..def process(se
18f0: 6c 66 2c 20 69 64 2c 20 73 69 74 65 2c 20 69 70 lf, id, site, ip
1900: 5f 61 64 64 72 65 73 73 2c 20 75 72 6c 5f 70 61 _address, url_pa
1910: 74 68 2c 20 6c 69 6e 65 29 3a 0a 09 09 23 20 73 th, line):...# s
1920: 69 6d 70 6c 79 20 61 64 64 69 6e 67 20 64 61 74 imply adding dat
1930: 61 20 74 6f 20 74 68 65 20 71 75 65 75 65 0a 09 a to the queue..
1940: 09 73 65 6c 66 2e 5f 71 75 65 75 65 2e 61 70 70 .self._queue.app
1950: 65 6e 64 28 28 69 64 2c 20 73 69 74 65 2c 20 69 end((id, site, i
1960: 70 5f 61 64 64 72 65 73 73 2c 20 75 72 6c 5f 70 p_address, url_p
1970: 61 74 68 29 29 0a 09 09 73 65 6c 66 2e 5f 6c 6f ath))...self._lo
1980: 67 2e 69 6e 66 6f 28 27 72 65 71 75 65 73 74 20 g.info('request
1990: 7b 7d 20 71 75 65 75 65 64 20 28 7b 7d 29 5c 6e {} queued ({})\n
19a0: 27 2e 66 6f 72 6d 61 74 28 69 64 2c 20 6c 69 6e '.format(id, lin
19b0: 65 29 29 0a 0a 23 20 74 68 69 73 20 63 6c 61 73 e))..# this clas
19c0: 73 65 73 20 70 72 6f 63 65 73 73 65 73 20 63 6f ses processes co
19d0: 6e 66 69 67 20 66 69 6c 65 20 61 6e 64 20 73 75 nfig file and su
19e0: 62 73 74 69 74 75 74 65 73 20 64 65 66 61 75 6c bstitutes defaul
19f0: 74 20 76 61 6c 75 65 73 0a 63 6c 61 73 73 20 43 t values.class C
1a00: 6f 6e 66 69 67 3a 0a 09 5f 5f 73 6c 6f 74 73 5f onfig:..__slots_
1a10: 5f 20 3d 20 66 72 6f 7a 65 6e 73 65 74 28 5b 27 _ = frozenset(['
1a20: 5f 63 6f 6e 66 69 67 27 2c 20 27 5f 64 65 66 61 _config', '_defa
1a30: 75 6c 74 27 2c 20 27 5f 73 65 63 74 69 6f 6e 27 ult', '_section'
1a40: 5d 29 0a 09 5f 64 65 66 61 75 6c 74 20 3d 20 7b ]).._default = {
1a50: 0a 09 09 27 72 65 61 63 74 6f 72 27 3a 20 7b 0a ...'reactor': {.
1a60: 09 09 09 27 72 65 61 63 74 6f 72 27 3a 20 27 74 ...'reactor': 't
1a70: 68 72 65 61 64 27 2c 0a 09 09 7d 2c 0a 09 09 27 hread',...},...'
1a80: 6c 6f 67 27 3a 20 7b 0a 09 09 09 27 73 69 6c 65 log': {....'sile
1a90: 6e 74 27 3a 20 27 6e 6f 27 2c 0a 09 09 7d 2c 0a nt': 'no',...},.
1aa0: 09 09 27 64 61 74 61 62 61 73 65 27 3a 20 7b 0a ..'database': {.
1ab0: 09 09 09 27 68 6f 73 74 27 3a 20 27 6c 6f 63 61 ...'host': 'loca
1ac0: 6c 68 6f 73 74 27 2c 0a 09 09 09 27 64 61 74 61 lhost',....'data
1ad0: 62 61 73 65 27 3a 20 27 73 71 75 69 64 54 61 67 base': 'squidTag
1ae0: 27 2c 0a 09 7d 2c 7d 0a 0a 09 23 20 66 75 6e 63 ',..},}...# func
1af0: 74 69 6f 6e 20 74 6f 20 72 65 61 64 20 69 6e 20 tion to read in
1b00: 63 6f 6e 66 69 67 20 66 69 6c 65 0a 09 64 65 66 config file..def
1b10: 20 5f 5f 69 6e 69 74 5f 5f 28 73 65 6c 66 29 3a __init__(self):
1b20: 0a 09 09 70 61 72 73 65 72 20 3d 20 6f 70 74 70 ...parser = optp
1b30: 61 72 73 65 2e 4f 70 74 69 6f 6e 50 61 72 73 65 arse.OptionParse
1b40: 72 28 29 0a 09 09 70 61 72 73 65 72 2e 61 64 64 r()...parser.add
1b50: 5f 6f 70 74 69 6f 6e 28 27 2d 63 27 2c 20 27 2d _option('-c', '-
1b60: 2d 63 6f 6e 66 69 67 27 2c 20 64 65 73 74 20 3d -config', dest =
1b70: 20 27 63 6f 6e 66 69 67 27 2c 0a 09 09 09 68 65 'config',....he
1b80: 6c 70 20 3d 20 27 63 6f 6e 66 69 67 20 66 69 6c lp = 'config fil
1b90: 65 20 6c 6f 63 61 74 69 6f 6e 27 2c 20 6d 65 74 e location', met
1ba0: 61 76 61 72 20 3d 20 27 46 49 4c 45 27 2c 0a 09 avar = 'FILE',..
1bb0: 09 09 64 65 66 61 75 6c 74 20 3d 20 27 2f 75 73 ..default = '/us
1bc0: 72 2f 6c 6f 63 61 6c 2f 65 74 63 2f 73 71 75 69 r/local/etc/squi
1bd0: 64 2d 74 61 67 67 65 72 2e 63 6f 6e 66 27 29 0a d-tagger.conf').
1be0: 0a 09 09 28 6f 70 74 69 6f 6e 73 2c 20 61 72 67 ...(options, arg
1bf0: 73 29 20 3d 20 70 61 72 73 65 72 2e 70 61 72 73 s) = parser.pars
1c00: 65 5f 61 72 67 73 28 29 0a 0a 09 09 61 73 73 65 e_args()....asse
1c10: 72 74 20 6f 73 2e 61 63 63 65 73 73 28 6f 70 74 rt os.access(opt
1c20: 69 6f 6e 73 2e 63 6f 6e 66 69 67 2c 20 6f 73 2e ions.config, os.
1c30: 52 5f 4f 4b 29 2c 20 22 46 61 74 61 6c 20 65 72 R_OK), "Fatal er
1c40: 72 6f 72 3a 20 63 61 6e 27 74 20 72 65 61 64 20 ror: can't read
1c50: 7b 7d 22 2e 66 6f 72 6d 61 74 28 6f 70 74 69 6f {}".format(optio
1c60: 6e 73 2e 63 6f 6e 66 69 67 29 0a 0a 09 09 73 65 ns.config)....se
1c70: 6c 66 2e 5f 63 6f 6e 66 69 67 20 3d 20 63 6f 6e lf._config = con
1c80: 66 69 67 70 61 72 73 65 72 2e 43 6f 6e 66 69 67 figparser.Config
1c90: 50 61 72 73 65 72 28 29 0a 09 09 73 65 6c 66 2e Parser()...self.
1ca0: 5f 63 6f 6e 66 69 67 2e 72 65 61 64 66 70 28 6f _config.readfp(o
1cb0: 70 65 6e 28 6f 70 74 69 6f 6e 73 2e 63 6f 6e 66 pen(options.conf
1cc0: 69 67 29 29 0a 0a 09 23 20 66 75 6e 63 74 69 6f ig))...# functio
1cd0: 6e 20 74 6f 20 73 65 6c 65 63 74 20 63 6f 6e 66 n to select conf
1ce0: 69 67 20 66 69 6c 65 20 73 65 63 74 69 6f 6e 20 ig file section
1cf0: 6f 72 20 63 72 65 61 74 65 20 6f 6e 65 0a 09 64 or create one..d
1d00: 65 66 20 73 65 63 74 69 6f 6e 28 73 65 6c 66 2c ef section(self,
1d10: 20 73 65 63 74 69 6f 6e 29 3a 0a 09 09 69 66 20 section):...if
1d20: 6e 6f 74 20 73 65 6c 66 2e 5f 63 6f 6e 66 69 67 not self._config
1d30: 2e 68 61 73 5f 73 65 63 74 69 6f 6e 28 73 65 63 .has_section(sec
1d40: 74 69 6f 6e 29 3a 0a 09 09 09 73 65 6c 66 2e 5f tion):....self._
1d50: 63 6f 6e 66 69 67 2e 61 64 64 5f 73 65 63 74 69 config.add_secti
1d60: 6f 6e 28 73 65 63 74 69 6f 6e 29 0a 09 09 73 65 on(section)...se
1d70: 6c 66 2e 5f 73 65 63 74 69 6f 6e 20 3d 20 73 65 lf._section = se
1d80: 63 74 69 6f 6e 0a 0a 09 23 20 66 75 6e 63 74 69 ction...# functi
1d90: 6f 6e 20 74 6f 20 67 65 74 20 63 6f 6e 66 69 67 on to get config
1da0: 20 70 61 72 61 6d 65 74 65 72 2c 20 69 66 20 70 parameter, if p
1db0: 61 72 61 6d 65 74 65 72 20 64 6f 65 73 6e 27 74 arameter doesn't
1dc0: 20 65 78 69 73 74 73 20 74 68 65 20 64 65 66 61 exists the defa
1dd0: 75 6c 74 0a 09 23 20 76 61 6c 75 65 20 6f 72 20 ult..# value or
1de0: 4e 6f 6e 65 20 69 73 20 73 75 62 73 74 69 74 75 None is substitu
1df0: 74 65 64 0a 09 64 65 66 20 5f 5f 67 65 74 69 74 ted..def __getit
1e00: 65 6d 5f 5f 28 73 65 6c 66 2c 20 6e 61 6d 65 29 em__(self, name)
1e10: 3a 0a 09 09 69 66 20 6e 6f 74 20 73 65 6c 66 2e :...if not self.
1e20: 5f 63 6f 6e 66 69 67 2e 68 61 73 5f 6f 70 74 69 _config.has_opti
1e30: 6f 6e 28 73 65 6c 66 2e 5f 73 65 63 74 69 6f 6e on(self._section
1e40: 2c 20 6e 61 6d 65 29 3a 0a 09 09 09 69 66 20 73 , name):....if s
1e50: 65 6c 66 2e 5f 73 65 63 74 69 6f 6e 20 69 6e 20 elf._section in
1e60: 73 65 6c 66 2e 5f 64 65 66 61 75 6c 74 3a 0a 09 self._default:..
1e70: 09 09 09 69 66 20 6e 61 6d 65 20 69 6e 20 73 65 ...if name in se
1e80: 6c 66 2e 5f 64 65 66 61 75 6c 74 5b 73 65 6c 66 lf._default[self
1e90: 2e 5f 73 65 63 74 69 6f 6e 5d 3a 0a 09 09 09 09 ._section]:.....
1ea0: 09 73 65 6c 66 2e 5f 63 6f 6e 66 69 67 2e 73 65 .self._config.se
1eb0: 74 28 73 65 6c 66 2e 5f 73 65 63 74 69 6f 6e 2c t(self._section,
1ec0: 20 6e 61 6d 65 2c 20 73 65 6c 66 2e 5f 64 65 66 name, self._def
1ed0: 61 75 6c 74 5b 73 65 6c 66 2e 5f 73 65 63 74 69 ault[self._secti
1ee0: 6f 6e 5d 5b 6e 61 6d 65 5d 29 0a 09 09 09 09 65 on][name]).....e
1ef0: 6c 73 65 3a 0a 09 09 09 09 09 73 65 6c 66 2e 5f lse:......self._
1f00: 63 6f 6e 66 69 67 2e 73 65 74 28 73 65 6c 66 2e config.set(self.
1f10: 5f 73 65 63 74 69 6f 6e 2c 20 6e 61 6d 65 2c 20 _section, name,
1f20: 4e 6f 6e 65 29 0a 09 09 09 65 6c 73 65 3a 0a 09 None)....else:..
1f30: 09 09 09 73 65 6c 66 2e 5f 63 6f 6e 66 69 67 2e ...self._config.
1f40: 73 65 74 28 73 65 6c 66 2e 5f 73 65 63 74 69 6f set(self._sectio
1f50: 6e 2c 20 6e 61 6d 65 2c 20 4e 6f 6e 65 29 0a 09 n, name, None)..
1f60: 09 72 65 74 75 72 6e 28 73 65 6c 66 2e 5f 63 6f .return(self._co
1f70: 6e 66 69 67 2e 67 65 74 28 73 65 6c 66 2e 5f 73 nfig.get(self._s
1f80: 65 63 74 69 6f 6e 2c 20 6e 61 6d 65 29 29 0a 0a ection, name))..
1f90: 23 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 61 # initializing a
1fa0: 6e 64 20 72 65 61 64 69 6e 67 20 69 6e 20 63 6f nd reading in co
1fb0: 6e 66 69 67 20 66 69 6c 65 0a 63 6f 6e 66 69 67 nfig file.config
1fc0: 20 3d 20 43 6f 6e 66 69 67 28 29 0a 0a 63 6f 6e = Config()..con
1fd0: 66 69 67 2e 73 65 63 74 69 6f 6e 28 27 72 65 61 fig.section('rea
1fe0: 63 74 6f 72 27 29 0a 69 66 20 63 6f 6e 66 69 67 ctor').if config
1ff0: 5b 27 72 65 61 63 74 6f 72 27 5d 20 3d 3d 20 27 ['reactor'] == '
2000: 74 68 72 65 61 64 27 3a 0a 09 63 68 65 63 6b 65 thread':..checke
2010: 72 20 3d 20 43 68 65 63 6b 65 72 54 68 72 65 61 r = CheckerThrea
2020: 64 28 29 0a 65 6c 69 66 20 63 6f 6e 66 69 67 5b d().elif config[
2030: 27 72 65 61 63 74 6f 72 27 5d 20 3d 3d 20 27 70 'reactor'] == 'p
2040: 6c 61 69 6e 27 3a 0a 09 63 68 65 63 6b 65 72 20 lain':..checker
2050: 3d 20 43 68 65 63 6b 65 72 28 29 0a 65 6c 69 66 = Checker().elif
2060: 20 63 6f 6e 66 69 67 5b 27 72 65 61 63 74 6f 72 config['reactor
2070: 27 5d 20 3d 3d 20 27 6b 71 75 65 75 65 27 3a 0a '] == 'kqueue':.
2080: 09 63 68 65 63 6b 65 72 20 3d 20 43 68 65 63 6b .checker = Check
2090: 65 72 4b 71 75 65 75 65 28 29 0a 0a 63 68 65 63 erKqueue()..chec
20a0: 6b 65 72 2e 6c 6f 6f 70 28 29 0a ker.loop().