Squid url redirector

Annotation For squid-tagger.py
anonymous

Annotation For squid-tagger.py

Origin for each line in squid-tagger.py from check-in fc934cead1:

d500448801 2009-10-05 c.kworr@d4daf: #!/usr/bin/env python3.1
d500448801 2009-10-05 c.kworr@d4daf: 
d500448801 2009-10-05 c.kworr@d4daf: import configparser, optparse, os, postgresql.api, re, sys, _thread
d500448801 2009-10-05 c.kworr@d4daf: 
d500448801 2009-10-05 c.kworr@d4daf: class Logger:
d500448801 2009-10-05 c.kworr@d4daf: 	__slots__ = frozenset(['_syslog'])
d500448801 2009-10-05 c.kworr@d4daf: 
d500448801 2009-10-05 c.kworr@d4daf: 	def __init__(self):
d500448801 2009-10-05 c.kworr@d4daf: 		config.section('log')
d500448801 2009-10-05 c.kworr@d4daf: 		if config['silent'] == 'yes':
d500448801 2009-10-05 c.kworr@d4daf: 			self._syslog = None
d500448801 2009-10-05 c.kworr@d4daf: 		else:
d500448801 2009-10-05 c.kworr@d4daf: 			import syslog
d500448801 2009-10-05 c.kworr@d4daf: 			self._syslog = syslog
d500448801 2009-10-05 c.kworr@d4daf: 			self._syslog.openlog('squidTag')
d500448801 2009-10-05 c.kworr@d4daf: 
d500448801 2009-10-05 c.kworr@d4daf: 	def info(self, message):
4b22e25f24 2009-10-07 c.kworr@d4daf: 		if self._syslog:
d500448801 2009-10-05 c.kworr@d4daf: 			self._syslog.syslog(self._syslog.LOG_INFO, message)
d500448801 2009-10-05 c.kworr@d4daf: 
d500448801 2009-10-05 c.kworr@d4daf: 	def notice(self, message):
4b22e25f24 2009-10-07 c.kworr@d4daf: 		if self._syslog:
d500448801 2009-10-05 c.kworr@d4daf: 			self._syslog.syslog(self._syslog.LOG_NOTICE, message)
d500448801 2009-10-05 c.kworr@d4daf: 
d500448801 2009-10-05 c.kworr@d4daf: class tagDB:
d500448801 2009-10-05 c.kworr@d4daf: 	__slots__ = frozenset(['_prepared', '_check_stmt', '_db'])
d500448801 2009-10-05 c.kworr@d4daf: 
d500448801 2009-10-05 c.kworr@d4daf: 	def __init__(self):
d500448801 2009-10-05 c.kworr@d4daf: 		self._prepared = set()
d500448801 2009-10-05 c.kworr@d4daf: 		self._db = False
88c03b5440 2009-10-09 c.kworr@d4daf: 		self._check_stmt = self._curs().prepare("select redirect_url, regexp from site_rule where site <@ tripdomain($1) and netmask >> $2::text::inet order by array_length(site, 1) desc")
d500448801 2009-10-05 c.kworr@d4daf: 
d500448801 2009-10-05 c.kworr@d4daf: 	def _curs(self):
d500448801 2009-10-05 c.kworr@d4daf: 		if not self._db:
d500448801 2009-10-05 c.kworr@d4daf: 			config.section('database')
d500448801 2009-10-05 c.kworr@d4daf: 			self._db = postgresql.open(
442d7bf53a 2009-10-12 c.kworr@d4daf: 				'pq://{}:{}@{}/{}'.format(
d500448801 2009-10-05 c.kworr@d4daf: 					config['user'],
d500448801 2009-10-05 c.kworr@d4daf: 					config['password'],
d500448801 2009-10-05 c.kworr@d4daf: 					config['host'],
d500448801 2009-10-05 c.kworr@d4daf: 					config['database'],
d500448801 2009-10-05 c.kworr@d4daf: 			) )
d500448801 2009-10-05 c.kworr@d4daf: 		return(self._db)
d500448801 2009-10-05 c.kworr@d4daf: 
d500448801 2009-10-05 c.kworr@d4daf: 	def check(self, ip_address, site):
88c03b5440 2009-10-09 c.kworr@d4daf: 		return self._check_stmt(site, ip_address)
d500448801 2009-10-05 c.kworr@d4daf: 
d500448801 2009-10-05 c.kworr@d4daf: class CheckerThread:
d500448801 2009-10-05 c.kworr@d4daf: 	__slots__ = frozenset(['_db', '_lock', '_lock_queue', '_log', '_queue'])
d500448801 2009-10-05 c.kworr@d4daf: 
d500448801 2009-10-05 c.kworr@d4daf: 	def __init__(self, db, log):
d500448801 2009-10-05 c.kworr@d4daf: 		self._db = db
d500448801 2009-10-05 c.kworr@d4daf: 		self._log = log
88c03b5440 2009-10-09 c.kworr@d4daf: 		# Spin lock. Loop acquires it on start then releases it when holding queue
88c03b5440 2009-10-09 c.kworr@d4daf: 		# lock. This way the thread proceeds without stops while queue has data and
88c03b5440 2009-10-09 c.kworr@d4daf: 		# gets stalled when no data present. The lock is released by queue writer
88c03b5440 2009-10-09 c.kworr@d4daf: 		# after storing something into the queue
d500448801 2009-10-05 c.kworr@d4daf: 		self._lock = _thread.allocate_lock()
d500448801 2009-10-05 c.kworr@d4daf: 		self._lock_queue = _thread.allocate_lock()
d500448801 2009-10-05 c.kworr@d4daf: 		self._lock.acquire()
d500448801 2009-10-05 c.kworr@d4daf: 		self._queue = []
d500448801 2009-10-05 c.kworr@d4daf: 		_thread.start_new_thread(self._start, ())
d500448801 2009-10-05 c.kworr@d4daf: 
d500448801 2009-10-05 c.kworr@d4daf: 	def _start(self):
d500448801 2009-10-05 c.kworr@d4daf: 		while True:
d500448801 2009-10-05 c.kworr@d4daf: 			self._lock.acquire()
d500448801 2009-10-05 c.kworr@d4daf: 			self._lock_queue.acquire()
88c03b5440 2009-10-09 c.kworr@d4daf: 			# yes this should be written this way, and yes, this is why I hate threading
d500448801 2009-10-05 c.kworr@d4daf: 			if len(self._queue) > 1 and self._lock.locked():
d500448801 2009-10-05 c.kworr@d4daf: 				self._lock.release()
d500448801 2009-10-05 c.kworr@d4daf: 			req = self._queue.pop(0)
d500448801 2009-10-05 c.kworr@d4daf: 			self._lock_queue.release()
442d7bf53a 2009-10-12 c.kworr@d4daf: 			self._log.info('trying {}\n'.format(req[1]))
88c03b5440 2009-10-09 c.kworr@d4daf: 			result = self._db.check(req[2], req[1])
7e3418d94f 2009-10-12 c.kworr@d4daf: 			#reply = '{}://{}/{}'.format(req[4], req[1], req[3])
7e3418d94f 2009-10-12 c.kworr@d4daf: 			reply = '-'
88c03b5440 2009-10-09 c.kworr@d4daf: 			for row in result:
88c03b5440 2009-10-09 c.kworr@d4daf: 				if row != None and row[0] != None:
88c03b5440 2009-10-09 c.kworr@d4daf: 					if row[1] != None:
442d7bf53a 2009-10-12 c.kworr@d4daf: 						self._log.info('trying regexp "{}" versus "{}"\n'.format(row[1], req[3]))
88c03b5440 2009-10-09 c.kworr@d4daf: 						if re.compile(row[1]).match(req[3]):
7e3418d94f 2009-10-12 c.kworr@d4daf: 							reply = '302:' + row[0]
88c03b5440 2009-10-09 c.kworr@d4daf: 							break
88c03b5440 2009-10-09 c.kworr@d4daf: 						else:
88c03b5440 2009-10-09 c.kworr@d4daf: 							continue
88c03b5440 2009-10-09 c.kworr@d4daf: 					else:
7e3418d94f 2009-10-12 c.kworr@d4daf: 						reply = '302:' + row[0]
88c03b5440 2009-10-09 c.kworr@d4daf: 						break
7e3418d94f 2009-10-12 c.kworr@d4daf: 			writeline('{} {}\n'.format(req[0], reply))
d500448801 2009-10-05 c.kworr@d4daf: 
d500448801 2009-10-05 c.kworr@d4daf: 	def check(self, line):
d500448801 2009-10-05 c.kworr@d4daf: 		request = re.compile('^([0-9]+)\ (http|ftp):\/\/([-\w.:]+)\/([^ ]*)\ ([0-9.]+)\/(-|[\w\.]+)\ (-|\w+)\ (-|GET|HEAD|POST).*$').match(line)
d500448801 2009-10-05 c.kworr@d4daf: 		if request:
d500448801 2009-10-05 c.kworr@d4daf: 			id = request.group(1)
7e3418d94f 2009-10-12 c.kworr@d4daf: 			#proto = request.group(2)
d500448801 2009-10-05 c.kworr@d4daf: 			site = request.group(3)
d500448801 2009-10-05 c.kworr@d4daf: 			url_path = request.group(4)
d500448801 2009-10-05 c.kworr@d4daf: 			ip_address = request.group(5)
d500448801 2009-10-05 c.kworr@d4daf: 			self._lock_queue.acquire()
7e3418d94f 2009-10-12 c.kworr@d4daf: 			self._queue.append((id, site, ip_address, url_path))
d500448801 2009-10-05 c.kworr@d4daf: 			if self._lock.locked():
d500448801 2009-10-05 c.kworr@d4daf: 				self._lock.release()
d500448801 2009-10-05 c.kworr@d4daf: 			self._lock_queue.release()
442d7bf53a 2009-10-12 c.kworr@d4daf: 			self._log.info('request {} queued ({})\n'.format(id, line))
d500448801 2009-10-05 c.kworr@d4daf: 		else:
d500448801 2009-10-05 c.kworr@d4daf: 			self._log.info('bad request\n')
d500448801 2009-10-05 c.kworr@d4daf: 			writeline(line)
d500448801 2009-10-05 c.kworr@d4daf: 
d500448801 2009-10-05 c.kworr@d4daf: def writeline(string):
442d7bf53a 2009-10-12 c.kworr@d4daf: 	log.info('sending: ' + string)
d500448801 2009-10-05 c.kworr@d4daf: 	sys.stdout.write(string)
d500448801 2009-10-05 c.kworr@d4daf: 	sys.stdout.flush()
d500448801 2009-10-05 c.kworr@d4daf: 
fc934cead1 2009-10-13 c.kworr@d4daf: # this classes processes config file and substitutes default values
d500448801 2009-10-05 c.kworr@d4daf: class Config:
fc934cead1 2009-10-13 c.kworr@d4daf: 	__slots__ = frozenset(['_config', '_defaults', '_section'])
fc934cead1 2009-10-13 c.kworr@d4daf: 	_defaults = {
fc934cead1 2009-10-13 c.kworr@d4daf: 		'log': {
fc934cead1 2009-10-13 c.kworr@d4daf: 			'silent': 'no',
fc934cead1 2009-10-13 c.kworr@d4daf: 		},
fc934cead1 2009-10-13 c.kworr@d4daf: 		'database': {
fc934cead1 2009-10-13 c.kworr@d4daf: 			'host': 'localhost',
fc934cead1 2009-10-13 c.kworr@d4daf: 			'database': 'squidTag',
fc934cead1 2009-10-13 c.kworr@d4daf: 	},}
d500448801 2009-10-05 c.kworr@d4daf: 
fc934cead1 2009-10-13 c.kworr@d4daf: 	# function to read in config file
d500448801 2009-10-05 c.kworr@d4daf: 	def __init__(self):
d500448801 2009-10-05 c.kworr@d4daf: 		parser = optparse.OptionParser()
d500448801 2009-10-05 c.kworr@d4daf: 		parser.add_option('-c', '--config', dest = 'config',
d500448801 2009-10-05 c.kworr@d4daf: 			help = 'config file location', metavar = 'FILE',
d500448801 2009-10-05 c.kworr@d4daf: 			default = '/usr/local/etc/squid-tagger.conf')
d500448801 2009-10-05 c.kworr@d4daf: 
d500448801 2009-10-05 c.kworr@d4daf: 		(options, args) = parser.parse_args()
d500448801 2009-10-05 c.kworr@d4daf: 
d500448801 2009-10-05 c.kworr@d4daf: 		if not os.access(options.config, os.R_OK):
d500448801 2009-10-05 c.kworr@d4daf: 			print("Can't read {}: exitting".format(options.config))
d500448801 2009-10-05 c.kworr@d4daf: 			sys.exit(2)
d500448801 2009-10-05 c.kworr@d4daf: 
d500448801 2009-10-05 c.kworr@d4daf: 		self._config = configparser.ConfigParser()
d500448801 2009-10-05 c.kworr@d4daf: 		self._config.readfp(open(options.config))
d500448801 2009-10-05 c.kworr@d4daf: 
fc934cead1 2009-10-13 c.kworr@d4daf: 	# function to select config file section or create one
d500448801 2009-10-05 c.kworr@d4daf: 	def section(self, section):
fc934cead1 2009-10-13 c.kworr@d4daf: 		if not self._config.has_section(section):
fc934cead1 2009-10-13 c.kworr@d4daf: 			self._config.add_section(section)
d500448801 2009-10-05 c.kworr@d4daf: 		self._section = section
d500448801 2009-10-05 c.kworr@d4daf: 
fc934cead1 2009-10-13 c.kworr@d4daf: 	# function to get config parameter, if parameter doesn't exists the default
fc934cead1 2009-10-13 c.kworr@d4daf: 	# value or None is substituted
d500448801 2009-10-05 c.kworr@d4daf: 	def __getitem__(self, name):
fc934cead1 2009-10-13 c.kworr@d4daf: 		if not self._config.has_option(self._section, name):
fc934cead1 2009-10-13 c.kworr@d4daf: 			if self._default.has_key(self._section):
fc934cead1 2009-10-13 c.kworr@d4daf: 				if self._default[self._section].has_key(name):
fc934cead1 2009-10-13 c.kworr@d4daf: 					self._config.set(self._section, name, self._default[self._section][name])
fc934cead1 2009-10-13 c.kworr@d4daf: 				else:
fc934cead1 2009-10-13 c.kworr@d4daf: 					self._config.set(self._section, name, None)
fc934cead1 2009-10-13 c.kworr@d4daf: 			else:
fc934cead1 2009-10-13 c.kworr@d4daf: 				self._config.set(self._section, name, None)
d500448801 2009-10-05 c.kworr@d4daf: 		return self._config.get(self._section, name)
d500448801 2009-10-05 c.kworr@d4daf: 
fc934cead1 2009-10-13 c.kworr@d4daf: # initializing and reading in config file
d500448801 2009-10-05 c.kworr@d4daf: config = Config()
d500448801 2009-10-05 c.kworr@d4daf: 
d500448801 2009-10-05 c.kworr@d4daf: log = Logger()
d500448801 2009-10-05 c.kworr@d4daf: db = tagDB()
d500448801 2009-10-05 c.kworr@d4daf: checker = CheckerThread(db,log)
d500448801 2009-10-05 c.kworr@d4daf: 
d500448801 2009-10-05 c.kworr@d4daf: while True:
d500448801 2009-10-05 c.kworr@d4daf: 	line = sys.stdin.readline()
d500448801 2009-10-05 c.kworr@d4daf: 	if len(line) == 0:
d500448801 2009-10-05 c.kworr@d4daf: 		break
d500448801 2009-10-05 c.kworr@d4daf: 	checker.check(line)