d0c6dcb865 2009-09-11 c.kworr@d4daf: #!/usr/bin/env python-shared
d0c6dcb865 2009-09-11 c.kworr@d4daf:
d0c6dcb865 2009-09-11 c.kworr@d4daf: import cPickle,psycopg2,re,sys,thread
d0c6dcb865 2009-09-11 c.kworr@d4daf:
d0c6dcb865 2009-09-11 c.kworr@d4daf: class Logger(object):
d0c6dcb865 2009-09-11 c.kworr@d4daf: __slots__=frozenset(['_silent','_syslog'])
d0c6dcb865 2009-09-11 c.kworr@d4daf: def __init__(this,silent=True):
d0c6dcb865 2009-09-11 c.kworr@d4daf: if silent:
d0c6dcb865 2009-09-11 c.kworr@d4daf: this._silent=True
d0c6dcb865 2009-09-11 c.kworr@d4daf: else:
d0c6dcb865 2009-09-11 c.kworr@d4daf: import syslog
d0c6dcb865 2009-09-11 c.kworr@d4daf: this._syslog=syslog
d0c6dcb865 2009-09-11 c.kworr@d4daf: this._syslog.openlog('squidTag')
d0c6dcb865 2009-09-11 c.kworr@d4daf: this._silent=False
d0c6dcb865 2009-09-11 c.kworr@d4daf: def info(this,message):
d0c6dcb865 2009-09-11 c.kworr@d4daf: if not this._silent:
d0c6dcb865 2009-09-11 c.kworr@d4daf: this._syslog.syslog(this._syslog.LOG_INFO,message)
d0c6dcb865 2009-09-11 c.kworr@d4daf: def notice(this,message):
d0c6dcb865 2009-09-11 c.kworr@d4daf: if not this._silent:
d0c6dcb865 2009-09-11 c.kworr@d4daf: this._syslog.syslog(this._syslog.LOG_NOTICE,message)
d0c6dcb865 2009-09-11 c.kworr@d4daf:
d0c6dcb865 2009-09-11 c.kworr@d4daf: class tagDB(object):
d0c6dcb865 2009-09-11 c.kworr@d4daf: __slots__=frozenset(['_prepared','_cursor'])
d0c6dcb865 2009-09-11 c.kworr@d4daf: def __init__(this):
d0c6dcb865 2009-09-11 c.kworr@d4daf: this._prepared=set()
d0c6dcb865 2009-09-11 c.kworr@d4daf: this._cursor=False
d0c6dcb865 2009-09-11 c.kworr@d4daf: def _curs(this):
d0c6dcb865 2009-09-11 c.kworr@d4daf: if not this._cursor:
d0c6dcb865 2009-09-11 c.kworr@d4daf: this._cursor=psycopg2.connect('host=%s dbname=%s user=%s password=%s'%('pkunk','squidTag','squidTag','NachJas%')).cursor()
d0c6dcb865 2009-09-11 c.kworr@d4daf: return this._cursor
d0c6dcb865 2009-09-11 c.kworr@d4daf: def check(this,ip_address,site):
d0c6dcb865 2009-09-11 c.kworr@d4daf: return this._curs().execute("select redirect_url from site_rules where site <@ tripdomain(%s) and netmask >> %s limit 1",(site,ip_address,))
d0c6dcb865 2009-09-11 c.kworr@d4daf: def statusmessage(this):
d0c6dcb865 2009-09-11 c.kworr@d4daf: return this._curs().statusmessage
d0c6dcb865 2009-09-11 c.kworr@d4daf: def fetchone(this):
d0c6dcb865 2009-09-11 c.kworr@d4daf: return this._curs().fetchone()
d0c6dcb865 2009-09-11 c.kworr@d4daf:
d0c6dcb865 2009-09-11 c.kworr@d4daf: class CheckerThread(object):
d0c6dcb865 2009-09-11 c.kworr@d4daf: __slots__=frozenset(['_db','_lock','_lock_queue','_log','_queue'])
d0c6dcb865 2009-09-11 c.kworr@d4daf: def __init__(this,db,log):
d0c6dcb865 2009-09-11 c.kworr@d4daf: this._db=db
d0c6dcb865 2009-09-11 c.kworr@d4daf: this._log=log
d0c6dcb865 2009-09-11 c.kworr@d4daf: this._lock=thread.allocate_lock()
d0c6dcb865 2009-09-11 c.kworr@d4daf: this._lock_queue=thread.allocate_lock()
d0c6dcb865 2009-09-11 c.kworr@d4daf: this._lock.acquire()
d0c6dcb865 2009-09-11 c.kworr@d4daf: this._queue=[]
d0c6dcb865 2009-09-11 c.kworr@d4daf: thread.start_new_thread(this._start,())
d0c6dcb865 2009-09-11 c.kworr@d4daf: def _start(this):
d0c6dcb865 2009-09-11 c.kworr@d4daf: while True:
d0c6dcb865 2009-09-11 c.kworr@d4daf: this._lock.acquire()
d0c6dcb865 2009-09-11 c.kworr@d4daf: this._lock_queue.acquire()
d0c6dcb865 2009-09-11 c.kworr@d4daf: if len(this._queue)>1 and this._lock.locked():
d0c6dcb865 2009-09-11 c.kworr@d4daf: this._lock.release()
d0c6dcb865 2009-09-11 c.kworr@d4daf: req=this._queue.pop(0)
d0c6dcb865 2009-09-11 c.kworr@d4daf: this._lock_queue.release()
d0c6dcb865 2009-09-11 c.kworr@d4daf: this._log.info('trying %s\n'%req[1])
d0c6dcb865 2009-09-11 c.kworr@d4daf: this._db.check(req[2],req[1])
d0c6dcb865 2009-09-11 c.kworr@d4daf: this._log.info("Got '%s' from database.\n"%this._db.statusmessage())
d0c6dcb865 2009-09-11 c.kworr@d4daf: row=this._db.fetchone()
d0c6dcb865 2009-09-11 c.kworr@d4daf: if row != None and row[0] != None:
d0c6dcb865 2009-09-11 c.kworr@d4daf: writeline('%s 302:%s\n'%(req[0],row[0]))
d0c6dcb865 2009-09-11 c.kworr@d4daf: else:
d0c6dcb865 2009-09-11 c.kworr@d4daf: writeline('%s -\n'%req[0])
d0c6dcb865 2009-09-11 c.kworr@d4daf: def check(this,line):
d0c6dcb865 2009-09-11 c.kworr@d4daf: request=re.compile('^([0-9]+)\ (http|ftp):\/\/([-\w.:]+)\/([^ ]*)\ ([0-9.]+)\/(-|[\w\.]+)\ (-|\w+)\ (-|GET|HEAD|POST).*$').match(line)
d0c6dcb865 2009-09-11 c.kworr@d4daf: if request:
d0c6dcb865 2009-09-11 c.kworr@d4daf: site=request.group(3)
d0c6dcb865 2009-09-11 c.kworr@d4daf: ip_address=request.group(5)
d0c6dcb865 2009-09-11 c.kworr@d4daf: id=request.group(1)
d0c6dcb865 2009-09-11 c.kworr@d4daf: this._lock_queue.acquire()
d0c6dcb865 2009-09-11 c.kworr@d4daf: this._queue.append((id,site,ip_address))
d0c6dcb865 2009-09-11 c.kworr@d4daf: if this._lock.locked():
d0c6dcb865 2009-09-11 c.kworr@d4daf: this._lock.release()
d0c6dcb865 2009-09-11 c.kworr@d4daf: this._lock_queue.release()
d0c6dcb865 2009-09-11 c.kworr@d4daf: this._log.info('request %s queued (%s)\n'%(id,line))
d0c6dcb865 2009-09-11 c.kworr@d4daf: else:
d0c6dcb865 2009-09-11 c.kworr@d4daf: this._log.info('bad request\n')
d0c6dcb865 2009-09-11 c.kworr@d4daf: writeline(line)
d0c6dcb865 2009-09-11 c.kworr@d4daf:
d0c6dcb865 2009-09-11 c.kworr@d4daf: def writeline(string):
d0c6dcb865 2009-09-11 c.kworr@d4daf: log.info('sending: %s'%string)
d0c6dcb865 2009-09-11 c.kworr@d4daf: sys.stdout.write(string)
d0c6dcb865 2009-09-11 c.kworr@d4daf: sys.stdout.flush()
d0c6dcb865 2009-09-11 c.kworr@d4daf:
d0c6dcb865 2009-09-11 c.kworr@d4daf: log=Logger(False)
d0c6dcb865 2009-09-11 c.kworr@d4daf: db=tagDB()
d0c6dcb865 2009-09-11 c.kworr@d4daf: checker=CheckerThread(db,log)
d0c6dcb865 2009-09-11 c.kworr@d4daf:
d0c6dcb865 2009-09-11 c.kworr@d4daf: while True:
d0c6dcb865 2009-09-11 c.kworr@d4daf: line=sys.stdin.readline()
d0c6dcb865 2009-09-11 c.kworr@d4daf: if len(line)==0:
d0c6dcb865 2009-09-11 c.kworr@d4daf: break
d0c6dcb865 2009-09-11 c.kworr@d4daf: checker.check(line)