Squid url redirector

Hex Artifact Content
anonymous

Hex Artifact Content

Artifact e86ad177a58339fc9ea7c9835d96f9af68628690c9a12c850f5f50b72f3cd709:


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 72 65 63 6f 72 64 2e 6d 73 67 2e 65  g = record.msg.e
0f40: 6e 63 6f 64 65 28 27 75 74 66 2d 38 27 29 0a 09  ncode('utf-8')..
0f50: 09 74 72 79 3a 0a 09 09 09 73 65 6c 66 2e 5f 74  .try:....self._t
0f60: 61 69 6c 2e 70 75 74 28 72 65 63 6f 72 64 29 0a  ail.put(record).
0f70: 09 09 65 78 63 65 70 74 20 28 4b 65 79 62 6f 61  ..except (Keyboa
0f80: 72 64 49 6e 74 65 72 72 75 70 74 2c 20 53 79 73  rdInterrupt, Sys
0f90: 74 65 6d 45 78 69 74 29 3a 0a 09 09 09 72 61 69  temExit):....rai
0fa0: 73 65 0a 09 09 65 78 63 65 70 74 3a 0a 09 09 09  se...except:....
0fb0: 73 65 6c 66 2e 68 61 6e 64 6c 65 45 72 72 6f 72  self.handleError
0fc0: 28 72 65 63 6f 72 64 29 0a 09 09 69 66 20 73 65  (record)...if se
0fd0: 6c 66 2e 5f 77 6f 72 6b 65 72 20 3d 3d 20 4e 6f  lf._worker == No
0fe0: 6e 65 3a 0a 09 09 09 23 20 69 6e 20 63 61 73 65  ne:....# in case
0ff0: 20 71 75 65 75 65 20 69 73 20 65 6d 70 74 79 20   queue is empty 
1000: 77 65 20 77 69 6c 6c 20 73 70 61 77 6e 20 6e 65  we will spawn ne
1010: 77 20 77 6f 72 6b 65 72 0a 09 09 09 23 20 61 6c  w worker....# al
1020: 6c 20 77 6f 72 6b 65 72 73 20 61 72 65 20 6c 6f  l workers are lo
1030: 67 67 65 64 20 73 6f 20 77 65 20 63 61 6e 20 6b  gged so we can k
1040: 69 6c 6c 20 74 68 65 6d 20 6f 6e 20 63 6c 6f 73  ill them on clos
1050: 65 28 29 0a 09 09 09 73 65 6c 66 2e 5f 77 6f 72  e()....self._wor
1060: 6b 65 72 20 3d 20 67 65 76 65 6e 74 2e 73 70 61  ker = gevent.spa
1070: 77 6e 28 73 65 6c 66 2e 5f 77 72 69 74 65 72 29  wn(self._writer)
1080: 0a 0a 09 64 65 66 20 5f 77 72 69 74 65 72 28 73  ...def _writer(s
1090: 65 6c 66 29 3a 0a 09 09 23 20 68 65 72 65 20 77  elf):...# here w
10a0: 65 20 61 72 65 20 6c 6f 63 6b 69 6e 67 20 74 68  e are locking th
10b0: 65 20 71 75 65 75 65 20 73 6f 20 77 65 20 63 61  e queue so we ca
10c0: 6e 20 62 65 20 73 75 72 65 20 77 65 20 61 72 65  n be sure we are
10d0: 20 74 68 65 20 6f 6e 6c 79 20 6f 6e 65 0a 09 09   the only one...
10e0: 77 68 69 6c 65 20 6e 6f 74 20 73 65 6c 66 2e 5f  while not self._
10f0: 74 61 69 6c 2e 65 6d 70 74 79 28 29 3a 0a 09 09  tail.empty():...
1100: 09 6c 6f 67 67 69 6e 67 2e 68 61 6e 64 6c 65 72  .logging.handler
1110: 73 2e 53 79 73 4c 6f 67 48 61 6e 64 6c 65 72 2e  s.SysLogHandler.
1120: 65 6d 69 74 28 73 65 6c 66 2c 20 73 65 6c 66 2e  emit(self, self.
1130: 5f 74 61 69 6c 2e 67 65 74 28 29 29 0a 09 09 73  _tail.get())...s
1140: 65 6c 66 2e 5f 77 6f 72 6b 65 72 20 3d 20 4e 6f  elf._worker = No
1150: 6e 65 0a 0a 09 64 65 66 20 63 6c 6f 73 65 28 73  ne...def close(s
1160: 65 6c 66 29 3a 0a 09 09 69 66 20 73 65 6c 66 2e  elf):...if self.
1170: 5f 77 6f 72 6b 65 72 20 21 3d 20 4e 6f 6e 65 3a  _worker != None:
1180: 0a 09 09 09 67 65 76 65 6e 74 2e 6b 69 6c 6c 28  ....gevent.kill(
1190: 73 65 6c 66 2e 5f 77 6f 72 6b 65 72 29 0a 09 09  self._worker)...
11a0: 6c 6f 67 67 69 6e 67 2e 68 61 6e 64 6c 65 72 73  logging.handlers
11b0: 2e 53 79 73 4c 6f 67 48 61 6e 64 6c 65 72 2e 63  .SysLogHandler.c
11c0: 6c 6f 73 65 28 73 65 6c 66 29 0a 0a 6c 6f 67 67  lose(self)..logg
11d0: 65 72 20 3d 20 6c 6f 67 67 69 6e 67 2e 67 65 74  er = logging.get
11e0: 4c 6f 67 67 65 72 28 27 73 71 75 69 64 54 61 67  Logger('squidTag
11f0: 27 29 0a 6c 6f 67 67 65 72 2e 73 65 74 4c 65 76  ').logger.setLev
1200: 65 6c 28 6c 6f 67 67 69 6e 67 2e 49 4e 46 4f 29  el(logging.INFO)
1210: 0a 68 61 6e 64 6c 65 72 20 3d 20 53 79 73 4c 6f  .handler = SysLo
1220: 67 48 61 6e 64 6c 65 72 51 75 65 75 65 28 29 0a  gHandlerQueue().
1230: 68 61 6e 64 6c 65 72 2e 73 65 74 46 6f 72 6d 61  handler.setForma
1240: 74 74 65 72 28 6c 6f 67 67 69 6e 67 2e 46 6f 72  tter(logging.For
1250: 6d 61 74 74 65 72 28 73 74 72 28 27 73 71 75 69  matter(str('squi
1260: 64 54 61 67 5b 25 28 70 72 6f 63 65 73 73 29 73  dTag[%(process)s
1270: 5d 3a 20 25 28 6d 65 73 73 61 67 65 29 73 27 29  ]: %(message)s')
1280: 29 29 0a 6c 6f 67 67 65 72 2e 61 64 64 48 61 6e  )).logger.addHan
1290: 64 6c 65 72 28 68 61 6e 64 6c 65 72 29 0a 0a 23  dler(handler)..#
12a0: 20 74 69 6e 79 20 77 72 61 70 70 65 72 20 61 72   tiny wrapper ar
12b0: 6f 75 6e 64 20 61 20 66 69 6c 65 20 74 6f 20 6d  ound a file to m
12c0: 61 6b 65 20 72 65 61 64 73 20 66 72 6f 6d 20 69  ake reads from i
12d0: 74 20 67 65 76 65 6e 74 61 62 6c 65 0a 23 20 6f  t geventable.# o
12e0: 72 20 73 68 6f 75 6c 64 20 69 20 6d 6f 76 65 20  r should i move 
12f0: 74 68 69 73 20 73 6f 6d 65 77 68 65 72 65 3f 0a  this somewhere?.
1300: 0a 63 6c 61 73 73 20 46 52 65 61 64 6c 69 6e 65  .class FReadline
1310: 51 75 65 75 65 28 67 65 76 65 6e 74 2e 71 75 65  Queue(gevent.que
1320: 75 65 2e 51 75 65 75 65 29 3a 0a 09 23 20 73 74  ue.Queue):..# st
1330: 6f 72 69 6e 67 20 66 69 6c 65 20 64 65 73 63 72  oring file descr
1340: 69 70 74 6f 72 2c 20 6c 65 66 74 6f 76 65 72 0a  iptor, leftover.
1350: 09 5f 5f 73 6c 6f 74 73 5f 5f 20 3d 20 66 72 6f  .__slots__ = fro
1360: 7a 65 6e 73 65 74 28 5b 27 5f 69 6f 27 2c 20 27  zenset(['_io', '
1370: 5f 66 69 6c 65 6e 6f 27 2c 20 27 5f 74 61 69 6c  _fileno', '_tail
1380: 27 5d 29 0a 0a 09 64 65 66 20 5f 5f 69 6e 69 74  '])...def __init
1390: 5f 5f 28 73 65 6c 66 2c 20 66 64 2c 20 63 6c 6f  __(self, fd, clo
13a0: 73 65 66 64 20 3d 20 54 72 75 65 29 3a 0a 09 09  sefd = True):...
13b0: 69 6d 70 6f 72 74 20 69 6f 0a 09 09 23 20 69 6e  import io...# in
13c0: 69 74 69 61 6c 69 73 69 6e 67 20 63 6c 61 73 73  itialising class
13d0: 0a 09 09 67 65 76 65 6e 74 2e 71 75 65 75 65 2e  ...gevent.queue.
13e0: 51 75 65 75 65 2e 5f 5f 69 6e 69 74 5f 5f 28 73  Queue.__init__(s
13f0: 65 6c 66 29 0a 09 09 23 20 73 74 6f 72 69 6e 67  elf)...# storing
1400: 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
1410: 0a 09 09 73 65 6c 66 2e 5f 66 69 6c 65 6e 6f 20  ...self._fileno 
1420: 3d 20 66 64 2e 66 69 6c 65 6e 6f 28 29 0a 09 09  = fd.fileno()...
1430: 73 65 6c 66 2e 5f 69 6f 20 3d 20 69 6f 2e 46 69  self._io = io.Fi
1440: 6c 65 49 4f 28 73 65 6c 66 2e 5f 66 69 6c 65 6e  leIO(self._filen
1450: 6f 2c 20 27 72 27 2c 20 63 6c 6f 73 65 66 64 29  o, 'r', closefd)
1460: 0a 09 09 23 20 75 73 69 6e 67 20 65 6d 70 74 79  ...# using empty
1470: 20 74 61 69 6c 0a 09 09 73 65 6c 66 2e 5f 74 61   tail...self._ta
1480: 69 6c 20 3d 20 27 27 0a 09 09 23 20 73 65 74 74  il = ''...# sett
1490: 69 6e 67 20 75 70 20 65 76 65 6e 74 0a 09 09 73  ing up event...s
14a0: 65 6c 66 2e 5f 69 6e 73 74 61 6c 6c 5f 77 61 69  elf._install_wai
14b0: 74 28 29 0a 0a 09 64 65 66 20 5f 69 6e 73 74 61  t()...def _insta
14c0: 6c 6c 5f 77 61 69 74 28 73 65 6c 66 29 3a 0a 09  ll_wait(self):..
14d0: 09 23 20 70 75 74 74 69 6e 67 20 66 69 6c 65 20  .# putting file 
14e0: 74 6f 20 6e 6f 6e 62 6c 6f 63 6b 69 6e 67 20 6d  to nonblocking m
14f0: 6f 64 65 0a 09 09 66 63 6e 74 6c 2e 66 63 6e 74  ode...fcntl.fcnt
1500: 6c 28 73 65 6c 66 2e 5f 66 69 6c 65 6e 6f 2c 20  l(self._fileno, 
1510: 66 63 6e 74 6c 2e 46 5f 53 45 54 46 4c 2c 20 66  fcntl.F_SETFL, f
1520: 63 6e 74 6c 2e 66 63 6e 74 6c 28 73 65 6c 66 2e  cntl.fcntl(self.
1530: 5f 66 69 6c 65 6e 6f 2c 20 66 63 6e 74 6c 2e 46  _fileno, fcntl.F
1540: 5f 47 45 54 46 4c 29 20 20 7c 20 6f 73 2e 4f 5f  _GETFL)  | os.O_
1550: 4e 4f 4e 42 4c 4f 43 4b 29 0a 09 09 23 20 69 6e  NONBLOCK)...# in
1560: 73 74 61 6c 6c 69 6e 67 20 65 76 65 6e 74 20 68  stalling event h
1570: 61 6e 64 6c 65 72 0a 09 09 67 65 76 65 6e 74 2e  andler...gevent.
1580: 63 6f 72 65 2e 72 65 61 64 5f 65 76 65 6e 74 28  core.read_event(
1590: 73 65 6c 66 2e 5f 66 69 6c 65 6e 6f 2c 20 73 65  self._fileno, se
15a0: 6c 66 2e 5f 77 61 69 74 5f 68 65 6c 70 65 72 29  lf._wait_helper)
15b0: 0a 0a 09 64 65 66 20 5f 77 61 69 74 5f 68 65 6c  ...def _wait_hel
15c0: 70 65 72 28 73 65 6c 66 2c 20 65 76 2c 20 65 76  per(self, ev, ev
15d0: 74 79 70 65 29 3a 0a 09 09 23 20 72 65 61 64 69  type):...# readi
15e0: 6e 67 20 6f 6e 65 20 62 75 66 66 65 72 20 66 72  ng one buffer fr
15f0: 6f 6d 20 73 74 72 65 61 6d 0a 09 09 62 75 66 20  om stream...buf 
1600: 3d 20 73 65 6c 66 2e 5f 69 6f 2e 72 65 61 64 28  = self._io.read(
1610: 34 30 39 36 29 0a 09 09 23 20 73 70 6c 69 74 74  4096)...# splitt
1620: 69 6e 67 20 73 74 72 65 61 6d 20 62 79 20 6c 69  ing stream by li
1630: 6e 65 20 65 6e 64 73 0a 09 09 72 6f 77 73 20 3d  ne ends...rows =
1640: 20 62 75 66 2e 64 65 63 6f 64 65 28 27 6c 31 27   buf.decode('l1'
1650: 29 2e 73 70 6c 69 74 28 27 5c 6e 27 29 0a 09 09  ).split('\n')...
1660: 23 20 61 64 64 69 6e 67 20 74 61 69 6c 20 74 6f  # adding tail to
1670: 20 74 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65   the first eleme
1680: 6e 74 20 69 66 20 74 68 65 72 65 20 69 73 20 73  nt if there is s
1690: 6f 6d 65 20 74 61 69 6c 0a 09 09 69 66 20 6c 65  ome tail...if le
16a0: 6e 28 73 65 6c 66 2e 5f 74 61 69 6c 29 20 3e 20  n(self._tail) > 
16b0: 30 3a 0a 09 09 09 72 6f 77 73 5b 30 5d 20 3d 20  0:....rows[0] = 
16c0: 73 65 6c 66 2e 5f 74 61 69 6c 20 2b 20 72 6f 77  self._tail + row
16d0: 73 5b 30 5d 0a 09 09 23 20 70 6f 70 70 69 6e 67  s[0]...# popping
16e0: 20 6f 75 74 20 6c 61 73 74 20 28 69 6e 63 6f 6d   out last (incom
16f0: 70 6c 65 74 65 29 20 65 6c 65 6d 65 6e 74 0a 09  plete) element..
1700: 09 73 65 6c 66 2e 5f 74 61 69 6c 20 3d 20 72 6f  .self._tail = ro
1710: 77 73 2e 70 6f 70 28 2d 31 29 0a 09 09 23 20 64  ws.pop(-1)...# d
1720: 72 6f 70 70 69 6e 67 20 61 6c 6c 20 63 6f 6d 70  ropping all comp
1730: 6c 65 74 65 20 65 6c 65 6d 65 6e 74 73 20 74 6f  lete elements to
1740: 20 74 68 65 20 71 75 65 75 65 0a 09 09 66 6f 72   the queue...for
1750: 20 72 6f 77 20 69 6e 20 72 6f 77 73 3a 0a 09 09   row in rows:...
1760: 09 73 65 6c 66 2e 70 75 74 5f 6e 6f 77 61 69 74  .self.put_nowait
1770: 28 72 6f 77 29 0a 09 09 09 6c 6f 67 67 65 72 2e  (row)....logger.
1780: 69 6e 66 6f 28 27 3c 20 27 20 2b 20 72 6f 77 29  info('< ' + row)
1790: 0a 09 09 69 66 20 6c 65 6e 28 62 75 66 29 20 3e  ...if len(buf) >
17a0: 20 30 3a 0a 09 09 09 23 20 6e 6f 20 45 4f 46 2c   0:....# no EOF,
17b0: 20 72 65 69 6e 73 74 61 6c 6c 69 6e 67 20 65 76   reinstalling ev
17c0: 65 6e 74 20 68 61 6e 64 6c 65 72 0a 09 09 09 67  ent handler....g
17d0: 65 76 65 6e 74 2e 63 6f 72 65 2e 72 65 61 64 5f  event.core.read_
17e0: 65 76 65 6e 74 28 73 65 6c 66 2e 5f 66 69 6c 65  event(self._file
17f0: 6e 6f 2c 20 73 65 6c 66 2e 5f 77 61 69 74 5f 68  no, self._wait_h
1800: 65 6c 70 65 72 29 0a 09 09 65 6c 73 65 3a 0a 09  elper)...else:..
1810: 09 09 23 20 45 4f 46 20 66 6f 75 6e 64 2c 20 73  ..# EOF found, s
1820: 65 6e 64 69 6e 67 20 45 4f 46 20 74 6f 20 71 75  ending EOF to qu
1830: 65 75 65 0a 09 09 09 73 65 6c 66 2e 70 75 74 5f  eue....self.put_
1840: 6e 6f 77 61 69 74 28 4e 6f 6e 65 29 0a 0a 73 74  nowait(None)..st
1850: 64 69 6e 20 3d 20 46 52 65 61 64 6c 69 6e 65 51  din = FReadlineQ
1860: 75 65 75 65 28 73 79 73 2e 73 74 64 69 6e 2c 20  ueue(sys.stdin, 
1870: 46 61 6c 73 65 29 0a 0a 23 20 77 72 61 70 70 65  False)..# wrappe
1880: 72 20 61 67 61 69 6e 73 74 20 66 69 6c 65 20 68  r against file h
1890: 61 6e 64 6c 65 72 20 74 68 61 74 20 6d 61 6b 65  andler that make
18a0: 73 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 71 75  s possible to qu
18b0: 65 75 65 20 73 6f 6d 65 20 77 72 69 74 65 73 20  eue some writes 
18c0: 77 69 74 68 6f 75 74 20 73 74 61 6c 6c 69 6e 67  without stalling
18d0: 0a 0a 63 6c 61 73 73 20 46 57 72 69 74 65 6c 69  ..class FWriteli
18e0: 6e 65 51 75 65 75 65 28 67 65 76 65 6e 74 2e 71  neQueue(gevent.q
18f0: 75 65 75 65 2e 4a 6f 69 6e 61 62 6c 65 51 75 65  ueue.JoinableQue
1900: 75 65 29 3a 0a 09 23 20 73 74 6f 72 69 6e 67 20  ue):..# storing 
1910: 66 69 6c 65 6e 6f 2c 20 69 6f 20 69 6e 74 65 72  fileno, io inter
1920: 66 61 63 65 2c 20 6c 65 66 74 6f 76 65 72 0a 09  face, leftover..
1930: 5f 5f 73 6c 6f 74 73 5f 5f 20 3d 20 66 72 6f 7a  __slots__ = froz
1940: 65 6e 73 65 74 28 5b 27 5f 66 69 6c 65 6e 6f 27  enset(['_fileno'
1950: 2c 20 27 5f 69 6f 27 2c 20 27 5f 74 61 69 6c 27  , '_io', '_tail'
1960: 5d 29 0a 0a 09 64 65 66 20 5f 5f 69 6e 69 74 5f  ])...def __init_
1970: 5f 28 73 65 6c 66 2c 20 66 64 2c 20 63 6c 6f 73  _(self, fd, clos
1980: 65 66 64 20 3d 20 54 72 75 65 29 3a 0a 09 09 69  efd = True):...i
1990: 6d 70 6f 72 74 20 69 6f 0a 09 09 23 20 69 6e 69  mport io...# ini
19a0: 74 69 61 6c 69 73 69 6e 67 20 63 6c 61 73 73 0a  tialising class.
19b0: 09 09 67 65 76 65 6e 74 2e 71 75 65 75 65 2e 4a  ..gevent.queue.J
19c0: 6f 69 6e 61 62 6c 65 51 75 65 75 65 2e 5f 5f 69  oinableQueue.__i
19d0: 6e 69 74 5f 5f 28 73 65 6c 66 29 0a 09 09 23 20  nit__(self)...# 
19e0: 73 74 6f 72 69 6e 67 20 66 69 6c 65 6e 6f 0a 09  storing fileno..
19f0: 09 73 65 6c 66 2e 5f 66 69 6c 65 6e 6f 20 3d 20  .self._fileno = 
1a00: 66 64 2e 66 69 6c 65 6e 6f 28 29 0a 09 09 23 20  fd.fileno()...# 
1a10: 63 72 65 61 74 69 6e 67 20 69 6e 74 65 72 66 61  creating interfa
1a20: 63 65 0a 09 09 73 65 6c 66 2e 5f 69 6f 20 3d 20  ce...self._io = 
1a30: 69 6f 2e 46 69 6c 65 49 4f 28 73 65 6c 66 2e 5f  io.FileIO(self._
1a40: 66 69 6c 65 6e 6f 2c 20 27 77 27 2c 20 63 6c 6f  fileno, 'w', clo
1a50: 73 65 66 64 29 0a 09 09 23 20 75 73 69 6e 67 20  sefd)...# using 
1a60: 65 6d 70 74 79 20 74 61 69 6c 0a 09 09 73 65 6c  empty tail...sel
1a70: 66 2e 5f 74 61 69 6c 20 3d 20 4e 6f 6e 65 0a 09  f._tail = None..
1a80: 09 23 20 70 75 74 74 69 6e 67 20 66 69 6c 65 20  .# putting file 
1a90: 74 6f 20 6e 6f 6e 62 6c 6f 63 6b 69 6e 67 20 6d  to nonblocking m
1aa0: 6f 64 65 0a 09 09 66 63 6e 74 6c 2e 66 63 6e 74  ode...fcntl.fcnt
1ab0: 6c 28 73 65 6c 66 2e 5f 66 69 6c 65 6e 6f 2c 20  l(self._fileno, 
1ac0: 66 63 6e 74 6c 2e 46 5f 53 45 54 46 4c 2c 20 66  fcntl.F_SETFL, f
1ad0: 63 6e 74 6c 2e 66 63 6e 74 6c 28 73 65 6c 66 2e  cntl.fcntl(self.
1ae0: 5f 66 69 6c 65 6e 6f 2c 20 66 63 6e 74 6c 2e 46  _fileno, fcntl.F
1af0: 5f 47 45 54 46 4c 29 20 7c 20 6f 73 2e 4f 5f 4e  _GETFL) | os.O_N
1b00: 4f 4e 42 4c 4f 43 4b 29 0a 0a 09 64 65 66 20 5f  ONBLOCK)...def _
1b10: 5f 64 65 6c 5f 5f 28 73 65 6c 66 29 3a 0a 09 09  _del__(self):...
1b20: 23 20 70 75 72 67 65 20 71 75 65 75 65 20 62 65  # purge queue be
1b30: 66 6f 72 65 20 64 65 6c 65 74 69 6e 67 0a 09 09  fore deleting...
1b40: 69 66 20 6e 6f 74 20 73 65 6c 66 2e 65 6d 70 74  if not self.empt
1b50: 79 28 29 3a 0a 09 09 09 73 65 6c 66 2e 6a 6f 69  y():....self.joi
1b60: 6e 28 29 0a 0a 09 64 65 66 20 70 75 74 28 73 65  n()...def put(se
1b70: 6c 66 2c 20 69 74 65 6d 2c 20 62 6c 6f 63 6b 3d  lf, item, block=
1b80: 54 72 75 65 2c 20 74 69 6d 65 6f 75 74 3d 4e 6f  True, timeout=No
1b90: 6e 65 29 3a 0a 09 09 23 20 63 61 6c 6c 69 6e 67  ne):...# calling
1ba0: 20 72 65 61 6c 20 70 75 74 0a 09 09 67 65 76 65   real put...geve
1bb0: 6e 74 2e 71 75 65 75 65 2e 4a 6f 69 6e 61 62 6c  nt.queue.Joinabl
1bc0: 65 51 75 65 75 65 2e 70 75 74 28 73 65 6c 66 2c  eQueue.put(self,
1bd0: 20 69 74 65 6d 2c 20 62 6c 6f 63 6b 2c 20 74 69   item, block, ti
1be0: 6d 65 6f 75 74 29 0a 09 09 23 20 69 6e 73 74 61  meout)...# insta
1bf0: 6c 6c 69 6e 67 20 65 76 65 6e 74 20 68 61 6e 64  lling event hand
1c00: 6c 65 72 0a 09 09 67 65 76 65 6e 74 2e 63 6f 72  ler...gevent.cor
1c10: 65 2e 77 72 69 74 65 5f 65 76 65 6e 74 28 73 65  e.write_event(se
1c20: 6c 66 2e 5f 66 69 6c 65 6e 6f 2c 20 73 65 6c 66  lf._fileno, self
1c30: 2e 5f 77 61 69 74 5f 68 65 6c 70 65 72 29 0a 0a  ._wait_helper)..
1c40: 09 64 65 66 20 5f 77 61 69 74 5f 68 65 6c 70 65  .def _wait_helpe
1c50: 72 28 73 65 6c 66 2c 20 65 76 2c 20 65 76 74 79  r(self, ev, evty
1c60: 70 65 29 3a 0a 09 09 23 20 58 58 58 20 65 76 2c  pe):...# XXX ev,
1c70: 20 65 76 74 79 70 65 20 63 68 65 63 6b 69 6e 67   evtype checking
1c80: 3f 0a 09 09 23 20 63 68 65 63 6b 69 6e 67 20 6c  ?...# checking l
1c90: 65 66 74 6f 76 65 72 0a 09 09 77 68 69 6c 65 20  eftover...while 
1ca0: 54 72 75 65 3a 0a 09 09 09 69 66 20 73 65 6c 66  True:....if self
1cb0: 2e 5f 74 61 69 6c 20 3d 3d 20 4e 6f 6e 65 3a 0a  ._tail == None:.
1cc0: 09 09 09 09 74 72 79 3a 0a 09 09 09 09 09 73 65  ....try:......se
1cd0: 6c 66 2e 5f 74 61 69 6c 20 3d 20 73 74 72 28 73  lf._tail = str(s
1ce0: 65 6c 66 2e 67 65 74 5f 6e 6f 77 61 69 74 28 29  elf.get_nowait()
1cf0: 29 2e 65 6e 63 6f 64 65 28 27 75 74 66 2d 38 27  ).encode('utf-8'
1d00: 29 20 2b 20 27 5c 6e 27 0a 09 09 09 09 65 78 63  ) + '\n'.....exc
1d10: 65 70 74 20 67 65 76 65 6e 74 2e 71 75 65 75 65  ept gevent.queue
1d20: 2e 45 6d 70 74 79 3a 0a 09 09 09 09 09 73 65 6c  .Empty:......sel
1d30: 66 2e 5f 74 61 69 6c 20 3d 20 4e 6f 6e 65 0a 09  f._tail = None..
1d40: 09 09 09 09 72 65 74 75 72 6e 0a 09 09 09 23 20  ....return....# 
1d50: 77 72 69 74 69 6e 67 20 74 61 69 6c 0a 09 09 09  writing tail....
1d60: 77 72 69 74 74 65 6e 20 3d 20 73 65 6c 66 2e 5f  written = self._
1d70: 69 6f 2e 77 72 69 74 65 28 73 65 6c 66 2e 5f 74  io.write(self._t
1d80: 61 69 6c 29 0a 09 09 09 6c 65 6e 67 74 68 20 3d  ail)....length =
1d90: 20 6c 65 6e 28 73 65 6c 66 2e 5f 74 61 69 6c 29   len(self._tail)
1da0: 0a 09 09 09 69 66 20 77 72 69 74 74 65 6e 20 3d  ....if written =
1db0: 3d 20 6c 65 6e 67 74 68 3a 0a 09 09 09 09 73 65  = length:.....se
1dc0: 6c 66 2e 5f 74 61 69 6c 20 3d 20 4e 6f 6e 65 0a  lf._tail = None.
1dd0: 09 09 09 65 6c 69 66 20 77 72 69 74 74 65 6e 20  ...elif written 
1de0: 3c 20 6c 65 6e 67 74 68 3a 0a 09 09 09 09 73 65  < length:.....se
1df0: 6c 66 2e 5f 74 61 69 6c 20 3d 20 73 65 6c 66 2e  lf._tail = self.
1e00: 5f 74 61 69 6c 5b 77 72 69 74 74 65 6e 3a 5d 0a  _tail[written:].
1e10: 09 09 09 09 62 72 65 61 6b 0a 09 09 09 65 6c 73  ....break....els
1e20: 65 3a 0a 09 09 09 09 62 72 65 61 6b 0a 09 09 23  e:.....break...#
1e30: 20 72 65 69 6e 73 74 61 6c 6c 69 6e 67 20 65 76   reinstalling ev
1e40: 65 6e 74 20 68 61 6e 64 6c 65 72 0a 09 09 67 65  ent handler...ge
1e50: 76 65 6e 74 2e 63 6f 72 65 2e 77 72 69 74 65 5f  vent.core.write_
1e60: 65 76 65 6e 74 28 73 65 6c 66 2e 5f 66 69 6c 65  event(self._file
1e70: 6e 6f 2c 20 73 65 6c 66 2e 5f 77 61 69 74 5f 68  no, self._wait_h
1e80: 65 6c 70 65 72 29 0a 0a 23 20 77 72 61 70 70 65  elper)..# wrappe
1e90: 72 20 61 72 6f 75 6e 64 20 64 61 74 61 62 61 73  r around databas
1ea0: 65 0a 63 6c 61 73 73 20 74 61 67 44 42 28 6f 62  e.class tagDB(ob
1eb0: 6a 65 63 74 29 3a 0a 09 5f 5f 73 6c 6f 74 73 5f  ject):..__slots_
1ec0: 5f 20 3d 20 66 72 6f 7a 65 6e 73 65 74 28 5b 27  _ = frozenset(['
1ed0: 5f 63 75 72 73 6f 72 27 2c 20 27 5f 64 62 27 5d  _cursor', '_db']
1ee0: 29 0a 0a 09 64 65 66 20 5f 5f 69 6e 69 74 5f 5f  )...def __init__
1ef0: 28 73 65 6c 66 29 3a 0a 09 09 63 6f 6e 66 69 67  (self):...config
1f00: 2e 73 65 63 74 69 6f 6e 28 27 64 61 74 61 62 61  .section('databa
1f10: 73 65 27 29 0a 09 09 69 66 20 63 6f 6e 66 69 67  se')...if config
1f20: 5b 27 68 6f 73 74 27 5d 20 3d 3d 20 4e 6f 6e 65  ['host'] == None
1f30: 3a 0a 09 09 09 73 65 6c 66 2e 5f 64 62 20 3d 20  :....self._db = 
1f40: 70 73 79 63 6f 70 67 32 2e 63 6f 6e 6e 65 63 74  psycopg2.connect
1f50: 28 0a 09 09 09 09 64 61 74 61 62 61 73 65 20 3d  (.....database =
1f60: 20 63 6f 6e 66 69 67 5b 27 64 61 74 61 62 61 73   config['databas
1f70: 65 27 5d 2c 0a 09 09 09 09 75 73 65 72 20 3d 20  e'],.....user = 
1f80: 63 6f 6e 66 69 67 5b 27 75 73 65 72 27 5d 2c 0a  config['user'],.
1f90: 09 09 09 09 70 61 73 73 77 6f 72 64 20 3d 20 63  ....password = c
1fa0: 6f 6e 66 69 67 5b 27 70 61 73 73 77 6f 72 64 27  onfig['password'
1fb0: 5d 0a 09 09 09 29 0a 09 09 65 6c 73 65 3a 0a 09  ]....)...else:..
1fc0: 09 09 73 65 6c 66 2e 5f 64 62 20 3d 20 70 73 79  ..self._db = psy
1fd0: 63 6f 70 67 32 2e 63 6f 6e 6e 65 63 74 28 0a 09  copg2.connect(..
1fe0: 09 09 09 64 61 74 61 62 61 73 65 20 3d 20 63 6f  ...database = co
1ff0: 6e 66 69 67 5b 27 64 61 74 61 62 61 73 65 27 5d  nfig['database']
2000: 2c 0a 09 09 09 09 68 6f 73 74 20 3d 20 63 6f 6e  ,.....host = con
2010: 66 69 67 5b 27 68 6f 73 74 27 5d 2c 0a 09 09 09  fig['host'],....
2020: 09 75 73 65 72 20 3d 20 63 6f 6e 66 69 67 5b 27  .user = config['
2030: 75 73 65 72 27 5d 2c 0a 09 09 09 09 70 61 73 73  user'],.....pass
2040: 77 6f 72 64 20 3d 20 63 6f 6e 66 69 67 5b 27 70  word = config['p
2050: 61 73 73 77 6f 72 64 27 5d 0a 09 09 09 29 0a 09  assword']....)..
2060: 09 73 65 6c 66 2e 5f 63 75 72 73 6f 72 20 3d 20  .self._cursor = 
2070: 73 65 6c 66 2e 5f 64 62 2e 63 75 72 73 6f 72 28  self._db.cursor(
2080: 29 0a 0a 09 64 65 66 20 5f 66 69 65 6c 64 5f 6e  )...def _field_n
2090: 61 6d 65 73 28 73 65 6c 66 29 3a 0a 09 09 6e 61  ames(self):...na
20a0: 6d 65 73 20 3d 20 5b 5d 0a 09 09 66 6f 72 20 72  mes = []...for r
20b0: 65 63 6f 72 64 20 69 6e 20 73 65 6c 66 2e 5f 63  ecord in self._c
20c0: 75 72 73 6f 72 2e 64 65 73 63 72 69 70 74 69 6f  ursor.descriptio
20d0: 6e 3a 0a 09 09 09 6e 61 6d 65 73 2e 61 70 70 65  n:....names.appe
20e0: 6e 64 28 72 65 63 6f 72 64 2e 6e 61 6d 65 29 0a  nd(record.name).
20f0: 09 09 72 65 74 75 72 6e 28 6e 61 6d 65 73 29 0a  ..return(names).
2100: 0a 09 64 65 66 20 63 68 65 63 6b 28 73 65 6c 66  ..def check(self
2110: 2c 20 73 69 74 65 2c 20 69 70 5f 61 64 64 72 65  , site, ip_addre
2120: 73 73 29 3a 0a 09 09 73 65 6c 66 2e 5f 63 75 72  ss):...self._cur
2130: 73 6f 72 2e 65 78 65 63 75 74 65 28 22 73 65 6c  sor.execute("sel
2140: 65 63 74 20 2a 20 66 72 6f 6d 20 28 73 65 6c 65  ect * from (sele
2150: 63 74 20 72 65 64 69 72 65 63 74 5f 75 72 6c 2c  ct redirect_url,
2160: 20 72 65 67 65 78 70 20 66 72 6f 6d 20 73 69 74   regexp from sit
2170: 65 5f 72 75 6c 65 20 77 68 65 72 65 20 73 69 74  e_rule where sit
2180: 65 20 3c 40 20 74 72 69 70 64 6f 6d 61 69 6e 28  e <@ tripdomain(
2190: 25 73 29 20 61 6e 64 20 6e 65 74 6d 61 73 6b 20  %s) and netmask 
21a0: 3e 3e 3d 20 25 73 20 6f 72 64 65 72 20 62 79 20  >>= %s order by 
21b0: 61 72 72 61 79 5f 6c 65 6e 67 74 68 28 73 69 74  array_length(sit
21c0: 65 2c 20 31 29 20 64 65 73 63 29 20 61 20 67 72  e, 1) desc) a gr
21d0: 6f 75 70 20 62 79 20 72 65 64 69 72 65 63 74 5f  oup by redirect_
21e0: 75 72 6c 2c 20 72 65 67 65 78 70 22 2c 20 5b 73  url, regexp", [s
21f0: 69 74 65 2c 20 69 70 5f 61 64 64 72 65 73 73 5d  ite, ip_address]
2200: 29 0a 09 09 72 65 74 75 72 6e 28 73 65 6c 66 2e  )...return(self.
2210: 5f 63 75 72 73 6f 72 2e 66 65 74 63 68 61 6c 6c  _cursor.fetchall
2220: 28 29 29 0a 0a 09 64 65 66 20 64 75 6d 70 28 73  ())...def dump(s
2230: 65 6c 66 29 3a 0a 09 09 73 65 6c 66 2e 5f 63 75  elf):...self._cu
2240: 72 73 6f 72 2e 65 78 65 63 75 74 65 28 22 73 65  rsor.execute("se
2250: 6c 65 63 74 20 75 6e 74 72 69 70 28 73 69 74 65  lect untrip(site
2260: 29 20 61 73 20 73 69 74 65 2c 20 74 61 67 3a 3a  ) as site, tag::
2270: 74 65 78 74 2c 20 72 65 67 65 78 70 20 66 72 6f  text, regexp fro
2280: 6d 20 75 72 6c 73 20 6f 72 64 65 72 20 62 79 20  m urls order by 
2290: 73 69 74 65 2c 20 74 61 67 22 29 0a 09 09 72 65  site, tag")...re
22a0: 74 75 72 6e 28 73 65 6c 66 2e 5f 66 69 65 6c 64  turn(self._field
22b0: 5f 6e 61 6d 65 73 28 29 2c 20 73 65 6c 66 2e 5f  _names(), self._
22c0: 63 75 72 73 6f 72 2e 66 65 74 63 68 61 6c 6c 28  cursor.fetchall(
22d0: 29 29 0a 0a 09 64 65 66 20 6c 6f 61 64 28 73 65  ))...def load(se
22e0: 6c 66 2c 20 64 61 74 61 29 3a 0a 09 09 69 66 20  lf, data):...if 
22f0: 63 6f 6e 66 69 67 2e 6f 70 74 69 6f 6e 73 2e 66  config.options.f
2300: 6c 75 73 68 5f 64 62 3a 0a 09 09 09 73 65 6c 66  lush_db:....self
2310: 2e 5f 63 75 72 73 6f 72 2e 65 78 65 63 75 74 65  ._cursor.execute
2320: 28 27 64 65 6c 65 74 65 20 66 72 6f 6d 20 75 72  ('delete from ur
2330: 6c 73 3b 27 29 0a 09 09 62 75 6e 64 6c 65 20 3d  ls;')...bundle =
2340: 20 5b 5d 0a 09 09 66 6f 72 20 72 6f 77 20 69 6e   []...for row in
2350: 20 64 61 74 61 3a 0a 09 09 09 69 66 20 6c 65 6e   data:....if len
2360: 28 72 6f 77 29 20 3d 3d 20 32 3a 0a 09 09 09 09  (row) == 2:.....
2370: 62 75 6e 64 6c 65 2e 61 70 70 65 6e 64 28 5b 72  bundle.append([r
2380: 6f 77 5b 30 5d 2c 20 72 6f 77 5b 31 5d 2c 20 4e  ow[0], row[1], N
2390: 6f 6e 65 5d 29 0a 09 09 09 65 6c 73 65 3a 0a 09  one])....else:..
23a0: 09 09 09 62 75 6e 64 6c 65 2e 61 70 70 65 6e 64  ...bundle.append
23b0: 28 5b 72 6f 77 5b 30 5d 2c 20 72 6f 77 5b 31 5d  ([row[0], row[1]
23c0: 2c 20 72 6f 77 5b 32 5d 5d 29 0a 09 09 73 65 6c  , row[2]])...sel
23d0: 66 2e 5f 63 75 72 73 6f 72 2e 65 78 65 63 75 74  f._cursor.execut
23e0: 65 6d 61 6e 79 28 22 69 6e 73 65 72 74 20 69 6e  emany("insert in
23f0: 74 6f 20 75 72 6c 73 20 28 73 69 74 65 2c 20 74  to urls (site, t
2400: 61 67 2c 20 72 65 67 65 78 70 29 20 76 61 6c 75  ag, regexp) valu
2410: 65 73 20 28 74 72 69 70 64 6f 6d 61 69 6e 28 25  es (tripdomain(%
2420: 73 29 2c 20 25 73 2c 20 25 73 29 22 2c 20 62 75  s), %s, %s)", bu
2430: 6e 64 6c 65 29 0a 09 09 73 65 6c 66 2e 5f 63 75  ndle)...self._cu
2440: 72 73 6f 72 2e 65 78 65 63 75 74 65 28 22 75 70  rsor.execute("up
2450: 64 61 74 65 20 75 72 6c 73 20 73 65 74 20 72 65  date urls set re
2460: 67 65 78 70 20 3d 20 4e 55 4c 4c 20 77 68 65 72  gexp = NULL wher
2470: 65 20 72 65 67 65 78 70 20 3d 20 27 27 22 29 0a  e regexp = ''").
2480: 09 09 73 65 6c 66 2e 5f 64 62 2e 63 6f 6d 6d 69  ..self._db.commi
2490: 74 28 29 0a 0a 09 64 65 66 20 6c 6f 61 64 5f 63  t()...def load_c
24a0: 6f 6e 66 28 73 65 6c 66 2c 20 63 73 76 5f 64 61  onf(self, csv_da
24b0: 74 61 29 3a 0a 09 09 73 65 6c 66 2e 5f 63 75 72  ta):...self._cur
24c0: 73 6f 72 2e 65 78 65 63 75 74 65 28 27 64 65 6c  sor.execute('del
24d0: 65 74 65 20 66 72 6f 6d 20 72 75 6c 65 73 3b 27  ete from rules;'
24e0: 29 0a 09 09 62 75 6e 64 6c 65 20 3d 20 5b 5d 0a  )...bundle = [].
24f0: 09 09 66 6f 72 20 72 6f 77 20 69 6e 20 63 73 76  ..for row in csv
2500: 5f 64 61 74 61 3a 0a 09 09 09 62 75 6e 64 6c 65  _data:....bundle
2510: 2e 61 70 70 65 6e 64 28 5b 72 6f 77 5b 30 5d 2c  .append([row[0],
2520: 20 72 6f 77 5b 31 5d 2c 20 69 6e 74 28 72 6f 77   row[1], int(row
2530: 5b 32 5d 29 2c 20 69 6e 74 28 72 6f 77 5b 33 5d  [2]), int(row[3]
2540: 29 2c 20 72 6f 77 5b 34 5d 2c 20 72 6f 77 5b 35  ), row[4], row[5
2550: 5d 2c 20 72 6f 77 5b 36 5d 5d 29 0a 09 09 73 65  ], row[6]])...se
2560: 6c 66 2e 5f 63 75 72 73 6f 72 2e 65 78 65 63 75  lf._cursor.execu
2570: 74 65 6d 61 6e 79 28 22 69 6e 73 65 72 74 20 69  temany("insert i
2580: 6e 74 6f 20 72 75 6c 65 73 20 28 6e 65 74 6d 61  nto rules (netma
2590: 73 6b 2c 20 72 65 64 69 72 65 63 74 5f 75 72 6c  sk, redirect_url
25a0: 2c 20 66 72 6f 6d 5f 77 65 65 6b 64 61 79 2c 20  , from_weekday, 
25b0: 74 6f 5f 77 65 65 6b 64 61 79 2c 20 66 72 6f 6d  to_weekday, from
25c0: 5f 74 69 6d 65 2c 20 74 6f 5f 74 69 6d 65 2c 20  _time, to_time, 
25d0: 74 61 67 29 20 76 61 6c 75 65 73 20 28 25 73 3a  tag) values (%s:
25e0: 3a 74 65 78 74 3a 3a 63 69 64 72 2c 20 25 73 2c  :text::cidr, %s,
25f0: 20 25 73 2c 20 25 73 2c 20 25 73 3a 3a 74 65 78   %s, %s, %s::tex
2600: 74 3a 3a 74 69 6d 65 2c 20 25 73 3a 3a 74 65 78  t::time, %s::tex
2610: 74 3a 3a 74 69 6d 65 2c 20 25 73 3a 3a 74 65 78  t::time, %s::tex
2620: 74 3a 3a 74 65 78 74 5b 5d 29 22 2c 20 62 75 6e  t::text[])", bun
2630: 64 6c 65 29 0a 09 09 73 65 6c 66 2e 5f 64 62 2e  dle)...self._db.
2640: 63 6f 6d 6d 69 74 28 29 0a 0a 09 64 65 66 20 64  commit()...def d
2650: 75 6d 70 5f 63 6f 6e 66 28 73 65 6c 66 29 3a 0a  ump_conf(self):.
2660: 09 09 73 65 6c 66 2e 5f 63 75 72 73 6f 72 2e 65  ..self._cursor.e
2670: 78 65 63 75 74 65 28 22 73 65 6c 65 63 74 20 6e  xecute("select n
2680: 65 74 6d 61 73 6b 2c 20 72 65 64 69 72 65 63 74  etmask, redirect
2690: 5f 75 72 6c 2c 20 66 72 6f 6d 5f 77 65 65 6b 64  _url, from_weekd
26a0: 61 79 2c 20 74 6f 5f 77 65 65 6b 64 61 79 2c 20  ay, to_weekday, 
26b0: 66 72 6f 6d 5f 74 69 6d 65 2c 20 74 6f 5f 74 69  from_time, to_ti
26c0: 6d 65 2c 20 74 61 67 3a 3a 74 65 78 74 20 66 72  me, tag::text fr
26d0: 6f 6d 20 72 75 6c 65 73 22 29 0a 09 09 72 65 74  om rules")...ret
26e0: 75 72 6e 28 73 65 6c 66 2e 5f 66 69 65 6c 64 5f  urn(self._field_
26f0: 6e 61 6d 65 73 28 29 2c 20 73 65 6c 66 2e 5f 63  names(), self._c
2700: 75 72 73 6f 72 2e 66 65 74 63 68 61 6c 6c 28 29  ursor.fetchall()
2710: 29 0a 0a 23 20 61 62 73 74 72 61 63 74 20 63 6c  )..# abstract cl
2720: 61 73 73 20 77 69 74 68 20 62 61 73 69 63 20 63  ass with basic c
2730: 68 65 63 6b 69 6e 67 20 66 75 6e 63 74 69 6f 6e  hecking function
2740: 61 6c 69 74 79 0a 63 6c 61 73 73 20 43 68 65 63  ality.class Chec
2750: 6b 65 72 28 6f 62 6a 65 63 74 29 3a 0a 09 5f 5f  ker(object):..__
2760: 73 6c 6f 74 73 5f 5f 20 3d 20 66 72 6f 7a 65 6e  slots__ = frozen
2770: 73 65 74 28 5b 27 5f 64 62 27 2c 20 27 5f 6c 6f  set(['_db', '_lo
2780: 67 27 2c 20 27 5f 71 75 65 75 65 27 2c 20 27 5f  g', '_queue', '_
2790: 72 65 71 75 65 73 74 27 2c 20 27 5f 73 74 64 6f  request', '_stdo
27a0: 75 74 27 5d 29 0a 0a 09 64 65 66 20 5f 5f 69 6e  ut'])...def __in
27b0: 69 74 5f 5f 28 73 65 6c 66 2c 20 71 75 65 75 65  it__(self, queue
27c0: 2c 20 6c 6f 67 67 65 72 29 3a 0a 09 09 73 65 6c  , logger):...sel
27d0: 66 2e 5f 64 62 20 3d 20 74 61 67 44 42 28 29 0a  f._db = tagDB().
27e0: 09 09 73 65 6c 66 2e 5f 6c 6f 67 20 3d 20 6c 6f  ..self._log = lo
27f0: 67 67 65 72 0a 09 09 73 65 6c 66 2e 5f 6c 6f 67  gger...self._log
2800: 2e 69 6e 66 6f 28 27 73 74 61 72 74 65 64 27 29  .info('started')
2810: 0a 09 09 73 65 6c 66 2e 5f 72 65 71 75 65 73 74  ...self._request
2820: 20 3d 20 72 65 2e 63 6f 6d 70 69 6c 65 28 27 5e   = re.compile('^
2830: 28 5b 30 2d 39 5d 2b 29 5c 20 28 68 74 74 70 7c  ([0-9]+)\ (http|
2840: 66 74 70 29 3a 5c 2f 5c 2f 28 5b 2d 5c 77 2e 3a  ftp):\/\/([-\w.:
2850: 5d 2b 29 5c 2f 28 5b 5e 20 5d 2a 29 5c 20 28 5b  ]+)\/([^ ]*)\ ([
2860: 30 2d 39 2e 3a 5d 2b 29 5c 2f 28 2d 7c 5b 5c 77  0-9.:]+)\/(-|[\w
2870: 5c 2e 5d 2b 29 5c 20 28 2d 7c 5c 77 2b 29 5c 20  \.]+)\ (-|\w+)\ 
2880: 28 2d 7c 47 45 54 7c 48 45 41 44 7c 50 4f 53 54  (-|GET|HEAD|POST
2890: 29 2e 2a 24 27 29 0a 09 09 73 65 6c 66 2e 5f 71  ).*$')...self._q
28a0: 75 65 75 65 20 3d 20 71 75 65 75 65 0a 09 09 73  ueue = queue...s
28b0: 65 6c 66 2e 5f 73 74 64 6f 75 74 20 3d 20 46 57  elf._stdout = FW
28c0: 72 69 74 65 6c 69 6e 65 51 75 65 75 65 28 73 79  ritelineQueue(sy
28d0: 73 2e 73 74 64 6f 75 74 2c 20 46 61 6c 73 65 29  s.stdout, False)
28e0: 0a 0a 09 64 65 66 20 70 72 6f 63 65 73 73 28 73  ...def process(s
28f0: 65 6c 66 2c 20 69 64 2c 20 73 69 74 65 2c 20 69  elf, id, site, i
2900: 70 5f 61 64 64 72 65 73 73 2c 20 75 72 6c 5f 70  p_address, url_p
2910: 61 74 68 2c 20 6c 69 6e 65 20 3d 20 4e 6f 6e 65  ath, line = None
2920: 29 3a 0a 09 09 23 73 65 6c 66 2e 5f 6c 6f 67 2e  ):...#self._log.
2930: 69 6e 66 6f 28 27 74 72 79 69 6e 67 20 7b 7d 27  info('trying {}'
2940: 2e 66 6f 72 6d 61 74 28 73 69 74 65 29 29 0a 09  .format(site))..
2950: 09 72 65 73 75 6c 74 20 3d 20 73 65 6c 66 2e 5f  .result = self._
2960: 64 62 2e 63 68 65 63 6b 28 73 69 74 65 2c 20 69  db.check(site, i
2970: 70 5f 61 64 64 72 65 73 73 29 0a 09 09 72 65 70  p_address)...rep
2980: 6c 79 20 3d 20 4e 6f 6e 65 0a 09 09 23 73 65 6c  ly = None...#sel
2990: 66 2e 5f 6c 6f 67 2e 69 6e 66 6f 28 27 67 6f 74  f._log.info('got
29a0: 20 7b 7d 20 6c 69 6e 65 73 20 66 72 6f 6d 20 64   {} lines from d
29b0: 61 74 61 62 61 73 65 27 2e 66 6f 72 6d 61 74 28  atabase'.format(
29c0: 6c 65 6e 28 72 65 73 75 6c 74 29 29 29 0a 09 09  len(result)))...
29d0: 66 6f 72 20 72 6f 77 20 69 6e 20 72 65 73 75 6c  for row in resul
29e0: 74 3a 0a 09 09 09 69 66 20 72 6f 77 20 21 3d 20  t:....if row != 
29f0: 4e 6f 6e 65 20 61 6e 64 20 72 6f 77 5b 30 5d 20  None and row[0] 
2a00: 21 3d 20 4e 6f 6e 65 3a 0a 09 09 09 09 69 66 20  != None:.....if 
2a10: 72 6f 77 5b 31 5d 20 21 3d 20 4e 6f 6e 65 3a 0a  row[1] != None:.
2a20: 09 09 09 09 09 73 65 6c 66 2e 5f 6c 6f 67 2e 69  .....self._log.i
2a30: 6e 66 6f 28 27 74 72 79 69 6e 67 20 72 65 67 65  nfo('trying rege
2a40: 78 70 20 22 7b 7d 22 20 76 65 72 73 75 73 20 22  xp "{}" versus "
2a50: 7b 7d 22 27 2e 66 6f 72 6d 61 74 28 72 6f 77 5b  {}"'.format(row[
2a60: 31 5d 2c 20 75 72 6c 5f 70 61 74 68 29 29 0a 09  1], url_path))..
2a70: 09 09 09 09 74 72 79 3a 0a 09 09 09 09 09 09 69  ....try:.......i
2a80: 66 20 72 65 2e 63 6f 6d 70 69 6c 65 28 72 6f 77  f re.compile(row
2a90: 5b 31 5d 29 2e 6d 61 74 63 68 28 75 72 6c 5f 70  [1]).match(url_p
2aa0: 61 74 68 29 3a 0a 09 09 09 09 09 09 09 72 65 70  ath):........rep
2ab0: 6c 79 20 3d 20 72 6f 77 5b 30 5d 2e 66 6f 72 6d  ly = row[0].form
2ac0: 61 74 28 75 72 6c 5f 70 61 74 68 29 0a 09 09 09  at(url_path)....
2ad0: 09 09 09 65 6c 73 65 3a 0a 09 09 09 09 09 09 09  ...else:........
2ae0: 63 6f 6e 74 69 6e 75 65 0a 09 09 09 09 09 65 78  continue......ex
2af0: 63 65 70 74 3a 0a 09 09 09 09 09 09 73 65 6c 66  cept:.......self
2b00: 2e 5f 6c 6f 67 2e 69 6e 66 6f 28 22 63 61 6e 27  ._log.info("can'
2b10: 74 20 63 6f 6d 70 69 6c 65 20 72 65 67 65 78 70  t compile regexp
2b20: 22 29 0a 09 09 09 09 65 6c 73 65 3a 0a 09 09 09  ").....else:....
2b30: 09 09 72 65 70 6c 79 20 3d 20 72 6f 77 5b 30 5d  ..reply = row[0]
2b40: 2e 66 6f 72 6d 61 74 28 75 72 6c 5f 70 61 74 68  .format(url_path
2b50: 29 0a 09 09 09 69 66 20 72 65 70 6c 79 20 21 3d  )....if reply !=
2b60: 20 4e 6f 6e 65 3a 0a 09 09 09 09 73 65 6c 66 2e   None:.....self.
2b70: 77 72 69 74 65 6c 69 6e 65 28 27 7b 7d 20 7b 7d  writeline('{} {}
2b80: 27 2e 66 6f 72 6d 61 74 28 69 64 2c 20 72 65 70  '.format(id, rep
2b90: 6c 79 29 29 0a 09 09 09 09 72 65 74 75 72 6e 28  ly)).....return(
2ba0: 54 72 75 65 29 0a 09 09 73 65 6c 66 2e 77 72 69  True)...self.wri
2bb0: 74 65 6c 69 6e 65 28 27 7b 7d 27 2e 66 6f 72 6d  teline('{}'.form
2bc0: 61 74 28 69 64 29 29 0a 0a 09 64 65 66 20 6c 6f  at(id))...def lo
2bd0: 6f 70 28 73 65 6c 66 29 3a 0a 09 09 77 68 69 6c  op(self):...whil
2be0: 65 20 54 72 75 65 3a 0a 09 09 09 6c 69 6e 65 20  e True:....line 
2bf0: 3d 20 73 65 6c 66 2e 5f 71 75 65 75 65 2e 67 65  = self._queue.ge
2c00: 74 28 29 0a 09 09 09 69 66 20 6c 69 6e 65 20 3d  t()....if line =
2c10: 3d 20 4e 6f 6e 65 3a 0a 09 09 09 09 62 72 65 61  = None:.....brea
2c20: 6b 0a 09 09 09 23 73 65 6c 66 2e 5f 6c 6f 67 2e  k....#self._log.
2c30: 69 6e 66 6f 28 27 72 65 71 75 65 73 74 3a 20 27  info('request: '
2c40: 20 2b 20 6c 69 6e 65 29 0a 09 09 09 72 65 71 75   + line)....requ
2c50: 65 73 74 20 3d 20 73 65 6c 66 2e 5f 72 65 71 75  est = self._requ
2c60: 65 73 74 2e 6d 61 74 63 68 28 6c 69 6e 65 29 0a  est.match(line).
2c70: 09 09 09 69 66 20 72 65 71 75 65 73 74 3a 0a 09  ...if request:..
2c80: 09 09 09 69 64 20 3d 20 72 65 71 75 65 73 74 2e  ...id = request.
2c90: 67 72 6f 75 70 28 31 29 0a 09 09 09 09 23 70 72  group(1).....#pr
2ca0: 6f 74 6f 20 3d 20 72 65 71 75 65 73 74 2e 67 72  oto = request.gr
2cb0: 6f 75 70 28 32 29 0a 09 09 09 09 73 69 74 65 20  oup(2).....site 
2cc0: 3d 20 72 65 71 75 65 73 74 2e 67 72 6f 75 70 28  = request.group(
2cd0: 33 29 0a 09 09 09 09 75 72 6c 5f 70 61 74 68 20  3).....url_path 
2ce0: 3d 20 72 65 71 75 65 73 74 2e 67 72 6f 75 70 28  = request.group(
2cf0: 34 29 0a 09 09 09 09 69 70 5f 61 64 64 72 65 73  4).....ip_addres
2d00: 73 20 3d 20 72 65 71 75 65 73 74 2e 67 72 6f 75  s = request.grou
2d10: 70 28 35 29 0a 09 09 09 09 73 65 6c 66 2e 70 72  p(5).....self.pr
2d20: 6f 63 65 73 73 28 69 64 2c 20 73 69 74 65 2c 20  ocess(id, site, 
2d30: 69 70 5f 61 64 64 72 65 73 73 2c 20 75 72 6c 5f  ip_address, url_
2d40: 70 61 74 68 2c 20 6c 69 6e 65 29 0a 09 09 09 65  path, line)....e
2d50: 6c 73 65 3a 0a 09 09 09 09 73 65 6c 66 2e 5f 6c  lse:.....self._l
2d60: 6f 67 2e 69 6e 66 6f 28 27 62 61 64 20 72 65 71  og.info('bad req
2d70: 75 65 73 74 27 29 0a 09 09 09 09 73 65 6c 66 2e  uest').....self.
2d80: 77 72 69 74 65 6c 69 6e 65 28 6c 69 6e 65 29 0a  writeline(line).
2d90: 0a 09 64 65 66 20 77 72 69 74 65 6c 69 6e 65 28  ..def writeline(
2da0: 73 65 6c 66 2c 20 73 74 72 69 6e 67 29 3a 0a 09  self, string):..
2db0: 09 73 65 6c 66 2e 5f 6c 6f 67 2e 69 6e 66 6f 28  .self._log.info(
2dc0: 27 3e 20 27 20 2b 20 73 74 72 69 6e 67 29 0a 09  '> ' + string)..
2dd0: 09 73 65 6c 66 2e 5f 73 74 64 6f 75 74 2e 70 75  .self._stdout.pu
2de0: 74 28 73 74 72 69 6e 67 29 0a 0a 69 66 20 63 6f  t(string)..if co
2df0: 6e 66 69 67 2e 6f 70 74 69 6f 6e 73 2e 64 75 6d  nfig.options.dum
2e00: 70 20 6f 72 20 63 6f 6e 66 69 67 2e 6f 70 74 69  p or config.opti
2e10: 6f 6e 73 2e 6c 6f 61 64 20 6f 72 20 63 6f 6e 66  ons.load or conf
2e20: 69 67 2e 6f 70 74 69 6f 6e 73 2e 64 75 6d 70 5f  ig.options.dump_
2e30: 63 6f 6e 66 20 6f 72 20 63 6f 6e 66 69 67 2e 6f  conf or config.o
2e40: 70 74 69 6f 6e 73 2e 6c 6f 61 64 5f 63 6f 6e 66  ptions.load_conf
2e50: 3a 0a 09 69 6d 70 6f 72 74 20 63 73 76 0a 0a 09  :..import csv...
2e60: 74 61 67 64 62 20 3d 20 74 61 67 44 42 28 29 0a  tagdb = tagDB().
2e70: 09 64 61 74 61 5f 66 69 65 6c 64 73 20 3d 20 5b  .data_fields = [
2e80: 27 73 69 74 65 27 2c 20 27 74 61 67 27 2c 20 27  'site', 'tag', '
2e90: 72 65 67 65 78 70 27 5d 0a 09 63 6f 6e 66 5f 66  regexp']..conf_f
2ea0: 69 65 6c 64 73 20 3d 20 5b 27 6e 65 74 6d 61 73  ields = ['netmas
2eb0: 6b 27 2c 20 27 72 65 64 69 72 65 63 74 5f 75 72  k', 'redirect_ur
2ec0: 6c 27 2c 20 27 66 72 6f 6d 5f 77 65 65 6b 64 61  l', 'from_weekda
2ed0: 79 27 2c 20 27 74 6f 5f 77 65 65 6b 64 61 79 27  y', 'to_weekday'
2ee0: 2c 20 27 66 72 6f 6d 5f 74 69 6d 65 27 2c 20 27  , 'from_time', '
2ef0: 74 6f 5f 74 69 6d 65 27 2c 20 27 74 61 67 27 5d  to_time', 'tag']
2f00: 0a 0a 09 69 66 20 63 6f 6e 66 69 67 2e 6f 70 74  ...if config.opt
2f10: 69 6f 6e 73 2e 64 75 6d 70 20 6f 72 20 63 6f 6e  ions.dump or con
2f20: 66 69 67 2e 6f 70 74 69 6f 6e 73 2e 64 75 6d 70  fig.options.dump
2f30: 5f 63 6f 6e 66 3a 0a 09 09 63 73 76 5f 77 72 69  _conf:...csv_wri
2f40: 74 65 72 20 3d 20 63 73 76 2e 77 72 69 74 65 72  ter = csv.writer
2f50: 28 73 79 73 2e 73 74 64 6f 75 74 29 0a 09 09 69  (sys.stdout)...i
2f60: 66 20 63 6f 6e 66 69 67 2e 6f 70 74 69 6f 6e 73  f config.options
2f70: 2e 64 75 6d 70 3a 0a 09 09 09 64 75 6d 70 20 3d  .dump:....dump =
2f80: 20 74 61 67 64 62 2e 64 75 6d 70 28 29 0a 09 09   tagdb.dump()...
2f90: 65 6c 69 66 20 63 6f 6e 66 69 67 2e 6f 70 74 69  elif config.opti
2fa0: 6f 6e 73 2e 64 75 6d 70 5f 63 6f 6e 66 3a 0a 09  ons.dump_conf:..
2fb0: 09 09 64 75 6d 70 20 3d 20 74 61 67 64 62 2e 64  ..dump = tagdb.d
2fc0: 75 6d 70 5f 63 6f 6e 66 28 29 0a 0a 09 09 63 73  ump_conf()....cs
2fd0: 76 5f 77 72 69 74 65 72 2e 77 72 69 74 65 72 6f  v_writer.writero
2fe0: 77 28 64 75 6d 70 5b 30 5d 29 0a 09 09 66 6f 72  w(dump[0])...for
2ff0: 20 6c 69 6e 65 20 69 6e 20 64 75 6d 70 5b 31 5d   line in dump[1]
3000: 3a 0a 09 09 09 63 73 76 5f 77 72 69 74 65 72 2e  :....csv_writer.
3010: 77 72 69 74 65 72 6f 77 28 6c 69 6e 65 29 0a 0a  writerow(line)..
3020: 09 65 6c 69 66 20 63 6f 6e 66 69 67 2e 6f 70 74  .elif config.opt
3030: 69 6f 6e 73 2e 6c 6f 61 64 20 6f 72 20 63 6f 6e  ions.load or con
3040: 66 69 67 2e 6f 70 74 69 6f 6e 73 2e 6c 6f 61 64  fig.options.load
3050: 5f 63 6f 6e 66 3a 0a 09 09 63 73 76 5f 72 65 61  _conf:...csv_rea
3060: 64 65 72 20 3d 20 63 73 76 2e 72 65 61 64 65 72  der = csv.reader
3070: 28 73 79 73 2e 73 74 64 69 6e 29 0a 09 09 66 69  (sys.stdin)...fi
3080: 72 73 74 5f 72 6f 77 20 3d 20 6e 65 78 74 28 63  rst_row = next(c
3090: 73 76 5f 72 65 61 64 65 72 29 0a 0a 09 09 69 66  sv_reader)....if
30a0: 20 63 6f 6e 66 69 67 2e 6f 70 74 69 6f 6e 73 2e   config.options.
30b0: 6c 6f 61 64 3a 0a 09 09 09 66 69 65 6c 64 73 20  load:....fields 
30c0: 3d 20 64 61 74 61 5f 66 69 65 6c 64 73 0a 09 09  = data_fields...
30d0: 09 6c 6f 61 64 20 3d 20 74 61 67 64 62 2e 6c 6f  .load = tagdb.lo
30e0: 61 64 0a 09 09 65 6c 69 66 20 63 6f 6e 66 69 67  ad...elif config
30f0: 2e 6f 70 74 69 6f 6e 73 2e 6c 6f 61 64 5f 63 6f  .options.load_co
3100: 6e 66 3a 0a 09 09 09 66 69 65 6c 64 73 20 3d 20  nf:....fields = 
3110: 63 6f 6e 66 5f 66 69 65 6c 64 73 0a 09 09 09 6c  conf_fields....l
3120: 6f 61 64 20 3d 20 74 61 67 64 62 2e 6c 6f 61 64  oad = tagdb.load
3130: 5f 63 6f 6e 66 0a 0a 09 09 61 73 73 65 72 74 20  _conf....assert 
3140: 66 69 72 73 74 5f 72 6f 77 20 3d 3d 20 66 69 65  first_row == fie
3150: 6c 64 73 2c 20 27 46 69 6c 65 20 6d 75 73 74 20  lds, 'File must 
3160: 63 6f 6e 74 61 69 6e 20 63 73 76 20 64 61 74 61  contain csv data
3170: 20 77 69 74 68 20 74 68 65 65 73 65 20 63 6f 6c   with theese col
3180: 75 6d 6e 73 3a 20 27 20 2b 20 72 65 70 72 28 66  umns: ' + repr(f
3190: 69 65 6c 64 73 29 0a 09 09 6c 6f 61 64 28 63 73  ields)...load(cs
31a0: 76 5f 72 65 61 64 65 72 29 0a 0a 65 6c 73 65 3a  v_reader)..else:
31b0: 0a 09 23 20 6d 61 69 6e 20 6c 6f 6f 70 0a 09 43  ..# main loop..C
31c0: 68 65 63 6b 65 72 28 73 74 64 69 6e 2c 20 6c 6f  hecker(stdin, lo
31d0: 67 67 65 72 29 2e 6c 6f 6f 70 28 29 0a           gger).loop().