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 c.kworr@d4daf: #!/usr/bin/env python3.1
7d2bc0649d 2009-09-11 c.kworr@d4daf: 
7d2bc0649d 2009-09-11 c.kworr@d4daf: import configparser, optparse, os, postgresql.api, re, sys, _thread
d0c6dcb865 2009-09-11 c.kworr@d4daf: 
7d2bc0649d 2009-09-11 c.kworr@d4daf: class Logger:
7d2bc0649d 2009-09-11 c.kworr@d4daf: 	__slots__ = frozenset(['_silent', '_syslog'])
d0c6dcb865 2009-09-11 c.kworr@d4daf: 
7d2bc0649d 2009-09-11 c.kworr@d4daf: 	def __init__(self, silent = True):
d0c6dcb865 2009-09-11 c.kworr@d4daf: 		if silent:
7d2bc0649d 2009-09-11 c.kworr@d4daf: 			self._silent = True
d0c6dcb865 2009-09-11 c.kworr@d4daf: 		else:
d0c6dcb865 2009-09-11 c.kworr@d4daf: 			import syslog
7d2bc0649d 2009-09-11 c.kworr@d4daf: 			self._syslog = syslog
7d2bc0649d 2009-09-11 c.kworr@d4daf: 			self._syslog.openlog('squidTag')
7d2bc0649d 2009-09-11 c.kworr@d4daf: 			self._silent = False
7d2bc0649d 2009-09-11 c.kworr@d4daf: 
7d2bc0649d 2009-09-11 c.kworr@d4daf: 	def info(self, message):
7d2bc0649d 2009-09-11 c.kworr@d4daf: 		if not self._silent:
7d2bc0649d 2009-09-11 c.kworr@d4daf: 			self._syslog.syslog(self._syslog.LOG_INFO, message)
7d2bc0649d 2009-09-11 c.kworr@d4daf: 
7d2bc0649d 2009-09-11 c.kworr@d4daf: 	def notice(self, message):
7d2bc0649d 2009-09-11 c.kworr@d4daf: 		if not self._silent:
7d2bc0649d 2009-09-11 c.kworr@d4daf: 			self._syslog.syslog(self._syslog.LOG_NOTICE, message)
7d2bc0649d 2009-09-11 c.kworr@d4daf: 
7d2bc0649d 2009-09-11 c.kworr@d4daf: class tagDB:
7d2bc0649d 2009-09-11 c.kworr@d4daf: 	__slots__ = frozenset(['_prepared', '_db'])
7d2bc0649d 2009-09-11 c.kworr@d4daf: 
7d2bc0649d 2009-09-11 c.kworr@d4daf: 	def __init__(self):
7d2bc0649d 2009-09-11 c.kworr@d4daf: 		self._prepared = set()
7d2bc0649d 2009-09-11 c.kworr@d4daf: 		self._db = False
7d2bc0649d 2009-09-11 c.kworr@d4daf: 
7d2bc0649d 2009-09-11 c.kworr@d4daf: 	def _curs(self):
7d2bc0649d 2009-09-11 c.kworr@d4daf: 		if not self._db:
7d2bc0649d 2009-09-11 c.kworr@d4daf: 			config.section('database')
7d2bc0649d 2009-09-11 c.kworr@d4daf: 			# needs thinking
7d2bc0649d 2009-09-11 c.kworr@d4daf: 			#connector = postgresql.api.Connector(
7d2bc0649d 2009-09-11 c.kworr@d4daf: 				#user = config['user'], password = config['password'],
7d2bc0649d 2009-09-11 c.kworr@d4daf: 				#database = config['database'], 
7d2bc0649d 2009-09-11 c.kworr@d4daf: 			self._db = postgresql.open(
7d2bc0649d 2009-09-11 c.kworr@d4daf: 				'pq://{0}:{1}@{2}/{3}'.format(
7d2bc0649d 2009-09-11 c.kworr@d4daf: 					config['user'],
7d2bc0649d 2009-09-11 c.kworr@d4daf: 					config['password'],
7d2bc0649d 2009-09-11 c.kworr@d4daf: 					config['host'],
7d2bc0649d 2009-09-11 c.kworr@d4daf: 					config['database'],
7d2bc0649d 2009-09-11 c.kworr@d4daf: 			))
7d2bc0649d 2009-09-11 c.kworr@d4daf: 		return(self._db)
7d2bc0649d 2009-09-11 c.kworr@d4daf: 
7d2bc0649d 2009-09-11 c.kworr@d4daf: 	def check(self, ip_address, site):
7d2bc0649d 2009-09-11 c.kworr@d4daf: 		# doesn't work for inet
7d2bc0649d 2009-09-11 c.kworr@d4daf: 		#stmt = self._curs().prepare("select redirect_url from site_rules where site <@ tripdomain($1) and netmask >> '$2' limit 1")
7d2bc0649d 2009-09-11 c.kworr@d4daf: 		#result = stmt(site, ip_address)
7d2bc0649d 2009-09-11 c.kworr@d4daf: 		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 c.kworr@d4daf: 		result = stmt()
7d2bc0649d 2009-09-11 c.kworr@d4daf: 		if len(result) > 0:
7d2bc0649d 2009-09-11 c.kworr@d4daf: 			return result[0]
7d2bc0649d 2009-09-11 c.kworr@d4daf: 		else:
7d2bc0649d 2009-09-11 c.kworr@d4daf: 			return None
7d2bc0649d 2009-09-11 c.kworr@d4daf: 
7d2bc0649d 2009-09-11 c.kworr@d4daf: class CheckerThread:
7d2bc0649d 2009-09-11 c.kworr@d4daf: 	__slots__ = frozenset(['_db', '_lock', '_lock_queue', '_log', '_queue'])
7d2bc0649d 2009-09-11 c.kworr@d4daf: 
7d2bc0649d 2009-09-11 c.kworr@d4daf: 	def __init__(self, db, log):
7d2bc0649d 2009-09-11 c.kworr@d4daf: 		self._db = db
7d2bc0649d 2009-09-11 c.kworr@d4daf: 		self._log = log
7d2bc0649d 2009-09-11 c.kworr@d4daf: 		self._lock = _thread.allocate_lock()
7d2bc0649d 2009-09-11 c.kworr@d4daf: 		self._lock_queue = _thread.allocate_lock()
7d2bc0649d 2009-09-11 c.kworr@d4daf: 		self._lock.acquire()
7d2bc0649d 2009-09-11 c.kworr@d4daf: 		self._queue = []
7d2bc0649d 2009-09-11 c.kworr@d4daf: 		_thread.start_new_thread(self._start, ())
7d2bc0649d 2009-09-11 c.kworr@d4daf: 
7d2bc0649d 2009-09-11 c.kworr@d4daf: 	def _start(self):
d0c6dcb865 2009-09-11 c.kworr@d4daf: 		while True:
7d2bc0649d 2009-09-11 c.kworr@d4daf: 			self._lock.acquire()
7d2bc0649d 2009-09-11 c.kworr@d4daf: 			self._lock_queue.acquire()
7d2bc0649d 2009-09-11 c.kworr@d4daf: 			if len(self._queue) > 1 and self._lock.locked():
7d2bc0649d 2009-09-11 c.kworr@d4daf: 				self._lock.release()
7d2bc0649d 2009-09-11 c.kworr@d4daf: 			req = self._queue.pop(0)
7d2bc0649d 2009-09-11 c.kworr@d4daf: 			self._lock_queue.release()
7d2bc0649d 2009-09-11 c.kworr@d4daf: 			self._log.info('trying %s\n'%req[1])
7d2bc0649d 2009-09-11 c.kworr@d4daf: 			row = self._db.check(req[2], req[1])
d0c6dcb865 2009-09-11 c.kworr@d4daf: 			if row != None and row[0] != None:
7d2bc0649d 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])
7d2bc0649d 2009-09-11 c.kworr@d4daf: 
7d2bc0649d 2009-09-11 c.kworr@d4daf: 	def check(self, line):
7d2bc0649d 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:
7d2bc0649d 2009-09-11 c.kworr@d4daf: 			site = request.group(3)
7d2bc0649d 2009-09-11 c.kworr@d4daf: 			ip_address = request.group(5)
7d2bc0649d 2009-09-11 c.kworr@d4daf: 			id = request.group(1)
7d2bc0649d 2009-09-11 c.kworr@d4daf: 			self._lock_queue.acquire()
7d2bc0649d 2009-09-11 c.kworr@d4daf: 			self._queue.append((id, site, ip_address))
7d2bc0649d 2009-09-11 c.kworr@d4daf: 			if self._lock.locked():
7d2bc0649d 2009-09-11 c.kworr@d4daf: 				self._lock.release()
7d2bc0649d 2009-09-11 c.kworr@d4daf: 			self._lock_queue.release()
7d2bc0649d 2009-09-11 c.kworr@d4daf: 			self._log.info('request %s queued (%s)\n'%(id, line))
d0c6dcb865 2009-09-11 c.kworr@d4daf: 		else:
7d2bc0649d 2009-09-11 c.kworr@d4daf: 			self._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: 
7d2bc0649d 2009-09-11 c.kworr@d4daf: class Config:
7d2bc0649d 2009-09-11 c.kworr@d4daf: 	__slots__ = frozenset(['_config', '_section'])
7d2bc0649d 2009-09-11 c.kworr@d4daf: 
7d2bc0649d 2009-09-11 c.kworr@d4daf: 	def __init__(self):
7d2bc0649d 2009-09-11 c.kworr@d4daf: 		parser = optparse.OptionParser()
7d2bc0649d 2009-09-11 c.kworr@d4daf: 		parser.add_option('-c', '--config', dest = 'config',
7d2bc0649d 2009-09-11 c.kworr@d4daf: 			help = 'config file location', metavar = 'FILE',
7d2bc0649d 2009-09-11 c.kworr@d4daf: 			default = '/usr/local/etc/squid-tagger.conf')
7d2bc0649d 2009-09-11 c.kworr@d4daf: 
7d2bc0649d 2009-09-11 c.kworr@d4daf: 		(options, args) = parser.parse_args()
7d2bc0649d 2009-09-11 c.kworr@d4daf: 
7d2bc0649d 2009-09-11 c.kworr@d4daf: 		if not os.access(options.config, os.R_OK):
7d2bc0649d 2009-09-11 c.kworr@d4daf: 			print("Can't read {0}: exitting".format(options.config))
7d2bc0649d 2009-09-11 c.kworr@d4daf: 			sys.exit(2)
7d2bc0649d 2009-09-11 c.kworr@d4daf: 
7d2bc0649d 2009-09-11 c.kworr@d4daf: 		self._config = configparser.ConfigParser()
7d2bc0649d 2009-09-11 c.kworr@d4daf: 		self._config.readfp(open(options.config))
7d2bc0649d 2009-09-11 c.kworr@d4daf: 
7d2bc0649d 2009-09-11 c.kworr@d4daf: 	def section(self, section):
7d2bc0649d 2009-09-11 c.kworr@d4daf: 		self._section = section
7d2bc0649d 2009-09-11 c.kworr@d4daf: 
7d2bc0649d 2009-09-11 c.kworr@d4daf: 	def __getitem__(self, name):
7d2bc0649d 2009-09-11 c.kworr@d4daf: 		return self._config.get(self._section, name)
7d2bc0649d 2009-09-11 c.kworr@d4daf: 
7d2bc0649d 2009-09-11 c.kworr@d4daf: config = Config()
7d2bc0649d 2009-09-11 c.kworr@d4daf: 
7d2bc0649d 2009-09-11 c.kworr@d4daf: log = Logger(False)
7d2bc0649d 2009-09-11 c.kworr@d4daf: db = tagDB()
7d2bc0649d 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:
7d2bc0649d 2009-09-11 c.kworr@d4daf: 	line = sys.stdin.readline()
7d2bc0649d 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)