blockinator/sqlite.py
Scott Wallace 5ce554fc48 Removed cookie checking -- deprecated.
Added better NULL input handling.
Altered the logging levels for blocklist matches and module initialisation.
2012-11-08 14:03:38 +00:00

42 lines
1.3 KiB
Python
Executable file

#!/usr/bin/python
import blocklist
import sys
import logging
if sys.version_info >= (2,5):
import sqlite3
else:
from pysqlite2 import dbapi2 as sqlite3
class SQLiteBlockList(blocklist.BlockList):
"""New class to extend the main BlockList class for implementation with Varnish."""
def export(self):
"""Exports blocklist criteria to a SQLite file."""
try:
db = sqlite3.connect(self.config.get("sqlite", "database"))
cur = db.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS blocklist(remote_ip VARCHAR(15), forwarded_ip VARCHAR(15), useragent VARCHAR(256), cookie VARCHAR(1024), PRIMARY KEY(remote_ip))")
cur.execute("DELETE FROM blocklist")
for key,item in self.data.iteritems():
if item["useragent"] == 'NULL':
item["useragent"] = '(null)'
cur.execute("INSERT INTO blocklist VALUES ('%s', '%s', '%s', '%s')" % (item["remote_ip"], item["forwarded_ip"], item["useragent"], item["cookie"]))
db.commit()
cur.close()
except Exception, e:
logging.error("There was a problem exporting the data to SQLite. %s" % e)
if __name__ == "__main__":
blocklist = SQLiteBlockList()
blocklist.read("http://example.com/default/blocks.txt")
blocklist.export()
sys.exit(0)