Squid url redirector

Check-in [25bd939a42]
anonymous

Check-in [25bd939a42]

Overview
Comment:* Support for CONNECT queries. Matches only if no regexp specified for site as we can't see url. * Remove syslog recode tweak.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | master | trunk
Files: files | file ages | folders
SHA3-256: 25bd939a421df6690cfb84901f54cdf2d3065945e801a673a2252d4f5fa4a65a
User & Date: arcade@b1t.name on 2013-08-07 13:29:36.000
Other Links: branch diff | manifest | tags
Context
2013-11-28
14:45
gevent 1.0 welcome... check-in: 9d7d80e594 user: arcade@b1t.name tags: master, trunk
2013-08-07
13:29
* Support for CONNECT queries. Matches only if no regexp specified for site as we can't see url. * Remove syslog recode tweak. check-in: 25bd939a42 user: arcade@b1t.name tags: master, trunk
2013-03-13
17:22
support for substituting domain and path when redirecting check-in: a326d03ba1 user: arcade@b1t.name tags: master, trunk
Changes
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
103
104
105
106
107
108
109


110
111
112
113
114
115
116







-
-








	def __init__(self):
		logging.handlers.SysLogHandler.__init__(self, '/dev/log')
		self._tail = gevent.queue.Queue()
		self._worker = None

	def emit(self, record):
		# my syslog is broken and cannot into UTF-8 BOM
		record.msg = record.msg.encode('utf-8')
		try:
			self._tail.put(record)
		except (KeyboardInterrupt, SystemExit):
			raise
		except:
			self.handleError(record)
		if self._worker == None:
302
303
304
305
306
307
308
309

310
311
312
313
314
315
316
317
318
319
320

321
322
323
324
325
326
327
328

329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348




349
350
351
352
353
354
355
300
301
302
303
304
305
306

307
308
309
310
311
312
313
314
315
316
317

318
319
320
321
322
323
324
325

326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342




343
344
345
346
347
348
349
350
351
352
353







-
+










-
+







-
+
















-
-
-
-
+
+
+
+







class Checker(object):
	__slots__ = frozenset(['_db', '_log', '_queue', '_request', '_stdout'])

	def __init__(self, queue, logger):
		self._db = tagDB()
		self._log = logger
		self._log.info('started')
		self._request = re.compile('^([0-9]+)\ (http|ftp):\/\/([-\w.:]+)\/([^ ]*)\ ([0-9.:]+)\/(-|[\w\.]+)\ (-|\w+)\ (-|GET|HEAD|POST).*$')
		self._request = re.compile('^([0-9]+)\ ((http|ftp):\/\/)?([-\w.]+)(:[0-9]+)?(\/([^ ]*))?\ ([0-9.:]+)\/(-|[\w\.]+)\ (-|\w+)\ (-|GET|HEAD|POST|CONNECT).*$')
		self._queue = queue
		self._stdout = FWritelineQueue(sys.stdout, False)

	def process(self, id, site, ip_address, url_path, line = None):
		#self._log.info('trying {}'.format(site))
		result = self._db.check(site, ip_address)
		reply = None
		#self._log.info('got {} lines from database'.format(len(result)))
		for row in result:
			if row != None and row[0] != None:
				if row[1] != None:
				if row[1] != None and url_path != None:
					self._log.info('trying regexp "{}" versus "{}"'.format(row[1], url_path))
					try:
						if re.compile(row[1]).match(url_path):
							reply = row[0].format(host = site, path = url_path)
						else:
							continue
					except:
						self._log.info("can't compile regexp")
						self._log.info("can't compile or execute regexp")
				else:
					reply = row[0].format(host = site, path = url_path)
			if reply != None:
				self.writeline('{} {}'.format(id, reply))
				return(True)
		self.writeline('{}'.format(id))

	def loop(self):
		while True:
			line = self._queue.get()
			if line == None:
				break
			#self._log.info('request: ' + line)
			request = self._request.match(line)
			if request:
				id = request.group(1)
				#proto = request.group(2)
				site = request.group(3)
				url_path = request.group(4)
				ip_address = request.group(5)
				#proto = request.group(3)
				site = request.group(4)
				url_path = request.group(7)
				ip_address = request.group(8)
				self.process(id, site, ip_address, url_path, line)
			else:
				self._log.info('bad request')
				self.writeline(line)

	def writeline(self, string):
		self._log.info('> ' + string)