92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
|
self._config.set(self._section, name, None)
return(self._config.get(self._section, name))
# initializing and reading in config file
config = Config()
import logging, logging.handlers
# wrapper around logging handler to make it queue records and don't stall when sending them
class SysLogHandlerQueue(logging.handlers.SysLogHandler):
__slots__ = frozenset(['_event', '_tail', '_workers'])
def __init__(self):
logging.handlers.SysLogHandler.__init__(self, '/dev/log')
self._event = gevent.event.Event()
self._event.set()
self._tail = gevent.queue.Queue()
self._workers = set()
def emit(self, record):
# my syslog is broken and cannot into UTF-8 BOM
record.msg = str(record.msg)
self._tail.put(record)
if self._tail.qsize() != 0:
# in case queue is empty we will spawn new worker
# all workers are logged so we can kill them on close()
self._workers.add(gevent.spawn(self._writer))
def _writer(self):
# here we are locking the queue so we can be sure we are the only one
self._event.wait()
self._event.clear()
while not self._tail.empty():
logging.handlers.SysLogHandler.emit(self, self._tail.get())
self._event.set()
self._workers.remove(gevent.getcurrent())
def close(self):
for worker in self._workers:
gevent.kill(worker)
logging.handlers.SysLogHandler.close(self)
logger = logging.getLogger('squidTag')
logger.setLevel(logging.INFO)
handler = logging.handlers.SysLogHandler('/dev/log')
handler = SysLogHandlerQueue()
handler.setFormatter(logging.Formatter(str('squidTag[%(process)s]: %(message)s')))
logger.addHandler(handler)
# tiny wrapper around a file to make reads from it geventable
# or should i move this somewhere?
class FReadlineQueue(gevent.queue.Queue):
|
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
-
+
+
+
|
if len(self._tail) > 0:
rows[0] = self._tail + rows[0]
# popping out last (incomplete) element
self._tail = rows.pop(-1)
# dropping all complete elements to the queue
for row in rows:
self.put_nowait(row)
logger.info(str('< ' + row))
logger.info('< ' + row)
if len(buf) > 0:
# no EOF, reinstalling event handler
gevent.core.read_event(self._fd.fileno(), self._wait_helper)
else:
# EOF found, sending EOF to queue
self.put_nowait(None)
stdin = FReadlineQueue(sys.stdin)
# wrapper against file handler that makes possible to queue some writes without stalling
class FWritelineQueue(gevent.queue.JoinableQueue):
# storing fileno, io interface, leftover
__slots__ = frozenset(['_fileno', '_io', '_tail'])
def __init__(self, fd, closefd = True):
import io
|
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
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
|
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
354
355
356
357
358
359
360
361
362
363
364
|
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
-
-
-
-
|
# abstract class with basic checking functionality
class Checker(object):
__slots__ = frozenset(['_db', '_log', '_queue', '_request', '_stdout'])
def __init__(self, queue, logger):
self._db = tagDB()
self._log = logger
self._log.info(str('started'))
self._log.info('started')
self._request = re.compile('^([0-9]+)\ (http|ftp):\/\/([-\w.:]+)\/([^ ]*)\ ([0-9.]+)\/(-|[\w\.]+)\ (-|\w+)\ (-|GET|HEAD|POST).*$')
self._queue = queue
self._stdout = FWritelineQueue(sys.stdout, False)
def process(self, id, site, ip_address, url_path, line = None):
#self._log.info(str('trying {}'.format(site)))
#self._log.info('trying {}'.format(site))
result = self._db.check(site, ip_address)
reply = None
#self._log.info(str('got {} lines from database'.format(len(result))))
#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:
self._log.info(str('trying regexp "{}" versus "{}"'.format(row[1], url_path)))
self._log.info('trying regexp "{}" versus "{}"'.format(row[1], url_path))
try:
if re.compile(row[1]).match(url_path):
reply = row[0].format(url_path)
else:
continue
except:
self._log.info(str("can't compile regexp"))
self._log.info("can't compile regexp")
else:
reply = row[0].format(url_path)
if reply != None:
self.writeline('{} {}'.format(id, reply))
return(True)
self.writeline('{}'.format(id))
def check(self):
def loop(self):
while True:
line = self._queue.get()
if line == None:
break
#self._log.info(str('request: ' + line))
#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)
self.process(id, site, ip_address, url_path, line)
else:
self._log.info(str('bad request'))
self._log.info('bad request')
self.writeline(line)
def writeline(self, string):
self._log.info(str('> ' + string))
self._log.info('> ' + string)
self._stdout.put(string)
def loop(self):
pool = gevent.pool.Pool()
pool.spawn(self.check)
pool.join()
if config.options.dump or config.options.load or config.options.dump_conf or config.options.load_conf:
import csv
tagdb = tagDB()
data_fields = ['site', 'tag', 'regexp']
conf_fields = ['netmask', 'redirect_url', 'from_weekday', 'to_weekday', 'from_time', 'to_time', 'tag']
|