Created
October 22, 2015 09:58
-
-
Save anonymous/d02c6438813add79a387 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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