d0c6dcb865 2009-09-11 1: #!/usr/bin/env python-shared
d0c6dcb865 2009-09-11 2:
d0c6dcb865 2009-09-11 3: import cPickle,psycopg2,re,sys,thread
d0c6dcb865 2009-09-11 4:
d0c6dcb865 2009-09-11 5: class Logger(object):
d0c6dcb865 2009-09-11 6: __slots__=frozenset(['_silent','_syslog'])
d0c6dcb865 2009-09-11 7: def __init__(this,silent=True):
d0c6dcb865 2009-09-11 8: if silent:
d0c6dcb865 2009-09-11 9: this._silent=True
d0c6dcb865 2009-09-11 10: else:
d0c6dcb865 2009-09-11 11: import syslog
d0c6dcb865 2009-09-11 12: this._syslog=syslog
d0c6dcb865 2009-09-11 13: this._syslog.openlog('squidTag')
d0c6dcb865 2009-09-11 14: this._silent=False
d0c6dcb865 2009-09-11 15: def info(this,message):
d0c6dcb865 2009-09-11 16: if not this._silent:
d0c6dcb865 2009-09-11 17: this._syslog.syslog(this._syslog.LOG_INFO,message)
d0c6dcb865 2009-09-11 18: def notice(this,message):
d0c6dcb865 2009-09-11 19: if not this._silent:
d0c6dcb865 2009-09-11 20: this._syslog.syslog(this._syslog.LOG_NOTICE,message)
d0c6dcb865 2009-09-11 21:
d0c6dcb865 2009-09-11 22: class tagDB(object):
d0c6dcb865 2009-09-11 23: __slots__=frozenset(['_prepared','_cursor'])
d0c6dcb865 2009-09-11 24: def __init__(this):
d0c6dcb865 2009-09-11 25: this._prepared=set()
d0c6dcb865 2009-09-11 26: this._cursor=False
d0c6dcb865 2009-09-11 27: def _curs(this):
d0c6dcb865 2009-09-11 28: if not this._cursor:
d0c6dcb865 2009-09-11 29: this._cursor=psycopg2.connect('host=%s dbname=%s user=%s password=%s'%('pkunk','squidTag','squidTag','NachJas%')).cursor()
d0c6dcb865 2009-09-11 30: return this._cursor
d0c6dcb865 2009-09-11 31: def check(this,ip_address,site):
d0c6dcb865 2009-09-11 32: 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 33: def statusmessage(this):
d0c6dcb865 2009-09-11 34: return this._curs().statusmessage
d0c6dcb865 2009-09-11 35: def fetchone(this):
d0c6dcb865 2009-09-11 36: return this._curs().fetchone()
d0c6dcb865 2009-09-11 37:
d0c6dcb865 2009-09-11 38: class CheckerThread(object):
d0c6dcb865 2009-09-11 39: __slots__=frozenset(['_db','_lock','_lock_queue','_log','_queue'])
d0c6dcb865 2009-09-11 40: def __init__(this,db,log):
d0c6dcb865 2009-09-11 41: this._db=db
d0c6dcb865 2009-09-11 42: this._log=log
d0c6dcb865 2009-09-11 43: this._lock=thread.allocate_lock()
d0c6dcb865 2009-09-11 44: this._lock_queue=thread.allocate_lock()
d0c6dcb865 2009-09-11 45: this._lock.acquire()
d0c6dcb865 2009-09-11 46: this._queue=[]
d0c6dcb865 2009-09-11 47: thread.start_new_thread(this._start,())
d0c6dcb865 2009-09-11 48: def _start(this):
d0c6dcb865 2009-09-11 49: while True:
d0c6dcb865 2009-09-11 50: this._lock.acquire()
d0c6dcb865 2009-09-11 51: this._lock_queue.acquire()
d0c6dcb865 2009-09-11 52: if len(this._queue)>1 and this._lock.locked():
d0c6dcb865 2009-09-11 53: this._lock.release()
d0c6dcb865 2009-09-11 54: req=this._queue.pop(0)
d0c6dcb865 2009-09-11 55: this._lock_queue.release()
d0c6dcb865 2009-09-11 56: this._log.info('trying %s\n'%req[1])
d0c6dcb865 2009-09-11 57: this._db.check(req[2],req[1])
d0c6dcb865 2009-09-11 58: this._log.info("Got '%s' from database.\n"%this._db.statusmessage())
d0c6dcb865 2009-09-11 59: row=this._db.fetchone()
d0c6dcb865 2009-09-11 60: if row != None and row[0] != None:
d0c6dcb865 2009-09-11 61: writeline('%s 302:%s\n'%(req[0],row[0]))
d0c6dcb865 2009-09-11 62: else:
d0c6dcb865 2009-09-11 63: writeline('%s -\n'%req[0])
d0c6dcb865 2009-09-11 64: def check(this,line):
d0c6dcb865 2009-09-11 65: request=re.compile('^([0-9]+)\ (http|ftp):\/\/([-\w.:]+)\/([^ ]*)\ ([0-9.]+)\/(-|[\w\.]+)\ (-|\w+)\ (-|GET|HEAD|POST).*$').match(line)
d0c6dcb865 2009-09-11 66: if request:
d0c6dcb865 2009-09-11 67: site=request.group(3)
d0c6dcb865 2009-09-11 68: ip_address=request.group(5)
d0c6dcb865 2009-09-11 69: id=request.group(1)
d0c6dcb865 2009-09-11 70: this._lock_queue.acquire()
d0c6dcb865 2009-09-11 71: this._queue.append((id,site,ip_address))
d0c6dcb865 2009-09-11 72: if this._lock.locked():
d0c6dcb865 2009-09-11 73: this._lock.release()
d0c6dcb865 2009-09-11 74: this._lock_queue.release()
d0c6dcb865 2009-09-11 75: this._log.info('request %s queued (%s)\n'%(id,line))
d0c6dcb865 2009-09-11 76: else:
d0c6dcb865 2009-09-11 77: this._log.info('bad request\n')
d0c6dcb865 2009-09-11 78: writeline(line)
d0c6dcb865 2009-09-11 79:
d0c6dcb865 2009-09-11 80: def writeline(string):
d0c6dcb865 2009-09-11 81: log.info('sending: %s'%string)
d0c6dcb865 2009-09-11 82: sys.stdout.write(string)
d0c6dcb865 2009-09-11 83: sys.stdout.flush()
d0c6dcb865 2009-09-11 84:
d0c6dcb865 2009-09-11 85: log=Logger(False)
d0c6dcb865 2009-09-11 86: db=tagDB()
d0c6dcb865 2009-09-11 87: checker=CheckerThread(db,log)
d0c6dcb865 2009-09-11 88:
d0c6dcb865 2009-09-11 89: while True:
d0c6dcb865 2009-09-11 90: line=sys.stdin.readline()
d0c6dcb865 2009-09-11 91: if len(line)==0:
d0c6dcb865 2009-09-11 92: break
d0c6dcb865 2009-09-11 93: checker.check(line)