7d2bc0649d 2009-09-11 1: #!/usr/bin/env python3.1
7d2bc0649d 2009-09-11 2:
7d2bc0649d 2009-09-11 3: import configparser, optparse, os, postgresql.api, re, sys, _thread
d0c6dcb865 2009-09-11 4:
7d2bc0649d 2009-09-11 5: class Logger:
7d2bc0649d 2009-09-11 6: __slots__ = frozenset(['_silent', '_syslog'])
d0c6dcb865 2009-09-11 7:
7d2bc0649d 2009-09-11 8: def __init__(self, silent = True):
d0c6dcb865 2009-09-11 9: if silent:
7d2bc0649d 2009-09-11 10: self._silent = True
d0c6dcb865 2009-09-11 11: else:
d0c6dcb865 2009-09-11 12: import syslog
7d2bc0649d 2009-09-11 13: self._syslog = syslog
7d2bc0649d 2009-09-11 14: self._syslog.openlog('squidTag')
7d2bc0649d 2009-09-11 15: self._silent = False
7d2bc0649d 2009-09-11 16:
7d2bc0649d 2009-09-11 17: def info(self, message):
7d2bc0649d 2009-09-11 18: if not self._silent:
7d2bc0649d 2009-09-11 19: self._syslog.syslog(self._syslog.LOG_INFO, message)
7d2bc0649d 2009-09-11 20:
7d2bc0649d 2009-09-11 21: def notice(self, message):
7d2bc0649d 2009-09-11 22: if not self._silent:
7d2bc0649d 2009-09-11 23: self._syslog.syslog(self._syslog.LOG_NOTICE, message)
7d2bc0649d 2009-09-11 24:
7d2bc0649d 2009-09-11 25: class tagDB:
09a01deb52 2009-10-01 26: __slots__ = frozenset(['_prepared', '_check_stmt', '_db'])
7d2bc0649d 2009-09-11 27:
7d2bc0649d 2009-09-11 28: def __init__(self):
7d2bc0649d 2009-09-11 29: self._prepared = set()
7d2bc0649d 2009-09-11 30: self._db = False
09a01deb52 2009-10-01 31: self._check_stmt = self._curs().prepare("select redirect_url from site_rule where site <@ tripdomain($1) and netmask >> $2::text::inet limit 1")
7d2bc0649d 2009-09-11 32:
7d2bc0649d 2009-09-11 33: def _curs(self):
7d2bc0649d 2009-09-11 34: if not self._db:
7d2bc0649d 2009-09-11 35: config.section('database')
7d2bc0649d 2009-09-11 36: self._db = postgresql.open(
7d2bc0649d 2009-09-11 37: 'pq://{0}:{1}@{2}/{3}'.format(
7d2bc0649d 2009-09-11 38: config['user'],
7d2bc0649d 2009-09-11 39: config['password'],
7d2bc0649d 2009-09-11 40: config['host'],
7d2bc0649d 2009-09-11 41: config['database'],
09a01deb52 2009-10-01 42: ) )
7d2bc0649d 2009-09-11 43: return(self._db)
7d2bc0649d 2009-09-11 44:
7d2bc0649d 2009-09-11 45: def check(self, ip_address, site):
09a01deb52 2009-10-01 46: result = self._check_stmt(site, ip_address)
7d2bc0649d 2009-09-11 47: if len(result) > 0:
7d2bc0649d 2009-09-11 48: return result[0]
7d2bc0649d 2009-09-11 49: else:
7d2bc0649d 2009-09-11 50: return None
7d2bc0649d 2009-09-11 51:
7d2bc0649d 2009-09-11 52: class CheckerThread:
7d2bc0649d 2009-09-11 53: __slots__ = frozenset(['_db', '_lock', '_lock_queue', '_log', '_queue'])
7d2bc0649d 2009-09-11 54:
7d2bc0649d 2009-09-11 55: def __init__(self, db, log):
7d2bc0649d 2009-09-11 56: self._db = db
7d2bc0649d 2009-09-11 57: self._log = log
7d2bc0649d 2009-09-11 58: self._lock = _thread.allocate_lock()
7d2bc0649d 2009-09-11 59: self._lock_queue = _thread.allocate_lock()
7d2bc0649d 2009-09-11 60: self._lock.acquire()
7d2bc0649d 2009-09-11 61: self._queue = []
7d2bc0649d 2009-09-11 62: _thread.start_new_thread(self._start, ())
7d2bc0649d 2009-09-11 63:
7d2bc0649d 2009-09-11 64: def _start(self):
d0c6dcb865 2009-09-11 65: while True:
7d2bc0649d 2009-09-11 66: self._lock.acquire()
7d2bc0649d 2009-09-11 67: self._lock_queue.acquire()
7d2bc0649d 2009-09-11 68: if len(self._queue) > 1 and self._lock.locked():
7d2bc0649d 2009-09-11 69: self._lock.release()
7d2bc0649d 2009-09-11 70: req = self._queue.pop(0)
7d2bc0649d 2009-09-11 71: self._lock_queue.release()
7d2bc0649d 2009-09-11 72: self._log.info('trying %s\n'%req[1])
7d2bc0649d 2009-09-11 73: row = self._db.check(req[2], req[1])
d0c6dcb865 2009-09-11 74: if row != None and row[0] != None:
7d2bc0649d 2009-09-11 75: writeline('%s 302:%s\n'%(req[0], row[0]))
d0c6dcb865 2009-09-11 76: else:
d0c6dcb865 2009-09-11 77: writeline('%s -\n'%req[0])
7d2bc0649d 2009-09-11 78:
7d2bc0649d 2009-09-11 79: def check(self, line):
7d2bc0649d 2009-09-11 80: request = re.compile('^([0-9]+)\ (http|ftp):\/\/([-\w.:]+)\/([^ ]*)\ ([0-9.]+)\/(-|[\w\.]+)\ (-|\w+)\ (-|GET|HEAD|POST).*$').match(line)
d0c6dcb865 2009-09-11 81: if request:
7d2bc0649d 2009-09-11 82: site = request.group(3)
7d2bc0649d 2009-09-11 83: ip_address = request.group(5)
7d2bc0649d 2009-09-11 84: id = request.group(1)
7d2bc0649d 2009-09-11 85: self._lock_queue.acquire()
7d2bc0649d 2009-09-11 86: self._queue.append((id, site, ip_address))
7d2bc0649d 2009-09-11 87: if self._lock.locked():
7d2bc0649d 2009-09-11 88: self._lock.release()
7d2bc0649d 2009-09-11 89: self._lock_queue.release()
7d2bc0649d 2009-09-11 90: self._log.info('request %s queued (%s)\n'%(id, line))
d0c6dcb865 2009-09-11 91: else:
7d2bc0649d 2009-09-11 92: self._log.info('bad request\n')
d0c6dcb865 2009-09-11 93: writeline(line)
d0c6dcb865 2009-09-11 94:
d0c6dcb865 2009-09-11 95: def writeline(string):
d0c6dcb865 2009-09-11 96: log.info('sending: %s'%string)
d0c6dcb865 2009-09-11 97: sys.stdout.write(string)
d0c6dcb865 2009-09-11 98: sys.stdout.flush()
d0c6dcb865 2009-09-11 99:
7d2bc0649d 2009-09-11 100: class Config:
7d2bc0649d 2009-09-11 101: __slots__ = frozenset(['_config', '_section'])
7d2bc0649d 2009-09-11 102:
7d2bc0649d 2009-09-11 103: def __init__(self):
7d2bc0649d 2009-09-11 104: parser = optparse.OptionParser()
7d2bc0649d 2009-09-11 105: parser.add_option('-c', '--config', dest = 'config',
7d2bc0649d 2009-09-11 106: help = 'config file location', metavar = 'FILE',
7d2bc0649d 2009-09-11 107: default = '/usr/local/etc/squid-tagger.conf')
7d2bc0649d 2009-09-11 108:
7d2bc0649d 2009-09-11 109: (options, args) = parser.parse_args()
7d2bc0649d 2009-09-11 110:
7d2bc0649d 2009-09-11 111: if not os.access(options.config, os.R_OK):
7d2bc0649d 2009-09-11 112: print("Can't read {0}: exitting".format(options.config))
7d2bc0649d 2009-09-11 113: sys.exit(2)
7d2bc0649d 2009-09-11 114:
7d2bc0649d 2009-09-11 115: self._config = configparser.ConfigParser()
7d2bc0649d 2009-09-11 116: self._config.readfp(open(options.config))
7d2bc0649d 2009-09-11 117:
7d2bc0649d 2009-09-11 118: def section(self, section):
7d2bc0649d 2009-09-11 119: self._section = section
7d2bc0649d 2009-09-11 120:
7d2bc0649d 2009-09-11 121: def __getitem__(self, name):
7d2bc0649d 2009-09-11 122: return self._config.get(self._section, name)
7d2bc0649d 2009-09-11 123:
7d2bc0649d 2009-09-11 124: config = Config()
7d2bc0649d 2009-09-11 125:
7d2bc0649d 2009-09-11 126: log = Logger(False)
7d2bc0649d 2009-09-11 127: db = tagDB()
7d2bc0649d 2009-09-11 128: checker = CheckerThread(db,log)
d0c6dcb865 2009-09-11 129:
d0c6dcb865 2009-09-11 130: while True:
7d2bc0649d 2009-09-11 131: line = sys.stdin.readline()
7d2bc0649d 2009-09-11 132: if len(line) == 0:
d0c6dcb865 2009-09-11 133: break
d0c6dcb865 2009-09-11 134: checker.check(line)