Make referrer verification exceptions non-fatal
This commit is contained in:
parent
b0a5a53b09
commit
bccfcff19d
|
@ -6,7 +6,8 @@ import time
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
import rophako.jsondb as JsonDB
|
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):
|
def track_visit(request, session):
|
||||||
"""Main logic to track and log visitor details."""
|
"""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.
|
# See if the URL really links back to us.
|
||||||
hostname = server_name()
|
hostname = server_name()
|
||||||
r = requests.get(link)
|
try:
|
||||||
if hostname in r.text:
|
r = requests.get(link,
|
||||||
# Log it.
|
timeout=5,
|
||||||
db = list()
|
verify=False, # Don't do SSL verification
|
||||||
if JsonDB.exists("traffic/referrers"):
|
)
|
||||||
# Don't cache the result -- the list can get huge!
|
if hostname in r.text:
|
||||||
db = JsonDB.get("traffic/referrers", cache=False)
|
# Log it.
|
||||||
db.append(link)
|
db = list()
|
||||||
JsonDB.commit("traffic/referrers", db, cache=False)
|
if JsonDB.exists("traffic/referrers"):
|
||||||
return link
|
# 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
|
return None
|
||||||
|
|
||||||
|
|
|
@ -214,7 +214,7 @@ def handle_exception(error):
|
||||||
|
|
||||||
username = "anonymous"
|
username = "anonymous"
|
||||||
try:
|
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"]
|
username = g.info["session"]["username"]
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
@ -227,7 +227,12 @@ def handle_exception(error):
|
||||||
stacktrace = error + "\n\n" \
|
stacktrace = error + "\n\n" \
|
||||||
+ "==== Start Traceback ====\n" \
|
+ "==== Start Traceback ====\n" \
|
||||||
+ traceback.format_exc() \
|
+ 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
|
# Construct the subject and message
|
||||||
subject = "Internal Server Error on {} - {} - {}".format(
|
subject = "Internal Server Error on {} - {} - {}".format(
|
||||||
|
|
Loading…
Reference in New Issue
Block a user