Make referrer verification exceptions non-fatal

This commit is contained in:
Noah 2014-12-05 22:33:42 +00:00
parent b0a5a53b09
commit bccfcff19d
2 changed files with 26 additions and 13 deletions

View File

@ -6,7 +6,8 @@ import time
import requests
import rophako.jsondb as JsonDB
from rophako.utils import remote_addr, pretty_time, server_name
from rophako.utils import (remote_addr, pretty_time, server_name,
handle_exception)
def track_visit(request, session):
"""Main logic to track and log visitor details."""
@ -91,16 +92,23 @@ def log_referrer(request, link):
# See if the URL really links back to us.
hostname = server_name()
r = requests.get(link)
if hostname in r.text:
# Log it.
db = list()
if JsonDB.exists("traffic/referrers"):
# Don't cache the result -- the list can get huge!
db = JsonDB.get("traffic/referrers", cache=False)
db.append(link)
JsonDB.commit("traffic/referrers", db, cache=False)
return link
try:
r = requests.get(link,
timeout=5,
verify=False, # Don't do SSL verification
)
if hostname in r.text:
# Log it.
db = list()
if JsonDB.exists("traffic/referrers"):
# Don't cache the result -- the list can get huge!
db = JsonDB.get("traffic/referrers", cache=False)
db.append(link)
JsonDB.commit("traffic/referrers", db, cache=False)
return link
except Exception as e:
handle_exception(e)
return None
return None

View File

@ -214,7 +214,7 @@ def handle_exception(error):
username = "anonymous"
try:
if "username" in g.info["session"]:
if hasattr(g, "info") and "session" in g.info and "username" in g.info["session"]:
username = g.info["session"]["username"]
except:
pass
@ -227,7 +227,12 @@ def handle_exception(error):
stacktrace = error + "\n\n" \
+ "==== Start Traceback ====\n" \
+ traceback.format_exc() \
+ "==== End Traceback ====\n"
+ "==== End Traceback ====\n\n" \
+ "Request Information\n" \
+ "-------------------\n" \
+ "Address: " + remote_addr() + "\n" \
+ "User Agent: " + request.user_agent.string + "\n" \
+ "Referrer: " + request.referrer
# Construct the subject and message
subject = "Internal Server Error on {} - {} - {}".format(