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