Squid url redirector

Hex Artifact Content
anonymous

Hex Artifact Content

Artifact 568859367f9460d3717359c5b8d56464ba282ac972760effc8157e16da48bc21:


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 23 20 6d 79 20 73 79 73 6c 6f 67  ):...# my syslog
0f00: 20 69 73 20 62 72 6f 6b 65 6e 20 61 6e 64 20 63   is broken and c
0f10: 61 6e 6e 6f 74 20 69 6e 74 6f 20 55 54 46 2d 38  annot into UTF-8
0f20: 20 42 4f 4d 0a 09 09 72 65 63 6f 72 64 2e 6d 73   BOM...record.ms
0f30: 67 20 3d 20 73 74 72 28 72 65 63 6f 72 64 2e 6d  g = str(record.m
0f40: 73 67 29 0a 09 09 73 65 6c 66 2e 5f 74 61 69 6c  sg)...self._tail
0f50: 2e 70 75 74 28 72 65 63 6f 72 64 29 0a 09 09 69  .put(record)...i
0f60: 66 20 73 65 6c 66 2e 5f 77 6f 72 6b 65 72 20 3d  f self._worker =
0f70: 3d 20 4e 6f 6e 65 3a 0a 09 09 09 23 20 69 6e 20  = None:....# in 
0f80: 63 61 73 65 20 71 75 65 75 65 20 69 73 20 65 6d  case queue is em
0f90: 70 74 79 20 77 65 20 77 69 6c 6c 20 73 70 61 77  pty we will spaw
0fa0: 6e 20 6e 65 77 20 77 6f 72 6b 65 72 0a 09 09 09  n new worker....
0fb0: 23 20 61 6c 6c 20 77 6f 72 6b 65 72 73 20 61 72  # all workers ar
0fc0: 65 20 6c 6f 67 67 65 64 20 73 6f 20 77 65 20 63  e logged so we c
0fd0: 61 6e 20 6b 69 6c 6c 20 74 68 65 6d 20 6f 6e 20  an kill them on 
0fe0: 63 6c 6f 73 65 28 29 0a 09 09 09 73 65 6c 66 2e  close()....self.
0ff0: 5f 77 6f 72 6b 65 72 20 3d 20 67 65 76 65 6e 74  _worker = gevent
1000: 2e 73 70 61 77 6e 28 73 65 6c 66 2e 5f 77 72 69  .spawn(self._wri
1010: 74 65 72 29 0a 0a 09 64 65 66 20 5f 77 72 69 74  ter)...def _writ
1020: 65 72 28 73 65 6c 66 29 3a 0a 09 09 23 20 68 65  er(self):...# he
1030: 72 65 20 77 65 20 61 72 65 20 6c 6f 63 6b 69 6e  re we are lockin
1040: 67 20 74 68 65 20 71 75 65 75 65 20 73 6f 20 77  g the queue so w
1050: 65 20 63 61 6e 20 62 65 20 73 75 72 65 20 77 65  e can be sure we
1060: 20 61 72 65 20 74 68 65 20 6f 6e 6c 79 20 6f 6e   are the only on
1070: 65 0a 09 09 70 72 69 6e 74 28 27 73 79 73 6c 6f  e...print('syslo
1080: 67 20 73 74 61 72 74 27 29 0a 09 09 77 68 69 6c  g start')...whil
1090: 65 20 6e 6f 74 20 73 65 6c 66 2e 5f 74 61 69 6c  e not self._tail
10a0: 2e 65 6d 70 74 79 28 29 3a 0a 09 09 09 6c 6f 67  .empty():....log
10b0: 67 69 6e 67 2e 68 61 6e 64 6c 65 72 73 2e 53 79  ging.handlers.Sy
10c0: 73 4c 6f 67 48 61 6e 64 6c 65 72 2e 65 6d 69 74  sLogHandler.emit
10d0: 28 73 65 6c 66 2c 20 73 65 6c 66 2e 5f 74 61 69  (self, self._tai
10e0: 6c 2e 67 65 74 28 29 29 0a 09 09 73 65 6c 66 2e  l.get())...self.
10f0: 5f 77 6f 72 6b 65 72 20 3d 20 4e 6f 6e 65 0a 09  _worker = None..
1100: 09 70 72 69 6e 74 28 27 73 79 73 6c 6f 67 20 65  .print('syslog e
1110: 6e 64 27 29 0a 0a 09 64 65 66 20 63 6c 6f 73 65  nd')...def close
1120: 28 73 65 6c 66 29 3a 0a 09 09 69 66 20 73 65 6c  (self):...if sel
1130: 66 2e 5f 77 6f 72 6b 65 72 20 21 3d 20 4e 6f 6e  f._worker != Non
1140: 65 3a 0a 09 09 09 67 65 76 65 6e 74 2e 6b 69 6c  e:....gevent.kil
1150: 6c 28 73 65 6c 66 2e 5f 77 6f 72 6b 65 72 29 0a  l(self._worker).
1160: 09 09 6c 6f 67 67 69 6e 67 2e 68 61 6e 64 6c 65  ..logging.handle
1170: 72 73 2e 53 79 73 4c 6f 67 48 61 6e 64 6c 65 72  rs.SysLogHandler
1180: 2e 63 6c 6f 73 65 28 73 65 6c 66 29 0a 0a 6c 6f  .close(self)..lo
1190: 67 67 65 72 20 3d 20 6c 6f 67 67 69 6e 67 2e 67  gger = logging.g
11a0: 65 74 4c 6f 67 67 65 72 28 27 73 71 75 69 64 54  etLogger('squidT
11b0: 61 67 27 29 0a 6c 6f 67 67 65 72 2e 73 65 74 4c  ag').logger.setL
11c0: 65 76 65 6c 28 6c 6f 67 67 69 6e 67 2e 49 4e 46  evel(logging.INF
11d0: 4f 29 0a 68 61 6e 64 6c 65 72 20 3d 20 53 79 73  O).handler = Sys
11e0: 4c 6f 67 48 61 6e 64 6c 65 72 51 75 65 75 65 28  LogHandlerQueue(
11f0: 29 0a 68 61 6e 64 6c 65 72 2e 73 65 74 46 6f 72  ).handler.setFor
1200: 6d 61 74 74 65 72 28 6c 6f 67 67 69 6e 67 2e 46  matter(logging.F
1210: 6f 72 6d 61 74 74 65 72 28 73 74 72 28 27 73 71  ormatter(str('sq
1220: 75 69 64 54 61 67 5b 25 28 70 72 6f 63 65 73 73  uidTag[%(process
1230: 29 73 5d 3a 20 25 28 6d 65 73 73 61 67 65 29 73  )s]: %(message)s
1240: 27 29 29 29 0a 6c 6f 67 67 65 72 2e 61 64 64 48  '))).logger.addH
1250: 61 6e 64 6c 65 72 28 68 61 6e 64 6c 65 72 29 0a  andler(handler).
1260: 0a 23 20 74 69 6e 79 20 77 72 61 70 70 65 72 20  .# tiny wrapper 
1270: 61 72 6f 75 6e 64 20 61 20 66 69 6c 65 20 74 6f  around a file to
1280: 20 6d 61 6b 65 20 72 65 61 64 73 20 66 72 6f 6d   make reads from
1290: 20 69 74 20 67 65 76 65 6e 74 61 62 6c 65 0a 23   it geventable.#
12a0: 20 6f 72 20 73 68 6f 75 6c 64 20 69 20 6d 6f 76   or should i mov
12b0: 65 20 74 68 69 73 20 73 6f 6d 65 77 68 65 72 65  e this somewhere
12c0: 3f 0a 0a 63 6c 61 73 73 20 46 52 65 61 64 6c 69  ?..class FReadli
12d0: 6e 65 51 75 65 75 65 28 67 65 76 65 6e 74 2e 71  neQueue(gevent.q
12e0: 75 65 75 65 2e 51 75 65 75 65 29 3a 0a 09 23 20  ueue.Queue):..# 
12f0: 73 74 6f 72 69 6e 67 20 66 69 6c 65 20 64 65 73  storing file des
1300: 63 72 69 70 74 6f 72 2c 20 6c 65 66 74 6f 76 65  criptor, leftove
1310: 72 0a 09 5f 5f 73 6c 6f 74 73 5f 5f 20 3d 20 66  r..__slots__ = f
1320: 72 6f 7a 65 6e 73 65 74 28 5b 27 5f 69 6f 27 2c  rozenset(['_io',
1330: 20 27 5f 66 69 6c 65 6e 6f 27 2c 20 27 5f 74 61   '_fileno', '_ta
1340: 69 6c 27 5d 29 0a 0a 09 64 65 66 20 5f 5f 69 6e  il'])...def __in
1350: 69 74 5f 5f 28 73 65 6c 66 2c 20 66 64 2c 20 63  it__(self, fd, c
1360: 6c 6f 73 65 66 64 20 3d 20 54 72 75 65 29 3a 0a  losefd = True):.
1370: 09 09 69 6d 70 6f 72 74 20 69 6f 0a 09 09 23 20  ..import io...# 
1380: 69 6e 69 74 69 61 6c 69 73 69 6e 67 20 63 6c 61  initialising cla
1390: 73 73 0a 09 09 67 65 76 65 6e 74 2e 71 75 65 75  ss...gevent.queu
13a0: 65 2e 51 75 65 75 65 2e 5f 5f 69 6e 69 74 5f 5f  e.Queue.__init__
13b0: 28 73 65 6c 66 29 0a 09 09 23 20 73 74 6f 72 69  (self)...# stori
13c0: 6e 67 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  ng file descript
13d0: 6f 72 0a 09 09 73 65 6c 66 2e 5f 66 69 6c 65 6e  or...self._filen
13e0: 6f 20 3d 20 66 64 2e 66 69 6c 65 6e 6f 28 29 0a  o = fd.fileno().
13f0: 09 09 73 65 6c 66 2e 5f 69 6f 20 3d 20 69 6f 2e  ..self._io = io.
1400: 46 69 6c 65 49 4f 28 73 65 6c 66 2e 5f 66 69 6c  FileIO(self._fil
1410: 65 6e 6f 2c 20 27 72 27 2c 20 63 6c 6f 73 65 66  eno, 'r', closef
1420: 64 29 0a 09 09 23 20 75 73 69 6e 67 20 65 6d 70  d)...# using emp
1430: 74 79 20 74 61 69 6c 0a 09 09 73 65 6c 66 2e 5f  ty tail...self._
1440: 74 61 69 6c 20 3d 20 27 27 0a 09 09 23 20 73 65  tail = ''...# se
1450: 74 74 69 6e 67 20 75 70 20 65 76 65 6e 74 0a 09  tting up event..
1460: 09 73 65 6c 66 2e 5f 69 6e 73 74 61 6c 6c 5f 77  .self._install_w
1470: 61 69 74 28 29 0a 0a 09 64 65 66 20 5f 69 6e 73  ait()...def _ins
1480: 74 61 6c 6c 5f 77 61 69 74 28 73 65 6c 66 29 3a  tall_wait(self):
1490: 0a 09 09 23 20 70 75 74 74 69 6e 67 20 66 69 6c  ...# putting fil
14a0: 65 20 74 6f 20 6e 6f 6e 62 6c 6f 63 6b 69 6e 67  e to nonblocking
14b0: 20 6d 6f 64 65 0a 09 09 66 63 6e 74 6c 2e 66 63   mode...fcntl.fc
14c0: 6e 74 6c 28 73 65 6c 66 2e 5f 66 69 6c 65 6e 6f  ntl(self._fileno
14d0: 2c 20 66 63 6e 74 6c 2e 46 5f 53 45 54 46 4c 2c  , fcntl.F_SETFL,
14e0: 20 66 63 6e 74 6c 2e 66 63 6e 74 6c 28 73 65 6c   fcntl.fcntl(sel
14f0: 66 2e 5f 66 69 6c 65 6e 6f 2c 20 66 63 6e 74 6c  f._fileno, fcntl
1500: 2e 46 5f 47 45 54 46 4c 29 20 20 7c 20 6f 73 2e  .F_GETFL)  | os.
1510: 4f 5f 4e 4f 4e 42 4c 4f 43 4b 29 0a 09 09 23 20  O_NONBLOCK)...# 
1520: 69 6e 73 74 61 6c 6c 69 6e 67 20 65 76 65 6e 74  installing event
1530: 20 68 61 6e 64 6c 65 72 0a 09 09 67 65 76 65 6e   handler...geven
1540: 74 2e 63 6f 72 65 2e 72 65 61 64 5f 65 76 65 6e  t.core.read_even
1550: 74 28 73 65 6c 66 2e 5f 66 69 6c 65 6e 6f 2c 20  t(self._fileno, 
1560: 73 65 6c 66 2e 5f 77 61 69 74 5f 68 65 6c 70 65  self._wait_helpe
1570: 72 29 0a 0a 09 64 65 66 20 5f 77 61 69 74 5f 68  r)...def _wait_h
1580: 65 6c 70 65 72 28 73 65 6c 66 2c 20 65 76 2c 20  elper(self, ev, 
1590: 65 76 74 79 70 65 29 3a 0a 09 09 23 20 72 65 61  evtype):...# rea
15a0: 64 69 6e 67 20 6f 6e 65 20 62 75 66 66 65 72 20  ding one buffer 
15b0: 66 72 6f 6d 20 73 74 72 65 61 6d 0a 09 09 62 75  from stream...bu
15c0: 66 20 3d 20 73 65 6c 66 2e 5f 69 6f 2e 72 65 61  f = self._io.rea
15d0: 64 28 34 30 39 36 29 0a 09 09 23 20 73 70 6c 69  d(4096)...# spli
15e0: 74 74 69 6e 67 20 73 74 72 65 61 6d 20 62 79 20  tting stream by 
15f0: 6c 69 6e 65 20 65 6e 64 73 0a 09 09 72 6f 77 73  line ends...rows
1600: 20 3d 20 62 75 66 2e 64 65 63 6f 64 65 28 27 6c   = buf.decode('l
1610: 31 27 29 2e 73 70 6c 69 74 28 27 5c 6e 27 29 0a  1').split('\n').
1620: 09 09 23 20 61 64 64 69 6e 67 20 74 61 69 6c 20  ..# adding tail 
1630: 74 6f 20 74 68 65 20 66 69 72 73 74 20 65 6c 65  to the first ele
1640: 6d 65 6e 74 20 69 66 20 74 68 65 72 65 20 69 73  ment if there is
1650: 20 73 6f 6d 65 20 74 61 69 6c 0a 09 09 69 66 20   some tail...if 
1660: 6c 65 6e 28 73 65 6c 66 2e 5f 74 61 69 6c 29 20  len(self._tail) 
1670: 3e 20 30 3a 0a 09 09 09 72 6f 77 73 5b 30 5d 20  > 0:....rows[0] 
1680: 3d 20 73 65 6c 66 2e 5f 74 61 69 6c 20 2b 20 72  = self._tail + r
1690: 6f 77 73 5b 30 5d 0a 09 09 23 20 70 6f 70 70 69  ows[0]...# poppi
16a0: 6e 67 20 6f 75 74 20 6c 61 73 74 20 28 69 6e 63  ng out last (inc
16b0: 6f 6d 70 6c 65 74 65 29 20 65 6c 65 6d 65 6e 74  omplete) element
16c0: 0a 09 09 73 65 6c 66 2e 5f 74 61 69 6c 20 3d 20  ...self._tail = 
16d0: 72 6f 77 73 2e 70 6f 70 28 2d 31 29 0a 09 09 23  rows.pop(-1)...#
16e0: 20 64 72 6f 70 70 69 6e 67 20 61 6c 6c 20 63 6f   dropping all co
16f0: 6d 70 6c 65 74 65 20 65 6c 65 6d 65 6e 74 73 20  mplete elements 
1700: 74 6f 20 74 68 65 20 71 75 65 75 65 0a 09 09 66  to the queue...f
1710: 6f 72 20 72 6f 77 20 69 6e 20 72 6f 77 73 3a 0a  or row in rows:.
1720: 09 09 09 73 65 6c 66 2e 70 75 74 5f 6e 6f 77 61  ...self.put_nowa
1730: 69 74 28 72 6f 77 29 0a 09 09 09 6c 6f 67 67 65  it(row)....logge
1740: 72 2e 69 6e 66 6f 28 27 3c 20 27 20 2b 20 72 6f  r.info('< ' + ro
1750: 77 29 0a 09 09 69 66 20 6c 65 6e 28 62 75 66 29  w)...if len(buf)
1760: 20 3e 20 30 3a 0a 09 09 09 23 20 6e 6f 20 45 4f   > 0:....# no EO
1770: 46 2c 20 72 65 69 6e 73 74 61 6c 6c 69 6e 67 20  F, reinstalling 
1780: 65 76 65 6e 74 20 68 61 6e 64 6c 65 72 0a 09 09  event handler...
1790: 09 67 65 76 65 6e 74 2e 63 6f 72 65 2e 72 65 61  .gevent.core.rea
17a0: 64 5f 65 76 65 6e 74 28 73 65 6c 66 2e 5f 66 69  d_event(self._fi
17b0: 6c 65 6e 6f 2c 20 73 65 6c 66 2e 5f 77 61 69 74  leno, self._wait
17c0: 5f 68 65 6c 70 65 72 29 0a 09 09 65 6c 73 65 3a  _helper)...else:
17d0: 0a 09 09 09 23 20 45 4f 46 20 66 6f 75 6e 64 2c  ....# EOF found,
17e0: 20 73 65 6e 64 69 6e 67 20 45 4f 46 20 74 6f 20   sending EOF to 
17f0: 71 75 65 75 65 0a 09 09 09 73 65 6c 66 2e 70 75  queue....self.pu
1800: 74 5f 6e 6f 77 61 69 74 28 4e 6f 6e 65 29 0a 0a  t_nowait(None)..
1810: 73 74 64 69 6e 20 3d 20 46 52 65 61 64 6c 69 6e  stdin = FReadlin
1820: 65 51 75 65 75 65 28 73 79 73 2e 73 74 64 69 6e  eQueue(sys.stdin
1830: 2c 20 46 61 6c 73 65 29 0a 0a 23 20 77 72 61 70  , False)..# wrap
1840: 70 65 72 20 61 67 61 69 6e 73 74 20 66 69 6c 65  per against file
1850: 20 68 61 6e 64 6c 65 72 20 74 68 61 74 20 6d 61   handler that ma
1860: 6b 65 73 20 70 6f 73 73 69 62 6c 65 20 74 6f 20  kes possible to 
1870: 71 75 65 75 65 20 73 6f 6d 65 20 77 72 69 74 65  queue some write
1880: 73 20 77 69 74 68 6f 75 74 20 73 74 61 6c 6c 69  s without stalli
1890: 6e 67 0a 0a 63 6c 61 73 73 20 46 57 72 69 74 65  ng..class FWrite
18a0: 6c 69 6e 65 51 75 65 75 65 28 67 65 76 65 6e 74  lineQueue(gevent
18b0: 2e 71 75 65 75 65 2e 4a 6f 69 6e 61 62 6c 65 51  .queue.JoinableQ
18c0: 75 65 75 65 29 3a 0a 09 23 20 73 74 6f 72 69 6e  ueue):..# storin
18d0: 67 20 66 69 6c 65 6e 6f 2c 20 69 6f 20 69 6e 74  g fileno, io int
18e0: 65 72 66 61 63 65 2c 20 6c 65 66 74 6f 76 65 72  erface, leftover
18f0: 0a 09 5f 5f 73 6c 6f 74 73 5f 5f 20 3d 20 66 72  ..__slots__ = fr
1900: 6f 7a 65 6e 73 65 74 28 5b 27 5f 66 69 6c 65 6e  ozenset(['_filen
1910: 6f 27 2c 20 27 5f 69 6f 27 2c 20 27 5f 74 61 69  o', '_io', '_tai
1920: 6c 27 5d 29 0a 0a 09 64 65 66 20 5f 5f 69 6e 69  l'])...def __ini
1930: 74 5f 5f 28 73 65 6c 66 2c 20 66 64 2c 20 63 6c  t__(self, fd, cl
1940: 6f 73 65 66 64 20 3d 20 54 72 75 65 29 3a 0a 09  osefd = True):..
1950: 09 69 6d 70 6f 72 74 20 69 6f 0a 09 09 23 20 69  .import io...# i
1960: 6e 69 74 69 61 6c 69 73 69 6e 67 20 63 6c 61 73  nitialising clas
1970: 73 0a 09 09 67 65 76 65 6e 74 2e 71 75 65 75 65  s...gevent.queue
1980: 2e 4a 6f 69 6e 61 62 6c 65 51 75 65 75 65 2e 5f  .JoinableQueue._
1990: 5f 69 6e 69 74 5f 5f 28 73 65 6c 66 29 0a 09 09  _init__(self)...
19a0: 23 20 73 74 6f 72 69 6e 67 20 66 69 6c 65 6e 6f  # storing fileno
19b0: 0a 09 09 73 65 6c 66 2e 5f 66 69 6c 65 6e 6f 20  ...self._fileno 
19c0: 3d 20 66 64 2e 66 69 6c 65 6e 6f 28 29 0a 09 09  = fd.fileno()...
19d0: 23 20 63 72 65 61 74 69 6e 67 20 69 6e 74 65 72  # creating inter
19e0: 66 61 63 65 0a 09 09 73 65 6c 66 2e 5f 69 6f 20  face...self._io 
19f0: 3d 20 69 6f 2e 46 69 6c 65 49 4f 28 73 65 6c 66  = io.FileIO(self
1a00: 2e 5f 66 69 6c 65 6e 6f 2c 20 27 77 27 2c 20 63  ._fileno, 'w', c
1a10: 6c 6f 73 65 66 64 29 0a 09 09 23 20 75 73 69 6e  losefd)...# usin
1a20: 67 20 65 6d 70 74 79 20 74 61 69 6c 0a 09 09 73  g empty tail...s
1a30: 65 6c 66 2e 5f 74 61 69 6c 20 3d 20 4e 6f 6e 65  elf._tail = None
1a40: 0a 09 09 23 20 70 75 74 74 69 6e 67 20 66 69 6c  ...# putting fil
1a50: 65 20 74 6f 20 6e 6f 6e 62 6c 6f 63 6b 69 6e 67  e to nonblocking
1a60: 20 6d 6f 64 65 0a 09 09 66 63 6e 74 6c 2e 66 63   mode...fcntl.fc
1a70: 6e 74 6c 28 73 65 6c 66 2e 5f 66 69 6c 65 6e 6f  ntl(self._fileno
1a80: 2c 20 66 63 6e 74 6c 2e 46 5f 53 45 54 46 4c 2c  , fcntl.F_SETFL,
1a90: 20 66 63 6e 74 6c 2e 66 63 6e 74 6c 28 73 65 6c   fcntl.fcntl(sel
1aa0: 66 2e 5f 66 69 6c 65 6e 6f 2c 20 66 63 6e 74 6c  f._fileno, fcntl
1ab0: 2e 46 5f 47 45 54 46 4c 29 20 20 7c 20 6f 73 2e  .F_GETFL)  | os.
1ac0: 4f 5f 4e 4f 4e 42 4c 4f 43 4b 29 0a 0a 09 64 65  O_NONBLOCK)...de
1ad0: 66 20 5f 5f 64 65 6c 5f 5f 28 73 65 6c 66 29 3a  f __del__(self):
1ae0: 0a 09 09 23 20 70 75 72 67 65 20 71 75 65 75 65  ...# purge queue
1af0: 20 62 65 66 6f 72 65 20 64 65 6c 65 74 69 6e 67   before deleting
1b00: 0a 09 09 69 66 20 6e 6f 74 20 73 65 6c 66 2e 65  ...if not self.e
1b10: 6d 70 74 79 28 29 3a 0a 09 09 09 73 65 6c 66 2e  mpty():....self.
1b20: 6a 6f 69 6e 28 29 0a 0a 09 64 65 66 20 70 75 74  join()...def put
1b30: 28 73 65 6c 66 2c 20 69 74 65 6d 2c 20 62 6c 6f  (self, item, blo
1b40: 63 6b 3d 54 72 75 65 2c 20 74 69 6d 65 6f 75 74  ck=True, timeout
1b50: 3d 4e 6f 6e 65 29 3a 0a 09 09 23 20 63 61 6c 6c  =None):...# call
1b60: 69 6e 67 20 72 65 61 6c 20 70 75 74 0a 09 09 67  ing real put...g
1b70: 65 76 65 6e 74 2e 71 75 65 75 65 2e 4a 6f 69 6e  event.queue.Join
1b80: 61 62 6c 65 51 75 65 75 65 2e 70 75 74 28 73 65  ableQueue.put(se
1b90: 6c 66 2c 20 69 74 65 6d 2c 20 62 6c 6f 63 6b 2c  lf, item, block,
1ba0: 20 74 69 6d 65 6f 75 74 29 0a 09 09 23 20 69 6e   timeout)...# in
1bb0: 73 74 61 6c 6c 69 6e 67 20 65 76 65 6e 74 20 68  stalling event h
1bc0: 61 6e 64 6c 65 72 0a 09 09 67 65 76 65 6e 74 2e  andler...gevent.
1bd0: 63 6f 72 65 2e 77 72 69 74 65 5f 65 76 65 6e 74  core.write_event
1be0: 28 73 65 6c 66 2e 5f 66 69 6c 65 6e 6f 2c 20 73  (self._fileno, s
1bf0: 65 6c 66 2e 5f 77 61 69 74 5f 68 65 6c 70 65 72  elf._wait_helper
1c00: 29 0a 0a 09 64 65 66 20 5f 77 61 69 74 5f 68 65  )...def _wait_he
1c10: 6c 70 65 72 28 73 65 6c 66 2c 20 65 76 2c 20 65  lper(self, ev, e
1c20: 76 74 79 70 65 29 3a 0a 09 09 23 20 58 58 58 20  vtype):...# XXX 
1c30: 65 76 2c 20 65 76 74 79 70 65 20 63 68 65 63 6b  ev, evtype check
1c40: 69 6e 67 3f 0a 09 09 23 20 63 68 65 63 6b 69 6e  ing?...# checkin
1c50: 67 20 6c 65 66 74 6f 76 65 72 0a 09 09 77 68 69  g leftover...whi
1c60: 6c 65 20 54 72 75 65 3a 0a 09 09 09 69 66 20 73  le True:....if s
1c70: 65 6c 66 2e 5f 74 61 69 6c 20 3d 3d 20 4e 6f 6e  elf._tail == Non
1c80: 65 3a 0a 09 09 09 09 74 72 79 3a 0a 09 09 09 09  e:.....try:.....
1c90: 09 73 65 6c 66 2e 5f 74 61 69 6c 20 3d 20 73 74  .self._tail = st
1ca0: 72 28 73 65 6c 66 2e 67 65 74 5f 6e 6f 77 61 69  r(self.get_nowai
1cb0: 74 28 29 29 2e 65 6e 63 6f 64 65 28 27 75 74 66  t()).encode('utf
1cc0: 2d 38 27 29 20 2b 20 27 5c 6e 27 0a 09 09 09 09  -8') + '\n'.....
1cd0: 65 78 63 65 70 74 20 67 65 76 65 6e 74 2e 71 75  except gevent.qu
1ce0: 65 75 65 2e 45 6d 70 74 79 3a 0a 09 09 09 09 09  eue.Empty:......
1cf0: 73 65 6c 66 2e 5f 74 61 69 6c 20 3d 20 4e 6f 6e  self._tail = Non
1d00: 65 0a 09 09 09 09 09 72 65 74 75 72 6e 0a 09 09  e......return...
1d10: 09 23 20 77 72 69 74 69 6e 67 20 74 61 69 6c 0a  .# writing tail.
1d20: 09 09 09 77 72 69 74 74 65 6e 20 3d 20 73 65 6c  ...written = sel
1d30: 66 2e 5f 69 6f 2e 77 72 69 74 65 28 73 65 6c 66  f._io.write(self
1d40: 2e 5f 74 61 69 6c 29 0a 09 09 09 6c 65 6e 67 74  ._tail)....lengt
1d50: 68 20 3d 20 6c 65 6e 28 73 65 6c 66 2e 5f 74 61  h = len(self._ta
1d60: 69 6c 29 0a 09 09 09 69 66 20 77 72 69 74 74 65  il)....if writte
1d70: 6e 20 3d 3d 20 6c 65 6e 67 74 68 3a 0a 09 09 09  n == length:....
1d80: 09 73 65 6c 66 2e 5f 74 61 69 6c 20 3d 20 4e 6f  .self._tail = No
1d90: 6e 65 0a 09 09 09 65 6c 69 66 20 77 72 69 74 74  ne....elif writt
1da0: 65 6e 20 3c 20 6c 65 6e 67 74 68 3a 0a 09 09 09  en < length:....
1db0: 09 73 65 6c 66 2e 5f 74 61 69 6c 20 3d 20 73 65  .self._tail = se
1dc0: 6c 66 2e 5f 74 61 69 6c 5b 77 72 69 74 74 65 6e  lf._tail[written
1dd0: 3a 5d 0a 09 09 09 09 62 72 65 61 6b 0a 09 09 09  :].....break....
1de0: 65 6c 73 65 3a 0a 09 09 09 09 62 72 65 61 6b 0a  else:.....break.
1df0: 09 09 23 20 72 65 69 6e 73 74 61 6c 6c 69 6e 67  ..# reinstalling
1e00: 20 65 76 65 6e 74 20 68 61 6e 64 6c 65 72 0a 09   event handler..
1e10: 09 67 65 76 65 6e 74 2e 63 6f 72 65 2e 77 72 69  .gevent.core.wri
1e20: 74 65 5f 65 76 65 6e 74 28 73 65 6c 66 2e 5f 66  te_event(self._f
1e30: 69 6c 65 6e 6f 2c 20 73 65 6c 66 2e 5f 77 61 69  ileno, self._wai
1e40: 74 5f 68 65 6c 70 65 72 29 0a 0a 23 20 77 72 61  t_helper)..# wra
1e50: 70 70 65 72 20 61 72 6f 75 6e 64 20 64 61 74 61  pper around data
1e60: 62 61 73 65 0a 63 6c 61 73 73 20 74 61 67 44 42  base.class tagDB
1e70: 28 6f 62 6a 65 63 74 29 3a 0a 09 5f 5f 73 6c 6f  (object):..__slo
1e80: 74 73 5f 5f 20 3d 20 66 72 6f 7a 65 6e 73 65 74  ts__ = frozenset
1e90: 28 5b 27 5f 63 75 72 73 6f 72 27 2c 20 27 5f 64  (['_cursor', '_d
1ea0: 62 27 5d 29 0a 0a 09 64 65 66 20 5f 5f 69 6e 69  b'])...def __ini
1eb0: 74 5f 5f 28 73 65 6c 66 29 3a 0a 09 09 63 6f 6e  t__(self):...con
1ec0: 66 69 67 2e 73 65 63 74 69 6f 6e 28 27 64 61 74  fig.section('dat
1ed0: 61 62 61 73 65 27 29 0a 09 09 69 66 20 63 6f 6e  abase')...if con
1ee0: 66 69 67 5b 27 68 6f 73 74 27 5d 20 3d 3d 20 4e  fig['host'] == N
1ef0: 6f 6e 65 3a 0a 09 09 09 73 65 6c 66 2e 5f 64 62  one:....self._db
1f00: 20 3d 20 70 73 79 63 6f 70 67 32 2e 63 6f 6e 6e   = psycopg2.conn
1f10: 65 63 74 28 0a 09 09 09 09 64 61 74 61 62 61 73  ect(.....databas
1f20: 65 20 3d 20 63 6f 6e 66 69 67 5b 27 64 61 74 61  e = config['data
1f30: 62 61 73 65 27 5d 2c 0a 09 09 09 09 75 73 65 72  base'],.....user
1f40: 20 3d 20 63 6f 6e 66 69 67 5b 27 75 73 65 72 27   = config['user'
1f50: 5d 2c 0a 09 09 09 09 70 61 73 73 77 6f 72 64 20  ],.....password 
1f60: 3d 20 63 6f 6e 66 69 67 5b 27 70 61 73 73 77 6f  = config['passwo
1f70: 72 64 27 5d 0a 09 09 09 29 0a 09 09 65 6c 73 65  rd']....)...else
1f80: 3a 0a 09 09 09 73 65 6c 66 2e 5f 64 62 20 3d 20  :....self._db = 
1f90: 70 73 79 63 6f 70 67 32 2e 63 6f 6e 6e 65 63 74  psycopg2.connect
1fa0: 28 0a 09 09 09 09 64 61 74 61 62 61 73 65 20 3d  (.....database =
1fb0: 20 63 6f 6e 66 69 67 5b 27 64 61 74 61 62 61 73   config['databas
1fc0: 65 27 5d 2c 0a 09 09 09 09 68 6f 73 74 20 3d 20  e'],.....host = 
1fd0: 63 6f 6e 66 69 67 5b 27 68 6f 73 74 27 5d 2c 0a  config['host'],.
1fe0: 09 09 09 09 75 73 65 72 20 3d 20 63 6f 6e 66 69  ....user = confi
1ff0: 67 5b 27 75 73 65 72 27 5d 2c 0a 09 09 09 09 70  g['user'],.....p
2000: 61 73 73 77 6f 72 64 20 3d 20 63 6f 6e 66 69 67  assword = config
2010: 5b 27 70 61 73 73 77 6f 72 64 27 5d 0a 09 09 09  ['password']....
2020: 29 0a 09 09 73 65 6c 66 2e 5f 63 75 72 73 6f 72  )...self._cursor
2030: 20 3d 20 73 65 6c 66 2e 5f 64 62 2e 63 75 72 73   = self._db.curs
2040: 6f 72 28 29 0a 0a 09 64 65 66 20 5f 66 69 65 6c  or()...def _fiel
2050: 64 5f 6e 61 6d 65 73 28 73 65 6c 66 29 3a 0a 09  d_names(self):..
2060: 09 6e 61 6d 65 73 20 3d 20 5b 5d 0a 09 09 66 6f  .names = []...fo
2070: 72 20 72 65 63 6f 72 64 20 69 6e 20 73 65 6c 66  r record in self
2080: 2e 5f 63 75 72 73 6f 72 2e 64 65 73 63 72 69 70  ._cursor.descrip
2090: 74 69 6f 6e 3a 0a 09 09 09 6e 61 6d 65 73 2e 61  tion:....names.a
20a0: 70 70 65 6e 64 28 72 65 63 6f 72 64 2e 6e 61 6d  ppend(record.nam
20b0: 65 29 0a 09 09 72 65 74 75 72 6e 28 6e 61 6d 65  e)...return(name
20c0: 73 29 0a 0a 09 64 65 66 20 63 68 65 63 6b 28 73  s)...def check(s
20d0: 65 6c 66 2c 20 73 69 74 65 2c 20 69 70 5f 61 64  elf, site, ip_ad
20e0: 64 72 65 73 73 29 3a 0a 09 09 73 65 6c 66 2e 5f  dress):...self._
20f0: 63 75 72 73 6f 72 2e 65 78 65 63 75 74 65 28 22  cursor.execute("
2100: 73 65 6c 65 63 74 20 2a 20 66 72 6f 6d 20 28 73  select * from (s
2110: 65 6c 65 63 74 20 72 65 64 69 72 65 63 74 5f 75  elect redirect_u
2120: 72 6c 2c 20 72 65 67 65 78 70 20 66 72 6f 6d 20  rl, regexp from 
2130: 73 69 74 65 5f 72 75 6c 65 20 77 68 65 72 65 20  site_rule where 
2140: 73 69 74 65 20 3c 40 20 74 72 69 70 64 6f 6d 61  site <@ tripdoma
2150: 69 6e 28 25 73 29 20 61 6e 64 20 6e 65 74 6d 61  in(%s) and netma
2160: 73 6b 20 3e 3e 3d 20 25 73 20 6f 72 64 65 72 20  sk >>= %s order 
2170: 62 79 20 61 72 72 61 79 5f 6c 65 6e 67 74 68 28  by array_length(
2180: 73 69 74 65 2c 20 31 29 20 64 65 73 63 29 20 61  site, 1) desc) a
2190: 20 67 72 6f 75 70 20 62 79 20 72 65 64 69 72 65   group by redire
21a0: 63 74 5f 75 72 6c 2c 20 72 65 67 65 78 70 22 2c  ct_url, regexp",
21b0: 20 5b 73 69 74 65 2c 20 69 70 5f 61 64 64 72 65   [site, ip_addre
21c0: 73 73 5d 29 0a 09 09 72 65 74 75 72 6e 28 73 65  ss])...return(se
21d0: 6c 66 2e 5f 63 75 72 73 6f 72 2e 66 65 74 63 68  lf._cursor.fetch
21e0: 61 6c 6c 28 29 29 0a 0a 09 64 65 66 20 64 75 6d  all())...def dum
21f0: 70 28 73 65 6c 66 29 3a 0a 09 09 73 65 6c 66 2e  p(self):...self.
2200: 5f 63 75 72 73 6f 72 2e 65 78 65 63 75 74 65 28  _cursor.execute(
2210: 22 73 65 6c 65 63 74 20 75 6e 74 72 69 70 28 73  "select untrip(s
2220: 69 74 65 29 20 61 73 20 73 69 74 65 2c 20 74 61  ite) as site, ta
2230: 67 3a 3a 74 65 78 74 2c 20 72 65 67 65 78 70 20  g::text, regexp 
2240: 66 72 6f 6d 20 75 72 6c 73 20 6f 72 64 65 72 20  from urls order 
2250: 62 79 20 73 69 74 65 2c 20 74 61 67 22 29 0a 09  by site, tag")..
2260: 09 72 65 74 75 72 6e 28 73 65 6c 66 2e 5f 66 69  .return(self._fi
2270: 65 6c 64 5f 6e 61 6d 65 73 28 29 2c 20 73 65 6c  eld_names(), sel
2280: 66 2e 5f 63 75 72 73 6f 72 2e 66 65 74 63 68 61  f._cursor.fetcha
2290: 6c 6c 28 29 29 0a 0a 09 64 65 66 20 6c 6f 61 64  ll())...def load
22a0: 28 73 65 6c 66 2c 20 64 61 74 61 29 3a 0a 09 09  (self, data):...
22b0: 69 66 20 63 6f 6e 66 69 67 2e 6f 70 74 69 6f 6e  if config.option
22c0: 73 2e 66 6c 75 73 68 5f 64 62 3a 0a 09 09 09 73  s.flush_db:....s
22d0: 65 6c 66 2e 5f 63 75 72 73 6f 72 2e 65 78 65 63  elf._cursor.exec
22e0: 75 74 65 28 27 64 65 6c 65 74 65 20 66 72 6f 6d  ute('delete from
22f0: 20 75 72 6c 73 3b 27 29 0a 09 09 62 75 6e 64 6c   urls;')...bundl
2300: 65 20 3d 20 5b 5d 0a 09 09 66 6f 72 20 72 6f 77  e = []...for row
2310: 20 69 6e 20 64 61 74 61 3a 0a 09 09 09 69 66 20   in data:....if 
2320: 6c 65 6e 28 72 6f 77 29 20 3d 3d 20 32 3a 0a 09  len(row) == 2:..
2330: 09 09 09 62 75 6e 64 6c 65 2e 61 70 70 65 6e 64  ...bundle.append
2340: 28 5b 72 6f 77 5b 30 5d 2c 20 72 6f 77 5b 31 5d  ([row[0], row[1]
2350: 2c 20 4e 6f 6e 65 5d 29 0a 09 09 09 65 6c 73 65  , None])....else
2360: 3a 0a 09 09 09 09 62 75 6e 64 6c 65 2e 61 70 70  :.....bundle.app
2370: 65 6e 64 28 5b 72 6f 77 5b 30 5d 2c 20 72 6f 77  end([row[0], row
2380: 5b 31 5d 2c 20 72 6f 77 5b 32 5d 5d 29 0a 09 09  [1], row[2]])...
2390: 73 65 6c 66 2e 5f 63 75 72 73 6f 72 2e 65 78 65  self._cursor.exe
23a0: 63 75 74 65 6d 61 6e 79 28 22 69 6e 73 65 72 74  cutemany("insert
23b0: 20 69 6e 74 6f 20 75 72 6c 73 20 28 73 69 74 65   into urls (site
23c0: 2c 20 74 61 67 2c 20 72 65 67 65 78 70 29 20 76  , tag, regexp) v
23d0: 61 6c 75 65 73 20 28 74 72 69 70 64 6f 6d 61 69  alues (tripdomai
23e0: 6e 28 25 73 29 2c 20 25 73 2c 20 25 73 29 22 2c  n(%s), %s, %s)",
23f0: 20 62 75 6e 64 6c 65 29 0a 09 09 73 65 6c 66 2e   bundle)...self.
2400: 5f 63 75 72 73 6f 72 2e 65 78 65 63 75 74 65 28  _cursor.execute(
2410: 22 75 70 64 61 74 65 20 75 72 6c 73 20 73 65 74  "update urls set
2420: 20 72 65 67 65 78 70 20 3d 20 4e 55 4c 4c 20 77   regexp = NULL w
2430: 68 65 72 65 20 72 65 67 65 78 70 20 3d 20 27 27  here regexp = ''
2440: 22 29 0a 09 09 73 65 6c 66 2e 5f 64 62 2e 63 6f  ")...self._db.co
2450: 6d 6d 69 74 28 29 0a 0a 09 64 65 66 20 6c 6f 61  mmit()...def loa
2460: 64 5f 63 6f 6e 66 28 73 65 6c 66 2c 20 63 73 76  d_conf(self, csv
2470: 5f 64 61 74 61 29 3a 0a 09 09 73 65 6c 66 2e 5f  _data):...self._
2480: 63 75 72 73 6f 72 2e 65 78 65 63 75 74 65 28 27  cursor.execute('
2490: 64 65 6c 65 74 65 20 66 72 6f 6d 20 72 75 6c 65  delete from rule
24a0: 73 3b 27 29 0a 09 09 62 75 6e 64 6c 65 20 3d 20  s;')...bundle = 
24b0: 5b 5d 0a 09 09 66 6f 72 20 72 6f 77 20 69 6e 20  []...for row in 
24c0: 63 73 76 5f 64 61 74 61 3a 0a 09 09 09 62 75 6e  csv_data:....bun
24d0: 64 6c 65 2e 61 70 70 65 6e 64 28 5b 72 6f 77 5b  dle.append([row[
24e0: 30 5d 2c 20 72 6f 77 5b 31 5d 2c 20 69 6e 74 28  0], row[1], int(
24f0: 72 6f 77 5b 32 5d 29 2c 20 69 6e 74 28 72 6f 77  row[2]), int(row
2500: 5b 33 5d 29 2c 20 72 6f 77 5b 34 5d 2c 20 72 6f  [3]), row[4], ro
2510: 77 5b 35 5d 2c 20 72 6f 77 5b 36 5d 5d 29 0a 09  w[5], row[6]])..
2520: 09 73 65 6c 66 2e 5f 63 75 72 73 6f 72 2e 65 78  .self._cursor.ex
2530: 65 63 75 74 65 6d 61 6e 79 28 22 69 6e 73 65 72  ecutemany("inser
2540: 74 20 69 6e 74 6f 20 72 75 6c 65 73 20 28 6e 65  t into rules (ne
2550: 74 6d 61 73 6b 2c 20 72 65 64 69 72 65 63 74 5f  tmask, redirect_
2560: 75 72 6c 2c 20 66 72 6f 6d 5f 77 65 65 6b 64 61  url, from_weekda
2570: 79 2c 20 74 6f 5f 77 65 65 6b 64 61 79 2c 20 66  y, to_weekday, f
2580: 72 6f 6d 5f 74 69 6d 65 2c 20 74 6f 5f 74 69 6d  rom_time, to_tim
2590: 65 2c 20 74 61 67 29 20 76 61 6c 75 65 73 20 28  e, tag) values (
25a0: 25 73 3a 3a 74 65 78 74 3a 3a 63 69 64 72 2c 20  %s::text::cidr, 
25b0: 25 73 2c 20 25 73 2c 20 25 73 2c 20 25 73 3a 3a  %s, %s, %s, %s::
25c0: 74 65 78 74 3a 3a 74 69 6d 65 2c 20 25 73 3a 3a  text::time, %s::
25d0: 74 65 78 74 3a 3a 74 69 6d 65 2c 20 25 73 3a 3a  text::time, %s::
25e0: 74 65 78 74 3a 3a 74 65 78 74 5b 5d 29 22 2c 20  text::text[])", 
25f0: 62 75 6e 64 6c 65 29 0a 09 09 73 65 6c 66 2e 5f  bundle)...self._
2600: 64 62 2e 63 6f 6d 6d 69 74 28 29 0a 0a 09 64 65  db.commit()...de
2610: 66 20 64 75 6d 70 5f 63 6f 6e 66 28 73 65 6c 66  f dump_conf(self
2620: 29 3a 0a 09 09 73 65 6c 66 2e 5f 63 75 72 73 6f  ):...self._curso
2630: 72 2e 65 78 65 63 75 74 65 28 22 73 65 6c 65 63  r.execute("selec
2640: 74 20 6e 65 74 6d 61 73 6b 2c 20 72 65 64 69 72  t netmask, redir
2650: 65 63 74 5f 75 72 6c 2c 20 66 72 6f 6d 5f 77 65  ect_url, from_we
2660: 65 6b 64 61 79 2c 20 74 6f 5f 77 65 65 6b 64 61  ekday, to_weekda
2670: 79 2c 20 66 72 6f 6d 5f 74 69 6d 65 2c 20 74 6f  y, from_time, to
2680: 5f 74 69 6d 65 2c 20 74 61 67 3a 3a 74 65 78 74  _time, tag::text
2690: 20 66 72 6f 6d 20 72 75 6c 65 73 22 29 0a 09 09   from rules")...
26a0: 72 65 74 75 72 6e 28 73 65 6c 66 2e 5f 66 69 65  return(self._fie
26b0: 6c 64 5f 6e 61 6d 65 73 28 29 2c 20 73 65 6c 66  ld_names(), self
26c0: 2e 5f 63 75 72 73 6f 72 2e 66 65 74 63 68 61 6c  ._cursor.fetchal
26d0: 6c 28 29 29 0a 0a 23 20 61 62 73 74 72 61 63 74  l())..# abstract
26e0: 20 63 6c 61 73 73 20 77 69 74 68 20 62 61 73 69   class with basi
26f0: 63 20 63 68 65 63 6b 69 6e 67 20 66 75 6e 63 74  c checking funct
2700: 69 6f 6e 61 6c 69 74 79 0a 63 6c 61 73 73 20 43  ionality.class C
2710: 68 65 63 6b 65 72 28 6f 62 6a 65 63 74 29 3a 0a  hecker(object):.
2720: 09 5f 5f 73 6c 6f 74 73 5f 5f 20 3d 20 66 72 6f  .__slots__ = fro
2730: 7a 65 6e 73 65 74 28 5b 27 5f 64 62 27 2c 20 27  zenset(['_db', '
2740: 5f 6c 6f 67 27 2c 20 27 5f 71 75 65 75 65 27 2c  _log', '_queue',
2750: 20 27 5f 72 65 71 75 65 73 74 27 2c 20 27 5f 73   '_request', '_s
2760: 74 64 6f 75 74 27 5d 29 0a 0a 09 64 65 66 20 5f  tdout'])...def _
2770: 5f 69 6e 69 74 5f 5f 28 73 65 6c 66 2c 20 71 75  _init__(self, qu
2780: 65 75 65 2c 20 6c 6f 67 67 65 72 29 3a 0a 09 09  eue, logger):...
2790: 73 65 6c 66 2e 5f 64 62 20 3d 20 74 61 67 44 42  self._db = tagDB
27a0: 28 29 0a 09 09 73 65 6c 66 2e 5f 6c 6f 67 20 3d  ()...self._log =
27b0: 20 6c 6f 67 67 65 72 0a 09 09 73 65 6c 66 2e 5f   logger...self._
27c0: 6c 6f 67 2e 69 6e 66 6f 28 27 73 74 61 72 74 65  log.info('starte
27d0: 64 27 29 0a 09 09 73 65 6c 66 2e 5f 72 65 71 75  d')...self._requ
27e0: 65 73 74 20 3d 20 72 65 2e 63 6f 6d 70 69 6c 65  est = re.compile
27f0: 28 27 5e 28 5b 30 2d 39 5d 2b 29 5c 20 28 68 74  ('^([0-9]+)\ (ht
2800: 74 70 7c 66 74 70 29 3a 5c 2f 5c 2f 28 5b 2d 5c  tp|ftp):\/\/([-\
2810: 77 2e 3a 5d 2b 29 5c 2f 28 5b 5e 20 5d 2a 29 5c  w.:]+)\/([^ ]*)\
2820: 20 28 5b 30 2d 39 2e 5d 2b 29 5c 2f 28 2d 7c 5b   ([0-9.]+)\/(-|[
2830: 5c 77 5c 2e 5d 2b 29 5c 20 28 2d 7c 5c 77 2b 29  \w\.]+)\ (-|\w+)
2840: 5c 20 28 2d 7c 47 45 54 7c 48 45 41 44 7c 50 4f  \ (-|GET|HEAD|PO
2850: 53 54 29 2e 2a 24 27 29 0a 09 09 73 65 6c 66 2e  ST).*$')...self.
2860: 5f 71 75 65 75 65 20 3d 20 71 75 65 75 65 0a 09  _queue = queue..
2870: 09 73 65 6c 66 2e 5f 73 74 64 6f 75 74 20 3d 20  .self._stdout = 
2880: 46 57 72 69 74 65 6c 69 6e 65 51 75 65 75 65 28  FWritelineQueue(
2890: 73 79 73 2e 73 74 64 6f 75 74 2c 20 46 61 6c 73  sys.stdout, Fals
28a0: 65 29 0a 0a 09 64 65 66 20 70 72 6f 63 65 73 73  e)...def process
28b0: 28 73 65 6c 66 2c 20 69 64 2c 20 73 69 74 65 2c  (self, id, site,
28c0: 20 69 70 5f 61 64 64 72 65 73 73 2c 20 75 72 6c   ip_address, url
28d0: 5f 70 61 74 68 2c 20 6c 69 6e 65 20 3d 20 4e 6f  _path, line = No
28e0: 6e 65 29 3a 0a 09 09 23 73 65 6c 66 2e 5f 6c 6f  ne):...#self._lo
28f0: 67 2e 69 6e 66 6f 28 27 74 72 79 69 6e 67 20 7b  g.info('trying {
2900: 7d 27 2e 66 6f 72 6d 61 74 28 73 69 74 65 29 29  }'.format(site))
2910: 0a 09 09 72 65 73 75 6c 74 20 3d 20 73 65 6c 66  ...result = self
2920: 2e 5f 64 62 2e 63 68 65 63 6b 28 73 69 74 65 2c  ._db.check(site,
2930: 20 69 70 5f 61 64 64 72 65 73 73 29 0a 09 09 72   ip_address)...r
2940: 65 70 6c 79 20 3d 20 4e 6f 6e 65 0a 09 09 23 73  eply = None...#s
2950: 65 6c 66 2e 5f 6c 6f 67 2e 69 6e 66 6f 28 27 67  elf._log.info('g
2960: 6f 74 20 7b 7d 20 6c 69 6e 65 73 20 66 72 6f 6d  ot {} lines from
2970: 20 64 61 74 61 62 61 73 65 27 2e 66 6f 72 6d 61   database'.forma
2980: 74 28 6c 65 6e 28 72 65 73 75 6c 74 29 29 29 0a  t(len(result))).
2990: 09 09 66 6f 72 20 72 6f 77 20 69 6e 20 72 65 73  ..for row in res
29a0: 75 6c 74 3a 0a 09 09 09 69 66 20 72 6f 77 20 21  ult:....if row !
29b0: 3d 20 4e 6f 6e 65 20 61 6e 64 20 72 6f 77 5b 30  = None and row[0
29c0: 5d 20 21 3d 20 4e 6f 6e 65 3a 0a 09 09 09 09 69  ] != None:.....i
29d0: 66 20 72 6f 77 5b 31 5d 20 21 3d 20 4e 6f 6e 65  f row[1] != None
29e0: 3a 0a 09 09 09 09 09 73 65 6c 66 2e 5f 6c 6f 67  :......self._log
29f0: 2e 69 6e 66 6f 28 27 74 72 79 69 6e 67 20 72 65  .info('trying re
2a00: 67 65 78 70 20 22 7b 7d 22 20 76 65 72 73 75 73  gexp "{}" versus
2a10: 20 22 7b 7d 22 27 2e 66 6f 72 6d 61 74 28 72 6f   "{}"'.format(ro
2a20: 77 5b 31 5d 2c 20 75 72 6c 5f 70 61 74 68 29 29  w[1], url_path))
2a30: 0a 09 09 09 09 09 74 72 79 3a 0a 09 09 09 09 09  ......try:......
2a40: 09 69 66 20 72 65 2e 63 6f 6d 70 69 6c 65 28 72  .if re.compile(r
2a50: 6f 77 5b 31 5d 29 2e 6d 61 74 63 68 28 75 72 6c  ow[1]).match(url
2a60: 5f 70 61 74 68 29 3a 0a 09 09 09 09 09 09 09 72  _path):........r
2a70: 65 70 6c 79 20 3d 20 72 6f 77 5b 30 5d 2e 66 6f  eply = row[0].fo
2a80: 72 6d 61 74 28 75 72 6c 5f 70 61 74 68 29 0a 09  rmat(url_path)..
2a90: 09 09 09 09 09 65 6c 73 65 3a 0a 09 09 09 09 09  .....else:......
2aa0: 09 09 63 6f 6e 74 69 6e 75 65 0a 09 09 09 09 09  ..continue......
2ab0: 65 78 63 65 70 74 3a 0a 09 09 09 09 09 09 73 65  except:.......se
2ac0: 6c 66 2e 5f 6c 6f 67 2e 69 6e 66 6f 28 22 63 61  lf._log.info("ca
2ad0: 6e 27 74 20 63 6f 6d 70 69 6c 65 20 72 65 67 65  n't compile rege
2ae0: 78 70 22 29 0a 09 09 09 09 65 6c 73 65 3a 0a 09  xp").....else:..
2af0: 09 09 09 09 72 65 70 6c 79 20 3d 20 72 6f 77 5b  ....reply = row[
2b00: 30 5d 2e 66 6f 72 6d 61 74 28 75 72 6c 5f 70 61  0].format(url_pa
2b10: 74 68 29 0a 09 09 09 69 66 20 72 65 70 6c 79 20  th)....if reply 
2b20: 21 3d 20 4e 6f 6e 65 3a 0a 09 09 09 09 73 65 6c  != None:.....sel
2b30: 66 2e 77 72 69 74 65 6c 69 6e 65 28 27 7b 7d 20  f.writeline('{} 
2b40: 7b 7d 27 2e 66 6f 72 6d 61 74 28 69 64 2c 20 72  {}'.format(id, r
2b50: 65 70 6c 79 29 29 0a 09 09 09 09 72 65 74 75 72  eply)).....retur
2b60: 6e 28 54 72 75 65 29 0a 09 09 73 65 6c 66 2e 77  n(True)...self.w
2b70: 72 69 74 65 6c 69 6e 65 28 27 7b 7d 27 2e 66 6f  riteline('{}'.fo
2b80: 72 6d 61 74 28 69 64 29 29 0a 0a 09 64 65 66 20  rmat(id))...def 
2b90: 6c 6f 6f 70 28 73 65 6c 66 29 3a 0a 09 09 77 68  loop(self):...wh
2ba0: 69 6c 65 20 54 72 75 65 3a 0a 09 09 09 6c 69 6e  ile True:....lin
2bb0: 65 20 3d 20 73 65 6c 66 2e 5f 71 75 65 75 65 2e  e = self._queue.
2bc0: 67 65 74 28 29 0a 09 09 09 69 66 20 6c 69 6e 65  get()....if line
2bd0: 20 3d 3d 20 4e 6f 6e 65 3a 0a 09 09 09 09 62 72   == None:.....br
2be0: 65 61 6b 0a 09 09 09 23 73 65 6c 66 2e 5f 6c 6f  eak....#self._lo
2bf0: 67 2e 69 6e 66 6f 28 27 72 65 71 75 65 73 74 3a  g.info('request:
2c00: 20 27 20 2b 20 6c 69 6e 65 29 0a 09 09 09 72 65   ' + line)....re
2c10: 71 75 65 73 74 20 3d 20 73 65 6c 66 2e 5f 72 65  quest = self._re
2c20: 71 75 65 73 74 2e 6d 61 74 63 68 28 6c 69 6e 65  quest.match(line
2c30: 29 0a 09 09 09 69 66 20 72 65 71 75 65 73 74 3a  )....if request:
2c40: 0a 09 09 09 09 69 64 20 3d 20 72 65 71 75 65 73  .....id = reques
2c50: 74 2e 67 72 6f 75 70 28 31 29 0a 09 09 09 09 23  t.group(1).....#
2c60: 70 72 6f 74 6f 20 3d 20 72 65 71 75 65 73 74 2e  proto = request.
2c70: 67 72 6f 75 70 28 32 29 0a 09 09 09 09 73 69 74  group(2).....sit
2c80: 65 20 3d 20 72 65 71 75 65 73 74 2e 67 72 6f 75  e = request.grou
2c90: 70 28 33 29 0a 09 09 09 09 75 72 6c 5f 70 61 74  p(3).....url_pat
2ca0: 68 20 3d 20 72 65 71 75 65 73 74 2e 67 72 6f 75  h = request.grou
2cb0: 70 28 34 29 0a 09 09 09 09 69 70 5f 61 64 64 72  p(4).....ip_addr
2cc0: 65 73 73 20 3d 20 72 65 71 75 65 73 74 2e 67 72  ess = request.gr
2cd0: 6f 75 70 28 35 29 0a 09 09 09 09 73 65 6c 66 2e  oup(5).....self.
2ce0: 70 72 6f 63 65 73 73 28 69 64 2c 20 73 69 74 65  process(id, site
2cf0: 2c 20 69 70 5f 61 64 64 72 65 73 73 2c 20 75 72  , ip_address, ur
2d00: 6c 5f 70 61 74 68 2c 20 6c 69 6e 65 29 0a 09 09  l_path, line)...
2d10: 09 65 6c 73 65 3a 0a 09 09 09 09 73 65 6c 66 2e  .else:.....self.
2d20: 5f 6c 6f 67 2e 69 6e 66 6f 28 27 62 61 64 20 72  _log.info('bad r
2d30: 65 71 75 65 73 74 27 29 0a 09 09 09 09 73 65 6c  equest').....sel
2d40: 66 2e 77 72 69 74 65 6c 69 6e 65 28 6c 69 6e 65  f.writeline(line
2d50: 29 0a 0a 09 64 65 66 20 77 72 69 74 65 6c 69 6e  )...def writelin
2d60: 65 28 73 65 6c 66 2c 20 73 74 72 69 6e 67 29 3a  e(self, string):
2d70: 0a 09 09 73 65 6c 66 2e 5f 6c 6f 67 2e 69 6e 66  ...self._log.inf
2d80: 6f 28 27 3e 20 27 20 2b 20 73 74 72 69 6e 67 29  o('> ' + string)
2d90: 0a 09 09 73 65 6c 66 2e 5f 73 74 64 6f 75 74 2e  ...self._stdout.
2da0: 70 75 74 28 73 74 72 69 6e 67 29 0a 0a 69 66 20  put(string)..if 
2db0: 63 6f 6e 66 69 67 2e 6f 70 74 69 6f 6e 73 2e 64  config.options.d
2dc0: 75 6d 70 20 6f 72 20 63 6f 6e 66 69 67 2e 6f 70  ump or config.op
2dd0: 74 69 6f 6e 73 2e 6c 6f 61 64 20 6f 72 20 63 6f  tions.load or co
2de0: 6e 66 69 67 2e 6f 70 74 69 6f 6e 73 2e 64 75 6d  nfig.options.dum
2df0: 70 5f 63 6f 6e 66 20 6f 72 20 63 6f 6e 66 69 67  p_conf or config
2e00: 2e 6f 70 74 69 6f 6e 73 2e 6c 6f 61 64 5f 63 6f  .options.load_co
2e10: 6e 66 3a 0a 09 69 6d 70 6f 72 74 20 63 73 76 0a  nf:..import csv.
2e20: 0a 09 74 61 67 64 62 20 3d 20 74 61 67 44 42 28  ..tagdb = tagDB(
2e30: 29 0a 09 64 61 74 61 5f 66 69 65 6c 64 73 20 3d  )..data_fields =
2e40: 20 5b 27 73 69 74 65 27 2c 20 27 74 61 67 27 2c   ['site', 'tag',
2e50: 20 27 72 65 67 65 78 70 27 5d 0a 09 63 6f 6e 66   'regexp']..conf
2e60: 5f 66 69 65 6c 64 73 20 3d 20 5b 27 6e 65 74 6d  _fields = ['netm
2e70: 61 73 6b 27 2c 20 27 72 65 64 69 72 65 63 74 5f  ask', 'redirect_
2e80: 75 72 6c 27 2c 20 27 66 72 6f 6d 5f 77 65 65 6b  url', 'from_week
2e90: 64 61 79 27 2c 20 27 74 6f 5f 77 65 65 6b 64 61  day', 'to_weekda
2ea0: 79 27 2c 20 27 66 72 6f 6d 5f 74 69 6d 65 27 2c  y', 'from_time',
2eb0: 20 27 74 6f 5f 74 69 6d 65 27 2c 20 27 74 61 67   'to_time', 'tag
2ec0: 27 5d 0a 0a 09 69 66 20 63 6f 6e 66 69 67 2e 6f  ']...if config.o
2ed0: 70 74 69 6f 6e 73 2e 64 75 6d 70 20 6f 72 20 63  ptions.dump or c
2ee0: 6f 6e 66 69 67 2e 6f 70 74 69 6f 6e 73 2e 64 75  onfig.options.du
2ef0: 6d 70 5f 63 6f 6e 66 3a 0a 09 09 63 73 76 5f 77  mp_conf:...csv_w
2f00: 72 69 74 65 72 20 3d 20 63 73 76 2e 77 72 69 74  riter = csv.writ
2f10: 65 72 28 73 79 73 2e 73 74 64 6f 75 74 29 0a 09  er(sys.stdout)..
2f20: 09 69 66 20 63 6f 6e 66 69 67 2e 6f 70 74 69 6f  .if config.optio
2f30: 6e 73 2e 64 75 6d 70 3a 0a 09 09 09 64 75 6d 70  ns.dump:....dump
2f40: 20 3d 20 74 61 67 64 62 2e 64 75 6d 70 28 29 0a   = tagdb.dump().
2f50: 09 09 65 6c 69 66 20 63 6f 6e 66 69 67 2e 6f 70  ..elif config.op
2f60: 74 69 6f 6e 73 2e 64 75 6d 70 5f 63 6f 6e 66 3a  tions.dump_conf:
2f70: 0a 09 09 09 64 75 6d 70 20 3d 20 74 61 67 64 62  ....dump = tagdb
2f80: 2e 64 75 6d 70 5f 63 6f 6e 66 28 29 0a 0a 09 09  .dump_conf()....
2f90: 63 73 76 5f 77 72 69 74 65 72 2e 77 72 69 74 65  csv_writer.write
2fa0: 72 6f 77 28 64 75 6d 70 5b 30 5d 29 0a 09 09 66  row(dump[0])...f
2fb0: 6f 72 20 6c 69 6e 65 20 69 6e 20 64 75 6d 70 5b  or line in dump[
2fc0: 31 5d 3a 0a 09 09 09 63 73 76 5f 77 72 69 74 65  1]:....csv_write
2fd0: 72 2e 77 72 69 74 65 72 6f 77 28 6c 69 6e 65 29  r.writerow(line)
2fe0: 0a 0a 09 65 6c 69 66 20 63 6f 6e 66 69 67 2e 6f  ...elif config.o
2ff0: 70 74 69 6f 6e 73 2e 6c 6f 61 64 20 6f 72 20 63  ptions.load or c
3000: 6f 6e 66 69 67 2e 6f 70 74 69 6f 6e 73 2e 6c 6f  onfig.options.lo
3010: 61 64 5f 63 6f 6e 66 3a 0a 09 09 63 73 76 5f 72  ad_conf:...csv_r
3020: 65 61 64 65 72 20 3d 20 63 73 76 2e 72 65 61 64  eader = csv.read
3030: 65 72 28 73 79 73 2e 73 74 64 69 6e 29 0a 09 09  er(sys.stdin)...
3040: 66 69 72 73 74 5f 72 6f 77 20 3d 20 6e 65 78 74  first_row = next
3050: 28 63 73 76 5f 72 65 61 64 65 72 29 0a 0a 09 09  (csv_reader)....
3060: 69 66 20 63 6f 6e 66 69 67 2e 6f 70 74 69 6f 6e  if config.option
3070: 73 2e 6c 6f 61 64 3a 0a 09 09 09 66 69 65 6c 64  s.load:....field
3080: 73 20 3d 20 64 61 74 61 5f 66 69 65 6c 64 73 0a  s = data_fields.
3090: 09 09 09 6c 6f 61 64 20 3d 20 74 61 67 64 62 2e  ...load = tagdb.
30a0: 6c 6f 61 64 0a 09 09 65 6c 69 66 20 63 6f 6e 66  load...elif conf
30b0: 69 67 2e 6f 70 74 69 6f 6e 73 2e 6c 6f 61 64 5f  ig.options.load_
30c0: 63 6f 6e 66 3a 0a 09 09 09 66 69 65 6c 64 73 20  conf:....fields 
30d0: 3d 20 63 6f 6e 66 5f 66 69 65 6c 64 73 0a 09 09  = conf_fields...
30e0: 09 6c 6f 61 64 20 3d 20 74 61 67 64 62 2e 6c 6f  .load = tagdb.lo
30f0: 61 64 5f 63 6f 6e 66 0a 0a 09 09 61 73 73 65 72  ad_conf....asser
3100: 74 20 66 69 72 73 74 5f 72 6f 77 20 3d 3d 20 66  t first_row == f
3110: 69 65 6c 64 73 2c 20 27 46 69 6c 65 20 6d 75 73  ields, 'File mus
3120: 74 20 63 6f 6e 74 61 69 6e 20 63 73 76 20 64 61  t contain csv da
3130: 74 61 20 77 69 74 68 20 74 68 65 65 73 65 20 63  ta with theese c
3140: 6f 6c 75 6d 6e 73 3a 20 27 20 2b 20 72 65 70 72  olumns: ' + repr
3150: 28 66 69 65 6c 64 73 29 0a 09 09 6c 6f 61 64 28  (fields)...load(
3160: 63 73 76 5f 72 65 61 64 65 72 29 0a 0a 65 6c 73  csv_reader)..els
3170: 65 3a 0a 09 23 20 6d 61 69 6e 20 6c 6f 6f 70 0a  e:..# main loop.
3180: 09 43 68 65 63 6b 65 72 28 73 74 64 69 6e 2c 20  .Checker(stdin, 
3190: 6c 6f 67 67 65 72 29 2e 6c 6f 6f 70 28 29 0a     logger).loop().