Skip to content

Instantly share code, notes, and snippets.

Created October 22, 2015 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/d02c6438813add79a387 to your computer and use it in GitHub Desktop.
Save anonymous/d02c6438813add79a387 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
from flask import Flask, make_response, request
import base64
import datetime
import hashlib
import ujson
import time
import urllib
img = base64.b64decode(b'R0lGODlhAQABAIABAAAAAP///yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==')
class Session(object):
def __init__(self, request):
self.request = request
self.expire_date = datetime.datetime.now()
self.expire_date = self.expire_date + datetime.timedelta(days=15*365)
def make_id(self):
s = request.headers.getlist("X-Forwarded-For")[0] + str(self.expire_date.strftime("%Y%m%d%H%M%S"))
return(hashlib.sha1(s.encode("utf-8")).hexdigest())
app = Flask(__name__)
@app.route("/")
def index():
response = make_response(img)
response.headers['Content-Type'] = 'image/gif'
response.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0'
response.headers['Pragma'] = 'no-cache'
response.headers['Expires'] = '-1'
if request.cookies.get("sid") == None:
session = Session(request)
sid = session.make_id()
response.set_cookie('sid',
value=sid)
else:
sid = request.cookies.get('sid')
if request.cookies.get("cid") == None:
session = Session(request)
cid = session.make_id()
response.set_cookie('cid',
value=cid,
expires=session.expire_date)
else:
cid = request.cookies.get('cid')
data = {"site": request.args.get("site"),
"url": urllib.parse.unquote(request.referrer),
"ip": request.headers.getlist("X-Forwarded-For")[0],
"user-agent": request.headers.get("User-Agent"),
"date": datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S"),
"unixtime": time.time(),
"referrer": request.args.get("referrer"),
"sid": sid,
"cid": cid}
print(data)
f = open("data/" + datetime.datetime.now().strftime("%Y%m%d"), "a")
f.write(ujson.dumps(data)+"\n")
f.close()
return response
if __name__ == "__main__":
app.run(host="0.0.0.0", port=6000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment