Squid url redirector

Annotation For squid-tagger
anonymous

Annotation For squid-tagger

Origin for each line in squid-tagger from check-in 7d2bc0649d:

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