-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathviews.py
88 lines (71 loc) · 3.57 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import logging
import arpreq
from flask import render_template, request
from flask_babel import _, lazy_gettext
from hades.common.db import get_groups, get_latest_auth_attempt
from hades.portal import app, babel
logger = logging.getLogger(__name__)
messages = {
'traffic': lazy_gettext("You've exceeded your traffic limit."),
'violation': lazy_gettext("You violated our Terms of Services. "
"Please contact our support."),
'security': lazy_gettext("We had to block you due to security problems "
"with your devices. "
"Please contact our support."),
'default_in_payment': lazy_gettext(
"You are late with paying your fees. "
"Please pay the outstanding fees. "
"To regain network connectivity immediately, inform our support. "
"Otherwise your account will be re-enabled as soon as your money "
"arrived on our bank account."),
'wrong_port': lazy_gettext("According to our records you live in a "
"different room. "
"Please inform us of your relocation."),
'unknown': lazy_gettext("We don't recognize your MAC address. "
"If you are already a AG DSN member, you are "
"probably using a different device, please tell us "
"its MAC address. "
"If you are not a member, please apply."),
'membership_ended': lazy_gettext("Your current membership status does not "
"allow network access. "
"Please contact our support.")
}
@babel.localeselector
def get_locale():
return request.accept_languages.best_match(['de', 'en'])
@app.route("/")
def index():
ip = request.remote_addr
try:
mac = arpreq.arpreq(ip)
except OSError as e:
logger.exception("Could not resolve IP {} into MAC: {}".format(ip, e))
content = render_template("error.html",
message=_("An unexpected error occurred "
"while resolving your IP address "
"into a MAC address."))
return content, 500
if mac is None:
content = render_template("error.html",
error=_("No MAC address could be found for "
"your IP address {}".format(ip)))
return content, 500
mac_groups = list(get_groups(mac))
if not mac_groups:
return render_template("status.html", reasons=[messages['unknown']],
mac=mac, show_mac=True)
latest_auth_attempt = get_latest_auth_attempt(mac)
if not latest_auth_attempt:
content = render_template("error.html",
error=_("No authentication attempt found for "
"your MAC address."))
return content, 500
nas_ip_address, nas_port_id, groups, reply, auth_date = latest_auth_attempt
port_groups = [group for nai, npi, group in mac_groups
if nas_ip_address == nai and nas_port_id == npi]
if not port_groups:
return render_template("status.html", reasons=[messages['wrong_port']],
mac=mac, show_mac=False)
reasons = [messages[group] for group in port_groups if group in messages]
return render_template("status.html", reasons=reasons,
mac=mac, show_mac=False)