Squid url redirector

Annotation For squid-tagger
anonymous

Annotation For squid-tagger

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

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