-
Notifications
You must be signed in to change notification settings - Fork 0
/
CaptivePortal.html
295 lines (257 loc) · 7.48 KB
/
CaptivePortal.html
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
</head>
<script type="text/javascript">
var ws;
var CurrentPvd = null;
var CurrentPvdList = [];
var AllAttributes = {};
var PvdAttributesContent;
var attributesRetrieved = {};
var pvdWithPortal = [];
var pvdPortal = {};
var pvdName = {};
var pvdHasInternet = {};
var allPvd = [];
var currentSelected = null;
const DONE_GREEN = '<i class="material-icons" style="font-size:48px;color:green">done</i>';
function GetPvdElement(Pvd, prefix = "pvd") {
return(Pvd == null ?
null :
document.getElementById(prefix + "-" + Pvd));
}
function ActivePvD(Pvd) {
if ((Item = GetPvdElement(Pvd, "label")) != null) {
Item.setAttribute(
"style",
"display:block; width:100%; font-style:normal; font-weight:normal; color:black");
}
}
function SetClickAuthenticate(Pvd) {
if (pvdPortal[Pvd] == null) {
return;
}
if ((Item = GetPvdElement(Pvd)) != null) {
Item.addEventListener("click", function(e) {
window.open(pvdPortal[Pvd]);
if ((Item = GetPvdElement(Pvd, "auth-status")) != null) {
Item.innerHTML = DONE_GREEN;
}
}, false);
}
}
function SetSelectStatus(Pvd, st) {
if ((Item = GetPvdElement(Pvd, "select-status")) != null) {
Item.innerHTML = st;
}
}
function TickSelected(Pvd) {
SetSelectStatus(Pvd, DONE_GREEN);
}
function SetClickSelect(Pvd) {
if ((Item = GetPvdElement(Pvd, "select")) != null) {
Item.addEventListener("click", function(e) {
allPvd.forEach(function(Pvd2) {
if (Pvd2 != Pvd) {
SetSelectStatus(Pvd2, ".");
}
});
TickSelected(Pvd);
currentSelected = Pvd;
SendWs("PVD_SELECT_PVD " + Pvd);
}, false);
}
}
function HandleMessage(m) {
if (m == null) {
return;
}
if (m.what == "pvdList") {
var pvdList = m.payload.pvdList;
console.log("pvdList : " + pvdList);
attributesRetrieved = {};
pvdWithPortal = [];
pvdPortal = {};
allPvd = [];
pvdList.forEach(function(Pvd) {
attributesRetrieved[Pvd] = false;
});
document.getElementById("pvdList").innerHTML = "<table/>";
console.log("New pvd list : ", attributesRetrieved);
return;
}
if (m.what == "pvdAttributes") {
var Pvd = m.payload.pvd;
var Attributes = m.payload.pvdAttributes;
if (Pvd == null || Attributes == null) {
return;
}
console.log("Attributes for " + Pvd + " : " + JSON.stringify(Attributes, null, "\t"));
pvdName[Pvd] =
Attributes.extraInfo == null ? Pvd :
Attributes.extraInfo.name == null ? Pvd :
Attributes.extraInfo.name;
pvdHasInternet[Pvd] =
Attributes.extraInfo == null ? true :
! Attributes.extraInfo.noInternet;
var PortalURL =
Attributes.captivePortalURL ||
(Attributes.extraInfo == null ? null :
Attributes.extraInfo.captivePortalURL);
if (allPvd.indexOf(Pvd) == -1) {
allPvd.push(Pvd);
}
if (PortalURL != null) {
if (pvdPortal[Pvd] == null) {
pvdWithPortal.push(Pvd);
}
console.log("Portal for " + Pvd + " received : " + pvdPortal[Pvd]);
console.log("pvdWithPortal : ", pvdWithPortal);
}
else {
console.log("No portal for " + Pvd);
}
pvdPortal[Pvd] = PortalURL;
delete(attributesRetrieved[Pvd]);
console.log("attributesRetrieved : ", attributesRetrieved);
if (Object.keys(attributesRetrieved).length != 0) {
return;
}
console.log("All expected pvd received");
// All attributes have been retrieved : the list of
// PvD with a captive portal URL is stored in
// pvdWithPortal. Let's build the html table
var MyTable = "<table>" +
"<tr>" +
"<th>PvD Name</th>" +
"<th></th><td></th>" +
"<th>Authenticated</th>" +
"<th>Selected</th>" +
"<th>Internet access</th>" +
"</tr>" ;
allPvd.forEach(function(Pvd) {
var AuthHTML;
if (pvdPortal[Pvd] == null) {
AuthHTML =
"<td>" +
"<label type='label'> </label>" +
"</td>";
}
else {
AuthHTML =
"<td>" +
"<button type='button' id='pvd-" + Pvd + "'>" +
"Authenticate" +
"</button>" +
"</td>";
}
MyTable +=
"<tr>" +
"<td>" +
"<button type='label' id='label-" + Pvd + "'>" +
pvdName[Pvd] +
"</button>" +
"</td>" +
AuthHTML +
"<td>" +
"<button type='button' id='select-" + Pvd + "'>" +
"Select" +
"</button>" +
"</td>" +
'<td style="text-align: center;">' +
"<label type='label' id='auth-status-" + Pvd + "'>" +
(pvdPortal[Pvd] ?
'<i class="material-icons" style="font-size:48px;color:red">done</i>' :
'<i class="material-icons" style="font-size:48px;color:green">done</i>' ) +
"</label>" +
"</td>" +
'<td style="text-align: center;">' +
"<label type='label' id='select-status-" + Pvd + "'>" +
"." +
"</label>" +
"</td>" +
'<td style="text-align: center;">' +
"<label type='label' id='internet-status-" + Pvd + "'>" +
(pvdHasInternet[Pvd] ?
'<i class="material-icons" style="font-size:48px;color:green">cloud_queue</i>' :
'<i class="material-icons" style="font-size:48px;color:red">cloud_off</i>') +
"</label>" +
"</td>" +
"</tr>";
});
MyTable += "</table>";
document.getElementById("pvdList").innerHTML = MyTable;
// Set the attributes of the various PvD HTML items
// and attach callbacks
allPvd.forEach(function(Pvd) {
ActivePvD(Pvd);
SetClickAuthenticate(Pvd);
SetClickSelect(Pvd);
if (currentSelected == Pvd) {
TickSelected(currentSelected);
}
});
}
}
function SendWs(m) {
ws.send(m);
}
function GetJson(m) {
try {
return(JSON.parse(m));
}
catch (e) {
return(null);
}
}
// Attempt to perform the websocket connection. If it
// fails or if the connection is lost, a new attempt
// is done 1 second later
var ConnectionSucceeded = true; // true : not a mistake. Used to limit traces
function WSConnect(WSServer) {
if (ws == null) {
ws = new WebSocket(WSServer);
ws.onopen = function(ev) {
console.log("websocket connection established");
SendWs("PVD_GET_LIST");
SendWs("PVD_GET_ATTRIBUTES");
ConnectionSucceeded = true;
};
ws.onclose = function(ev) {
if (ConnectionSucceeded) {
console.log("websocket disconnected");
}
ConnectionSucceeded = false;
ws = null;
};
ws.onmessage = function(ev) {
HandleMessage(GetJson(ev.data));
};
ws.onerror = function(ev) {
if (ConnectionSucceeded) {
console.log("Error establishing websocket connection");
}
ConnectionSucceeded = false;
ws = null;
};
}
setTimeout(WSConnect, 1000, WSServer);
}
document.addEventListener("DOMContentLoaded", function() {
PvdAttributesTitle = document.getElementById("pvdAttributesLabel");
PvdAttributesContent = document.getElementById("pvdAttributes");
WSConnect("ws://" + window.location.host);
});
</script>
<body>
<div align="center" style="font-style:normal; font-weight:bold; background-color: blue; color:white">
<p style="display:inline-block" >Provisioning Domains and Captive Portals Demonstration</p>
</div>
<table width=100% height=100%>
<tr style="vertical-align:top" width=100%>
<td id="pvdList" align="left" style="background-color: AntiqueWhite" width=15% />
</tr>
</body>
</html>