diff --git a/netdb.i2p2/fixedapp.py b/netdb.i2p2/fixedapp.py new file mode 100644 index 00000000..5d11dcb2 --- /dev/null +++ b/netdb.i2p2/fixedapp.py @@ -0,0 +1,203 @@ +# +# Required software: +# sqlite3 3.6.23.1 or newer +# http://www.sqlite.org/ +# unixodbc +# http://www.unixodbc.org/ +# pyodbc-3.0.6 or newer +# http://www.ch-werner.de/sqliteodbc/ +# werkzeug +# http://werkzeug.pocoo.org/ +# python 2.6 or 2.7 +# http://python.org/ +# + + +# Modify as needed, or use a symlink. +netdbdir = 'netdb' +database = 'Driver=SQLite;DATABASE=I2PnetDb' + + +from werkzeug import BaseRequest, BaseResponse, ETagResponseMixin, escape, SharedDataMiddleware +from werkzeug.exceptions import HTTPException +import os +from time import time, gmtime +import calendar +#import time +from random import choice +import pyodbc + + +# +# Not needed? +# +#import sha + + +# db format +# +# table client ipaddress, time of access, file list index +# +# table html index, time of creation, html + + +# +# Init database if it does not exist. +# + +def checkdatabase(): + cnxn = pyodbc.connect(database) + cur = cnxn.cursor() + + try: + cur.execute("select * from client") + except pyodbc.Error: + #cur.execute("create table peer (ip string, time string, index string)") + print ("Creating new table 'client'") + cur.execute("create table client (ip string, whn float)") + cnxn.commit() + try: + cur.execute("select * from entry") + except pyodbc.Error: + #cur.execute("create table peer (ip string, time string, index string)") + print ("Creating new table 'entry'") + cur.execute("create table entry (whn float, wht string)") + cnxn.commit() + cnxn.close() + + + + +# try: +# cur.execute("select * from html") +# except pyodbc.Error: +# cur.execute("create table client (index, time, html)") + + +# cur.execute("insert into foo values(1, 'Me')") +# cnxn.commit() +# cur.execute("select * from foo") +# cnxn.close() + +class Request(BaseRequest): + """Useful subclass of the default request that knows how to build urls.""" + + def __init__(self, environ): + BaseRequest.__init__(self, environ) + + +class Response(BaseResponse, ETagResponseMixin): + """Subclass of base response that has a default mimetype of text/html.""" + default_mimetype = 'text/html' + +def app(environ, start_response): + """The WSGI application that connects all together.""" + req = Request(environ) + remote = req.remote_addr + now = time() + old = now - 3600 + oldest = now - 7200 + t = gmtime(now) + nowtag = calendar.timegm((t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, 0, 0, t.tm_wday, t.tm_yday, t.tm_isdst)) + + cnxn = pyodbc.connect(database) + cur = cnxn.cursor() + # + # Prune database from peers that are over 1 hour old + # + cur.execute("delete from client where whn < ?", old) + cnxn.commit() + # + # Prune database from entries that are over 2 hours old + # + cur.execute("delete from entry where whn < ?", oldest) + cnxn.commit() + + # + # Get peer info + # + cur.execute("select * from client where ip = ?", remote) + info = cur.fetchall() + # page + path = req.path[1:] + + + if path == '': + page = u'