Squid url redirector

Hex Artifact Content
anonymous

Hex Artifact Content

Artifact faa884292fc472b794aa412d29677b242441bc10ed750df593d3d9475c41e12a:


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 68 6f 73 74 27 3a 20 27  : {....'host': '
0530: 6c 6f 63 61 6c 68 6f 73 74 27 2c 0a 09 09 09 27  localhost',....'
0540: 64 61 74 61 62 61 73 65 27 3a 20 27 73 71 75 69  database': 'squi
0550: 64 54 61 67 27 2c 0a 09 7d 2c 7d 0a 0a 09 23 20  dTag',..},}...# 
0560: 66 75 6e 63 74 69 6f 6e 20 74 6f 20 72 65 61 64  function to read
0570: 20 69 6e 20 63 6f 6e 66 69 67 20 66 69 6c 65 0a   in config file.
0580: 09 64 65 66 20 5f 5f 69 6e 69 74 5f 5f 28 73 65  .def __init__(se
0590: 6c 66 29 3a 0a 09 09 69 6d 70 6f 72 74 20 43 6f  lf):...import Co
05a0: 6e 66 69 67 50 61 72 73 65 72 2c 20 6f 70 74 70  nfigParser, optp
05b0: 61 72 73 65 2c 20 6f 73 0a 0a 09 09 70 61 72 73  arse, os....pars
05c0: 65 72 20 3d 20 6f 70 74 70 61 72 73 65 2e 4f 70  er = optparse.Op
05d0: 74 69 6f 6e 50 61 72 73 65 72 28 29 0a 09 09 70  tionParser()...p
05e0: 61 72 73 65 72 2e 61 64 64 5f 6f 70 74 69 6f 6e  arser.add_option
05f0: 28 27 2d 63 27 2c 20 27 2d 2d 63 6f 6e 66 69 67  ('-c', '--config
0600: 27 2c 20 64 65 73 74 20 3d 20 27 63 6f 6e 66 69  ', dest = 'confi
0610: 67 27 2c 0a 09 09 09 68 65 6c 70 20 3d 20 27 63  g',....help = 'c
0620: 6f 6e 66 69 67 20 66 69 6c 65 20 6c 6f 63 61 74  onfig file locat
0630: 69 6f 6e 27 2c 20 6d 65 74 61 76 61 72 20 3d 20  ion', metavar = 
0640: 27 46 49 4c 45 27 2c 0a 09 09 09 64 65 66 61 75  'FILE',....defau
0650: 6c 74 20 3d 20 27 2f 75 73 72 2f 6c 6f 63 61 6c  lt = '/usr/local
0660: 2f 65 74 63 2f 73 71 75 69 64 2d 74 61 67 67 65  /etc/squid-tagge
0670: 72 2e 63 6f 6e 66 27 29 0a 09 09 70 61 72 73 65  r.conf')...parse
0680: 72 2e 61 64 64 5f 6f 70 74 69 6f 6e 28 27 2d 64  r.add_option('-d
0690: 27 2c 20 27 2d 2d 64 75 6d 70 27 2c 20 64 65 73  ', '--dump', des
06a0: 74 20 3d 20 27 64 75 6d 70 27 2c 0a 09 09 09 68  t = 'dump',....h
06b0: 65 6c 70 20 3d 20 27 64 75 6d 70 20 64 61 74 61  elp = 'dump data
06c0: 62 61 73 65 27 2c 20 61 63 74 69 6f 6e 20 3d 20  base', action = 
06d0: 27 73 74 6f 72 65 5f 74 72 75 65 27 2c 20 6d 65  'store_true', me
06e0: 74 61 76 61 72 20 3d 20 27 62 6f 6f 6c 27 2c 0a  tavar = 'bool',.
06f0: 09 09 09 64 65 66 61 75 6c 74 20 3d 20 46 61 6c  ...default = Fal
0700: 73 65 29 0a 09 09 70 61 72 73 65 72 2e 61 64 64  se)...parser.add
0710: 5f 6f 70 74 69 6f 6e 28 27 2d 66 27 2c 20 27 2d  _option('-f', '-
0720: 2d 66 6c 75 73 68 2d 64 61 74 61 62 61 73 65 27  -flush-database'
0730: 2c 20 64 65 73 74 20 3d 20 27 66 6c 75 73 68 5f  , dest = 'flush_
0740: 64 62 27 2c 0a 09 09 09 68 65 6c 70 20 3d 20 27  db',....help = '
0750: 66 6c 75 73 68 20 70 72 65 76 69 6f 75 73 20 64  flush previous d
0760: 61 74 61 62 61 73 65 20 6f 6e 20 6c 6f 61 64 27  atabase on load'
0770: 2c 20 64 65 66 61 75 6c 74 20 3d 20 46 61 6c 73  , default = Fals
0780: 65 2c 0a 09 09 09 61 63 74 69 6f 6e 20 3d 20 27  e,....action = '
0790: 73 74 6f 72 65 5f 74 72 75 65 27 2c 20 6d 65 74  store_true', met
07a0: 61 76 61 72 20 3d 20 27 62 6f 6f 6c 27 29 0a 09  avar = 'bool')..
07b0: 09 70 61 72 73 65 72 2e 61 64 64 5f 6f 70 74 69  .parser.add_opti
07c0: 6f 6e 28 27 2d 6c 27 2c 20 27 2d 2d 6c 6f 61 64  on('-l', '--load
07d0: 27 2c 20 64 65 73 74 20 3d 20 27 6c 6f 61 64 27  ', dest = 'load'
07e0: 2c 0a 09 09 09 68 65 6c 70 20 3d 20 27 6c 6f 61  ,....help = 'loa
07f0: 64 20 64 61 74 61 62 61 73 65 27 2c 20 61 63 74  d database', act
0800: 69 6f 6e 20 3d 20 27 73 74 6f 72 65 5f 74 72 75  ion = 'store_tru
0810: 65 27 2c 20 6d 65 74 61 76 61 72 20 3d 20 27 62  e', metavar = 'b
0820: 6f 6f 6c 27 2c 0a 09 09 09 64 65 66 61 75 6c 74  ool',....default
0830: 20 3d 20 46 61 6c 73 65 29 0a 09 09 70 61 72 73   = False)...pars
0840: 65 72 2e 61 64 64 5f 6f 70 74 69 6f 6e 28 27 2d  er.add_option('-
0850: 44 27 2c 20 27 2d 2d 64 75 6d 70 2d 63 6f 6e 66  D', '--dump-conf
0860: 27 2c 20 64 65 73 74 20 3d 20 27 64 75 6d 70 5f  ', dest = 'dump_
0870: 63 6f 6e 66 27 2c 0a 09 09 09 68 65 6c 70 20 3d  conf',....help =
0880: 20 27 64 75 6d 70 20 66 69 6c 74 65 72 69 6e 67   'dump filtering
0890: 20 72 75 6c 65 73 27 2c 20 64 65 66 61 75 6c 74   rules', default
08a0: 20 3d 20 46 61 6c 73 65 2c 20 6d 65 74 61 76 61   = False, metava
08b0: 72 20 3d 20 27 62 6f 6f 6c 27 2c 0a 09 09 09 61  r = 'bool',....a
08c0: 63 74 69 6f 6e 20 3d 20 27 73 74 6f 72 65 5f 74  ction = 'store_t
08d0: 72 75 65 27 29 0a 09 09 70 61 72 73 65 72 2e 61  rue')...parser.a
08e0: 64 64 5f 6f 70 74 69 6f 6e 28 27 2d 4c 27 2c 20  dd_option('-L', 
08f0: 27 2d 2d 6c 6f 61 64 2d 63 6f 6e 66 27 2c 20 64  '--load-conf', d
0900: 65 73 74 20 3d 20 27 6c 6f 61 64 5f 63 6f 6e 66  est = 'load_conf
0910: 27 2c 0a 09 09 09 68 65 6c 70 20 3d 20 27 6c 6f  ',....help = 'lo
0920: 61 64 20 66 69 6c 74 65 72 69 6e 67 20 72 75 6c  ad filtering rul
0930: 65 73 27 2c 20 64 65 66 61 75 6c 74 20 3d 20 46  es', default = F
0940: 61 6c 73 65 2c 20 6d 65 74 61 76 61 72 20 3d 20  alse, metavar = 
0950: 27 62 6f 6f 6c 27 2c 0a 09 09 09 61 63 74 69 6f  'bool',....actio
0960: 6e 20 3d 20 27 73 74 6f 72 65 5f 74 72 75 65 27  n = 'store_true'
0970: 29 0a 0a 09 09 28 73 65 6c 66 2e 6f 70 74 69 6f  )....(self.optio
0980: 6e 73 2c 20 61 72 67 73 29 20 3d 20 70 61 72 73  ns, args) = pars
0990: 65 72 2e 70 61 72 73 65 5f 61 72 67 73 28 29 0a  er.parse_args().
09a0: 0a 09 09 61 73 73 65 72 74 20 6f 73 2e 61 63 63  ...assert os.acc
09b0: 65 73 73 28 73 65 6c 66 2e 6f 70 74 69 6f 6e 73  ess(self.options
09c0: 2e 63 6f 6e 66 69 67 2c 20 6f 73 2e 52 5f 4f 4b  .config, os.R_OK
09d0: 29 2c 20 22 46 61 74 61 6c 20 65 72 72 6f 72 3a  ), "Fatal error:
09e0: 20 63 61 6e 27 74 20 72 65 61 64 20 7b 7d 22 2e   can't read {}".
09f0: 66 6f 72 6d 61 74 28 73 65 6c 66 2e 6f 70 74 69  format(self.opti
0a00: 6f 6e 73 2e 63 6f 6e 66 69 67 29 0a 0a 09 09 73  ons.config)....s
0a10: 65 6c 66 2e 5f 63 6f 6e 66 69 67 20 3d 20 43 6f  elf._config = Co
0a20: 6e 66 69 67 50 61 72 73 65 72 2e 43 6f 6e 66 69  nfigParser.Confi
0a30: 67 50 61 72 73 65 72 28 29 0a 09 09 73 65 6c 66  gParser()...self
0a40: 2e 5f 63 6f 6e 66 69 67 2e 72 65 61 64 66 70 28  ._config.readfp(
0a50: 6f 70 65 6e 28 73 65 6c 66 2e 6f 70 74 69 6f 6e  open(self.option
0a60: 73 2e 63 6f 6e 66 69 67 29 29 0a 0a 09 23 20 66  s.config))...# f
0a70: 75 6e 63 74 69 6f 6e 20 74 6f 20 73 65 6c 65 63  unction to selec
0a80: 74 20 63 6f 6e 66 69 67 20 66 69 6c 65 20 73 65  t config file se
0a90: 63 74 69 6f 6e 20 6f 72 20 63 72 65 61 74 65 20  ction or create 
0aa0: 6f 6e 65 0a 09 64 65 66 20 73 65 63 74 69 6f 6e  one..def section
0ab0: 28 73 65 6c 66 2c 20 73 65 63 74 69 6f 6e 29 3a  (self, section):
0ac0: 0a 09 09 69 66 20 6e 6f 74 20 73 65 6c 66 2e 5f  ...if not self._
0ad0: 63 6f 6e 66 69 67 2e 68 61 73 5f 73 65 63 74 69  config.has_secti
0ae0: 6f 6e 28 73 65 63 74 69 6f 6e 29 3a 0a 09 09 09  on(section):....
0af0: 73 65 6c 66 2e 5f 63 6f 6e 66 69 67 2e 61 64 64  self._config.add
0b00: 5f 73 65 63 74 69 6f 6e 28 73 65 63 74 69 6f 6e  _section(section
0b10: 29 0a 09 09 73 65 6c 66 2e 5f 73 65 63 74 69 6f  )...self._sectio
0b20: 6e 20 3d 20 73 65 63 74 69 6f 6e 0a 0a 09 23 20  n = section...# 
0b30: 66 75 6e 63 74 69 6f 6e 20 74 6f 20 67 65 74 20  function to get 
0b40: 63 6f 6e 66 69 67 20 70 61 72 61 6d 65 74 65 72  config parameter
0b50: 2c 20 69 66 20 70 61 72 61 6d 65 74 65 72 20 64  , if parameter d
0b60: 6f 65 73 6e 27 74 20 65 78 69 73 74 73 20 74 68  oesn't exists th
0b70: 65 20 64 65 66 61 75 6c 74 0a 09 23 20 76 61 6c  e default..# val
0b80: 75 65 20 6f 72 20 4e 6f 6e 65 20 69 73 20 73 75  ue or None is su
0b90: 62 73 74 69 74 75 74 65 64 0a 09 64 65 66 20 5f  bstituted..def _
0ba0: 5f 67 65 74 69 74 65 6d 5f 5f 28 73 65 6c 66 2c  _getitem__(self,
0bb0: 20 6e 61 6d 65 29 3a 0a 09 09 69 66 20 6e 6f 74   name):...if not
0bc0: 20 73 65 6c 66 2e 5f 63 6f 6e 66 69 67 2e 68 61   self._config.ha
0bd0: 73 5f 6f 70 74 69 6f 6e 28 73 65 6c 66 2e 5f 73  s_option(self._s
0be0: 65 63 74 69 6f 6e 2c 20 6e 61 6d 65 29 3a 0a 09  ection, name):..
0bf0: 09 09 69 66 20 73 65 6c 66 2e 5f 73 65 63 74 69  ..if self._secti
0c00: 6f 6e 20 69 6e 20 73 65 6c 66 2e 5f 64 65 66 61  on in self._defa
0c10: 75 6c 74 3a 0a 09 09 09 09 69 66 20 6e 61 6d 65  ult:.....if name
0c20: 20 69 6e 20 73 65 6c 66 2e 5f 64 65 66 61 75 6c   in self._defaul
0c30: 74 5b 73 65 6c 66 2e 5f 73 65 63 74 69 6f 6e 5d  t[self._section]
0c40: 3a 0a 09 09 09 09 09 73 65 6c 66 2e 5f 63 6f 6e  :......self._con
0c50: 66 69 67 2e 73 65 74 28 73 65 6c 66 2e 5f 73 65  fig.set(self._se
0c60: 63 74 69 6f 6e 2c 20 6e 61 6d 65 2c 20 73 65 6c  ction, name, sel
0c70: 66 2e 5f 64 65 66 61 75 6c 74 5b 73 65 6c 66 2e  f._default[self.
0c80: 5f 73 65 63 74 69 6f 6e 5d 5b 6e 61 6d 65 5d 29  _section][name])
0c90: 0a 09 09 09 09 65 6c 73 65 3a 0a 09 09 09 09 09  .....else:......
0ca0: 73 65 6c 66 2e 5f 63 6f 6e 66 69 67 2e 73 65 74  self._config.set
0cb0: 28 73 65 6c 66 2e 5f 73 65 63 74 69 6f 6e 2c 20  (self._section, 
0cc0: 6e 61 6d 65 2c 20 4e 6f 6e 65 29 0a 09 09 09 65  name, None)....e
0cd0: 6c 73 65 3a 0a 09 09 09 09 73 65 6c 66 2e 5f 63  lse:.....self._c
0ce0: 6f 6e 66 69 67 2e 73 65 74 28 73 65 6c 66 2e 5f  onfig.set(self._
0cf0: 73 65 63 74 69 6f 6e 2c 20 6e 61 6d 65 2c 20 4e  section, name, N
0d00: 6f 6e 65 29 0a 09 09 72 65 74 75 72 6e 28 73 65  one)...return(se
0d10: 6c 66 2e 5f 63 6f 6e 66 69 67 2e 67 65 74 28 73  lf._config.get(s
0d20: 65 6c 66 2e 5f 73 65 63 74 69 6f 6e 2c 20 6e 61  elf._section, na
0d30: 6d 65 29 29 0a 0a 23 20 69 6e 69 74 69 61 6c 69  me))..# initiali
0d40: 7a 69 6e 67 20 61 6e 64 20 72 65 61 64 69 6e 67  zing and reading
0d50: 20 69 6e 20 63 6f 6e 66 69 67 20 66 69 6c 65 0a   in config file.
0d60: 63 6f 6e 66 69 67 20 3d 20 43 6f 6e 66 69 67 28  config = Config(
0d70: 29 0a 0a 69 6d 70 6f 72 74 20 6c 6f 67 67 69 6e  )..import loggin
0d80: 67 2c 20 6c 6f 67 67 69 6e 67 2e 68 61 6e 64 6c  g, logging.handl
0d90: 65 72 73 0a 0a 23 20 77 72 61 70 70 65 72 20 61  ers..# wrapper a
0da0: 72 6f 75 6e 64 20 6c 6f 67 67 69 6e 67 20 68 61  round logging ha
0db0: 6e 64 6c 65 72 20 74 6f 20 6d 61 6b 65 20 69 74  ndler to make it
0dc0: 20 71 75 65 75 65 20 72 65 63 6f 72 64 73 20 61   queue records a
0dd0: 6e 64 20 64 6f 6e 27 74 20 73 74 61 6c 6c 20 77  nd don't stall w
0de0: 68 65 6e 20 73 65 6e 64 69 6e 67 20 74 68 65 6d  hen sending them
0df0: 0a 0a 63 6c 61 73 73 20 53 79 73 4c 6f 67 48 61  ..class SysLogHa
0e00: 6e 64 6c 65 72 51 75 65 75 65 28 6c 6f 67 67 69  ndlerQueue(loggi
0e10: 6e 67 2e 68 61 6e 64 6c 65 72 73 2e 53 79 73 4c  ng.handlers.SysL
0e20: 6f 67 48 61 6e 64 6c 65 72 29 3a 0a 09 5f 5f 73  ogHandler):..__s
0e30: 6c 6f 74 73 5f 5f 20 3d 20 66 72 6f 7a 65 6e 73  lots__ = frozens
0e40: 65 74 28 5b 27 5f 65 76 65 6e 74 27 2c 20 27 5f  et(['_event', '_
0e50: 74 61 69 6c 27 2c 20 27 5f 77 6f 72 6b 65 72 73  tail', '_workers
0e60: 27 5d 29 0a 0a 09 64 65 66 20 5f 5f 69 6e 69 74  '])...def __init
0e70: 5f 5f 28 73 65 6c 66 29 3a 0a 09 09 6c 6f 67 67  __(self):...logg
0e80: 69 6e 67 2e 68 61 6e 64 6c 65 72 73 2e 53 79 73  ing.handlers.Sys
0e90: 4c 6f 67 48 61 6e 64 6c 65 72 2e 5f 5f 69 6e 69  LogHandler.__ini
0ea0: 74 5f 5f 28 73 65 6c 66 2c 20 27 2f 64 65 76 2f  t__(self, '/dev/
0eb0: 6c 6f 67 27 29 0a 09 09 73 65 6c 66 2e 5f 65 76  log')...self._ev
0ec0: 65 6e 74 20 3d 20 67 65 76 65 6e 74 2e 65 76 65  ent = gevent.eve
0ed0: 6e 74 2e 45 76 65 6e 74 28 29 0a 09 09 73 65 6c  nt.Event()...sel
0ee0: 66 2e 5f 65 76 65 6e 74 2e 73 65 74 28 29 0a 09  f._event.set()..
0ef0: 09 73 65 6c 66 2e 5f 74 61 69 6c 20 3d 20 67 65  .self._tail = ge
0f00: 76 65 6e 74 2e 71 75 65 75 65 2e 51 75 65 75 65  vent.queue.Queue
0f10: 28 29 0a 09 09 73 65 6c 66 2e 5f 77 6f 72 6b 65  ()...self._worke
0f20: 72 73 20 3d 20 73 65 74 28 29 0a 0a 09 64 65 66  rs = set()...def
0f30: 20 65 6d 69 74 28 73 65 6c 66 2c 20 72 65 63 6f   emit(self, reco
0f40: 72 64 29 3a 0a 09 09 23 20 6d 79 20 73 79 73 6c  rd):...# my sysl
0f50: 6f 67 20 69 73 20 62 72 6f 6b 65 6e 20 61 6e 64  og is broken and
0f60: 20 63 61 6e 6e 6f 74 20 69 6e 74 6f 20 55 54 46   cannot into UTF
0f70: 2d 38 20 42 4f 4d 0a 09 09 72 65 63 6f 72 64 2e  -8 BOM...record.
0f80: 6d 73 67 20 3d 20 73 74 72 28 72 65 63 6f 72 64  msg = str(record
0f90: 2e 6d 73 67 29 0a 09 09 73 65 6c 66 2e 5f 74 61  .msg)...self._ta
0fa0: 69 6c 2e 70 75 74 28 72 65 63 6f 72 64 29 0a 09  il.put(record)..
0fb0: 09 69 66 20 73 65 6c 66 2e 5f 74 61 69 6c 2e 71  .if self._tail.q
0fc0: 73 69 7a 65 28 29 20 21 3d 20 30 3a 0a 09 09 09  size() != 0:....
0fd0: 23 20 69 6e 20 63 61 73 65 20 71 75 65 75 65 20  # in case queue 
0fe0: 69 73 20 65 6d 70 74 79 20 77 65 20 77 69 6c 6c  is empty we will
0ff0: 20 73 70 61 77 6e 20 6e 65 77 20 77 6f 72 6b 65   spawn new worke
1000: 72 0a 09 09 09 23 20 61 6c 6c 20 77 6f 72 6b 65  r....# all worke
1010: 72 73 20 61 72 65 20 6c 6f 67 67 65 64 20 73 6f  rs are logged so
1020: 20 77 65 20 63 61 6e 20 6b 69 6c 6c 20 74 68 65   we can kill the
1030: 6d 20 6f 6e 20 63 6c 6f 73 65 28 29 0a 09 09 09  m on close()....
1040: 73 65 6c 66 2e 5f 77 6f 72 6b 65 72 73 2e 61 64  self._workers.ad
1050: 64 28 67 65 76 65 6e 74 2e 73 70 61 77 6e 28 73  d(gevent.spawn(s
1060: 65 6c 66 2e 5f 77 72 69 74 65 72 29 29 0a 0a 09  elf._writer))...
1070: 64 65 66 20 5f 77 72 69 74 65 72 28 73 65 6c 66  def _writer(self
1080: 29 3a 0a 09 09 23 20 68 65 72 65 20 77 65 20 61  ):...# here we a
1090: 72 65 20 6c 6f 63 6b 69 6e 67 20 74 68 65 20 71  re locking the q
10a0: 75 65 75 65 20 73 6f 20 77 65 20 63 61 6e 20 62  ueue so we can b
10b0: 65 20 73 75 72 65 20 77 65 20 61 72 65 20 74 68  e sure we are th
10c0: 65 20 6f 6e 6c 79 20 6f 6e 65 0a 09 09 73 65 6c  e only one...sel
10d0: 66 2e 5f 65 76 65 6e 74 2e 77 61 69 74 28 29 0a  f._event.wait().
10e0: 09 09 73 65 6c 66 2e 5f 65 76 65 6e 74 2e 63 6c  ..self._event.cl
10f0: 65 61 72 28 29 0a 09 09 77 68 69 6c 65 20 6e 6f  ear()...while no
1100: 74 20 73 65 6c 66 2e 5f 74 61 69 6c 2e 65 6d 70  t self._tail.emp
1110: 74 79 28 29 3a 0a 09 09 09 6c 6f 67 67 69 6e 67  ty():....logging
1120: 2e 68 61 6e 64 6c 65 72 73 2e 53 79 73 4c 6f 67  .handlers.SysLog
1130: 48 61 6e 64 6c 65 72 2e 65 6d 69 74 28 73 65 6c  Handler.emit(sel
1140: 66 2c 20 73 65 6c 66 2e 5f 74 61 69 6c 2e 67 65  f, self._tail.ge
1150: 74 28 29 29 0a 09 09 73 65 6c 66 2e 5f 65 76 65  t())...self._eve
1160: 6e 74 2e 73 65 74 28 29 0a 09 09 73 65 6c 66 2e  nt.set()...self.
1170: 5f 77 6f 72 6b 65 72 73 2e 72 65 6d 6f 76 65 28  _workers.remove(
1180: 67 65 76 65 6e 74 2e 67 65 74 63 75 72 72 65 6e  gevent.getcurren
1190: 74 28 29 29 0a 0a 09 64 65 66 20 63 6c 6f 73 65  t())...def close
11a0: 28 73 65 6c 66 29 3a 0a 09 09 66 6f 72 20 77 6f  (self):...for wo
11b0: 72 6b 65 72 20 69 6e 20 73 65 6c 66 2e 5f 77 6f  rker in self._wo
11c0: 72 6b 65 72 73 3a 0a 09 09 09 67 65 76 65 6e 74  rkers:....gevent
11d0: 2e 6b 69 6c 6c 28 77 6f 72 6b 65 72 29 0a 09 09  .kill(worker)...
11e0: 6c 6f 67 67 69 6e 67 2e 68 61 6e 64 6c 65 72 73  logging.handlers
11f0: 2e 53 79 73 4c 6f 67 48 61 6e 64 6c 65 72 2e 63  .SysLogHandler.c
1200: 6c 6f 73 65 28 73 65 6c 66 29 0a 0a 6c 6f 67 67  lose(self)..logg
1210: 65 72 20 3d 20 6c 6f 67 67 69 6e 67 2e 67 65 74  er = logging.get
1220: 4c 6f 67 67 65 72 28 27 73 71 75 69 64 54 61 67  Logger('squidTag
1230: 27 29 0a 6c 6f 67 67 65 72 2e 73 65 74 4c 65 76  ').logger.setLev
1240: 65 6c 28 6c 6f 67 67 69 6e 67 2e 49 4e 46 4f 29  el(logging.INFO)
1250: 0a 68 61 6e 64 6c 65 72 20 3d 20 53 79 73 4c 6f  .handler = SysLo
1260: 67 48 61 6e 64 6c 65 72 51 75 65 75 65 28 29 0a  gHandlerQueue().
1270: 68 61 6e 64 6c 65 72 2e 73 65 74 46 6f 72 6d 61  handler.setForma
1280: 74 74 65 72 28 6c 6f 67 67 69 6e 67 2e 46 6f 72  tter(logging.For
1290: 6d 61 74 74 65 72 28 73 74 72 28 27 73 71 75 69  matter(str('squi
12a0: 64 54 61 67 5b 25 28 70 72 6f 63 65 73 73 29 73  dTag[%(process)s
12b0: 5d 3a 20 25 28 6d 65 73 73 61 67 65 29 73 27 29  ]: %(message)s')
12c0: 29 29 0a 6c 6f 67 67 65 72 2e 61 64 64 48 61 6e  )).logger.addHan
12d0: 64 6c 65 72 28 68 61 6e 64 6c 65 72 29 0a 0a 23  dler(handler)..#
12e0: 20 74 69 6e 79 20 77 72 61 70 70 65 72 20 61 72   tiny wrapper ar
12f0: 6f 75 6e 64 20 61 20 66 69 6c 65 20 74 6f 20 6d  ound a file to m
1300: 61 6b 65 20 72 65 61 64 73 20 66 72 6f 6d 20 69  ake reads from i
1310: 74 20 67 65 76 65 6e 74 61 62 6c 65 0a 23 20 6f  t geventable.# o
1320: 72 20 73 68 6f 75 6c 64 20 69 20 6d 6f 76 65 20  r should i move 
1330: 74 68 69 73 20 73 6f 6d 65 77 68 65 72 65 3f 0a  this somewhere?.
1340: 0a 63 6c 61 73 73 20 46 52 65 61 64 6c 69 6e 65  .class FReadline
1350: 51 75 65 75 65 28 67 65 76 65 6e 74 2e 71 75 65  Queue(gevent.que
1360: 75 65 2e 51 75 65 75 65 29 3a 0a 09 23 20 73 74  ue.Queue):..# st
1370: 6f 72 69 6e 67 20 66 69 6c 65 20 64 65 73 63 72  oring file descr
1380: 69 70 74 6f 72 2c 20 6c 65 66 74 6f 76 65 72 0a  iptor, leftover.
1390: 09 5f 5f 73 6c 6f 74 73 5f 5f 20 3d 20 66 72 6f  .__slots__ = fro
13a0: 7a 65 6e 73 65 74 28 5b 27 5f 66 64 27 2c 20 27  zenset(['_fd', '
13b0: 5f 74 61 69 6c 27 5d 29 0a 0a 09 64 65 66 20 5f  _tail'])...def _
13c0: 5f 69 6e 69 74 5f 5f 28 73 65 6c 66 2c 20 66 64  _init__(self, fd
13d0: 29 3a 0a 09 09 23 20 69 6e 69 74 69 61 6c 69 73  ):...# initialis
13e0: 69 6e 67 20 63 6c 61 73 73 0a 09 09 67 65 76 65  ing class...geve
13f0: 6e 74 2e 71 75 65 75 65 2e 51 75 65 75 65 2e 5f  nt.queue.Queue._
1400: 5f 69 6e 69 74 5f 5f 28 73 65 6c 66 29 0a 09 09  _init__(self)...
1410: 23 20 73 74 6f 72 69 6e 67 20 66 69 6c 65 20 64  # storing file d
1420: 65 73 63 72 69 70 74 6f 72 0a 09 09 73 65 6c 66  escriptor...self
1430: 2e 5f 66 64 20 3d 20 66 64 0a 09 09 23 20 75 73  ._fd = fd...# us
1440: 69 6e 67 20 65 6d 70 74 79 20 74 61 69 6c 0a 09  ing empty tail..
1450: 09 73 65 6c 66 2e 5f 74 61 69 6c 20 3d 20 27 27  .self._tail = ''
1460: 0a 09 09 23 20 73 65 74 74 69 6e 67 20 75 70 20  ...# setting up 
1470: 65 76 65 6e 74 0a 09 09 73 65 6c 66 2e 5f 69 6e  event...self._in
1480: 73 74 61 6c 6c 5f 77 61 69 74 28 29 0a 0a 09 64  stall_wait()...d
1490: 65 66 20 5f 69 6e 73 74 61 6c 6c 5f 77 61 69 74  ef _install_wait
14a0: 28 73 65 6c 66 29 3a 0a 09 09 66 69 6c 65 6e 6f  (self):...fileno
14b0: 20 3d 20 73 65 6c 66 2e 5f 66 64 2e 66 69 6c 65   = self._fd.file
14c0: 6e 6f 28 29 0a 09 09 23 20 70 75 74 74 69 6e 67  no()...# putting
14d0: 20 66 69 6c 65 20 74 6f 20 6e 6f 6e 62 6c 6f 63   file to nonbloc
14e0: 6b 69 6e 67 20 6d 6f 64 65 0a 09 09 66 63 6e 74  king mode...fcnt
14f0: 6c 2e 66 63 6e 74 6c 28 66 69 6c 65 6e 6f 2c 20  l.fcntl(fileno, 
1500: 66 63 6e 74 6c 2e 46 5f 53 45 54 46 4c 2c 20 66  fcntl.F_SETFL, f
1510: 63 6e 74 6c 2e 66 63 6e 74 6c 28 66 69 6c 65 6e  cntl.fcntl(filen
1520: 6f 2c 20 66 63 6e 74 6c 2e 46 5f 47 45 54 46 4c  o, fcntl.F_GETFL
1530: 29 20 20 7c 20 6f 73 2e 4f 5f 4e 4f 4e 42 4c 4f  )  | os.O_NONBLO
1540: 43 4b 29 0a 09 09 23 20 69 6e 73 74 61 6c 6c 69  CK)...# installi
1550: 6e 67 20 65 76 65 6e 74 20 68 61 6e 64 6c 65 72  ng event handler
1560: 0a 09 09 67 65 76 65 6e 74 2e 63 6f 72 65 2e 72  ...gevent.core.r
1570: 65 61 64 5f 65 76 65 6e 74 28 66 69 6c 65 6e 6f  ead_event(fileno
1580: 2c 20 73 65 6c 66 2e 5f 77 61 69 74 5f 68 65 6c  , self._wait_hel
1590: 70 65 72 29 0a 0a 09 64 65 66 20 5f 77 61 69 74  per)...def _wait
15a0: 5f 68 65 6c 70 65 72 28 73 65 6c 66 2c 20 65 76  _helper(self, ev
15b0: 2c 20 65 76 74 79 70 65 29 3a 0a 09 09 23 20 72  , evtype):...# r
15c0: 65 61 64 69 6e 67 20 6f 6e 65 20 62 75 66 66 65  eading one buffe
15d0: 72 20 66 72 6f 6d 20 73 74 72 65 61 6d 0a 09 09  r from stream...
15e0: 62 75 66 20 3d 20 73 65 6c 66 2e 5f 66 64 2e 72  buf = self._fd.r
15f0: 65 61 64 28 34 30 39 36 29 0a 09 09 23 20 73 70  ead(4096)...# sp
1600: 6c 69 74 74 69 6e 67 20 73 74 72 65 61 6d 20 62  litting stream b
1610: 79 20 6c 69 6e 65 20 65 6e 64 73 0a 09 09 72 6f  y line ends...ro
1620: 77 73 20 3d 20 62 75 66 2e 64 65 63 6f 64 65 28  ws = buf.decode(
1630: 27 6c 31 27 29 2e 73 70 6c 69 74 28 27 5c 6e 27  'l1').split('\n'
1640: 29 0a 09 09 23 20 61 64 64 69 6e 67 20 74 61 69  )...# adding tai
1650: 6c 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 65  l to the first e
1660: 6c 65 6d 65 6e 74 20 69 66 20 74 68 65 72 65 20  lement if there 
1670: 69 73 20 73 6f 6d 65 20 74 61 69 6c 0a 09 09 69  is some tail...i
1680: 66 20 6c 65 6e 28 73 65 6c 66 2e 5f 74 61 69 6c  f len(self._tail
1690: 29 20 3e 20 30 3a 0a 09 09 09 72 6f 77 73 5b 30  ) > 0:....rows[0
16a0: 5d 20 3d 20 73 65 6c 66 2e 5f 74 61 69 6c 20 2b  ] = self._tail +
16b0: 20 72 6f 77 73 5b 30 5d 0a 09 09 23 20 70 6f 70   rows[0]...# pop
16c0: 70 69 6e 67 20 6f 75 74 20 6c 61 73 74 20 28 69  ping out last (i
16d0: 6e 63 6f 6d 70 6c 65 74 65 29 20 65 6c 65 6d 65  ncomplete) eleme
16e0: 6e 74 0a 09 09 73 65 6c 66 2e 5f 74 61 69 6c 20  nt...self._tail 
16f0: 3d 20 72 6f 77 73 2e 70 6f 70 28 2d 31 29 0a 09  = rows.pop(-1)..
1700: 09 23 20 64 72 6f 70 70 69 6e 67 20 61 6c 6c 20  .# dropping all 
1710: 63 6f 6d 70 6c 65 74 65 20 65 6c 65 6d 65 6e 74  complete element
1720: 73 20 74 6f 20 74 68 65 20 71 75 65 75 65 0a 09  s to the queue..
1730: 09 66 6f 72 20 72 6f 77 20 69 6e 20 72 6f 77 73  .for row in rows
1740: 3a 0a 09 09 09 73 65 6c 66 2e 70 75 74 5f 6e 6f  :....self.put_no
1750: 77 61 69 74 28 72 6f 77 29 0a 09 09 09 6c 6f 67  wait(row)....log
1760: 67 65 72 2e 69 6e 66 6f 28 27 3c 20 27 20 2b 20  ger.info('< ' + 
1770: 72 6f 77 29 0a 09 09 69 66 20 6c 65 6e 28 62 75  row)...if len(bu
1780: 66 29 20 3e 20 30 3a 0a 09 09 09 23 20 6e 6f 20  f) > 0:....# no 
1790: 45 4f 46 2c 20 72 65 69 6e 73 74 61 6c 6c 69 6e  EOF, reinstallin
17a0: 67 20 65 76 65 6e 74 20 68 61 6e 64 6c 65 72 0a  g event handler.
17b0: 09 09 09 67 65 76 65 6e 74 2e 63 6f 72 65 2e 72  ...gevent.core.r
17c0: 65 61 64 5f 65 76 65 6e 74 28 73 65 6c 66 2e 5f  ead_event(self._
17d0: 66 64 2e 66 69 6c 65 6e 6f 28 29 2c 20 73 65 6c  fd.fileno(), sel
17e0: 66 2e 5f 77 61 69 74 5f 68 65 6c 70 65 72 29 0a  f._wait_helper).
17f0: 09 09 65 6c 73 65 3a 0a 09 09 09 23 20 45 4f 46  ..else:....# EOF
1800: 20 66 6f 75 6e 64 2c 20 73 65 6e 64 69 6e 67 20   found, sending 
1810: 45 4f 46 20 74 6f 20 71 75 65 75 65 0a 09 09 09  EOF to queue....
1820: 73 65 6c 66 2e 70 75 74 5f 6e 6f 77 61 69 74 28  self.put_nowait(
1830: 4e 6f 6e 65 29 0a 0a 73 74 64 69 6e 20 3d 20 46  None)..stdin = F
1840: 52 65 61 64 6c 69 6e 65 51 75 65 75 65 28 73 79  ReadlineQueue(sy
1850: 73 2e 73 74 64 69 6e 29 0a 0a 23 20 77 72 61 70  s.stdin)..# wrap
1860: 70 65 72 20 61 67 61 69 6e 73 74 20 66 69 6c 65  per against file
1870: 20 68 61 6e 64 6c 65 72 20 74 68 61 74 20 6d 61   handler that ma
1880: 6b 65 73 20 70 6f 73 73 69 62 6c 65 20 74 6f 20  kes possible to 
1890: 71 75 65 75 65 20 73 6f 6d 65 20 77 72 69 74 65  queue some write
18a0: 73 20 77 69 74 68 6f 75 74 20 73 74 61 6c 6c 69  s without stalli
18b0: 6e 67 0a 0a 63 6c 61 73 73 20 46 57 72 69 74 65  ng..class FWrite
18c0: 6c 69 6e 65 51 75 65 75 65 28 67 65 76 65 6e 74  lineQueue(gevent
18d0: 2e 71 75 65 75 65 2e 4a 6f 69 6e 61 62 6c 65 51  .queue.JoinableQ
18e0: 75 65 75 65 29 3a 0a 09 23 20 73 74 6f 72 69 6e  ueue):..# storin
18f0: 67 20 66 69 6c 65 6e 6f 2c 20 69 6f 20 69 6e 74  g fileno, io int
1900: 65 72 66 61 63 65 2c 20 6c 65 66 74 6f 76 65 72  erface, leftover
1910: 0a 09 5f 5f 73 6c 6f 74 73 5f 5f 20 3d 20 66 72  ..__slots__ = fr
1920: 6f 7a 65 6e 73 65 74 28 5b 27 5f 66 69 6c 65 6e  ozenset(['_filen
1930: 6f 27 2c 20 27 5f 69 6f 27 2c 20 27 5f 74 61 69  o', '_io', '_tai
1940: 6c 27 5d 29 0a 0a 09 64 65 66 20 5f 5f 69 6e 69  l'])...def __ini
1950: 74 5f 5f 28 73 65 6c 66 2c 20 66 64 2c 20 63 6c  t__(self, fd, cl
1960: 6f 73 65 66 64 20 3d 20 54 72 75 65 29 3a 0a 09  osefd = True):..
1970: 09 69 6d 70 6f 72 74 20 69 6f 0a 09 09 23 20 69  .import io...# i
1980: 6e 69 74 69 61 6c 69 73 69 6e 67 20 63 6c 61 73  nitialising clas
1990: 73 0a 09 09 67 65 76 65 6e 74 2e 71 75 65 75 65  s...gevent.queue
19a0: 2e 4a 6f 69 6e 61 62 6c 65 51 75 65 75 65 2e 5f  .JoinableQueue._
19b0: 5f 69 6e 69 74 5f 5f 28 73 65 6c 66 29 0a 09 09  _init__(self)...
19c0: 23 20 73 74 6f 72 69 6e 67 20 66 69 6c 65 6e 6f  # storing fileno
19d0: 0a 09 09 73 65 6c 66 2e 5f 66 69 6c 65 6e 6f 20  ...self._fileno 
19e0: 3d 20 66 64 2e 66 69 6c 65 6e 6f 28 29 0a 09 09  = fd.fileno()...
19f0: 23 20 63 72 65 61 74 69 6e 67 20 69 6e 74 65 72  # creating inter
1a00: 66 61 63 65 0a 09 09 73 65 6c 66 2e 5f 69 6f 20  face...self._io 
1a10: 3d 20 69 6f 2e 46 69 6c 65 49 4f 28 73 65 6c 66  = io.FileIO(self
1a20: 2e 5f 66 69 6c 65 6e 6f 2c 20 27 77 27 2c 20 63  ._fileno, 'w', c
1a30: 6c 6f 73 65 66 64 29 0a 09 09 23 20 75 73 69 6e  losefd)...# usin
1a40: 67 20 65 6d 70 74 79 20 74 61 69 6c 0a 09 09 73  g empty tail...s
1a50: 65 6c 66 2e 5f 74 61 69 6c 20 3d 20 4e 6f 6e 65  elf._tail = None
1a60: 0a 09 09 23 20 70 75 74 74 69 6e 67 20 66 69 6c  ...# putting fil
1a70: 65 20 74 6f 20 6e 6f 6e 62 6c 6f 63 6b 69 6e 67  e to nonblocking
1a80: 20 6d 6f 64 65 0a 09 09 66 63 6e 74 6c 2e 66 63   mode...fcntl.fc
1a90: 6e 74 6c 28 73 65 6c 66 2e 5f 66 69 6c 65 6e 6f  ntl(self._fileno
1aa0: 2c 20 66 63 6e 74 6c 2e 46 5f 53 45 54 46 4c 2c  , fcntl.F_SETFL,
1ab0: 20 66 63 6e 74 6c 2e 66 63 6e 74 6c 28 73 65 6c   fcntl.fcntl(sel
1ac0: 66 2e 5f 66 69 6c 65 6e 6f 2c 20 66 63 6e 74 6c  f._fileno, fcntl
1ad0: 2e 46 5f 47 45 54 46 4c 29 20 20 7c 20 6f 73 2e  .F_GETFL)  | os.
1ae0: 4f 5f 4e 4f 4e 42 4c 4f 43 4b 29 0a 0a 09 64 65  O_NONBLOCK)...de
1af0: 66 20 5f 5f 64 65 6c 5f 5f 28 73 65 6c 66 29 3a  f __del__(self):
1b00: 0a 09 09 23 20 70 75 72 67 65 20 71 75 65 75 65  ...# purge queue
1b10: 20 62 65 66 6f 72 65 20 64 65 6c 65 74 69 6e 67   before deleting
1b20: 0a 09 09 69 66 20 6e 6f 74 20 73 65 6c 66 2e 65  ...if not self.e
1b30: 6d 70 74 79 28 29 3a 0a 09 09 09 73 65 6c 66 2e  mpty():....self.
1b40: 6a 6f 69 6e 28 29 0a 0a 09 64 65 66 20 70 75 74  join()...def put
1b50: 28 73 65 6c 66 2c 20 69 74 65 6d 2c 20 62 6c 6f  (self, item, blo
1b60: 63 6b 3d 54 72 75 65 2c 20 74 69 6d 65 6f 75 74  ck=True, timeout
1b70: 3d 4e 6f 6e 65 29 3a 0a 09 09 23 20 63 61 6c 6c  =None):...# call
1b80: 69 6e 67 20 72 65 61 6c 20 70 75 74 0a 09 09 67  ing real put...g
1b90: 65 76 65 6e 74 2e 71 75 65 75 65 2e 4a 6f 69 6e  event.queue.Join
1ba0: 61 62 6c 65 51 75 65 75 65 2e 70 75 74 28 73 65  ableQueue.put(se
1bb0: 6c 66 2c 20 69 74 65 6d 2c 20 62 6c 6f 63 6b 2c  lf, item, block,
1bc0: 20 74 69 6d 65 6f 75 74 29 0a 09 09 23 20 69 6e   timeout)...# in
1bd0: 73 74 61 6c 6c 69 6e 67 20 65 76 65 6e 74 20 68  stalling event h
1be0: 61 6e 64 6c 65 72 0a 09 09 67 65 76 65 6e 74 2e  andler...gevent.
1bf0: 63 6f 72 65 2e 77 72 69 74 65 5f 65 76 65 6e 74  core.write_event
1c00: 28 73 65 6c 66 2e 5f 66 69 6c 65 6e 6f 2c 20 73  (self._fileno, s
1c10: 65 6c 66 2e 5f 77 61 69 74 5f 68 65 6c 70 65 72  elf._wait_helper
1c20: 29 0a 0a 09 64 65 66 20 5f 77 61 69 74 5f 68 65  )...def _wait_he
1c30: 6c 70 65 72 28 73 65 6c 66 2c 20 65 76 2c 20 65  lper(self, ev, e
1c40: 76 74 79 70 65 29 3a 0a 09 09 23 20 58 58 58 20  vtype):...# XXX 
1c50: 65 76 2c 20 65 76 74 79 70 65 20 63 68 65 63 6b  ev, evtype check
1c60: 69 6e 67 3f 0a 09 09 23 20 63 68 65 63 6b 69 6e  ing?...# checkin
1c70: 67 20 6c 65 66 74 6f 76 65 72 0a 09 09 77 68 69  g leftover...whi
1c80: 6c 65 20 54 72 75 65 3a 0a 09 09 09 69 66 20 73  le True:....if s
1c90: 65 6c 66 2e 5f 74 61 69 6c 20 3d 3d 20 4e 6f 6e  elf._tail == Non
1ca0: 65 3a 0a 09 09 09 09 74 72 79 3a 0a 09 09 09 09  e:.....try:.....
1cb0: 09 73 65 6c 66 2e 5f 74 61 69 6c 20 3d 20 73 74  .self._tail = st
1cc0: 72 28 73 65 6c 66 2e 67 65 74 5f 6e 6f 77 61 69  r(self.get_nowai
1cd0: 74 28 29 29 2e 65 6e 63 6f 64 65 28 27 75 74 66  t()).encode('utf
1ce0: 2d 38 27 29 20 2b 20 27 5c 6e 27 0a 09 09 09 09  -8') + '\n'.....
1cf0: 65 78 63 65 70 74 20 67 65 76 65 6e 74 2e 71 75  except gevent.qu
1d00: 65 75 65 2e 45 6d 70 74 79 3a 0a 09 09 09 09 09  eue.Empty:......
1d10: 73 65 6c 66 2e 5f 74 61 69 6c 20 3d 20 4e 6f 6e  self._tail = Non
1d20: 65 0a 09 09 09 09 09 72 65 74 75 72 6e 0a 09 09  e......return...
1d30: 09 23 20 77 72 69 74 69 6e 67 20 74 61 69 6c 0a  .# writing tail.
1d40: 09 09 09 77 72 69 74 74 65 6e 20 3d 20 73 65 6c  ...written = sel
1d50: 66 2e 5f 69 6f 2e 77 72 69 74 65 28 73 65 6c 66  f._io.write(self
1d60: 2e 5f 74 61 69 6c 29 0a 09 09 09 6c 65 6e 67 74  ._tail)....lengt
1d70: 68 20 3d 20 6c 65 6e 28 73 65 6c 66 2e 5f 74 61  h = len(self._ta
1d80: 69 6c 29 0a 09 09 09 69 66 20 77 72 69 74 74 65  il)....if writte
1d90: 6e 20 3d 3d 20 6c 65 6e 67 74 68 3a 0a 09 09 09  n == length:....
1da0: 09 73 65 6c 66 2e 5f 74 61 69 6c 20 3d 20 4e 6f  .self._tail = No
1db0: 6e 65 0a 09 09 09 65 6c 69 66 20 77 72 69 74 74  ne....elif writt
1dc0: 65 6e 20 3c 20 6c 65 6e 67 74 68 3a 0a 09 09 09  en < length:....
1dd0: 09 73 65 6c 66 2e 5f 74 61 69 6c 20 3d 20 73 65  .self._tail = se
1de0: 6c 66 2e 5f 74 61 69 6c 5b 77 72 69 74 74 65 6e  lf._tail[written
1df0: 3a 5d 0a 09 09 09 09 62 72 65 61 6b 0a 09 09 09  :].....break....
1e00: 65 6c 73 65 3a 0a 09 09 09 09 62 72 65 61 6b 0a  else:.....break.
1e10: 09 09 23 20 72 65 69 6e 73 74 61 6c 6c 69 6e 67  ..# reinstalling
1e20: 20 65 76 65 6e 74 20 68 61 6e 64 6c 65 72 0a 09   event handler..
1e30: 09 67 65 76 65 6e 74 2e 63 6f 72 65 2e 77 72 69  .gevent.core.wri
1e40: 74 65 5f 65 76 65 6e 74 28 73 65 6c 66 2e 5f 66  te_event(self._f
1e50: 69 6c 65 6e 6f 2c 20 73 65 6c 66 2e 5f 77 61 69  ileno, self._wai
1e60: 74 5f 68 65 6c 70 65 72 29 0a 0a 23 20 77 72 61  t_helper)..# wra
1e70: 70 70 65 72 20 61 72 6f 75 6e 64 20 64 61 74 61  pper around data
1e80: 62 61 73 65 0a 63 6c 61 73 73 20 74 61 67 44 42  base.class tagDB
1e90: 28 6f 62 6a 65 63 74 29 3a 0a 09 5f 5f 73 6c 6f  (object):..__slo
1ea0: 74 73 5f 5f 20 3d 20 66 72 6f 7a 65 6e 73 65 74  ts__ = frozenset
1eb0: 28 5b 27 5f 63 75 72 73 6f 72 27 2c 20 27 5f 64  (['_cursor', '_d
1ec0: 62 27 5d 29 0a 0a 09 64 65 66 20 5f 5f 69 6e 69  b'])...def __ini
1ed0: 74 5f 5f 28 73 65 6c 66 29 3a 0a 09 09 63 6f 6e  t__(self):...con
1ee0: 66 69 67 2e 73 65 63 74 69 6f 6e 28 27 64 61 74  fig.section('dat
1ef0: 61 62 61 73 65 27 29 0a 09 09 69 66 20 63 6f 6e  abase')...if con
1f00: 66 69 67 5b 27 68 6f 73 74 27 5d 20 3d 3d 20 4e  fig['host'] == N
1f10: 6f 6e 65 3a 0a 09 09 09 73 65 6c 66 2e 5f 64 62  one:....self._db
1f20: 20 3d 20 70 73 79 63 6f 70 67 32 2e 63 6f 6e 6e   = psycopg2.conn
1f30: 65 63 74 28 0a 09 09 09 09 64 61 74 61 62 61 73  ect(.....databas
1f40: 65 20 3d 20 63 6f 6e 66 69 67 5b 27 64 61 74 61  e = config['data
1f50: 62 61 73 65 27 5d 2c 0a 09 09 09 09 75 73 65 72  base'],.....user
1f60: 20 3d 20 63 6f 6e 66 69 67 5b 27 75 73 65 72 27   = config['user'
1f70: 5d 2c 0a 09 09 09 09 70 61 73 73 77 6f 72 64 20  ],.....password 
1f80: 3d 20 63 6f 6e 66 69 67 5b 27 70 61 73 73 77 6f  = config['passwo
1f90: 72 64 27 5d 0a 09 09 09 29 0a 09 09 65 6c 73 65  rd']....)...else
1fa0: 3a 0a 09 09 09 73 65 6c 66 2e 5f 64 62 20 3d 20  :....self._db = 
1fb0: 70 73 79 63 6f 70 67 32 2e 63 6f 6e 6e 65 63 74  psycopg2.connect
1fc0: 28 0a 09 09 09 09 64 61 74 61 62 61 73 65 20 3d  (.....database =
1fd0: 20 63 6f 6e 66 69 67 5b 27 64 61 74 61 62 61 73   config['databas
1fe0: 65 27 5d 2c 0a 09 09 09 09 68 6f 73 74 20 3d 20  e'],.....host = 
1ff0: 63 6f 6e 66 69 67 5b 27 68 6f 73 74 27 5d 2c 0a  config['host'],.
2000: 09 09 09 09 75 73 65 72 20 3d 20 63 6f 6e 66 69  ....user = confi
2010: 67 5b 27 75 73 65 72 27 5d 2c 0a 09 09 09 09 70  g['user'],.....p
2020: 61 73 73 77 6f 72 64 20 3d 20 63 6f 6e 66 69 67  assword = config
2030: 5b 27 70 61 73 73 77 6f 72 64 27 5d 0a 09 09 09  ['password']....
2040: 29 0a 09 09 73 65 6c 66 2e 5f 63 75 72 73 6f 72  )...self._cursor
2050: 20 3d 20 73 65 6c 66 2e 5f 64 62 2e 63 75 72 73   = self._db.curs
2060: 6f 72 28 29 0a 0a 09 64 65 66 20 5f 66 69 65 6c  or()...def _fiel
2070: 64 5f 6e 61 6d 65 73 28 73 65 6c 66 29 3a 0a 09  d_names(self):..
2080: 09 6e 61 6d 65 73 20 3d 20 5b 5d 0a 09 09 66 6f  .names = []...fo
2090: 72 20 72 65 63 6f 72 64 20 69 6e 20 73 65 6c 66  r record in self
20a0: 2e 5f 63 75 72 73 6f 72 2e 64 65 73 63 72 69 70  ._cursor.descrip
20b0: 74 69 6f 6e 3a 0a 09 09 09 6e 61 6d 65 73 2e 61  tion:....names.a
20c0: 70 70 65 6e 64 28 72 65 63 6f 72 64 2e 6e 61 6d  ppend(record.nam
20d0: 65 29 0a 09 09 72 65 74 75 72 6e 28 6e 61 6d 65  e)...return(name
20e0: 73 29 0a 0a 09 64 65 66 20 63 68 65 63 6b 28 73  s)...def check(s
20f0: 65 6c 66 2c 20 73 69 74 65 2c 20 69 70 5f 61 64  elf, site, ip_ad
2100: 64 72 65 73 73 29 3a 0a 09 09 73 65 6c 66 2e 5f  dress):...self._
2110: 63 75 72 73 6f 72 2e 65 78 65 63 75 74 65 28 22  cursor.execute("
2120: 73 65 6c 65 63 74 20 2a 20 66 72 6f 6d 20 28 73  select * from (s
2130: 65 6c 65 63 74 20 72 65 64 69 72 65 63 74 5f 75  elect redirect_u
2140: 72 6c 2c 20 72 65 67 65 78 70 20 66 72 6f 6d 20  rl, regexp from 
2150: 73 69 74 65 5f 72 75 6c 65 20 77 68 65 72 65 20  site_rule where 
2160: 73 69 74 65 20 3c 40 20 74 72 69 70 64 6f 6d 61  site <@ tripdoma
2170: 69 6e 28 25 73 29 20 61 6e 64 20 6e 65 74 6d 61  in(%s) and netma
2180: 73 6b 20 3e 3e 3d 20 25 73 20 6f 72 64 65 72 20  sk >>= %s order 
2190: 62 79 20 61 72 72 61 79 5f 6c 65 6e 67 74 68 28  by array_length(
21a0: 73 69 74 65 2c 20 31 29 20 64 65 73 63 29 20 61  site, 1) desc) a
21b0: 20 67 72 6f 75 70 20 62 79 20 72 65 64 69 72 65   group by redire
21c0: 63 74 5f 75 72 6c 2c 20 72 65 67 65 78 70 22 2c  ct_url, regexp",
21d0: 20 5b 73 69 74 65 2c 20 69 70 5f 61 64 64 72 65   [site, ip_addre
21e0: 73 73 5d 29 0a 09 09 72 65 74 75 72 6e 28 73 65  ss])...return(se
21f0: 6c 66 2e 5f 63 75 72 73 6f 72 2e 66 65 74 63 68  lf._cursor.fetch
2200: 61 6c 6c 28 29 29 0a 0a 09 64 65 66 20 64 75 6d  all())...def dum
2210: 70 28 73 65 6c 66 29 3a 0a 09 09 73 65 6c 66 2e  p(self):...self.
2220: 5f 63 75 72 73 6f 72 2e 65 78 65 63 75 74 65 28  _cursor.execute(
2230: 22 73 65 6c 65 63 74 20 75 6e 74 72 69 70 28 73  "select untrip(s
2240: 69 74 65 29 20 61 73 20 73 69 74 65 2c 20 74 61  ite) as site, ta
2250: 67 3a 3a 74 65 78 74 2c 20 72 65 67 65 78 70 20  g::text, regexp 
2260: 66 72 6f 6d 20 75 72 6c 73 20 6f 72 64 65 72 20  from urls order 
2270: 62 79 20 73 69 74 65 2c 20 74 61 67 22 29 0a 09  by site, tag")..
2280: 09 72 65 74 75 72 6e 28 73 65 6c 66 2e 5f 66 69  .return(self._fi
2290: 65 6c 64 5f 6e 61 6d 65 73 28 29 2c 20 73 65 6c  eld_names(), sel
22a0: 66 2e 5f 63 75 72 73 6f 72 2e 66 65 74 63 68 61  f._cursor.fetcha
22b0: 6c 6c 28 29 29 0a 0a 09 64 65 66 20 6c 6f 61 64  ll())...def load
22c0: 28 73 65 6c 66 2c 20 64 61 74 61 29 3a 0a 09 09  (self, data):...
22d0: 69 66 20 63 6f 6e 66 69 67 2e 6f 70 74 69 6f 6e  if config.option
22e0: 73 2e 66 6c 75 73 68 5f 64 62 3a 0a 09 09 09 73  s.flush_db:....s
22f0: 65 6c 66 2e 5f 63 75 72 73 6f 72 2e 65 78 65 63  elf._cursor.exec
2300: 75 74 65 28 27 64 65 6c 65 74 65 20 66 72 6f 6d  ute('delete from
2310: 20 75 72 6c 73 3b 27 29 0a 09 09 62 75 6e 64 6c   urls;')...bundl
2320: 65 20 3d 20 5b 5d 0a 09 09 66 6f 72 20 72 6f 77  e = []...for row
2330: 20 69 6e 20 64 61 74 61 3a 0a 09 09 09 69 66 20   in data:....if 
2340: 6c 65 6e 28 72 6f 77 29 20 3d 3d 20 32 3a 0a 09  len(row) == 2:..
2350: 09 09 09 62 75 6e 64 6c 65 2e 61 70 70 65 6e 64  ...bundle.append
2360: 28 5b 72 6f 77 5b 30 5d 2c 20 72 6f 77 5b 31 5d  ([row[0], row[1]
2370: 2c 20 4e 6f 6e 65 5d 29 0a 09 09 09 65 6c 73 65  , None])....else
2380: 3a 0a 09 09 09 09 62 75 6e 64 6c 65 2e 61 70 70  :.....bundle.app
2390: 65 6e 64 28 5b 72 6f 77 5b 30 5d 2c 20 72 6f 77  end([row[0], row
23a0: 5b 31 5d 2c 20 72 6f 77 5b 32 5d 5d 29 0a 09 09  [1], row[2]])...
23b0: 73 65 6c 66 2e 5f 63 75 72 73 6f 72 2e 65 78 65  self._cursor.exe
23c0: 63 75 74 65 6d 61 6e 79 28 22 69 6e 73 65 72 74  cutemany("insert
23d0: 20 69 6e 74 6f 20 75 72 6c 73 20 28 73 69 74 65   into urls (site
23e0: 2c 20 74 61 67 2c 20 72 65 67 65 78 70 29 20 76  , tag, regexp) v
23f0: 61 6c 75 65 73 20 28 74 72 69 70 64 6f 6d 61 69  alues (tripdomai
2400: 6e 28 25 73 29 2c 20 25 73 2c 20 25 73 29 22 2c  n(%s), %s, %s)",
2410: 20 62 75 6e 64 6c 65 29 0a 09 09 73 65 6c 66 2e   bundle)...self.
2420: 5f 63 75 72 73 6f 72 2e 65 78 65 63 75 74 65 28  _cursor.execute(
2430: 22 75 70 64 61 74 65 20 75 72 6c 73 20 73 65 74  "update urls set
2440: 20 72 65 67 65 78 70 20 3d 20 4e 55 4c 4c 20 77   regexp = NULL w
2450: 68 65 72 65 20 72 65 67 65 78 70 20 3d 20 27 27  here regexp = ''
2460: 22 29 0a 09 09 73 65 6c 66 2e 5f 64 62 2e 63 6f  ")...self._db.co
2470: 6d 6d 69 74 28 29 0a 0a 09 64 65 66 20 6c 6f 61  mmit()...def loa
2480: 64 5f 63 6f 6e 66 28 73 65 6c 66 2c 20 63 73 76  d_conf(self, csv
2490: 5f 64 61 74 61 29 3a 0a 09 09 73 65 6c 66 2e 5f  _data):...self._
24a0: 63 75 72 73 6f 72 2e 65 78 65 63 75 74 65 28 27  cursor.execute('
24b0: 64 65 6c 65 74 65 20 66 72 6f 6d 20 72 75 6c 65  delete from rule
24c0: 73 3b 27 29 0a 09 09 62 75 6e 64 6c 65 20 3d 20  s;')...bundle = 
24d0: 5b 5d 0a 09 09 66 6f 72 20 72 6f 77 20 69 6e 20  []...for row in 
24e0: 63 73 76 5f 64 61 74 61 3a 0a 09 09 09 62 75 6e  csv_data:....bun
24f0: 64 6c 65 2e 61 70 70 65 6e 64 28 5b 72 6f 77 5b  dle.append([row[
2500: 30 5d 2c 20 72 6f 77 5b 31 5d 2c 20 69 6e 74 28  0], row[1], int(
2510: 72 6f 77 5b 32 5d 29 2c 20 69 6e 74 28 72 6f 77  row[2]), int(row
2520: 5b 33 5d 29 2c 20 72 6f 77 5b 34 5d 2c 20 72 6f  [3]), row[4], ro
2530: 77 5b 35 5d 2c 20 72 6f 77 5b 36 5d 5d 29 0a 09  w[5], row[6]])..
2540: 09 73 65 6c 66 2e 5f 63 75 72 73 6f 72 2e 65 78  .self._cursor.ex
2550: 65 63 75 74 65 6d 61 6e 79 28 22 69 6e 73 65 72  ecutemany("inser
2560: 74 20 69 6e 74 6f 20 72 75 6c 65 73 20 28 6e 65  t into rules (ne
2570: 74 6d 61 73 6b 2c 20 72 65 64 69 72 65 63 74 5f  tmask, redirect_
2580: 75 72 6c 2c 20 66 72 6f 6d 5f 77 65 65 6b 64 61  url, from_weekda
2590: 79 2c 20 74 6f 5f 77 65 65 6b 64 61 79 2c 20 66  y, to_weekday, f
25a0: 72 6f 6d 5f 74 69 6d 65 2c 20 74 6f 5f 74 69 6d  rom_time, to_tim
25b0: 65 2c 20 74 61 67 29 20 76 61 6c 75 65 73 20 28  e, tag) values (
25c0: 25 73 3a 3a 74 65 78 74 3a 3a 63 69 64 72 2c 20  %s::text::cidr, 
25d0: 25 73 2c 20 25 73 2c 20 25 73 2c 20 25 73 3a 3a  %s, %s, %s, %s::
25e0: 74 65 78 74 3a 3a 74 69 6d 65 2c 20 25 73 3a 3a  text::time, %s::
25f0: 74 65 78 74 3a 3a 74 69 6d 65 2c 20 25 73 3a 3a  text::time, %s::
2600: 74 65 78 74 3a 3a 74 65 78 74 5b 5d 29 22 2c 20  text::text[])", 
2610: 62 75 6e 64 6c 65 29 0a 09 09 73 65 6c 66 2e 5f  bundle)...self._
2620: 64 62 2e 63 6f 6d 6d 69 74 28 29 0a 0a 09 64 65  db.commit()...de
2630: 66 20 64 75 6d 70 5f 63 6f 6e 66 28 73 65 6c 66  f dump_conf(self
2640: 29 3a 0a 09 09 73 65 6c 66 2e 5f 63 75 72 73 6f  ):...self._curso
2650: 72 2e 65 78 65 63 75 74 65 28 22 73 65 6c 65 63  r.execute("selec
2660: 74 20 6e 65 74 6d 61 73 6b 2c 20 72 65 64 69 72  t netmask, redir
2670: 65 63 74 5f 75 72 6c 2c 20 66 72 6f 6d 5f 77 65  ect_url, from_we
2680: 65 6b 64 61 79 2c 20 74 6f 5f 77 65 65 6b 64 61  ekday, to_weekda
2690: 79 2c 20 66 72 6f 6d 5f 74 69 6d 65 2c 20 74 6f  y, from_time, to
26a0: 5f 74 69 6d 65 2c 20 74 61 67 3a 3a 74 65 78 74  _time, tag::text
26b0: 20 66 72 6f 6d 20 72 75 6c 65 73 22 29 0a 09 09   from rules")...
26c0: 72 65 74 75 72 6e 28 73 65 6c 66 2e 5f 66 69 65  return(self._fie
26d0: 6c 64 5f 6e 61 6d 65 73 28 29 2c 20 73 65 6c 66  ld_names(), self
26e0: 2e 5f 63 75 72 73 6f 72 2e 66 65 74 63 68 61 6c  ._cursor.fetchal
26f0: 6c 28 29 29 0a 0a 23 20 61 62 73 74 72 61 63 74  l())..# abstract
2700: 20 63 6c 61 73 73 20 77 69 74 68 20 62 61 73 69   class with basi
2710: 63 20 63 68 65 63 6b 69 6e 67 20 66 75 6e 63 74  c checking funct
2720: 69 6f 6e 61 6c 69 74 79 0a 63 6c 61 73 73 20 43  ionality.class C
2730: 68 65 63 6b 65 72 28 6f 62 6a 65 63 74 29 3a 0a  hecker(object):.
2740: 09 5f 5f 73 6c 6f 74 73 5f 5f 20 3d 20 66 72 6f  .__slots__ = fro
2750: 7a 65 6e 73 65 74 28 5b 27 5f 64 62 27 2c 20 27  zenset(['_db', '
2760: 5f 6c 6f 67 27 2c 20 27 5f 71 75 65 75 65 27 2c  _log', '_queue',
2770: 20 27 5f 72 65 71 75 65 73 74 27 2c 20 27 5f 73   '_request', '_s
2780: 74 64 6f 75 74 27 5d 29 0a 0a 09 64 65 66 20 5f  tdout'])...def _
2790: 5f 69 6e 69 74 5f 5f 28 73 65 6c 66 2c 20 71 75  _init__(self, qu
27a0: 65 75 65 2c 20 6c 6f 67 67 65 72 29 3a 0a 09 09  eue, logger):...
27b0: 73 65 6c 66 2e 5f 64 62 20 3d 20 74 61 67 44 42  self._db = tagDB
27c0: 28 29 0a 09 09 73 65 6c 66 2e 5f 6c 6f 67 20 3d  ()...self._log =
27d0: 20 6c 6f 67 67 65 72 0a 09 09 73 65 6c 66 2e 5f   logger...self._
27e0: 6c 6f 67 2e 69 6e 66 6f 28 27 73 74 61 72 74 65  log.info('starte
27f0: 64 27 29 0a 09 09 73 65 6c 66 2e 5f 72 65 71 75  d')...self._requ
2800: 65 73 74 20 3d 20 72 65 2e 63 6f 6d 70 69 6c 65  est = re.compile
2810: 28 27 5e 28 5b 30 2d 39 5d 2b 29 5c 20 28 68 74  ('^([0-9]+)\ (ht
2820: 74 70 7c 66 74 70 29 3a 5c 2f 5c 2f 28 5b 2d 5c  tp|ftp):\/\/([-\
2830: 77 2e 3a 5d 2b 29 5c 2f 28 5b 5e 20 5d 2a 29 5c  w.:]+)\/([^ ]*)\
2840: 20 28 5b 30 2d 39 2e 5d 2b 29 5c 2f 28 2d 7c 5b   ([0-9.]+)\/(-|[
2850: 5c 77 5c 2e 5d 2b 29 5c 20 28 2d 7c 5c 77 2b 29  \w\.]+)\ (-|\w+)
2860: 5c 20 28 2d 7c 47 45 54 7c 48 45 41 44 7c 50 4f  \ (-|GET|HEAD|PO
2870: 53 54 29 2e 2a 24 27 29 0a 09 09 73 65 6c 66 2e  ST).*$')...self.
2880: 5f 71 75 65 75 65 20 3d 20 71 75 65 75 65 0a 09  _queue = queue..
2890: 09 73 65 6c 66 2e 5f 73 74 64 6f 75 74 20 3d 20  .self._stdout = 
28a0: 46 57 72 69 74 65 6c 69 6e 65 51 75 65 75 65 28  FWritelineQueue(
28b0: 73 79 73 2e 73 74 64 6f 75 74 2c 20 46 61 6c 73  sys.stdout, Fals
28c0: 65 29 0a 0a 09 64 65 66 20 70 72 6f 63 65 73 73  e)...def process
28d0: 28 73 65 6c 66 2c 20 69 64 2c 20 73 69 74 65 2c  (self, id, site,
28e0: 20 69 70 5f 61 64 64 72 65 73 73 2c 20 75 72 6c   ip_address, url
28f0: 5f 70 61 74 68 2c 20 6c 69 6e 65 20 3d 20 4e 6f  _path, line = No
2900: 6e 65 29 3a 0a 09 09 23 73 65 6c 66 2e 5f 6c 6f  ne):...#self._lo
2910: 67 2e 69 6e 66 6f 28 27 74 72 79 69 6e 67 20 7b  g.info('trying {
2920: 7d 27 2e 66 6f 72 6d 61 74 28 73 69 74 65 29 29  }'.format(site))
2930: 0a 09 09 72 65 73 75 6c 74 20 3d 20 73 65 6c 66  ...result = self
2940: 2e 5f 64 62 2e 63 68 65 63 6b 28 73 69 74 65 2c  ._db.check(site,
2950: 20 69 70 5f 61 64 64 72 65 73 73 29 0a 09 09 72   ip_address)...r
2960: 65 70 6c 79 20 3d 20 4e 6f 6e 65 0a 09 09 23 73  eply = None...#s
2970: 65 6c 66 2e 5f 6c 6f 67 2e 69 6e 66 6f 28 27 67  elf._log.info('g
2980: 6f 74 20 7b 7d 20 6c 69 6e 65 73 20 66 72 6f 6d  ot {} lines from
2990: 20 64 61 74 61 62 61 73 65 27 2e 66 6f 72 6d 61   database'.forma
29a0: 74 28 6c 65 6e 28 72 65 73 75 6c 74 29 29 29 0a  t(len(result))).
29b0: 09 09 66 6f 72 20 72 6f 77 20 69 6e 20 72 65 73  ..for row in res
29c0: 75 6c 74 3a 0a 09 09 09 69 66 20 72 6f 77 20 21  ult:....if row !
29d0: 3d 20 4e 6f 6e 65 20 61 6e 64 20 72 6f 77 5b 30  = None and row[0
29e0: 5d 20 21 3d 20 4e 6f 6e 65 3a 0a 09 09 09 09 69  ] != None:.....i
29f0: 66 20 72 6f 77 5b 31 5d 20 21 3d 20 4e 6f 6e 65  f row[1] != None
2a00: 3a 0a 09 09 09 09 09 73 65 6c 66 2e 5f 6c 6f 67  :......self._log
2a10: 2e 69 6e 66 6f 28 27 74 72 79 69 6e 67 20 72 65  .info('trying re
2a20: 67 65 78 70 20 22 7b 7d 22 20 76 65 72 73 75 73  gexp "{}" versus
2a30: 20 22 7b 7d 22 27 2e 66 6f 72 6d 61 74 28 72 6f   "{}"'.format(ro
2a40: 77 5b 31 5d 2c 20 75 72 6c 5f 70 61 74 68 29 29  w[1], url_path))
2a50: 0a 09 09 09 09 09 74 72 79 3a 0a 09 09 09 09 09  ......try:......
2a60: 09 69 66 20 72 65 2e 63 6f 6d 70 69 6c 65 28 72  .if re.compile(r
2a70: 6f 77 5b 31 5d 29 2e 6d 61 74 63 68 28 75 72 6c  ow[1]).match(url
2a80: 5f 70 61 74 68 29 3a 0a 09 09 09 09 09 09 09 72  _path):........r
2a90: 65 70 6c 79 20 3d 20 72 6f 77 5b 30 5d 2e 66 6f  eply = row[0].fo
2aa0: 72 6d 61 74 28 75 72 6c 5f 70 61 74 68 29 0a 09  rmat(url_path)..
2ab0: 09 09 09 09 09 65 6c 73 65 3a 0a 09 09 09 09 09  .....else:......
2ac0: 09 09 63 6f 6e 74 69 6e 75 65 0a 09 09 09 09 09  ..continue......
2ad0: 65 78 63 65 70 74 3a 0a 09 09 09 09 09 09 73 65  except:.......se
2ae0: 6c 66 2e 5f 6c 6f 67 2e 69 6e 66 6f 28 22 63 61  lf._log.info("ca
2af0: 6e 27 74 20 63 6f 6d 70 69 6c 65 20 72 65 67 65  n't compile rege
2b00: 78 70 22 29 0a 09 09 09 09 65 6c 73 65 3a 0a 09  xp").....else:..
2b10: 09 09 09 09 72 65 70 6c 79 20 3d 20 72 6f 77 5b  ....reply = row[
2b20: 30 5d 2e 66 6f 72 6d 61 74 28 75 72 6c 5f 70 61  0].format(url_pa
2b30: 74 68 29 0a 09 09 09 69 66 20 72 65 70 6c 79 20  th)....if reply 
2b40: 21 3d 20 4e 6f 6e 65 3a 0a 09 09 09 09 73 65 6c  != None:.....sel
2b50: 66 2e 77 72 69 74 65 6c 69 6e 65 28 27 7b 7d 20  f.writeline('{} 
2b60: 7b 7d 27 2e 66 6f 72 6d 61 74 28 69 64 2c 20 72  {}'.format(id, r
2b70: 65 70 6c 79 29 29 0a 09 09 09 09 72 65 74 75 72  eply)).....retur
2b80: 6e 28 54 72 75 65 29 0a 09 09 73 65 6c 66 2e 77  n(True)...self.w
2b90: 72 69 74 65 6c 69 6e 65 28 27 7b 7d 27 2e 66 6f  riteline('{}'.fo
2ba0: 72 6d 61 74 28 69 64 29 29 0a 0a 09 64 65 66 20  rmat(id))...def 
2bb0: 6c 6f 6f 70 28 73 65 6c 66 29 3a 0a 09 09 77 68  loop(self):...wh
2bc0: 69 6c 65 20 54 72 75 65 3a 0a 09 09 09 6c 69 6e  ile True:....lin
2bd0: 65 20 3d 20 73 65 6c 66 2e 5f 71 75 65 75 65 2e  e = self._queue.
2be0: 67 65 74 28 29 0a 09 09 09 69 66 20 6c 69 6e 65  get()....if line
2bf0: 20 3d 3d 20 4e 6f 6e 65 3a 0a 09 09 09 09 62 72   == None:.....br
2c00: 65 61 6b 0a 09 09 09 23 73 65 6c 66 2e 5f 6c 6f  eak....#self._lo
2c10: 67 2e 69 6e 66 6f 28 27 72 65 71 75 65 73 74 3a  g.info('request:
2c20: 20 27 20 2b 20 6c 69 6e 65 29 0a 09 09 09 72 65   ' + line)....re
2c30: 71 75 65 73 74 20 3d 20 73 65 6c 66 2e 5f 72 65  quest = self._re
2c40: 71 75 65 73 74 2e 6d 61 74 63 68 28 6c 69 6e 65  quest.match(line
2c50: 29 0a 09 09 09 69 66 20 72 65 71 75 65 73 74 3a  )....if request:
2c60: 0a 09 09 09 09 69 64 20 3d 20 72 65 71 75 65 73  .....id = reques
2c70: 74 2e 67 72 6f 75 70 28 31 29 0a 09 09 09 09 23  t.group(1).....#
2c80: 70 72 6f 74 6f 20 3d 20 72 65 71 75 65 73 74 2e  proto = request.
2c90: 67 72 6f 75 70 28 32 29 0a 09 09 09 09 73 69 74  group(2).....sit
2ca0: 65 20 3d 20 72 65 71 75 65 73 74 2e 67 72 6f 75  e = request.grou
2cb0: 70 28 33 29 0a 09 09 09 09 75 72 6c 5f 70 61 74  p(3).....url_pat
2cc0: 68 20 3d 20 72 65 71 75 65 73 74 2e 67 72 6f 75  h = request.grou
2cd0: 70 28 34 29 0a 09 09 09 09 69 70 5f 61 64 64 72  p(4).....ip_addr
2ce0: 65 73 73 20 3d 20 72 65 71 75 65 73 74 2e 67 72  ess = request.gr
2cf0: 6f 75 70 28 35 29 0a 09 09 09 09 73 65 6c 66 2e  oup(5).....self.
2d00: 70 72 6f 63 65 73 73 28 69 64 2c 20 73 69 74 65  process(id, site
2d10: 2c 20 69 70 5f 61 64 64 72 65 73 73 2c 20 75 72  , ip_address, ur
2d20: 6c 5f 70 61 74 68 2c 20 6c 69 6e 65 29 0a 09 09  l_path, line)...
2d30: 09 65 6c 73 65 3a 0a 09 09 09 09 73 65 6c 66 2e  .else:.....self.
2d40: 5f 6c 6f 67 2e 69 6e 66 6f 28 27 62 61 64 20 72  _log.info('bad r
2d50: 65 71 75 65 73 74 27 29 0a 09 09 09 09 73 65 6c  equest').....sel
2d60: 66 2e 77 72 69 74 65 6c 69 6e 65 28 6c 69 6e 65  f.writeline(line
2d70: 29 0a 0a 09 64 65 66 20 77 72 69 74 65 6c 69 6e  )...def writelin
2d80: 65 28 73 65 6c 66 2c 20 73 74 72 69 6e 67 29 3a  e(self, string):
2d90: 0a 09 09 73 65 6c 66 2e 5f 6c 6f 67 2e 69 6e 66  ...self._log.inf
2da0: 6f 28 27 3e 20 27 20 2b 20 73 74 72 69 6e 67 29  o('> ' + string)
2db0: 0a 09 09 73 65 6c 66 2e 5f 73 74 64 6f 75 74 2e  ...self._stdout.
2dc0: 70 75 74 28 73 74 72 69 6e 67 29 0a 0a 69 66 20  put(string)..if 
2dd0: 63 6f 6e 66 69 67 2e 6f 70 74 69 6f 6e 73 2e 64  config.options.d
2de0: 75 6d 70 20 6f 72 20 63 6f 6e 66 69 67 2e 6f 70  ump or config.op
2df0: 74 69 6f 6e 73 2e 6c 6f 61 64 20 6f 72 20 63 6f  tions.load or co
2e00: 6e 66 69 67 2e 6f 70 74 69 6f 6e 73 2e 64 75 6d  nfig.options.dum
2e10: 70 5f 63 6f 6e 66 20 6f 72 20 63 6f 6e 66 69 67  p_conf or config
2e20: 2e 6f 70 74 69 6f 6e 73 2e 6c 6f 61 64 5f 63 6f  .options.load_co
2e30: 6e 66 3a 0a 09 69 6d 70 6f 72 74 20 63 73 76 0a  nf:..import csv.
2e40: 0a 09 74 61 67 64 62 20 3d 20 74 61 67 44 42 28  ..tagdb = tagDB(
2e50: 29 0a 09 64 61 74 61 5f 66 69 65 6c 64 73 20 3d  )..data_fields =
2e60: 20 5b 27 73 69 74 65 27 2c 20 27 74 61 67 27 2c   ['site', 'tag',
2e70: 20 27 72 65 67 65 78 70 27 5d 0a 09 63 6f 6e 66   'regexp']..conf
2e80: 5f 66 69 65 6c 64 73 20 3d 20 5b 27 6e 65 74 6d  _fields = ['netm
2e90: 61 73 6b 27 2c 20 27 72 65 64 69 72 65 63 74 5f  ask', 'redirect_
2ea0: 75 72 6c 27 2c 20 27 66 72 6f 6d 5f 77 65 65 6b  url', 'from_week
2eb0: 64 61 79 27 2c 20 27 74 6f 5f 77 65 65 6b 64 61  day', 'to_weekda
2ec0: 79 27 2c 20 27 66 72 6f 6d 5f 74 69 6d 65 27 2c  y', 'from_time',
2ed0: 20 27 74 6f 5f 74 69 6d 65 27 2c 20 27 74 61 67   'to_time', 'tag
2ee0: 27 5d 0a 0a 09 69 66 20 63 6f 6e 66 69 67 2e 6f  ']...if config.o
2ef0: 70 74 69 6f 6e 73 2e 64 75 6d 70 20 6f 72 20 63  ptions.dump or c
2f00: 6f 6e 66 69 67 2e 6f 70 74 69 6f 6e 73 2e 64 75  onfig.options.du
2f10: 6d 70 5f 63 6f 6e 66 3a 0a 09 09 63 73 76 5f 77  mp_conf:...csv_w
2f20: 72 69 74 65 72 20 3d 20 63 73 76 2e 77 72 69 74  riter = csv.writ
2f30: 65 72 28 73 79 73 2e 73 74 64 6f 75 74 29 0a 09  er(sys.stdout)..
2f40: 09 69 66 20 63 6f 6e 66 69 67 2e 6f 70 74 69 6f  .if config.optio
2f50: 6e 73 2e 64 75 6d 70 3a 0a 09 09 09 64 75 6d 70  ns.dump:....dump
2f60: 20 3d 20 74 61 67 64 62 2e 64 75 6d 70 28 29 0a   = tagdb.dump().
2f70: 09 09 65 6c 69 66 20 63 6f 6e 66 69 67 2e 6f 70  ..elif config.op
2f80: 74 69 6f 6e 73 2e 64 75 6d 70 5f 63 6f 6e 66 3a  tions.dump_conf:
2f90: 0a 09 09 09 64 75 6d 70 20 3d 20 74 61 67 64 62  ....dump = tagdb
2fa0: 2e 64 75 6d 70 5f 63 6f 6e 66 28 29 0a 0a 09 09  .dump_conf()....
2fb0: 63 73 76 5f 77 72 69 74 65 72 2e 77 72 69 74 65  csv_writer.write
2fc0: 72 6f 77 28 64 75 6d 70 5b 30 5d 29 0a 09 09 66  row(dump[0])...f
2fd0: 6f 72 20 6c 69 6e 65 20 69 6e 20 64 75 6d 70 5b  or line in dump[
2fe0: 31 5d 3a 0a 09 09 09 63 73 76 5f 77 72 69 74 65  1]:....csv_write
2ff0: 72 2e 77 72 69 74 65 72 6f 77 28 6c 69 6e 65 29  r.writerow(line)
3000: 0a 0a 09 65 6c 69 66 20 63 6f 6e 66 69 67 2e 6f  ...elif config.o
3010: 70 74 69 6f 6e 73 2e 6c 6f 61 64 20 6f 72 20 63  ptions.load or c
3020: 6f 6e 66 69 67 2e 6f 70 74 69 6f 6e 73 2e 6c 6f  onfig.options.lo
3030: 61 64 5f 63 6f 6e 66 3a 0a 09 09 63 73 76 5f 72  ad_conf:...csv_r
3040: 65 61 64 65 72 20 3d 20 63 73 76 2e 72 65 61 64  eader = csv.read
3050: 65 72 28 73 79 73 2e 73 74 64 69 6e 29 0a 09 09  er(sys.stdin)...
3060: 66 69 72 73 74 5f 72 6f 77 20 3d 20 6e 65 78 74  first_row = next
3070: 28 63 73 76 5f 72 65 61 64 65 72 29 0a 0a 09 09  (csv_reader)....
3080: 69 66 20 63 6f 6e 66 69 67 2e 6f 70 74 69 6f 6e  if config.option
3090: 73 2e 6c 6f 61 64 3a 0a 09 09 09 66 69 65 6c 64  s.load:....field
30a0: 73 20 3d 20 64 61 74 61 5f 66 69 65 6c 64 73 0a  s = data_fields.
30b0: 09 09 09 6c 6f 61 64 20 3d 20 74 61 67 64 62 2e  ...load = tagdb.
30c0: 6c 6f 61 64 0a 09 09 65 6c 69 66 20 63 6f 6e 66  load...elif conf
30d0: 69 67 2e 6f 70 74 69 6f 6e 73 2e 6c 6f 61 64 5f  ig.options.load_
30e0: 63 6f 6e 66 3a 0a 09 09 09 66 69 65 6c 64 73 20  conf:....fields 
30f0: 3d 20 63 6f 6e 66 5f 66 69 65 6c 64 73 0a 09 09  = conf_fields...
3100: 09 6c 6f 61 64 20 3d 20 74 61 67 64 62 2e 6c 6f  .load = tagdb.lo
3110: 61 64 5f 63 6f 6e 66 0a 0a 09 09 61 73 73 65 72  ad_conf....asser
3120: 74 20 66 69 72 73 74 5f 72 6f 77 20 3d 3d 20 66  t first_row == f
3130: 69 65 6c 64 73 2c 20 27 46 69 6c 65 20 6d 75 73  ields, 'File mus
3140: 74 20 63 6f 6e 74 61 69 6e 20 63 73 76 20 64 61  t contain csv da
3150: 74 61 20 77 69 74 68 20 74 68 65 65 73 65 20 63  ta with theese c
3160: 6f 6c 75 6d 6e 73 3a 20 27 20 2b 20 72 65 70 72  olumns: ' + repr
3170: 28 66 69 65 6c 64 73 29 0a 09 09 6c 6f 61 64 28  (fields)...load(
3180: 63 73 76 5f 72 65 61 64 65 72 29 0a 0a 65 6c 73  csv_reader)..els
3190: 65 3a 0a 09 23 20 6d 61 69 6e 20 6c 6f 6f 70 0a  e:..# main loop.
31a0: 09 43 68 65 63 6b 65 72 28 73 74 64 69 6e 2c 20  .Checker(stdin, 
31b0: 6c 6f 67 67 65 72 29 2e 6c 6f 6f 70 28 29 0a     logger).loop().