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 0a 0a 66 72 6f 6d 20 5f 5f 66 75 ython..from __fu
0020: 74 75 72 65 5f 5f 20 69 6d 70 6f 72 74 20 64 69 ture__ import di
0030: 76 69 73 69 6f 6e 2c 20 70 72 69 6e 74 5f 66 75 vision, print_fu
0040: 6e 63 74 69 6f 6e 2c 20 75 6e 69 63 6f 64 65 5f nction, unicode_
0050: 6c 69 74 65 72 61 6c 73 0a 0a 69 6d 70 6f 72 74 literals..import
0060: 20 67 65 76 65 6e 74 2e 6d 6f 6e 6b 65 79 0a 67 gevent.monkey.g
0070: 65 76 65 6e 74 2e 6d 6f 6e 6b 65 79 2e 70 61 74 event.monkey.pat
0080: 63 68 5f 61 6c 6c 28 29 0a 0a 69 6d 70 6f 72 74 ch_all()..import
0090: 20 66 63 6e 74 6c 2c 20 67 65 76 65 6e 74 2e 63 fcntl, gevent.c
00a0: 6f 72 65 2c 20 67 65 76 65 6e 74 2e 70 6f 6f 6c ore, gevent.pool
00b0: 2c 20 67 65 76 65 6e 74 2e 71 75 65 75 65 2c 20 , gevent.queue,
00c0: 67 65 76 65 6e 74 2e 73 6f 63 6b 65 74 2c 20 6f gevent.socket, o
00d0: 73 2c 20 70 73 79 63 6f 70 67 32 2c 20 72 65 2c s, psycopg2, re,
00e0: 20 73 79 73 0a 0a 23 20 2f 2f 69 6e 63 6c 75 73 sys..# //inclus
00f0: 69 6f 6e 20 73 74 61 72 74 0a 23 20 43 6f 70 79 ion start.# Copy
0100: 72 69 67 68 74 20 28 43 29 20 32 30 31 30 20 44 right (C) 2010 D
0110: 61 6e 69 65 6c 65 20 56 61 72 72 61 7a 7a 6f 20 aniele Varrazzo
0120: 3c 64 61 6e 69 65 6c 65 2e 76 61 72 72 61 7a 7a <daniele.varrazz
0130: 6f 40 67 6d 61 69 6c 2e 63 6f 6d 3e 0a 23 20 61 o@gmail.com>.# a
0140: 6e 64 20 6c 69 63 65 6e 73 65 64 20 75 6e 64 65 nd licensed unde
0150: 72 20 74 68 65 20 4d 49 54 20 6c 69 63 65 6e 73 r the MIT licens
0160: 65 3a 0a 0a 64 65 66 20 67 65 76 65 6e 74 5f 77 e:..def gevent_w
0170: 61 69 74 5f 63 61 6c 6c 62 61 63 6b 28 63 6f 6e ait_callback(con
0180: 6e 2c 20 74 69 6d 65 6f 75 74 3d 4e 6f 6e 65 29 n, timeout=None)
0190: 3a 0a 09 22 22 22 41 20 77 61 69 74 20 63 61 6c :.."""A wait cal
01a0: 6c 62 61 63 6b 20 75 73 65 66 75 6c 20 74 6f 20 lback useful to
01b0: 61 6c 6c 6f 77 20 67 65 76 65 6e 74 20 74 6f 20 allow gevent to
01c0: 77 6f 72 6b 20 77 69 74 68 20 50 73 79 63 6f 70 work with Psycop
01d0: 67 2e 22 22 22 0a 09 77 68 69 6c 65 20 31 3a 0a g."""..while 1:.
01e0: 09 09 73 74 61 74 65 20 3d 20 63 6f 6e 6e 2e 70 ..state = conn.p
01f0: 6f 6c 6c 28 29 0a 09 09 69 66 20 73 74 61 74 65 oll()...if state
0200: 20 3d 3d 20 70 73 79 63 6f 70 67 32 2e 65 78 74 == psycopg2.ext
0210: 65 6e 73 69 6f 6e 73 2e 50 4f 4c 4c 5f 4f 4b 3a ensions.POLL_OK:
0220: 0a 09 09 09 62 72 65 61 6b 0a 09 09 65 6c 69 66 ....break...elif
0230: 20 73 74 61 74 65 20 3d 3d 20 70 73 79 63 6f 70 state == psycop
0240: 67 32 2e 65 78 74 65 6e 73 69 6f 6e 73 2e 50 4f g2.extensions.PO
0250: 4c 4c 5f 52 45 41 44 3a 0a 09 09 09 67 65 76 65 LL_READ:....geve
0260: 6e 74 2e 73 6f 63 6b 65 74 2e 77 61 69 74 5f 72 nt.socket.wait_r
0270: 65 61 64 28 63 6f 6e 6e 2e 66 69 6c 65 6e 6f 28 ead(conn.fileno(
0280: 29 2c 20 74 69 6d 65 6f 75 74 3d 74 69 6d 65 6f ), timeout=timeo
0290: 75 74 29 0a 09 09 65 6c 69 66 20 73 74 61 74 65 ut)...elif state
02a0: 20 3d 3d 20 70 73 79 63 6f 70 67 32 2e 65 78 74 == psycopg2.ext
02b0: 65 6e 73 69 6f 6e 73 2e 50 4f 4c 4c 5f 57 52 49 ensions.POLL_WRI
02c0: 54 45 3a 0a 09 09 09 67 65 76 65 6e 74 2e 73 6f TE:....gevent.so
02d0: 63 6b 65 74 2e 77 61 69 74 5f 77 72 69 74 65 28 cket.wait_write(
02e0: 63 6f 6e 6e 2e 66 69 6c 65 6e 6f 28 29 2c 20 74 conn.fileno(), t
02f0: 69 6d 65 6f 75 74 3d 74 69 6d 65 6f 75 74 29 0a imeout=timeout).
0300: 09 09 65 6c 73 65 3a 0a 09 09 09 72 61 69 73 65 ..else:....raise
0310: 20 70 73 79 63 6f 70 67 32 2e 4f 70 65 72 61 74 psycopg2.Operat
0320: 69 6f 6e 61 6c 45 72 72 6f 72 28 22 42 61 64 20 ionalError("Bad
0330: 72 65 73 75 6c 74 20 66 72 6f 6d 20 70 6f 6c 6c result from poll
0340: 3a 20 25 72 22 20 25 20 73 74 61 74 65 29 0a 0a : %r" % state)..
0350: 69 66 20 6e 6f 74 20 68 61 73 61 74 74 72 28 70 if not hasattr(p
0360: 73 79 63 6f 70 67 32 2e 65 78 74 65 6e 73 69 6f sycopg2.extensio
0370: 6e 73 2c 20 27 73 65 74 5f 77 61 69 74 5f 63 61 ns, 'set_wait_ca
0380: 6c 6c 62 61 63 6b 27 29 3a 0a 09 72 61 69 73 65 llback'):..raise
0390: 20 49 6d 70 6f 72 74 45 72 72 6f 72 28 22 73 75 ImportError("su
03a0: 70 70 6f 72 74 20 66 6f 72 20 63 6f 72 6f 75 74 pport for corout
03b0: 69 6e 65 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 ines not availab
03c0: 6c 65 20 69 6e 20 74 68 69 73 20 50 73 79 63 6f le in this Psyco
03d0: 70 67 20 76 65 72 73 69 6f 6e 20 28 25 73 29 22 pg version (%s)"
03e0: 20 25 20 70 73 79 63 6f 70 67 32 2e 5f 5f 76 65 % psycopg2.__ve
03f0: 72 73 69 6f 6e 5f 5f 29 0a 09 70 73 79 63 6f 70 rsion__)..psycop
0400: 67 32 2e 65 78 74 65 6e 73 69 6f 6e 73 2e 73 65 g2.extensions.se
0410: 74 5f 77 61 69 74 5f 63 61 6c 6c 62 61 63 6b 28 t_wait_callback(
0420: 67 65 76 65 6e 74 5f 77 61 69 74 5f 63 61 6c 6c gevent_wait_call
0430: 62 61 63 6b 29 0a 0a 23 20 2f 2f 69 6e 63 6c 75 back)..# //inclu
0440: 73 69 6f 6e 20 65 6e 64 0a 0a 23 20 74 68 69 73 sion end..# this
0450: 20 63 6c 61 73 73 65 73 20 70 72 6f 63 65 73 73 classes process
0460: 65 73 20 63 6f 6e 66 69 67 20 66 69 6c 65 20 61 es config file a
0470: 6e 64 20 73 75 62 73 74 69 74 75 74 65 73 20 64 nd substitutes d
0480: 65 66 61 75 6c 74 20 76 61 6c 75 65 73 0a 63 6c efault values.cl
0490: 61 73 73 20 43 6f 6e 66 69 67 3a 0a 09 5f 5f 73 ass Config:..__s
04a0: 6c 6f 74 73 5f 5f 20 3d 20 66 72 6f 7a 65 6e 73 lots__ = frozens
04b0: 65 74 28 5b 27 5f 63 6f 6e 66 69 67 27 2c 20 27 et(['_config', '
04c0: 5f 64 65 66 61 75 6c 74 27 2c 20 27 5f 73 65 63 _default', '_sec
04d0: 74 69 6f 6e 27 2c 20 27 6f 70 74 69 6f 6e 73 27 tion', 'options'
04e0: 5d 29 0a 09 5f 64 65 66 61 75 6c 74 20 3d 20 7b ]).._default = {
04f0: 0a 09 09 27 6c 6f 67 27 3a 20 7b 0a 09 09 09 27 ...'log': {....'
0500: 73 69 6c 65 6e 74 27 3a 20 27 6e 6f 27 2c 0a 09 silent': 'no',..
0510: 09 7d 2c 0a 09 09 27 64 61 74 61 62 61 73 65 27 .},...'database'
0520: 3a 20 7b 0a 09 09 09 27 64 61 74 61 62 61 73 65 : {....'database
0530: 27 3a 20 27 73 71 75 69 64 54 61 67 27 2c 0a 09 ': 'squidTag',..
0540: 7d 2c 7d 0a 0a 09 23 20 66 75 6e 63 74 69 6f 6e },}...# function
0550: 20 74 6f 20 72 65 61 64 20 69 6e 20 63 6f 6e 66 to read in conf
0560: 69 67 20 66 69 6c 65 0a 09 64 65 66 20 5f 5f 69 ig file..def __i
0570: 6e 69 74 5f 5f 28 73 65 6c 66 29 3a 0a 09 09 69 nit__(self):...i
0580: 6d 70 6f 72 74 20 43 6f 6e 66 69 67 50 61 72 73 mport ConfigPars
0590: 65 72 2c 20 6f 70 74 70 61 72 73 65 2c 20 6f 73 er, optparse, os
05a0: 0a 0a 09 09 70 61 72 73 65 72 20 3d 20 6f 70 74 ....parser = opt
05b0: 70 61 72 73 65 2e 4f 70 74 69 6f 6e 50 61 72 73 parse.OptionPars
05c0: 65 72 28 29 0a 09 09 70 61 72 73 65 72 2e 61 64 er()...parser.ad
05d0: 64 5f 6f 70 74 69 6f 6e 28 27 2d 63 27 2c 20 27 d_option('-c', '
05e0: 2d 2d 63 6f 6e 66 69 67 27 2c 20 64 65 73 74 20 --config', dest
05f0: 3d 20 27 63 6f 6e 66 69 67 27 2c 0a 09 09 09 68 = 'config',....h
0600: 65 6c 70 20 3d 20 27 63 6f 6e 66 69 67 20 66 69 elp = 'config fi
0610: 6c 65 20 6c 6f 63 61 74 69 6f 6e 27 2c 20 6d 65 le location', me
0620: 74 61 76 61 72 20 3d 20 27 46 49 4c 45 27 2c 0a tavar = 'FILE',.
0630: 09 09 09 64 65 66 61 75 6c 74 20 3d 20 27 2f 75 ...default = '/u
0640: 73 72 2f 6c 6f 63 61 6c 2f 65 74 63 2f 73 71 75 sr/local/etc/squ
0650: 69 64 2d 74 61 67 67 65 72 2e 63 6f 6e 66 27 29 id-tagger.conf')
0660: 0a 09 09 70 61 72 73 65 72 2e 61 64 64 5f 6f 70 ...parser.add_op
0670: 74 69 6f 6e 28 27 2d 64 27 2c 20 27 2d 2d 64 75 tion('-d', '--du
0680: 6d 70 27 2c 20 64 65 73 74 20 3d 20 27 64 75 6d mp', dest = 'dum
0690: 70 27 2c 0a 09 09 09 68 65 6c 70 20 3d 20 27 64 p',....help = 'd
06a0: 75 6d 70 20 64 61 74 61 62 61 73 65 27 2c 20 61 ump database', a
06b0: 63 74 69 6f 6e 20 3d 20 27 73 74 6f 72 65 5f 74 ction = 'store_t
06c0: 72 75 65 27 2c 20 6d 65 74 61 76 61 72 20 3d 20 rue', metavar =
06d0: 27 62 6f 6f 6c 27 2c 0a 09 09 09 64 65 66 61 75 'bool',....defau
06e0: 6c 74 20 3d 20 46 61 6c 73 65 29 0a 09 09 70 61 lt = False)...pa
06f0: 72 73 65 72 2e 61 64 64 5f 6f 70 74 69 6f 6e 28 rser.add_option(
0700: 27 2d 66 27 2c 20 27 2d 2d 66 6c 75 73 68 2d 64 '-f', '--flush-d
0710: 61 74 61 62 61 73 65 27 2c 20 64 65 73 74 20 3d atabase', dest =
0720: 20 27 66 6c 75 73 68 5f 64 62 27 2c 0a 09 09 09 'flush_db',....
0730: 68 65 6c 70 20 3d 20 27 66 6c 75 73 68 20 70 72 help = 'flush pr
0740: 65 76 69 6f 75 73 20 64 61 74 61 62 61 73 65 20 evious database
0750: 6f 6e 20 6c 6f 61 64 27 2c 20 64 65 66 61 75 6c on load', defaul
0760: 74 20 3d 20 46 61 6c 73 65 2c 0a 09 09 09 61 63 t = False,....ac
0770: 74 69 6f 6e 20 3d 20 27 73 74 6f 72 65 5f 74 72 tion = 'store_tr
0780: 75 65 27 2c 20 6d 65 74 61 76 61 72 20 3d 20 27 ue', metavar = '
0790: 62 6f 6f 6c 27 29 0a 09 09 70 61 72 73 65 72 2e bool')...parser.
07a0: 61 64 64 5f 6f 70 74 69 6f 6e 28 27 2d 6c 27 2c add_option('-l',
07b0: 20 27 2d 2d 6c 6f 61 64 27 2c 20 64 65 73 74 20 '--load', dest
07c0: 3d 20 27 6c 6f 61 64 27 2c 0a 09 09 09 68 65 6c = 'load',....hel
07d0: 70 20 3d 20 27 6c 6f 61 64 20 64 61 74 61 62 61 p = 'load databa
07e0: 73 65 27 2c 20 61 63 74 69 6f 6e 20 3d 20 27 73 se', action = 's
07f0: 74 6f 72 65 5f 74 72 75 65 27 2c 20 6d 65 74 61 tore_true', meta
0800: 76 61 72 20 3d 20 27 62 6f 6f 6c 27 2c 0a 09 09 var = 'bool',...
0810: 09 64 65 66 61 75 6c 74 20 3d 20 46 61 6c 73 65 .default = False
0820: 29 0a 09 09 70 61 72 73 65 72 2e 61 64 64 5f 6f )...parser.add_o
0830: 70 74 69 6f 6e 28 27 2d 44 27 2c 20 27 2d 2d 64 ption('-D', '--d
0840: 75 6d 70 2d 63 6f 6e 66 27 2c 20 64 65 73 74 20 ump-conf', dest
0850: 3d 20 27 64 75 6d 70 5f 63 6f 6e 66 27 2c 0a 09 = 'dump_conf',..
0860: 09 09 68 65 6c 70 20 3d 20 27 64 75 6d 70 20 66 ..help = 'dump f
0870: 69 6c 74 65 72 69 6e 67 20 72 75 6c 65 73 27 2c iltering rules',
0880: 20 64 65 66 61 75 6c 74 20 3d 20 46 61 6c 73 65 default = False
0890: 2c 20 6d 65 74 61 76 61 72 20 3d 20 27 62 6f 6f , metavar = 'boo
08a0: 6c 27 2c 0a 09 09 09 61 63 74 69 6f 6e 20 3d 20 l',....action =
08b0: 27 73 74 6f 72 65 5f 74 72 75 65 27 29 0a 09 09 'store_true')...
08c0: 70 61 72 73 65 72 2e 61 64 64 5f 6f 70 74 69 6f parser.add_optio
08d0: 6e 28 27 2d 4c 27 2c 20 27 2d 2d 6c 6f 61 64 2d n('-L', '--load-
08e0: 63 6f 6e 66 27 2c 20 64 65 73 74 20 3d 20 27 6c conf', dest = 'l
08f0: 6f 61 64 5f 63 6f 6e 66 27 2c 0a 09 09 09 68 65 oad_conf',....he
0900: 6c 70 20 3d 20 27 6c 6f 61 64 20 66 69 6c 74 65 lp = 'load filte
0910: 72 69 6e 67 20 72 75 6c 65 73 27 2c 20 64 65 66 ring rules', def
0920: 61 75 6c 74 20 3d 20 46 61 6c 73 65 2c 20 6d 65 ault = False, me
0930: 74 61 76 61 72 20 3d 20 27 62 6f 6f 6c 27 2c 0a tavar = 'bool',.
0940: 09 09 09 61 63 74 69 6f 6e 20 3d 20 27 73 74 6f ...action = 'sto
0950: 72 65 5f 74 72 75 65 27 29 0a 0a 09 09 28 73 65 re_true')....(se
0960: 6c 66 2e 6f 70 74 69 6f 6e 73 2c 20 61 72 67 73 lf.options, args
0970: 29 20 3d 20 70 61 72 73 65 72 2e 70 61 72 73 65 ) = parser.parse
0980: 5f 61 72 67 73 28 29 0a 0a 09 09 61 73 73 65 72 _args()....asser
0990: 74 20 6f 73 2e 61 63 63 65 73 73 28 73 65 6c 66 t os.access(self
09a0: 2e 6f 70 74 69 6f 6e 73 2e 63 6f 6e 66 69 67 2c .options.config,
09b0: 20 6f 73 2e 52 5f 4f 4b 29 2c 20 22 46 61 74 61 os.R_OK), "Fata
09c0: 6c 20 65 72 72 6f 72 3a 20 63 61 6e 27 74 20 72 l error: can't r
09d0: 65 61 64 20 7b 7d 22 2e 66 6f 72 6d 61 74 28 73 ead {}".format(s
09e0: 65 6c 66 2e 6f 70 74 69 6f 6e 73 2e 63 6f 6e 66 elf.options.conf
09f0: 69 67 29 0a 0a 09 09 73 65 6c 66 2e 5f 63 6f 6e ig)....self._con
0a00: 66 69 67 20 3d 20 43 6f 6e 66 69 67 50 61 72 73 fig = ConfigPars
0a10: 65 72 2e 43 6f 6e 66 69 67 50 61 72 73 65 72 28 er.ConfigParser(
0a20: 29 0a 09 09 73 65 6c 66 2e 5f 63 6f 6e 66 69 67 )...self._config
0a30: 2e 72 65 61 64 66 70 28 6f 70 65 6e 28 73 65 6c .readfp(open(sel
0a40: 66 2e 6f 70 74 69 6f 6e 73 2e 63 6f 6e 66 69 67 f.options.config
0a50: 29 29 0a 0a 09 23 20 66 75 6e 63 74 69 6f 6e 20 ))...# function
0a60: 74 6f 20 73 65 6c 65 63 74 20 63 6f 6e 66 69 67 to select config
0a70: 20 66 69 6c 65 20 73 65 63 74 69 6f 6e 20 6f 72 file section or
0a80: 20 63 72 65 61 74 65 20 6f 6e 65 0a 09 64 65 66 create one..def
0a90: 20 73 65 63 74 69 6f 6e 28 73 65 6c 66 2c 20 73 section(self, s
0aa0: 65 63 74 69 6f 6e 29 3a 0a 09 09 69 66 20 6e 6f ection):...if no
0ab0: 74 20 73 65 6c 66 2e 5f 63 6f 6e 66 69 67 2e 68 t self._config.h
0ac0: 61 73 5f 73 65 63 74 69 6f 6e 28 73 65 63 74 69 as_section(secti
0ad0: 6f 6e 29 3a 0a 09 09 09 73 65 6c 66 2e 5f 63 6f on):....self._co
0ae0: 6e 66 69 67 2e 61 64 64 5f 73 65 63 74 69 6f 6e nfig.add_section
0af0: 28 73 65 63 74 69 6f 6e 29 0a 09 09 73 65 6c 66 (section)...self
0b00: 2e 5f 73 65 63 74 69 6f 6e 20 3d 20 73 65 63 74 ._section = sect
0b10: 69 6f 6e 0a 0a 09 23 20 66 75 6e 63 74 69 6f 6e ion...# function
0b20: 20 74 6f 20 67 65 74 20 63 6f 6e 66 69 67 20 70 to get config p
0b30: 61 72 61 6d 65 74 65 72 2c 20 69 66 20 70 61 72 arameter, if par
0b40: 61 6d 65 74 65 72 20 64 6f 65 73 6e 27 74 20 65 ameter doesn't e
0b50: 78 69 73 74 73 20 74 68 65 20 64 65 66 61 75 6c xists the defaul
0b60: 74 0a 09 23 20 76 61 6c 75 65 20 6f 72 20 4e 6f t..# value or No
0b70: 6e 65 20 69 73 20 73 75 62 73 74 69 74 75 74 65 ne is substitute
0b80: 64 0a 09 64 65 66 20 5f 5f 67 65 74 69 74 65 6d d..def __getitem
0b90: 5f 5f 28 73 65 6c 66 2c 20 6e 61 6d 65 29 3a 0a __(self, name):.
0ba0: 09 09 69 66 20 6e 6f 74 20 73 65 6c 66 2e 5f 63 ..if not self._c
0bb0: 6f 6e 66 69 67 2e 68 61 73 5f 6f 70 74 69 6f 6e onfig.has_option
0bc0: 28 73 65 6c 66 2e 5f 73 65 63 74 69 6f 6e 2c 20 (self._section,
0bd0: 6e 61 6d 65 29 3a 0a 09 09 09 69 66 20 73 65 6c name):....if sel
0be0: 66 2e 5f 73 65 63 74 69 6f 6e 20 69 6e 20 73 65 f._section in se
0bf0: 6c 66 2e 5f 64 65 66 61 75 6c 74 3a 0a 09 09 09 lf._default:....
0c00: 09 69 66 20 6e 61 6d 65 20 69 6e 20 73 65 6c 66 .if name in self
0c10: 2e 5f 64 65 66 61 75 6c 74 5b 73 65 6c 66 2e 5f ._default[self._
0c20: 73 65 63 74 69 6f 6e 5d 3a 0a 09 09 09 09 09 73 section]:......s
0c30: 65 6c 66 2e 5f 63 6f 6e 66 69 67 2e 73 65 74 28 elf._config.set(
0c40: 73 65 6c 66 2e 5f 73 65 63 74 69 6f 6e 2c 20 6e self._section, n
0c50: 61 6d 65 2c 20 73 65 6c 66 2e 5f 64 65 66 61 75 ame, self._defau
0c60: 6c 74 5b 73 65 6c 66 2e 5f 73 65 63 74 69 6f 6e lt[self._section
0c70: 5d 5b 6e 61 6d 65 5d 29 0a 09 09 09 09 65 6c 73 ][name]).....els
0c80: 65 3a 0a 09 09 09 09 09 73 65 6c 66 2e 5f 63 6f e:......self._co
0c90: 6e 66 69 67 2e 73 65 74 28 73 65 6c 66 2e 5f 73 nfig.set(self._s
0ca0: 65 63 74 69 6f 6e 2c 20 6e 61 6d 65 2c 20 4e 6f ection, name, No
0cb0: 6e 65 29 0a 09 09 09 65 6c 73 65 3a 0a 09 09 09 ne)....else:....
0cc0: 09 73 65 6c 66 2e 5f 63 6f 6e 66 69 67 2e 73 65 .self._config.se
0cd0: 74 28 73 65 6c 66 2e 5f 73 65 63 74 69 6f 6e 2c t(self._section,
0ce0: 20 6e 61 6d 65 2c 20 4e 6f 6e 65 29 0a 09 09 72 name, None)...r
0cf0: 65 74 75 72 6e 28 73 65 6c 66 2e 5f 63 6f 6e 66 eturn(self._conf
0d00: 69 67 2e 67 65 74 28 73 65 6c 66 2e 5f 73 65 63 ig.get(self._sec
0d10: 74 69 6f 6e 2c 20 6e 61 6d 65 29 29 0a 0a 23 20 tion, name))..#
0d20: 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 61 6e 64 initializing and
0d30: 20 72 65 61 64 69 6e 67 20 69 6e 20 63 6f 6e 66 reading in conf
0d40: 69 67 20 66 69 6c 65 0a 63 6f 6e 66 69 67 20 3d ig file.config =
0d50: 20 43 6f 6e 66 69 67 28 29 0a 0a 69 6d 70 6f 72 Config()..impor
0d60: 74 20 6c 6f 67 67 69 6e 67 2c 20 6c 6f 67 67 69 t logging, loggi
0d70: 6e 67 2e 68 61 6e 64 6c 65 72 73 0a 0a 23 20 77 ng.handlers..# w
0d80: 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 6c 6f rapper around lo
0d90: 67 67 69 6e 67 20 68 61 6e 64 6c 65 72 20 74 6f gging handler to
0da0: 20 6d 61 6b 65 20 69 74 20 71 75 65 75 65 20 72 make it queue r
0db0: 65 63 6f 72 64 73 20 61 6e 64 20 64 6f 6e 27 74 ecords and don't
0dc0: 20 73 74 61 6c 6c 20 77 68 65 6e 20 73 65 6e 64 stall when send
0dd0: 69 6e 67 20 74 68 65 6d 0a 0a 63 6c 61 73 73 20 ing them..class
0de0: 53 79 73 4c 6f 67 48 61 6e 64 6c 65 72 51 75 65 SysLogHandlerQue
0df0: 75 65 28 6c 6f 67 67 69 6e 67 2e 68 61 6e 64 6c ue(logging.handl
0e00: 65 72 73 2e 53 79 73 4c 6f 67 48 61 6e 64 6c 65 ers.SysLogHandle
0e10: 72 29 3a 0a 09 5f 5f 73 6c 6f 74 73 5f 5f 20 3d r):..__slots__ =
0e20: 20 66 72 6f 7a 65 6e 73 65 74 28 5b 27 5f 72 75 frozenset(['_ru
0e30: 6e 6e 69 6e 67 27 2c 20 27 5f 74 61 69 6c 27 2c nning', '_tail',
0e40: 20 27 5f 77 6f 72 6b 65 72 27 5d 29 0a 0a 09 64 '_worker'])...d
0e50: 65 66 20 5f 5f 69 6e 69 74 5f 5f 28 73 65 6c 66 ef __init__(self
0e60: 29 3a 0a 09 09 6c 6f 67 67 69 6e 67 2e 68 61 6e ):...logging.han
0e70: 64 6c 65 72 73 2e 53 79 73 4c 6f 67 48 61 6e 64 dlers.SysLogHand
0e80: 6c 65 72 2e 5f 5f 69 6e 69 74 5f 5f 28 73 65 6c ler.__init__(sel
0e90: 66 2c 20 27 2f 64 65 76 2f 6c 6f 67 27 29 0a 09 f, '/dev/log')..
0ea0: 09 73 65 6c 66 2e 5f 74 61 69 6c 20 3d 20 67 65 .self._tail = ge
0eb0: 76 65 6e 74 2e 71 75 65 75 65 2e 51 75 65 75 65 vent.queue.Queue
0ec0: 28 29 0a 09 09 73 65 6c 66 2e 5f 77 6f 72 6b 65 ()...self._worke
0ed0: 72 20 3d 20 4e 6f 6e 65 0a 0a 09 64 65 66 20 65 r = None...def e
0ee0: 6d 69 74 28 73 65 6c 66 2c 20 72 65 63 6f 72 64 mit(self, record
0ef0: 29 3a 0a 09 09 74 72 79 3a 0a 09 09 09 73 65 6c ):...try:....sel
0f00: 66 2e 5f 74 61 69 6c 2e 70 75 74 28 72 65 63 6f f._tail.put(reco
0f10: 72 64 29 0a 09 09 65 78 63 65 70 74 20 28 4b 65 rd)...except (Ke
0f20: 79 62 6f 61 72 64 49 6e 74 65 72 72 75 70 74 2c yboardInterrupt,
0f30: 20 53 79 73 74 65 6d 45 78 69 74 29 3a 0a 09 09 SystemExit):...
0f40: 09 72 61 69 73 65 0a 09 09 65 78 63 65 70 74 3a .raise...except:
0f50: 0a 09 09 09 73 65 6c 66 2e 68 61 6e 64 6c 65 45 ....self.handleE
0f60: 72 72 6f 72 28 72 65 63 6f 72 64 29 0a 09 09 69 rror(record)...i
0f70: 66 20 73 65 6c 66 2e 5f 77 6f 72 6b 65 72 20 3d f self._worker =
0f80: 3d 20 4e 6f 6e 65 3a 0a 09 09 09 23 20 69 6e 20 = None:....# in
0f90: 63 61 73 65 20 71 75 65 75 65 20 69 73 20 65 6d case queue is em
0fa0: 70 74 79 20 77 65 20 77 69 6c 6c 20 73 70 61 77 pty we will spaw
0fb0: 6e 20 6e 65 77 20 77 6f 72 6b 65 72 0a 09 09 09 n new worker....
0fc0: 23 20 61 6c 6c 20 77 6f 72 6b 65 72 73 20 61 72 # all workers ar
0fd0: 65 20 6c 6f 67 67 65 64 20 73 6f 20 77 65 20 63 e logged so we c
0fe0: 61 6e 20 6b 69 6c 6c 20 74 68 65 6d 20 6f 6e 20 an kill them on
0ff0: 63 6c 6f 73 65 28 29 0a 09 09 09 73 65 6c 66 2e close()....self.
1000: 5f 77 6f 72 6b 65 72 20 3d 20 67 65 76 65 6e 74 _worker = gevent
1010: 2e 73 70 61 77 6e 28 73 65 6c 66 2e 5f 77 72 69 .spawn(self._wri
1020: 74 65 72 29 0a 0a 09 64 65 66 20 5f 77 72 69 74 ter)...def _writ
1030: 65 72 28 73 65 6c 66 29 3a 0a 09 09 23 20 68 65 er(self):...# he
1040: 72 65 20 77 65 20 61 72 65 20 6c 6f 63 6b 69 6e re we are lockin
1050: 67 20 74 68 65 20 71 75 65 75 65 20 73 6f 20 77 g the queue so w
1060: 65 20 63 61 6e 20 62 65 20 73 75 72 65 20 77 65 e can be sure we
1070: 20 61 72 65 20 74 68 65 20 6f 6e 6c 79 20 6f 6e are the only on
1080: 65 0a 09 09 77 68 69 6c 65 20 6e 6f 74 20 73 65 e...while not se
1090: 6c 66 2e 5f 74 61 69 6c 2e 65 6d 70 74 79 28 29 lf._tail.empty()
10a0: 3a 0a 09 09 09 6c 6f 67 67 69 6e 67 2e 68 61 6e :....logging.han
10b0: 64 6c 65 72 73 2e 53 79 73 4c 6f 67 48 61 6e 64 dlers.SysLogHand
10c0: 6c 65 72 2e 65 6d 69 74 28 73 65 6c 66 2c 20 73 ler.emit(self, s
10d0: 65 6c 66 2e 5f 74 61 69 6c 2e 67 65 74 28 29 29 elf._tail.get())
10e0: 0a 09 09 73 65 6c 66 2e 5f 77 6f 72 6b 65 72 20 ...self._worker
10f0: 3d 20 4e 6f 6e 65 0a 0a 09 64 65 66 20 63 6c 6f = None...def clo
1100: 73 65 28 73 65 6c 66 29 3a 0a 09 09 69 66 20 73 se(self):...if s
1110: 65 6c 66 2e 5f 77 6f 72 6b 65 72 20 21 3d 20 4e elf._worker != N
1120: 6f 6e 65 3a 0a 09 09 09 67 65 76 65 6e 74 2e 6b one:....gevent.k
1130: 69 6c 6c 28 73 65 6c 66 2e 5f 77 6f 72 6b 65 72 ill(self._worker
1140: 29 0a 09 09 6c 6f 67 67 69 6e 67 2e 68 61 6e 64 )...logging.hand
1150: 6c 65 72 73 2e 53 79 73 4c 6f 67 48 61 6e 64 6c lers.SysLogHandl
1160: 65 72 2e 63 6c 6f 73 65 28 73 65 6c 66 29 0a 0a er.close(self)..
1170: 6c 6f 67 67 65 72 20 3d 20 6c 6f 67 67 69 6e 67 logger = logging
1180: 2e 67 65 74 4c 6f 67 67 65 72 28 27 73 71 75 69 .getLogger('squi
1190: 64 54 61 67 27 29 0a 6c 6f 67 67 65 72 2e 73 65 dTag').logger.se
11a0: 74 4c 65 76 65 6c 28 6c 6f 67 67 69 6e 67 2e 49 tLevel(logging.I
11b0: 4e 46 4f 29 0a 68 61 6e 64 6c 65 72 20 3d 20 53 NFO).handler = S
11c0: 79 73 4c 6f 67 48 61 6e 64 6c 65 72 51 75 65 75 ysLogHandlerQueu
11d0: 65 28 29 0a 68 61 6e 64 6c 65 72 2e 73 65 74 46 e().handler.setF
11e0: 6f 72 6d 61 74 74 65 72 28 6c 6f 67 67 69 6e 67 ormatter(logging
11f0: 2e 46 6f 72 6d 61 74 74 65 72 28 73 74 72 28 27 .Formatter(str('
1200: 73 71 75 69 64 54 61 67 5b 25 28 70 72 6f 63 65 squidTag[%(proce
1210: 73 73 29 73 5d 3a 20 25 28 6d 65 73 73 61 67 65 ss)s]: %(message
1220: 29 73 27 29 29 29 0a 6c 6f 67 67 65 72 2e 61 64 )s'))).logger.ad
1230: 64 48 61 6e 64 6c 65 72 28 68 61 6e 64 6c 65 72 dHandler(handler
1240: 29 0a 0a 23 20 74 69 6e 79 20 77 72 61 70 70 65 )..# tiny wrappe
1250: 72 20 61 72 6f 75 6e 64 20 61 20 66 69 6c 65 20 r around a file
1260: 74 6f 20 6d 61 6b 65 20 72 65 61 64 73 20 66 72 to make reads fr
1270: 6f 6d 20 69 74 20 67 65 76 65 6e 74 61 62 6c 65 om it geventable
1280: 0a 23 20 6f 72 20 73 68 6f 75 6c 64 20 69 20 6d .# or should i m
1290: 6f 76 65 20 74 68 69 73 20 73 6f 6d 65 77 68 65 ove this somewhe
12a0: 72 65 3f 0a 0a 63 6c 61 73 73 20 46 52 65 61 64 re?..class FRead
12b0: 6c 69 6e 65 51 75 65 75 65 28 67 65 76 65 6e 74 lineQueue(gevent
12c0: 2e 71 75 65 75 65 2e 51 75 65 75 65 29 3a 0a 09 .queue.Queue):..
12d0: 23 20 73 74 6f 72 69 6e 67 20 66 69 6c 65 20 64 # storing file d
12e0: 65 73 63 72 69 70 74 6f 72 2c 20 6c 65 66 74 6f escriptor, lefto
12f0: 76 65 72 0a 09 5f 5f 73 6c 6f 74 73 5f 5f 20 3d ver..__slots__ =
1300: 20 66 72 6f 7a 65 6e 73 65 74 28 5b 27 5f 69 6f frozenset(['_io
1310: 27 2c 20 27 5f 66 69 6c 65 6e 6f 27 2c 20 27 5f ', '_fileno', '_
1320: 74 61 69 6c 27 5d 29 0a 0a 09 64 65 66 20 5f 5f tail'])...def __
1330: 69 6e 69 74 5f 5f 28 73 65 6c 66 2c 20 66 64 2c init__(self, fd,
1340: 20 63 6c 6f 73 65 66 64 20 3d 20 54 72 75 65 29 closefd = True)
1350: 3a 0a 09 09 69 6d 70 6f 72 74 20 69 6f 0a 09 09 :...import io...
1360: 23 20 69 6e 69 74 69 61 6c 69 73 69 6e 67 20 63 # initialising c
1370: 6c 61 73 73 0a 09 09 67 65 76 65 6e 74 2e 71 75 lass...gevent.qu
1380: 65 75 65 2e 51 75 65 75 65 2e 5f 5f 69 6e 69 74 eue.Queue.__init
1390: 5f 5f 28 73 65 6c 66 29 0a 09 09 23 20 73 74 6f __(self)...# sto
13a0: 72 69 6e 67 20 66 69 6c 65 20 64 65 73 63 72 69 ring file descri
13b0: 70 74 6f 72 0a 09 09 73 65 6c 66 2e 5f 66 69 6c ptor...self._fil
13c0: 65 6e 6f 20 3d 20 66 64 2e 66 69 6c 65 6e 6f 28 eno = fd.fileno(
13d0: 29 0a 09 09 73 65 6c 66 2e 5f 69 6f 20 3d 20 69 )...self._io = i
13e0: 6f 2e 46 69 6c 65 49 4f 28 73 65 6c 66 2e 5f 66 o.FileIO(self._f
13f0: 69 6c 65 6e 6f 2c 20 27 72 27 2c 20 63 6c 6f 73 ileno, 'r', clos
1400: 65 66 64 29 0a 09 09 23 20 75 73 69 6e 67 20 65 efd)...# using e
1410: 6d 70 74 79 20 74 61 69 6c 0a 09 09 73 65 6c 66 mpty tail...self
1420: 2e 5f 74 61 69 6c 20 3d 20 27 27 0a 09 09 23 20 ._tail = ''...#
1430: 73 65 74 74 69 6e 67 20 75 70 20 65 76 65 6e 74 setting up event
1440: 0a 09 09 73 65 6c 66 2e 5f 69 6e 73 74 61 6c 6c ...self._install
1450: 5f 77 61 69 74 28 29 0a 0a 09 64 65 66 20 5f 69 _wait()...def _i
1460: 6e 73 74 61 6c 6c 5f 77 61 69 74 28 73 65 6c 66 nstall_wait(self
1470: 29 3a 0a 09 09 23 20 70 75 74 74 69 6e 67 20 66 ):...# putting f
1480: 69 6c 65 20 74 6f 20 6e 6f 6e 62 6c 6f 63 6b 69 ile to nonblocki
1490: 6e 67 20 6d 6f 64 65 0a 09 09 66 63 6e 74 6c 2e ng mode...fcntl.
14a0: 66 63 6e 74 6c 28 73 65 6c 66 2e 5f 66 69 6c 65 fcntl(self._file
14b0: 6e 6f 2c 20 66 63 6e 74 6c 2e 46 5f 53 45 54 46 no, fcntl.F_SETF
14c0: 4c 2c 20 66 63 6e 74 6c 2e 66 63 6e 74 6c 28 73 L, fcntl.fcntl(s
14d0: 65 6c 66 2e 5f 66 69 6c 65 6e 6f 2c 20 66 63 6e elf._fileno, fcn
14e0: 74 6c 2e 46 5f 47 45 54 46 4c 29 20 20 7c 20 6f tl.F_GETFL) | o
14f0: 73 2e 4f 5f 4e 4f 4e 42 4c 4f 43 4b 29 0a 09 09 s.O_NONBLOCK)...
1500: 23 20 69 6e 73 74 61 6c 6c 69 6e 67 20 65 76 65 # installing eve
1510: 6e 74 20 68 61 6e 64 6c 65 72 0a 09 09 67 65 76 nt handler...gev
1520: 65 6e 74 2e 63 6f 72 65 2e 72 65 61 64 5f 65 76 ent.core.read_ev
1530: 65 6e 74 28 73 65 6c 66 2e 5f 66 69 6c 65 6e 6f ent(self._fileno
1540: 2c 20 73 65 6c 66 2e 5f 77 61 69 74 5f 68 65 6c , self._wait_hel
1550: 70 65 72 29 0a 0a 09 64 65 66 20 5f 77 61 69 74 per)...def _wait
1560: 5f 68 65 6c 70 65 72 28 73 65 6c 66 2c 20 65 76 _helper(self, ev
1570: 2c 20 65 76 74 79 70 65 29 3a 0a 09 09 23 20 72 , evtype):...# r
1580: 65 61 64 69 6e 67 20 6f 6e 65 20 62 75 66 66 65 eading one buffe
1590: 72 20 66 72 6f 6d 20 73 74 72 65 61 6d 0a 09 09 r from stream...
15a0: 62 75 66 20 3d 20 73 65 6c 66 2e 5f 69 6f 2e 72 buf = self._io.r
15b0: 65 61 64 28 34 30 39 36 29 0a 09 09 23 20 73 70 ead(4096)...# sp
15c0: 6c 69 74 74 69 6e 67 20 73 74 72 65 61 6d 20 62 litting stream b
15d0: 79 20 6c 69 6e 65 20 65 6e 64 73 0a 09 09 72 6f y line ends...ro
15e0: 77 73 20 3d 20 62 75 66 2e 64 65 63 6f 64 65 28 ws = buf.decode(
15f0: 27 6c 31 27 29 2e 73 70 6c 69 74 28 27 5c 6e 27 'l1').split('\n'
1600: 29 0a 09 09 23 20 61 64 64 69 6e 67 20 74 61 69 )...# adding tai
1610: 6c 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 65 l to the first e
1620: 6c 65 6d 65 6e 74 20 69 66 20 74 68 65 72 65 20 lement if there
1630: 69 73 20 73 6f 6d 65 20 74 61 69 6c 0a 09 09 69 is some tail...i
1640: 66 20 6c 65 6e 28 73 65 6c 66 2e 5f 74 61 69 6c f len(self._tail
1650: 29 20 3e 20 30 3a 0a 09 09 09 72 6f 77 73 5b 30 ) > 0:....rows[0
1660: 5d 20 3d 20 73 65 6c 66 2e 5f 74 61 69 6c 20 2b ] = self._tail +
1670: 20 72 6f 77 73 5b 30 5d 0a 09 09 23 20 70 6f 70 rows[0]...# pop
1680: 70 69 6e 67 20 6f 75 74 20 6c 61 73 74 20 28 69 ping out last (i
1690: 6e 63 6f 6d 70 6c 65 74 65 29 20 65 6c 65 6d 65 ncomplete) eleme
16a0: 6e 74 0a 09 09 73 65 6c 66 2e 5f 74 61 69 6c 20 nt...self._tail
16b0: 3d 20 72 6f 77 73 2e 70 6f 70 28 2d 31 29 0a 09 = rows.pop(-1)..
16c0: 09 23 20 64 72 6f 70 70 69 6e 67 20 61 6c 6c 20 .# dropping all
16d0: 63 6f 6d 70 6c 65 74 65 20 65 6c 65 6d 65 6e 74 complete element
16e0: 73 20 74 6f 20 74 68 65 20 71 75 65 75 65 0a 09 s to the queue..
16f0: 09 66 6f 72 20 72 6f 77 20 69 6e 20 72 6f 77 73 .for row in rows
1700: 3a 0a 09 09 09 73 65 6c 66 2e 70 75 74 5f 6e 6f :....self.put_no
1710: 77 61 69 74 28 72 6f 77 29 0a 09 09 09 6c 6f 67 wait(row)....log
1720: 67 65 72 2e 69 6e 66 6f 28 27 3c 20 27 20 2b 20 ger.info('< ' +
1730: 72 6f 77 29 0a 09 09 69 66 20 6c 65 6e 28 62 75 row)...if len(bu
1740: 66 29 20 3e 20 30 3a 0a 09 09 09 23 20 6e 6f 20 f) > 0:....# no
1750: 45 4f 46 2c 20 72 65 69 6e 73 74 61 6c 6c 69 6e EOF, reinstallin
1760: 67 20 65 76 65 6e 74 20 68 61 6e 64 6c 65 72 0a g event handler.
1770: 09 09 09 67 65 76 65 6e 74 2e 63 6f 72 65 2e 72 ...gevent.core.r
1780: 65 61 64 5f 65 76 65 6e 74 28 73 65 6c 66 2e 5f ead_event(self._
1790: 66 69 6c 65 6e 6f 2c 20 73 65 6c 66 2e 5f 77 61 fileno, self._wa
17a0: 69 74 5f 68 65 6c 70 65 72 29 0a 09 09 65 6c 73 it_helper)...els
17b0: 65 3a 0a 09 09 09 23 20 45 4f 46 20 66 6f 75 6e e:....# EOF foun
17c0: 64 2c 20 73 65 6e 64 69 6e 67 20 45 4f 46 20 74 d, sending EOF t
17d0: 6f 20 71 75 65 75 65 0a 09 09 09 73 65 6c 66 2e o queue....self.
17e0: 70 75 74 5f 6e 6f 77 61 69 74 28 4e 6f 6e 65 29 put_nowait(None)
17f0: 0a 0a 73 74 64 69 6e 20 3d 20 46 52 65 61 64 6c ..stdin = FReadl
1800: 69 6e 65 51 75 65 75 65 28 73 79 73 2e 73 74 64 ineQueue(sys.std
1810: 69 6e 2c 20 46 61 6c 73 65 29 0a 0a 23 20 77 72 in, False)..# wr
1820: 61 70 70 65 72 20 61 67 61 69 6e 73 74 20 66 69 apper against fi
1830: 6c 65 20 68 61 6e 64 6c 65 72 20 74 68 61 74 20 le handler that
1840: 6d 61 6b 65 73 20 70 6f 73 73 69 62 6c 65 20 74 makes possible t
1850: 6f 20 71 75 65 75 65 20 73 6f 6d 65 20 77 72 69 o queue some wri
1860: 74 65 73 20 77 69 74 68 6f 75 74 20 73 74 61 6c tes without stal
1870: 6c 69 6e 67 0a 0a 63 6c 61 73 73 20 46 57 72 69 ling..class FWri
1880: 74 65 6c 69 6e 65 51 75 65 75 65 28 67 65 76 65 telineQueue(geve
1890: 6e 74 2e 71 75 65 75 65 2e 4a 6f 69 6e 61 62 6c nt.queue.Joinabl
18a0: 65 51 75 65 75 65 29 3a 0a 09 23 20 73 74 6f 72 eQueue):..# stor
18b0: 69 6e 67 20 66 69 6c 65 6e 6f 2c 20 69 6f 20 69 ing fileno, io i
18c0: 6e 74 65 72 66 61 63 65 2c 20 6c 65 66 74 6f 76 nterface, leftov
18d0: 65 72 0a 09 5f 5f 73 6c 6f 74 73 5f 5f 20 3d 20 er..__slots__ =
18e0: 66 72 6f 7a 65 6e 73 65 74 28 5b 27 5f 66 69 6c frozenset(['_fil
18f0: 65 6e 6f 27 2c 20 27 5f 69 6f 27 2c 20 27 5f 74 eno', '_io', '_t
1900: 61 69 6c 27 5d 29 0a 0a 09 64 65 66 20 5f 5f 69 ail'])...def __i
1910: 6e 69 74 5f 5f 28 73 65 6c 66 2c 20 66 64 2c 20 nit__(self, fd,
1920: 63 6c 6f 73 65 66 64 20 3d 20 54 72 75 65 29 3a closefd = True):
1930: 0a 09 09 69 6d 70 6f 72 74 20 69 6f 0a 09 09 23 ...import io...#
1940: 20 69 6e 69 74 69 61 6c 69 73 69 6e 67 20 63 6c initialising cl
1950: 61 73 73 0a 09 09 67 65 76 65 6e 74 2e 71 75 65 ass...gevent.que
1960: 75 65 2e 4a 6f 69 6e 61 62 6c 65 51 75 65 75 65 ue.JoinableQueue
1970: 2e 5f 5f 69 6e 69 74 5f 5f 28 73 65 6c 66 29 0a .__init__(self).
1980: 09 09 23 20 73 74 6f 72 69 6e 67 20 66 69 6c 65 ..# storing file
1990: 6e 6f 0a 09 09 73 65 6c 66 2e 5f 66 69 6c 65 6e no...self._filen
19a0: 6f 20 3d 20 66 64 2e 66 69 6c 65 6e 6f 28 29 0a o = fd.fileno().
19b0: 09 09 23 20 63 72 65 61 74 69 6e 67 20 69 6e 74 ..# creating int
19c0: 65 72 66 61 63 65 0a 09 09 73 65 6c 66 2e 5f 69 erface...self._i
19d0: 6f 20 3d 20 69 6f 2e 46 69 6c 65 49 4f 28 73 65 o = io.FileIO(se
19e0: 6c 66 2e 5f 66 69 6c 65 6e 6f 2c 20 27 77 27 2c lf._fileno, 'w',
19f0: 20 63 6c 6f 73 65 66 64 29 0a 09 09 23 20 75 73 closefd)...# us
1a00: 69 6e 67 20 65 6d 70 74 79 20 74 61 69 6c 0a 09 ing empty tail..
1a10: 09 73 65 6c 66 2e 5f 74 61 69 6c 20 3d 20 4e 6f .self._tail = No
1a20: 6e 65 0a 09 09 23 20 70 75 74 74 69 6e 67 20 66 ne...# putting f
1a30: 69 6c 65 20 74 6f 20 6e 6f 6e 62 6c 6f 63 6b 69 ile to nonblocki
1a40: 6e 67 20 6d 6f 64 65 0a 09 09 66 63 6e 74 6c 2e ng mode...fcntl.
1a50: 66 63 6e 74 6c 28 73 65 6c 66 2e 5f 66 69 6c 65 fcntl(self._file
1a60: 6e 6f 2c 20 66 63 6e 74 6c 2e 46 5f 53 45 54 46 no, fcntl.F_SETF
1a70: 4c 2c 20 66 63 6e 74 6c 2e 66 63 6e 74 6c 28 73 L, fcntl.fcntl(s
1a80: 65 6c 66 2e 5f 66 69 6c 65 6e 6f 2c 20 66 63 6e elf._fileno, fcn
1a90: 74 6c 2e 46 5f 47 45 54 46 4c 29 20 7c 20 6f 73 tl.F_GETFL) | os
1aa0: 2e 4f 5f 4e 4f 4e 42 4c 4f 43 4b 29 0a 0a 09 64 .O_NONBLOCK)...d
1ab0: 65 66 20 5f 5f 64 65 6c 5f 5f 28 73 65 6c 66 29 ef __del__(self)
1ac0: 3a 0a 09 09 23 20 70 75 72 67 65 20 71 75 65 75 :...# purge queu
1ad0: 65 20 62 65 66 6f 72 65 20 64 65 6c 65 74 69 6e e before deletin
1ae0: 67 0a 09 09 69 66 20 6e 6f 74 20 73 65 6c 66 2e g...if not self.
1af0: 65 6d 70 74 79 28 29 3a 0a 09 09 09 73 65 6c 66 empty():....self
1b00: 2e 6a 6f 69 6e 28 29 0a 0a 09 64 65 66 20 70 75 .join()...def pu
1b10: 74 28 73 65 6c 66 2c 20 69 74 65 6d 2c 20 62 6c t(self, item, bl
1b20: 6f 63 6b 3d 54 72 75 65 2c 20 74 69 6d 65 6f 75 ock=True, timeou
1b30: 74 3d 4e 6f 6e 65 29 3a 0a 09 09 23 20 63 61 6c t=None):...# cal
1b40: 6c 69 6e 67 20 72 65 61 6c 20 70 75 74 0a 09 09 ling real put...
1b50: 67 65 76 65 6e 74 2e 71 75 65 75 65 2e 4a 6f 69 gevent.queue.Joi
1b60: 6e 61 62 6c 65 51 75 65 75 65 2e 70 75 74 28 73 nableQueue.put(s
1b70: 65 6c 66 2c 20 69 74 65 6d 2c 20 62 6c 6f 63 6b elf, item, block
1b80: 2c 20 74 69 6d 65 6f 75 74 29 0a 09 09 23 20 69 , timeout)...# i
1b90: 6e 73 74 61 6c 6c 69 6e 67 20 65 76 65 6e 74 20 nstalling event
1ba0: 68 61 6e 64 6c 65 72 0a 09 09 67 65 76 65 6e 74 handler...gevent
1bb0: 2e 63 6f 72 65 2e 77 72 69 74 65 5f 65 76 65 6e .core.write_even
1bc0: 74 28 73 65 6c 66 2e 5f 66 69 6c 65 6e 6f 2c 20 t(self._fileno,
1bd0: 73 65 6c 66 2e 5f 77 61 69 74 5f 68 65 6c 70 65 self._wait_helpe
1be0: 72 29 0a 0a 09 64 65 66 20 5f 77 61 69 74 5f 68 r)...def _wait_h
1bf0: 65 6c 70 65 72 28 73 65 6c 66 2c 20 65 76 2c 20 elper(self, ev,
1c00: 65 76 74 79 70 65 29 3a 0a 09 09 23 20 58 58 58 evtype):...# XXX
1c10: 20 65 76 2c 20 65 76 74 79 70 65 20 63 68 65 63 ev, evtype chec
1c20: 6b 69 6e 67 3f 0a 09 09 23 20 63 68 65 63 6b 69 king?...# checki
1c30: 6e 67 20 6c 65 66 74 6f 76 65 72 0a 09 09 77 68 ng leftover...wh
1c40: 69 6c 65 20 54 72 75 65 3a 0a 09 09 09 69 66 20 ile True:....if
1c50: 73 65 6c 66 2e 5f 74 61 69 6c 20 3d 3d 20 4e 6f self._tail == No
1c60: 6e 65 3a 0a 09 09 09 09 74 72 79 3a 0a 09 09 09 ne:.....try:....
1c70: 09 09 73 65 6c 66 2e 5f 74 61 69 6c 20 3d 20 73 ..self._tail = s
1c80: 74 72 28 73 65 6c 66 2e 67 65 74 5f 6e 6f 77 61 tr(self.get_nowa
1c90: 69 74 28 29 29 2e 65 6e 63 6f 64 65 28 27 75 74 it()).encode('ut
1ca0: 66 2d 38 27 29 20 2b 20 27 5c 6e 27 0a 09 09 09 f-8') + '\n'....
1cb0: 09 65 78 63 65 70 74 20 67 65 76 65 6e 74 2e 71 .except gevent.q
1cc0: 75 65 75 65 2e 45 6d 70 74 79 3a 0a 09 09 09 09 ueue.Empty:.....
1cd0: 09 73 65 6c 66 2e 5f 74 61 69 6c 20 3d 20 4e 6f .self._tail = No
1ce0: 6e 65 0a 09 09 09 09 09 72 65 74 75 72 6e 0a 09 ne......return..
1cf0: 09 09 23 20 77 72 69 74 69 6e 67 20 74 61 69 6c ..# writing tail
1d00: 0a 09 09 09 77 72 69 74 74 65 6e 20 3d 20 73 65 ....written = se
1d10: 6c 66 2e 5f 69 6f 2e 77 72 69 74 65 28 73 65 6c lf._io.write(sel
1d20: 66 2e 5f 74 61 69 6c 29 0a 09 09 09 6c 65 6e 67 f._tail)....leng
1d30: 74 68 20 3d 20 6c 65 6e 28 73 65 6c 66 2e 5f 74 th = len(self._t
1d40: 61 69 6c 29 0a 09 09 09 69 66 20 77 72 69 74 74 ail)....if writt
1d50: 65 6e 20 3d 3d 20 6c 65 6e 67 74 68 3a 0a 09 09 en == length:...
1d60: 09 09 73 65 6c 66 2e 5f 74 61 69 6c 20 3d 20 4e ..self._tail = N
1d70: 6f 6e 65 0a 09 09 09 65 6c 69 66 20 77 72 69 74 one....elif writ
1d80: 74 65 6e 20 3c 20 6c 65 6e 67 74 68 3a 0a 09 09 ten < length:...
1d90: 09 09 73 65 6c 66 2e 5f 74 61 69 6c 20 3d 20 73 ..self._tail = s
1da0: 65 6c 66 2e 5f 74 61 69 6c 5b 77 72 69 74 74 65 elf._tail[writte
1db0: 6e 3a 5d 0a 09 09 09 09 62 72 65 61 6b 0a 09 09 n:].....break...
1dc0: 09 65 6c 73 65 3a 0a 09 09 09 09 62 72 65 61 6b .else:.....break
1dd0: 0a 09 09 23 20 72 65 69 6e 73 74 61 6c 6c 69 6e ...# reinstallin
1de0: 67 20 65 76 65 6e 74 20 68 61 6e 64 6c 65 72 0a g event handler.
1df0: 09 09 67 65 76 65 6e 74 2e 63 6f 72 65 2e 77 72 ..gevent.core.wr
1e00: 69 74 65 5f 65 76 65 6e 74 28 73 65 6c 66 2e 5f ite_event(self._
1e10: 66 69 6c 65 6e 6f 2c 20 73 65 6c 66 2e 5f 77 61 fileno, self._wa
1e20: 69 74 5f 68 65 6c 70 65 72 29 0a 0a 23 20 77 72 it_helper)..# wr
1e30: 61 70 70 65 72 20 61 72 6f 75 6e 64 20 64 61 74 apper around dat
1e40: 61 62 61 73 65 0a 63 6c 61 73 73 20 74 61 67 44 abase.class tagD
1e50: 42 28 6f 62 6a 65 63 74 29 3a 0a 09 5f 5f 73 6c B(object):..__sl
1e60: 6f 74 73 5f 5f 20 3d 20 66 72 6f 7a 65 6e 73 65 ots__ = frozense
1e70: 74 28 5b 27 5f 63 75 72 73 6f 72 27 2c 20 27 5f t(['_cursor', '_
1e80: 64 62 27 5d 29 0a 0a 09 64 65 66 20 5f 5f 69 6e db'])...def __in
1e90: 69 74 5f 5f 28 73 65 6c 66 29 3a 0a 09 09 63 6f it__(self):...co
1ea0: 6e 66 69 67 2e 73 65 63 74 69 6f 6e 28 27 64 61 nfig.section('da
1eb0: 74 61 62 61 73 65 27 29 0a 09 09 69 66 20 63 6f tabase')...if co
1ec0: 6e 66 69 67 5b 27 68 6f 73 74 27 5d 20 3d 3d 20 nfig['host'] ==
1ed0: 4e 6f 6e 65 3a 0a 09 09 09 73 65 6c 66 2e 5f 64 None:....self._d
1ee0: 62 20 3d 20 70 73 79 63 6f 70 67 32 2e 63 6f 6e b = psycopg2.con
1ef0: 6e 65 63 74 28 0a 09 09 09 09 64 61 74 61 62 61 nect(.....databa
1f00: 73 65 20 3d 20 63 6f 6e 66 69 67 5b 27 64 61 74 se = config['dat
1f10: 61 62 61 73 65 27 5d 2c 0a 09 09 09 09 75 73 65 abase'],.....use
1f20: 72 20 3d 20 63 6f 6e 66 69 67 5b 27 75 73 65 72 r = config['user
1f30: 27 5d 2c 0a 09 09 09 09 70 61 73 73 77 6f 72 64 '],.....password
1f40: 20 3d 20 63 6f 6e 66 69 67 5b 27 70 61 73 73 77 = config['passw
1f50: 6f 72 64 27 5d 0a 09 09 09 29 0a 09 09 65 6c 73 ord']....)...els
1f60: 65 3a 0a 09 09 09 73 65 6c 66 2e 5f 64 62 20 3d e:....self._db =
1f70: 20 70 73 79 63 6f 70 67 32 2e 63 6f 6e 6e 65 63 psycopg2.connec
1f80: 74 28 0a 09 09 09 09 64 61 74 61 62 61 73 65 20 t(.....database
1f90: 3d 20 63 6f 6e 66 69 67 5b 27 64 61 74 61 62 61 = config['databa
1fa0: 73 65 27 5d 2c 0a 09 09 09 09 68 6f 73 74 20 3d se'],.....host =
1fb0: 20 63 6f 6e 66 69 67 5b 27 68 6f 73 74 27 5d 2c config['host'],
1fc0: 0a 09 09 09 09 75 73 65 72 20 3d 20 63 6f 6e 66 .....user = conf
1fd0: 69 67 5b 27 75 73 65 72 27 5d 2c 0a 09 09 09 09 ig['user'],.....
1fe0: 70 61 73 73 77 6f 72 64 20 3d 20 63 6f 6e 66 69 password = confi
1ff0: 67 5b 27 70 61 73 73 77 6f 72 64 27 5d 0a 09 09 g['password']...
2000: 09 29 0a 09 09 73 65 6c 66 2e 5f 63 75 72 73 6f .)...self._curso
2010: 72 20 3d 20 73 65 6c 66 2e 5f 64 62 2e 63 75 72 r = self._db.cur
2020: 73 6f 72 28 29 0a 0a 09 64 65 66 20 5f 66 69 65 sor()...def _fie
2030: 6c 64 5f 6e 61 6d 65 73 28 73 65 6c 66 29 3a 0a ld_names(self):.
2040: 09 09 6e 61 6d 65 73 20 3d 20 5b 5d 0a 09 09 66 ..names = []...f
2050: 6f 72 20 72 65 63 6f 72 64 20 69 6e 20 73 65 6c or record in sel
2060: 66 2e 5f 63 75 72 73 6f 72 2e 64 65 73 63 72 69 f._cursor.descri
2070: 70 74 69 6f 6e 3a 0a 09 09 09 6e 61 6d 65 73 2e ption:....names.
2080: 61 70 70 65 6e 64 28 72 65 63 6f 72 64 2e 6e 61 append(record.na
2090: 6d 65 29 0a 09 09 72 65 74 75 72 6e 28 6e 61 6d me)...return(nam
20a0: 65 73 29 0a 0a 09 64 65 66 20 63 68 65 63 6b 28 es)...def check(
20b0: 73 65 6c 66 2c 20 73 69 74 65 2c 20 69 70 5f 61 self, site, ip_a
20c0: 64 64 72 65 73 73 29 3a 0a 09 09 73 65 6c 66 2e ddress):...self.
20d0: 5f 63 75 72 73 6f 72 2e 65 78 65 63 75 74 65 28 _cursor.execute(
20e0: 22 73 65 6c 65 63 74 20 2a 20 66 72 6f 6d 20 28 "select * from (
20f0: 73 65 6c 65 63 74 20 72 65 64 69 72 65 63 74 5f select redirect_
2100: 75 72 6c 2c 20 72 65 67 65 78 70 20 66 72 6f 6d url, regexp from
2110: 20 73 69 74 65 5f 72 75 6c 65 20 77 68 65 72 65 site_rule where
2120: 20 73 69 74 65 20 3c 40 20 74 72 69 70 64 6f 6d site <@ tripdom
2130: 61 69 6e 28 25 73 29 20 61 6e 64 20 6e 65 74 6d ain(%s) and netm
2140: 61 73 6b 20 3e 3e 3d 20 25 73 20 6f 72 64 65 72 ask >>= %s order
2150: 20 62 79 20 61 72 72 61 79 5f 6c 65 6e 67 74 68 by array_length
2160: 28 73 69 74 65 2c 20 31 29 20 64 65 73 63 29 20 (site, 1) desc)
2170: 61 20 67 72 6f 75 70 20 62 79 20 72 65 64 69 72 a group by redir
2180: 65 63 74 5f 75 72 6c 2c 20 72 65 67 65 78 70 22 ect_url, regexp"
2190: 2c 20 5b 73 69 74 65 2c 20 69 70 5f 61 64 64 72 , [site, ip_addr
21a0: 65 73 73 5d 29 0a 09 09 72 65 74 75 72 6e 28 73 ess])...return(s
21b0: 65 6c 66 2e 5f 63 75 72 73 6f 72 2e 66 65 74 63 elf._cursor.fetc
21c0: 68 61 6c 6c 28 29 29 0a 0a 09 64 65 66 20 64 75 hall())...def du
21d0: 6d 70 28 73 65 6c 66 29 3a 0a 09 09 73 65 6c 66 mp(self):...self
21e0: 2e 5f 63 75 72 73 6f 72 2e 65 78 65 63 75 74 65 ._cursor.execute
21f0: 28 22 73 65 6c 65 63 74 20 75 6e 74 72 69 70 28 ("select untrip(
2200: 73 69 74 65 29 20 61 73 20 73 69 74 65 2c 20 74 site) as site, t
2210: 61 67 3a 3a 74 65 78 74 2c 20 72 65 67 65 78 70 ag::text, regexp
2220: 20 66 72 6f 6d 20 75 72 6c 73 20 6f 72 64 65 72 from urls order
2230: 20 62 79 20 73 69 74 65 2c 20 74 61 67 22 29 0a by site, tag").
2240: 09 09 72 65 74 75 72 6e 28 73 65 6c 66 2e 5f 66 ..return(self._f
2250: 69 65 6c 64 5f 6e 61 6d 65 73 28 29 2c 20 73 65 ield_names(), se
2260: 6c 66 2e 5f 63 75 72 73 6f 72 2e 66 65 74 63 68 lf._cursor.fetch
2270: 61 6c 6c 28 29 29 0a 0a 09 64 65 66 20 6c 6f 61 all())...def loa
2280: 64 28 73 65 6c 66 2c 20 64 61 74 61 29 3a 0a 09 d(self, data):..
2290: 09 69 66 20 63 6f 6e 66 69 67 2e 6f 70 74 69 6f .if config.optio
22a0: 6e 73 2e 66 6c 75 73 68 5f 64 62 3a 0a 09 09 09 ns.flush_db:....
22b0: 73 65 6c 66 2e 5f 63 75 72 73 6f 72 2e 65 78 65 self._cursor.exe
22c0: 63 75 74 65 28 27 64 65 6c 65 74 65 20 66 72 6f cute('delete fro
22d0: 6d 20 75 72 6c 73 3b 27 29 0a 09 09 62 75 6e 64 m urls;')...bund
22e0: 6c 65 20 3d 20 5b 5d 0a 09 09 66 6f 72 20 72 6f le = []...for ro
22f0: 77 20 69 6e 20 64 61 74 61 3a 0a 09 09 09 69 66 w in data:....if
2300: 20 6c 65 6e 28 72 6f 77 29 20 3d 3d 20 32 3a 0a len(row) == 2:.
2310: 09 09 09 09 62 75 6e 64 6c 65 2e 61 70 70 65 6e ....bundle.appen
2320: 64 28 5b 72 6f 77 5b 30 5d 2c 20 72 6f 77 5b 31 d([row[0], row[1
2330: 5d 2c 20 4e 6f 6e 65 5d 29 0a 09 09 09 65 6c 73 ], None])....els
2340: 65 3a 0a 09 09 09 09 62 75 6e 64 6c 65 2e 61 70 e:.....bundle.ap
2350: 70 65 6e 64 28 5b 72 6f 77 5b 30 5d 2c 20 72 6f pend([row[0], ro
2360: 77 5b 31 5d 2c 20 72 6f 77 5b 32 5d 5d 29 0a 09 w[1], row[2]])..
2370: 09 73 65 6c 66 2e 5f 63 75 72 73 6f 72 2e 65 78 .self._cursor.ex
2380: 65 63 75 74 65 6d 61 6e 79 28 22 69 6e 73 65 72 ecutemany("inser
2390: 74 20 69 6e 74 6f 20 75 72 6c 73 20 28 73 69 74 t into urls (sit
23a0: 65 2c 20 74 61 67 2c 20 72 65 67 65 78 70 29 20 e, tag, regexp)
23b0: 76 61 6c 75 65 73 20 28 74 72 69 70 64 6f 6d 61 values (tripdoma
23c0: 69 6e 28 25 73 29 2c 20 25 73 2c 20 25 73 29 22 in(%s), %s, %s)"
23d0: 2c 20 62 75 6e 64 6c 65 29 0a 09 09 73 65 6c 66 , bundle)...self
23e0: 2e 5f 63 75 72 73 6f 72 2e 65 78 65 63 75 74 65 ._cursor.execute
23f0: 28 22 75 70 64 61 74 65 20 75 72 6c 73 20 73 65 ("update urls se
2400: 74 20 72 65 67 65 78 70 20 3d 20 4e 55 4c 4c 20 t regexp = NULL
2410: 77 68 65 72 65 20 72 65 67 65 78 70 20 3d 20 27 where regexp = '
2420: 27 22 29 0a 09 09 73 65 6c 66 2e 5f 64 62 2e 63 '")...self._db.c
2430: 6f 6d 6d 69 74 28 29 0a 0a 09 64 65 66 20 6c 6f ommit()...def lo
2440: 61 64 5f 63 6f 6e 66 28 73 65 6c 66 2c 20 63 73 ad_conf(self, cs
2450: 76 5f 64 61 74 61 29 3a 0a 09 09 73 65 6c 66 2e v_data):...self.
2460: 5f 63 75 72 73 6f 72 2e 65 78 65 63 75 74 65 28 _cursor.execute(
2470: 27 64 65 6c 65 74 65 20 66 72 6f 6d 20 72 75 6c 'delete from rul
2480: 65 73 3b 27 29 0a 09 09 62 75 6e 64 6c 65 20 3d es;')...bundle =
2490: 20 5b 5d 0a 09 09 66 6f 72 20 72 6f 77 20 69 6e []...for row in
24a0: 20 63 73 76 5f 64 61 74 61 3a 0a 09 09 09 62 75 csv_data:....bu
24b0: 6e 64 6c 65 2e 61 70 70 65 6e 64 28 5b 72 6f 77 ndle.append([row
24c0: 5b 30 5d 2c 20 72 6f 77 5b 31 5d 2c 20 69 6e 74 [0], row[1], int
24d0: 28 72 6f 77 5b 32 5d 29 2c 20 69 6e 74 28 72 6f (row[2]), int(ro
24e0: 77 5b 33 5d 29 2c 20 72 6f 77 5b 34 5d 2c 20 72 w[3]), row[4], r
24f0: 6f 77 5b 35 5d 2c 20 72 6f 77 5b 36 5d 5d 29 0a ow[5], row[6]]).
2500: 09 09 73 65 6c 66 2e 5f 63 75 72 73 6f 72 2e 65 ..self._cursor.e
2510: 78 65 63 75 74 65 6d 61 6e 79 28 22 69 6e 73 65 xecutemany("inse
2520: 72 74 20 69 6e 74 6f 20 72 75 6c 65 73 20 28 6e rt into rules (n
2530: 65 74 6d 61 73 6b 2c 20 72 65 64 69 72 65 63 74 etmask, redirect
2540: 5f 75 72 6c 2c 20 66 72 6f 6d 5f 77 65 65 6b 64 _url, from_weekd
2550: 61 79 2c 20 74 6f 5f 77 65 65 6b 64 61 79 2c 20 ay, to_weekday,
2560: 66 72 6f 6d 5f 74 69 6d 65 2c 20 74 6f 5f 74 69 from_time, to_ti
2570: 6d 65 2c 20 74 61 67 29 20 76 61 6c 75 65 73 20 me, tag) values
2580: 28 25 73 3a 3a 74 65 78 74 3a 3a 63 69 64 72 2c (%s::text::cidr,
2590: 20 25 73 2c 20 25 73 2c 20 25 73 2c 20 25 73 3a %s, %s, %s, %s:
25a0: 3a 74 65 78 74 3a 3a 74 69 6d 65 2c 20 25 73 3a :text::time, %s:
25b0: 3a 74 65 78 74 3a 3a 74 69 6d 65 2c 20 25 73 3a :text::time, %s:
25c0: 3a 74 65 78 74 3a 3a 74 65 78 74 5b 5d 29 22 2c :text::text[])",
25d0: 20 62 75 6e 64 6c 65 29 0a 09 09 73 65 6c 66 2e bundle)...self.
25e0: 5f 64 62 2e 63 6f 6d 6d 69 74 28 29 0a 0a 09 64 _db.commit()...d
25f0: 65 66 20 64 75 6d 70 5f 63 6f 6e 66 28 73 65 6c ef dump_conf(sel
2600: 66 29 3a 0a 09 09 73 65 6c 66 2e 5f 63 75 72 73 f):...self._curs
2610: 6f 72 2e 65 78 65 63 75 74 65 28 22 73 65 6c 65 or.execute("sele
2620: 63 74 20 6e 65 74 6d 61 73 6b 2c 20 72 65 64 69 ct netmask, redi
2630: 72 65 63 74 5f 75 72 6c 2c 20 66 72 6f 6d 5f 77 rect_url, from_w
2640: 65 65 6b 64 61 79 2c 20 74 6f 5f 77 65 65 6b 64 eekday, to_weekd
2650: 61 79 2c 20 66 72 6f 6d 5f 74 69 6d 65 2c 20 74 ay, from_time, t
2660: 6f 5f 74 69 6d 65 2c 20 74 61 67 3a 3a 74 65 78 o_time, tag::tex
2670: 74 20 66 72 6f 6d 20 72 75 6c 65 73 22 29 0a 09 t from rules")..
2680: 09 72 65 74 75 72 6e 28 73 65 6c 66 2e 5f 66 69 .return(self._fi
2690: 65 6c 64 5f 6e 61 6d 65 73 28 29 2c 20 73 65 6c eld_names(), sel
26a0: 66 2e 5f 63 75 72 73 6f 72 2e 66 65 74 63 68 61 f._cursor.fetcha
26b0: 6c 6c 28 29 29 0a 0a 23 20 61 62 73 74 72 61 63 ll())..# abstrac
26c0: 74 20 63 6c 61 73 73 20 77 69 74 68 20 62 61 73 t class with bas
26d0: 69 63 20 63 68 65 63 6b 69 6e 67 20 66 75 6e 63 ic checking func
26e0: 74 69 6f 6e 61 6c 69 74 79 0a 63 6c 61 73 73 20 tionality.class
26f0: 43 68 65 63 6b 65 72 28 6f 62 6a 65 63 74 29 3a Checker(object):
2700: 0a 09 5f 5f 73 6c 6f 74 73 5f 5f 20 3d 20 66 72 ..__slots__ = fr
2710: 6f 7a 65 6e 73 65 74 28 5b 27 5f 64 62 27 2c 20 ozenset(['_db',
2720: 27 5f 6c 6f 67 27 2c 20 27 5f 71 75 65 75 65 27 '_log', '_queue'
2730: 2c 20 27 5f 72 65 71 75 65 73 74 27 2c 20 27 5f , '_request', '_
2740: 73 74 64 6f 75 74 27 5d 29 0a 0a 09 64 65 66 20 stdout'])...def
2750: 5f 5f 69 6e 69 74 5f 5f 28 73 65 6c 66 2c 20 71 __init__(self, q
2760: 75 65 75 65 2c 20 6c 6f 67 67 65 72 29 3a 0a 09 ueue, logger):..
2770: 09 73 65 6c 66 2e 5f 64 62 20 3d 20 74 61 67 44 .self._db = tagD
2780: 42 28 29 0a 09 09 73 65 6c 66 2e 5f 6c 6f 67 20 B()...self._log
2790: 3d 20 6c 6f 67 67 65 72 0a 09 09 73 65 6c 66 2e = logger...self.
27a0: 5f 6c 6f 67 2e 69 6e 66 6f 28 27 73 74 61 72 74 _log.info('start
27b0: 65 64 27 29 0a 09 09 73 65 6c 66 2e 5f 72 65 71 ed')...self._req
27c0: 75 65 73 74 20 3d 20 72 65 2e 63 6f 6d 70 69 6c uest = re.compil
27d0: 65 28 27 5e 28 5b 30 2d 39 5d 2b 29 5c 20 28 28 e('^([0-9]+)\ ((
27e0: 68 74 74 70 7c 66 74 70 29 3a 5c 2f 5c 2f 29 3f http|ftp):\/\/)?
27f0: 28 5b 2d 5c 77 2e 5d 2b 29 28 3a 5b 30 2d 39 5d ([-\w.]+)(:[0-9]
2800: 2b 29 3f 28 5c 2f 28 5b 5e 20 5d 2a 29 29 3f 5c +)?(\/([^ ]*))?\
2810: 20 28 5b 30 2d 39 2e 3a 5d 2b 29 5c 2f 28 2d 7c ([0-9.:]+)\/(-|
2820: 5b 5c 77 5c 2e 5d 2b 29 5c 20 28 2d 7c 5c 77 2b [\w\.]+)\ (-|\w+
2830: 29 5c 20 28 2d 7c 47 45 54 7c 48 45 41 44 7c 50 )\ (-|GET|HEAD|P
2840: 4f 53 54 7c 43 4f 4e 4e 45 43 54 29 2e 2a 24 27 OST|CONNECT).*$'
2850: 29 0a 09 09 73 65 6c 66 2e 5f 71 75 65 75 65 20 )...self._queue
2860: 3d 20 71 75 65 75 65 0a 09 09 73 65 6c 66 2e 5f = queue...self._
2870: 73 74 64 6f 75 74 20 3d 20 46 57 72 69 74 65 6c stdout = FWritel
2880: 69 6e 65 51 75 65 75 65 28 73 79 73 2e 73 74 64 ineQueue(sys.std
2890: 6f 75 74 2c 20 46 61 6c 73 65 29 0a 0a 09 64 65 out, False)...de
28a0: 66 20 70 72 6f 63 65 73 73 28 73 65 6c 66 2c 20 f process(self,
28b0: 69 64 2c 20 73 69 74 65 2c 20 69 70 5f 61 64 64 id, site, ip_add
28c0: 72 65 73 73 2c 20 75 72 6c 5f 70 61 74 68 2c 20 ress, url_path,
28d0: 6c 69 6e 65 20 3d 20 4e 6f 6e 65 29 3a 0a 09 09 line = None):...
28e0: 23 73 65 6c 66 2e 5f 6c 6f 67 2e 69 6e 66 6f 28 #self._log.info(
28f0: 27 74 72 79 69 6e 67 20 7b 7d 27 2e 66 6f 72 6d 'trying {}'.form
2900: 61 74 28 73 69 74 65 29 29 0a 09 09 72 65 73 75 at(site))...resu
2910: 6c 74 20 3d 20 73 65 6c 66 2e 5f 64 62 2e 63 68 lt = self._db.ch
2920: 65 63 6b 28 73 69 74 65 2c 20 69 70 5f 61 64 64 eck(site, ip_add
2930: 72 65 73 73 29 0a 09 09 72 65 70 6c 79 20 3d 20 ress)...reply =
2940: 4e 6f 6e 65 0a 09 09 23 73 65 6c 66 2e 5f 6c 6f None...#self._lo
2950: 67 2e 69 6e 66 6f 28 27 67 6f 74 20 7b 7d 20 6c g.info('got {} l
2960: 69 6e 65 73 20 66 72 6f 6d 20 64 61 74 61 62 61 ines from databa
2970: 73 65 27 2e 66 6f 72 6d 61 74 28 6c 65 6e 28 72 se'.format(len(r
2980: 65 73 75 6c 74 29 29 29 0a 09 09 66 6f 72 20 72 esult)))...for r
2990: 6f 77 20 69 6e 20 72 65 73 75 6c 74 3a 0a 09 09 ow in result:...
29a0: 09 69 66 20 72 6f 77 20 21 3d 20 4e 6f 6e 65 20 .if row != None
29b0: 61 6e 64 20 72 6f 77 5b 30 5d 20 21 3d 20 4e 6f and row[0] != No
29c0: 6e 65 3a 0a 09 09 09 09 69 66 20 72 6f 77 5b 31 ne:.....if row[1
29d0: 5d 20 21 3d 20 4e 6f 6e 65 20 61 6e 64 20 75 72 ] != None and ur
29e0: 6c 5f 70 61 74 68 20 21 3d 20 4e 6f 6e 65 3a 0a l_path != None:.
29f0: 09 09 09 09 09 73 65 6c 66 2e 5f 6c 6f 67 2e 69 .....self._log.i
2a00: 6e 66 6f 28 27 74 72 79 69 6e 67 20 72 65 67 65 nfo('trying rege
2a10: 78 70 20 22 7b 7d 22 20 76 65 72 73 75 73 20 22 xp "{}" versus "
2a20: 7b 7d 22 27 2e 66 6f 72 6d 61 74 28 72 6f 77 5b {}"'.format(row[
2a30: 31 5d 2c 20 75 72 6c 5f 70 61 74 68 29 29 0a 09 1], url_path))..
2a40: 09 09 09 09 74 72 79 3a 0a 09 09 09 09 09 09 69 ....try:.......i
2a50: 66 20 72 65 2e 63 6f 6d 70 69 6c 65 28 72 6f 77 f re.compile(row
2a60: 5b 31 5d 29 2e 6d 61 74 63 68 28 75 72 6c 5f 70 [1]).match(url_p
2a70: 61 74 68 29 3a 0a 09 09 09 09 09 09 09 72 65 70 ath):........rep
2a80: 6c 79 20 3d 20 72 6f 77 5b 30 5d 2e 66 6f 72 6d ly = row[0].form
2a90: 61 74 28 68 6f 73 74 20 3d 20 73 69 74 65 2c 20 at(host = site,
2aa0: 70 61 74 68 20 3d 20 75 72 6c 5f 70 61 74 68 29 path = url_path)
2ab0: 0a 09 09 09 09 09 09 65 6c 73 65 3a 0a 09 09 09 .......else:....
2ac0: 09 09 09 09 63 6f 6e 74 69 6e 75 65 0a 09 09 09 ....continue....
2ad0: 09 09 65 78 63 65 70 74 3a 0a 09 09 09 09 09 09 ..except:.......
2ae0: 73 65 6c 66 2e 5f 6c 6f 67 2e 69 6e 66 6f 28 22 self._log.info("
2af0: 63 61 6e 27 74 20 63 6f 6d 70 69 6c 65 20 6f 72 can't compile or
2b00: 20 65 78 65 63 75 74 65 20 72 65 67 65 78 70 22 execute regexp"
2b10: 29 0a 09 09 09 09 65 6c 73 65 3a 0a 09 09 09 09 ).....else:.....
2b20: 09 72 65 70 6c 79 20 3d 20 72 6f 77 5b 30 5d 2e .reply = row[0].
2b30: 66 6f 72 6d 61 74 28 68 6f 73 74 20 3d 20 73 69 format(host = si
2b40: 74 65 2c 20 70 61 74 68 20 3d 20 75 72 6c 5f 70 te, path = url_p
2b50: 61 74 68 29 0a 09 09 09 69 66 20 72 65 70 6c 79 ath)....if reply
2b60: 20 21 3d 20 4e 6f 6e 65 3a 0a 09 09 09 09 73 65 != None:.....se
2b70: 6c 66 2e 77 72 69 74 65 6c 69 6e 65 28 27 7b 7d lf.writeline('{}
2b80: 20 7b 7d 27 2e 66 6f 72 6d 61 74 28 69 64 2c 20 {}'.format(id,
2b90: 72 65 70 6c 79 29 29 0a 09 09 09 09 72 65 74 75 reply)).....retu
2ba0: 72 6e 28 54 72 75 65 29 0a 09 09 73 65 6c 66 2e rn(True)...self.
2bb0: 77 72 69 74 65 6c 69 6e 65 28 27 7b 7d 27 2e 66 writeline('{}'.f
2bc0: 6f 72 6d 61 74 28 69 64 29 29 0a 0a 09 64 65 66 ormat(id))...def
2bd0: 20 6c 6f 6f 70 28 73 65 6c 66 29 3a 0a 09 09 77 loop(self):...w
2be0: 68 69 6c 65 20 54 72 75 65 3a 0a 09 09 09 6c 69 hile True:....li
2bf0: 6e 65 20 3d 20 73 65 6c 66 2e 5f 71 75 65 75 65 ne = self._queue
2c00: 2e 67 65 74 28 29 0a 09 09 09 69 66 20 6c 69 6e .get()....if lin
2c10: 65 20 3d 3d 20 4e 6f 6e 65 3a 0a 09 09 09 09 62 e == None:.....b
2c20: 72 65 61 6b 0a 09 09 09 23 73 65 6c 66 2e 5f 6c reak....#self._l
2c30: 6f 67 2e 69 6e 66 6f 28 27 72 65 71 75 65 73 74 og.info('request
2c40: 3a 20 27 20 2b 20 6c 69 6e 65 29 0a 09 09 09 72 : ' + line)....r
2c50: 65 71 75 65 73 74 20 3d 20 73 65 6c 66 2e 5f 72 equest = self._r
2c60: 65 71 75 65 73 74 2e 6d 61 74 63 68 28 6c 69 6e equest.match(lin
2c70: 65 29 0a 09 09 09 69 66 20 72 65 71 75 65 73 74 e)....if request
2c80: 3a 0a 09 09 09 09 69 64 20 3d 20 72 65 71 75 65 :.....id = reque
2c90: 73 74 2e 67 72 6f 75 70 28 31 29 0a 09 09 09 09 st.group(1).....
2ca0: 23 70 72 6f 74 6f 20 3d 20 72 65 71 75 65 73 74 #proto = request
2cb0: 2e 67 72 6f 75 70 28 33 29 0a 09 09 09 09 73 69 .group(3).....si
2cc0: 74 65 20 3d 20 72 65 71 75 65 73 74 2e 67 72 6f te = request.gro
2cd0: 75 70 28 34 29 0a 09 09 09 09 75 72 6c 5f 70 61 up(4).....url_pa
2ce0: 74 68 20 3d 20 72 65 71 75 65 73 74 2e 67 72 6f th = request.gro
2cf0: 75 70 28 37 29 0a 09 09 09 09 69 70 5f 61 64 64 up(7).....ip_add
2d00: 72 65 73 73 20 3d 20 72 65 71 75 65 73 74 2e 67 ress = request.g
2d10: 72 6f 75 70 28 38 29 0a 09 09 09 09 73 65 6c 66 roup(8).....self
2d20: 2e 70 72 6f 63 65 73 73 28 69 64 2c 20 73 69 74 .process(id, sit
2d30: 65 2c 20 69 70 5f 61 64 64 72 65 73 73 2c 20 75 e, ip_address, u
2d40: 72 6c 5f 70 61 74 68 2c 20 6c 69 6e 65 29 0a 09 rl_path, line)..
2d50: 09 09 65 6c 73 65 3a 0a 09 09 09 09 73 65 6c 66 ..else:.....self
2d60: 2e 5f 6c 6f 67 2e 69 6e 66 6f 28 27 62 61 64 20 ._log.info('bad
2d70: 72 65 71 75 65 73 74 27 29 0a 09 09 09 09 73 65 request').....se
2d80: 6c 66 2e 77 72 69 74 65 6c 69 6e 65 28 6c 69 6e lf.writeline(lin
2d90: 65 29 0a 0a 09 64 65 66 20 77 72 69 74 65 6c 69 e)...def writeli
2da0: 6e 65 28 73 65 6c 66 2c 20 73 74 72 69 6e 67 29 ne(self, string)
2db0: 3a 0a 09 09 73 65 6c 66 2e 5f 6c 6f 67 2e 69 6e :...self._log.in
2dc0: 66 6f 28 27 3e 20 27 20 2b 20 73 74 72 69 6e 67 fo('> ' + string
2dd0: 29 0a 09 09 73 65 6c 66 2e 5f 73 74 64 6f 75 74 )...self._stdout
2de0: 2e 70 75 74 28 73 74 72 69 6e 67 29 0a 0a 69 66 .put(string)..if
2df0: 20 63 6f 6e 66 69 67 2e 6f 70 74 69 6f 6e 73 2e config.options.
2e00: 64 75 6d 70 20 6f 72 20 63 6f 6e 66 69 67 2e 6f dump or config.o
2e10: 70 74 69 6f 6e 73 2e 6c 6f 61 64 20 6f 72 20 63 ptions.load or c
2e20: 6f 6e 66 69 67 2e 6f 70 74 69 6f 6e 73 2e 64 75 onfig.options.du
2e30: 6d 70 5f 63 6f 6e 66 20 6f 72 20 63 6f 6e 66 69 mp_conf or confi
2e40: 67 2e 6f 70 74 69 6f 6e 73 2e 6c 6f 61 64 5f 63 g.options.load_c
2e50: 6f 6e 66 3a 0a 09 69 6d 70 6f 72 74 20 63 73 76 onf:..import csv
2e60: 0a 0a 09 74 61 67 64 62 20 3d 20 74 61 67 44 42 ...tagdb = tagDB
2e70: 28 29 0a 09 64 61 74 61 5f 66 69 65 6c 64 73 20 ()..data_fields
2e80: 3d 20 5b 27 73 69 74 65 27 2c 20 27 74 61 67 27 = ['site', 'tag'
2e90: 2c 20 27 72 65 67 65 78 70 27 5d 0a 09 63 6f 6e , 'regexp']..con
2ea0: 66 5f 66 69 65 6c 64 73 20 3d 20 5b 27 6e 65 74 f_fields = ['net
2eb0: 6d 61 73 6b 27 2c 20 27 72 65 64 69 72 65 63 74 mask', 'redirect
2ec0: 5f 75 72 6c 27 2c 20 27 66 72 6f 6d 5f 77 65 65 _url', 'from_wee
2ed0: 6b 64 61 79 27 2c 20 27 74 6f 5f 77 65 65 6b 64 kday', 'to_weekd
2ee0: 61 79 27 2c 20 27 66 72 6f 6d 5f 74 69 6d 65 27 ay', 'from_time'
2ef0: 2c 20 27 74 6f 5f 74 69 6d 65 27 2c 20 27 74 61 , 'to_time', 'ta
2f00: 67 27 5d 0a 0a 09 69 66 20 63 6f 6e 66 69 67 2e g']...if config.
2f10: 6f 70 74 69 6f 6e 73 2e 64 75 6d 70 20 6f 72 20 options.dump or
2f20: 63 6f 6e 66 69 67 2e 6f 70 74 69 6f 6e 73 2e 64 config.options.d
2f30: 75 6d 70 5f 63 6f 6e 66 3a 0a 09 09 63 73 76 5f ump_conf:...csv_
2f40: 77 72 69 74 65 72 20 3d 20 63 73 76 2e 77 72 69 writer = csv.wri
2f50: 74 65 72 28 73 79 73 2e 73 74 64 6f 75 74 29 0a ter(sys.stdout).
2f60: 09 09 69 66 20 63 6f 6e 66 69 67 2e 6f 70 74 69 ..if config.opti
2f70: 6f 6e 73 2e 64 75 6d 70 3a 0a 09 09 09 64 75 6d ons.dump:....dum
2f80: 70 20 3d 20 74 61 67 64 62 2e 64 75 6d 70 28 29 p = tagdb.dump()
2f90: 0a 09 09 65 6c 69 66 20 63 6f 6e 66 69 67 2e 6f ...elif config.o
2fa0: 70 74 69 6f 6e 73 2e 64 75 6d 70 5f 63 6f 6e 66 ptions.dump_conf
2fb0: 3a 0a 09 09 09 64 75 6d 70 20 3d 20 74 61 67 64 :....dump = tagd
2fc0: 62 2e 64 75 6d 70 5f 63 6f 6e 66 28 29 0a 0a 09 b.dump_conf()...
2fd0: 09 63 73 76 5f 77 72 69 74 65 72 2e 77 72 69 74 .csv_writer.writ
2fe0: 65 72 6f 77 28 64 75 6d 70 5b 30 5d 29 0a 09 09 erow(dump[0])...
2ff0: 66 6f 72 20 6c 69 6e 65 20 69 6e 20 64 75 6d 70 for line in dump
3000: 5b 31 5d 3a 0a 09 09 09 63 73 76 5f 77 72 69 74 [1]:....csv_writ
3010: 65 72 2e 77 72 69 74 65 72 6f 77 28 6c 69 6e 65 er.writerow(line
3020: 29 0a 0a 09 65 6c 69 66 20 63 6f 6e 66 69 67 2e )...elif config.
3030: 6f 70 74 69 6f 6e 73 2e 6c 6f 61 64 20 6f 72 20 options.load or
3040: 63 6f 6e 66 69 67 2e 6f 70 74 69 6f 6e 73 2e 6c config.options.l
3050: 6f 61 64 5f 63 6f 6e 66 3a 0a 09 09 63 73 76 5f oad_conf:...csv_
3060: 72 65 61 64 65 72 20 3d 20 63 73 76 2e 72 65 61 reader = csv.rea
3070: 64 65 72 28 73 79 73 2e 73 74 64 69 6e 29 0a 09 der(sys.stdin)..
3080: 09 66 69 72 73 74 5f 72 6f 77 20 3d 20 6e 65 78 .first_row = nex
3090: 74 28 63 73 76 5f 72 65 61 64 65 72 29 0a 0a 09 t(csv_reader)...
30a0: 09 69 66 20 63 6f 6e 66 69 67 2e 6f 70 74 69 6f .if config.optio
30b0: 6e 73 2e 6c 6f 61 64 3a 0a 09 09 09 66 69 65 6c ns.load:....fiel
30c0: 64 73 20 3d 20 64 61 74 61 5f 66 69 65 6c 64 73 ds = data_fields
30d0: 0a 09 09 09 6c 6f 61 64 20 3d 20 74 61 67 64 62 ....load = tagdb
30e0: 2e 6c 6f 61 64 0a 09 09 65 6c 69 66 20 63 6f 6e .load...elif con
30f0: 66 69 67 2e 6f 70 74 69 6f 6e 73 2e 6c 6f 61 64 fig.options.load
3100: 5f 63 6f 6e 66 3a 0a 09 09 09 66 69 65 6c 64 73 _conf:....fields
3110: 20 3d 20 63 6f 6e 66 5f 66 69 65 6c 64 73 0a 09 = conf_fields..
3120: 09 09 6c 6f 61 64 20 3d 20 74 61 67 64 62 2e 6c ..load = tagdb.l
3130: 6f 61 64 5f 63 6f 6e 66 0a 0a 09 09 61 73 73 65 oad_conf....asse
3140: 72 74 20 66 69 72 73 74 5f 72 6f 77 20 3d 3d 20 rt first_row ==
3150: 66 69 65 6c 64 73 2c 20 27 46 69 6c 65 20 6d 75 fields, 'File mu
3160: 73 74 20 63 6f 6e 74 61 69 6e 20 63 73 76 20 64 st contain csv d
3170: 61 74 61 20 77 69 74 68 20 74 68 65 65 73 65 20 ata with theese
3180: 63 6f 6c 75 6d 6e 73 3a 20 27 20 2b 20 72 65 70 columns: ' + rep
3190: 72 28 66 69 65 6c 64 73 29 0a 09 09 6c 6f 61 64 r(fields)...load
31a0: 28 63 73 76 5f 72 65 61 64 65 72 29 0a 0a 65 6c (csv_reader)..el
31b0: 73 65 3a 0a 09 23 20 6d 61 69 6e 20 6c 6f 6f 70 se:..# main loop
31c0: 0a 09 43 68 65 63 6b 65 72 28 73 74 64 69 6e 2c ..Checker(stdin,
31d0: 20 6c 6f 67 67 65 72 29 2e 6c 6f 6f 70 28 29 0a logger).loop().