-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcaptive_portal.py
167 lines (133 loc) · 5.97 KB
/
captive_portal.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# The compiler needs a lot of space to process the server classes etc. so
# import them first before anything else starts to consume memory.
from slim.slim_server import SlimServer
from slim.slim_config import SlimConfig
from slim.fileserver_module import FileserverModule
from slim.web_route_module import WebRouteModule, RegisteredRoute, HttpMethod
from micro_dns_srv import MicroDNSSrv
from shim import join, dirname
import network
import select
import logging
from schedule import Scheduler, CancelJob
_logger = logging.getLogger("captive_portal")
# Rather than present a login page, this is a captive portal that lets you set up
# access to your network. See docs/captive-portal.md for more about captive portals.
class CaptivePortal:
def run(self, essid, connect):
self._schedule = Scheduler()
self._connect = connect
self._alive = True
self._timeout_job = None
self._ap = network.WLAN(network.AP_IF)
self._ap.active(True)
self._ap.config(essid=essid) # You can't set values before calling active(...).
poller = select.poll()
addr = self._ap.ifconfig()[0]
slim_server = self._create_slim_server(poller, essid)
dns = self._create_dns(poller, addr)
_logger.info("captive portal web server and DNS started on %s", addr)
# If no timeout is given `ipoll` blocks and the for-loop goes forever.
# With a timeout the for-loop exits every time the timeout expires.
# I.e. the underlying iterable reports that it has no more elements.
while self._alive:
# Under the covers polling is done with a non-blocking ioctl call and the timeout
# (or blocking forever) is implemented with a hard loop, so there's nothing to be
# gained (e.g. reduced power consumption) by using a timeout greater than 0.
for (s, event) in poller.ipoll(0):
# If event has bits other than POLLIN or POLLOUT then print it.
if event & ~(select.POLLIN | select.POLLOUT):
self._print_select_event(event)
slim_server.pump(s, event)
dns.pump(s, event)
slim_server.pump_expire() # Expire inactive client sockets.
self._schedule.run_pending()
slim_server.shutdown(poller)
dns.shutdown(poller)
self._ap.active(False)
def _create_slim_server(self, poller, essid):
# See the captive portal notes in docs/captive-portal.md for why we redirect not-found
# URLs and why we redirect them to an absolute URL (rather than a path like "/").
# `essid` is used as the target host but any name could be used, e.g. "wifi-setup".
config = SlimConfig(not_found_url="http://{}/".format(essid))
slim_server = SlimServer(poller, config=config)
# fmt: off
slim_server.add_module(WebRouteModule([
RegisteredRoute(HttpMethod.GET, "/api/access-points", self._request_access_points),
RegisteredRoute(HttpMethod.POST, "/api/access-point", self._request_access_point),
RegisteredRoute(HttpMethod.POST, "/api/alive", self._request_alive)
]))
# fmt: on
root = self._get_relative("www")
# fmt: off
slim_server.add_module(FileserverModule({
"html": "text/html",
"css": "text/css",
"js": "application/javascript",
"woff2": "font/woff2",
"ico": "image/x-icon",
"svg": "image/svg+xml"
}, root))
# fmt: on
return slim_server
# Find a file, given a path relative to the directory contain this `.py` file.
@staticmethod
def _get_relative(filename):
return join(dirname(__file__), filename)
@staticmethod
def _create_dns(poller, addr):
addr_bytes = MicroDNSSrv.ipV4StrToBytes(addr)
def resolve(name):
_logger.info("resolving %s", name)
return addr_bytes
return MicroDNSSrv(resolve, poller)
def _request_access_points(self, request):
# Tuples are of the form (SSID, BSSID, channel, RSSI, authmode, hidden).
points = [(p[0], p[3], p[4]) for p in self._ap.scan()]
request.Response.ReturnOkJSON(points)
def _request_access_point(self, request):
data = request.GetPostedURLEncodedForm()
_logger.debug("connect request data %s", data)
ssid = data.get("ssid", None)
if not ssid:
request.Response.ReturnBadRequest()
return
password = data.get("password", None)
result = self._connect(ssid, password)
if not result:
request.Response.ReturnForbidden()
else:
request.Response.ReturnOkJSON({"message": result})
def _request_alive(self, request):
data = request.GetPostedURLEncodedForm()
timeout = data.get("timeout", None)
if not timeout:
request.Response.ReturnBadRequest()
return
_logger.debug("timeout %s", timeout)
timeout = int(timeout) + self._TOLERANCE
if self._timeout_job:
self._schedule.cancel_job(self._timeout_job)
self._timeout_job = self._schedule.every(timeout).seconds.do(self._timed_out)
request.Response.Return(self._NO_CONTENT)
# If a client specifies a keep-alive period of Xs then they must ping again within Xs plus a fixed "tolerance".
_TOLERANCE = 1
_NO_CONTENT = 204
def _timed_out(self):
_logger.info("keep-alive timeout expired.")
self._alive = False
self._timeout_job = None
return CancelJob # Tell scheduler that we want one-shot behavior.
_POLL_EVENTS = {
select.POLLIN: "IN",
select.POLLOUT: "OUT",
select.POLLHUP: "HUP",
select.POLLERR: "ERR",
}
def _print_select_event(self, event):
mask = 1
while event:
if event & 1:
_logger.info("event %s", self._POLL_EVENTS.get(mask, mask))
event >>= 1
mask <<= 1