Index: database.sql ================================================================== --- database.sql +++ database.sql @@ -165,11 +165,12 @@ -- table to hold tag - site links CREATE TABLE urls ( date_added timestamp without time zone DEFAULT ('now'::text)::timestamp(0) without time zone NOT NULL, id_site smallint NOT NULL, - id_tag smallint NOT NULL + id_tag smallint NOT NULL, + regex text ); ALTER TABLE ONLY urls ADD CONSTRAINT urls_pkey PRIMARY KEY (date_added); @@ -176,17 +177,17 @@ CREATE UNIQUE INDEX urls_id_site ON urls USING btree (id_site); -- rule to join all tables into one to simplify access -- automaticall uses current day and time data CREATE VIEW site_rule AS -SELECT a.redirect_url, a.netmask, b.site +SELECT a.redirect_url, a.netmask, b.site, b.regexp FROM (( SELECT rules.redirect_url, tag.tag AS rule_tag, rules.netmask - FROM (rules NATURAL JOIN tag) - WHERE ((((('now'::text)::time without time zone >= rules.from_time) - AND (('now'::text)::time without time zone <= rules.to_time)) - AND (date_part('dow'::text, now()) >= (rules.from_weekday)::double precision)) - AND (date_part('dow'::text, now()) <= (rules.to_weekday)::double precision)) + FROM rules NATURAL JOIN tag + WHERE ('now'::text)::time without time zone >= rules.from_time + AND ('now'::text)::time without time zone <= rules.to_time + AND date_part('dow'::text, now()) >= (rules.from_weekday)::double precision + AND date_part('dow'::text, now()) <= (rules.to_weekday)::double precision ) a JOIN ( - SELECT site.site, tag.tag AS url_tag - FROM ((urls NATURAL JOIN tag) NATURAL JOIN site) -) b ON ((b.url_tag && a.rule_tag))); + SELECT site.site, tag.tag AS url_tag, regexp + FROM urls NATURAL JOIN tag NATURAL JOIN site +) b ON (b.url_tag && a.rule_tag)); Index: squid-tagger ================================================================== --- squid-tagger +++ squid-tagger @@ -3,12 +3,13 @@ import configparser, optparse, os, postgresql.api, re, sys, _thread class Logger: __slots__ = frozenset(['_silent', '_syslog']) - def __init__(self, silent = True): - if silent: + def __init__(self): + config.section('log') + if config['silent'] = 'yes': self._silent = True else: import syslog self._syslog = syslog self._syslog.openlog('squidTag') @@ -26,11 +27,11 @@ __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") + 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") def _curs(self): if not self._db: config.section('database') self._db = postgresql.open( @@ -43,10 +44,15 @@ return(self._db) def check(self, ip_address, site): result = self._check_stmt(site, ip_address) if len(result) > 0: + if result[1]: + if re.compile(result[1]).match(url_path): + return result[0] + else: + return None return result[0] else: return None class CheckerThread: @@ -107,11 +113,11 @@ default = '/usr/local/etc/squid-tagger.conf') (options, args) = parser.parse_args() if not os.access(options.config, os.R_OK): - print("Can't read {0}: exitting".format(options.config)) + print("Can't read {}: exitting".format(options.config)) sys.exit(2) self._config = configparser.ConfigParser() self._config.readfp(open(options.config)) @@ -121,14 +127,14 @@ def __getitem__(self, name): return self._config.get(self._section, name) config = Config() -log = Logger(False) +log = Logger() db = tagDB() checker = CheckerThread(db,log) while True: line = sys.stdin.readline() if len(line) == 0: break checker.check(line)