Overview
Comment: | version for python3.1: + config file support; + command line options. Needs testing probably... |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | master | trunk |
Files: | files | file ages | folders |
SHA3-256: |
7d2bc0649daef6cc6f419ac7525937a3 |
User & Date: | c.kworr@d4daf22a-8aaf-11de-a64d-234b64dd91b4 on 2009-09-11 12:47:54.000 |
Other Links: | branch diff | manifest | tags |
Context
2009-10-01
| ||
12:56 | resolved stmt work, added sample sql database structure check-in: 09a01deb52 user: c.kworr@d4daf22a-8aaf-11de-a64d-234b64dd91b4 tags: master, trunk | |
2009-09-11
| ||
12:47 | version for python3.1: + config file support; + command line options. Needs testing probably... check-in: 7d2bc0649d user: c.kworr@d4daf22a-8aaf-11de-a64d-234b64dd91b4 tags: master, trunk | |
08:52 | first version, python2.6 oriented check-in: d0c6dcb865 user: c.kworr@d4daf22a-8aaf-11de-a64d-234b64dd91b4 tags: master, trunk | |
Changes
Modified squid-tagger
from [b468bd500f]
to [db4ecab697].
|
| | | | | > | | | | | > | | > | | | | | | > | | > | | | < > | > > > | < < < > | < > > > > > > > > > > > > > > > > | > | | | | | | | | | > | | | | | | | | | < < | > | | | | | | | | | | | | > > > > > > > > > > > > > > > > > > > > > > > > > > | | | | | | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | #!/usr/bin/env python3.1 import configparser, optparse, os, postgresql.api, re, sys, _thread class Logger: __slots__ = frozenset(['_silent', '_syslog']) def __init__(self, silent = True): if silent: self._silent = True else: import syslog self._syslog = syslog self._syslog.openlog('squidTag') self._silent = False def info(self, message): if not self._silent: self._syslog.syslog(self._syslog.LOG_INFO, message) def notice(self, message): if not self._silent: self._syslog.syslog(self._syslog.LOG_NOTICE, message) class tagDB: __slots__ = frozenset(['_prepared', '_db']) def __init__(self): self._prepared = set() self._db = False def _curs(self): if not self._db: config.section('database') # needs thinking #connector = postgresql.api.Connector( #user = config['user'], password = config['password'], #database = config['database'], self._db = postgresql.open( 'pq://{0}:{1}@{2}/{3}'.format( config['user'], config['password'], config['host'], config['database'], )) return(self._db) def check(self, ip_address, site): # doesn't work for inet #stmt = self._curs().prepare("select redirect_url from site_rules where site <@ tripdomain($1) and netmask >> '$2' limit 1") #result = stmt(site, ip_address) stmt = self._curs().prepare("select redirect_url from site_rules where site <@ tripdomain('{0}') and netmask >> '{1}' limit 1".format(site, ip_address)) result = stmt() if len(result) > 0: return result[0] else: return None class CheckerThread: __slots__ = frozenset(['_db', '_lock', '_lock_queue', '_log', '_queue']) def __init__(self, db, log): self._db = db self._log = log self._lock = _thread.allocate_lock() self._lock_queue = _thread.allocate_lock() self._lock.acquire() self._queue = [] _thread.start_new_thread(self._start, ()) def _start(self): while True: self._lock.acquire() self._lock_queue.acquire() if len(self._queue) > 1 and self._lock.locked(): self._lock.release() req = self._queue.pop(0) self._lock_queue.release() self._log.info('trying %s\n'%req[1]) row = self._db.check(req[2], req[1]) if row != None and row[0] != None: writeline('%s 302:%s\n'%(req[0], row[0])) else: writeline('%s -\n'%req[0]) def check(self, line): request = re.compile('^([0-9]+)\ (http|ftp):\/\/([-\w.:]+)\/([^ ]*)\ ([0-9.]+)\/(-|[\w\.]+)\ (-|\w+)\ (-|GET|HEAD|POST).*$').match(line) if request: site = request.group(3) ip_address = request.group(5) id = request.group(1) self._lock_queue.acquire() self._queue.append((id, site, ip_address)) if self._lock.locked(): self._lock.release() self._lock_queue.release() self._log.info('request %s queued (%s)\n'%(id, line)) else: self._log.info('bad request\n') writeline(line) def writeline(string): log.info('sending: %s'%string) sys.stdout.write(string) sys.stdout.flush() class Config: __slots__ = frozenset(['_config', '_section']) def __init__(self): parser = optparse.OptionParser() parser.add_option('-c', '--config', dest = 'config', help = 'config file location', metavar = 'FILE', default = '/usr/local/etc/squid-tagger.conf') (options, args) = parser.parse_args() if not os.access(options.config, os.R_OK): print("Can't read {0}: exitting".format(options.config)) sys.exit(2) self._config = configparser.ConfigParser() self._config.readfp(open(options.config)) def section(self, section): self._section = section def __getitem__(self, name): return self._config.get(self._section, name) config = Config() log = Logger(False) db = tagDB() checker = CheckerThread(db,log) while True: line = sys.stdin.readline() if len(line) == 0: break checker.check(line) |