Squid url redirector

Diff
anonymous

Diff

Differences From Artifact [db4ecab697]:

To Artifact [de210bb16a]:


19
20
21
22
23
24
25
26

27
28
29
30

31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

46
47
48
49
50
51

52
53
54
55
56
57
58
59
60
19
20
21
22
23
24
25

26
27
28
29
30
31
32
33
34
35




36
37
38
39
40
41

42
43
44
45



46


47
48
49
50
51
52
53







-
+




+




-
-
-
-






-
+



-
-
-
+
-
-







			self._syslog.syslog(self._syslog.LOG_INFO, message)

	def notice(self, message):
		if not self._silent:
			self._syslog.syslog(self._syslog.LOG_NOTICE, message)

class tagDB:
	__slots__ = frozenset(['_prepared', '_db'])
	__slots__ = frozenset(['_prepared', '_check_stmt', '_db'])

	def __init__(self):
		self._prepared = set()
		self._db = False
		self._check_stmt = self._curs().prepare("select redirect_url from site_rule where site <@ tripdomain($1) and netmask >> $2::text::inet limit 1")

	def _curs(self):
		if not self._db:
			config.section('database')
			# needs thinking
			#connector = postgresql.api.Connector(
				#user = config['user'], password = config['password'],
				#database = config['database'], 
			self._db = postgresql.open(
				'pq://{0}:{1}@{2}/{3}'.format(
					config['user'],
					config['password'],
					config['host'],
					config['database'],
			))
			) )
		return(self._db)

	def check(self, ip_address, site):
		# doesn't work for inet
		#stmt = self._curs().prepare("select redirect_url from site_rules where site <@ tripdomain($1) and netmask >> '$2' limit 1")
		#result = stmt(site, ip_address)
		result = self._check_stmt(site, ip_address)
		stmt = self._curs().prepare("select redirect_url from site_rules where site <@ tripdomain('{0}') and netmask >> '{1}' limit 1".format(site, ip_address))
		result = stmt()
		if len(result) > 0:
			return result[0]
		else:
			return None

class CheckerThread:
	__slots__ = frozenset(['_db', '_lock', '_lock_queue', '_log', '_queue'])