-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWifiPortalPage.cpp
More file actions
175 lines (151 loc) · 4.43 KB
/
Copy pathWifiPortalPage.cpp
File metadata and controls
175 lines (151 loc) · 4.43 KB
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
#include "app/WifiPortalPage.h"
#include <algorithm>
#include <cctype>
#include <sstream>
namespace app
{
namespace
{
std::string trimCopy(const std::string &input)
{
std::size_t start = 0;
while (start < input.size() && std::isspace(static_cast<unsigned char>(input[start])))
{
++start;
}
std::size_t end = input.size();
while (end > start && std::isspace(static_cast<unsigned char>(input[end - 1])))
{
--end;
}
return input.substr(start, end - start);
}
bool isNineDigitCityCode(const std::string &input)
{
if (input.size() != 9)
{
return false;
}
return std::all_of(input.begin(), input.end(), [](char ch) {
return std::isdigit(static_cast<unsigned char>(ch)) != 0;
});
}
std::string escapeHtml(const std::string &input)
{
std::string escaped;
escaped.reserve(input.size() + 16);
for (char ch : input)
{
switch (ch)
{
case '&':
escaped += "&";
break;
case '<':
escaped += "<";
break;
case '>':
escaped += ">";
break;
case '"':
escaped += """;
break;
case '\'':
escaped += "'";
break;
default:
escaped.push_back(ch);
break;
}
}
return escaped;
}
} // namespace
std::string normalizeCityCodeInput(const std::string &input)
{
const std::string trimmed = trimCopy(input);
if (trimmed.empty() || trimmed == "0")
{
return "";
}
return isNineDigitCityCode(trimmed) ? trimmed : "";
}
std::string normalizeRemoteBaseUrlInput(const std::string &input)
{
std::string trimmed = trimCopy(input);
while (!trimmed.empty() && trimmed[trimmed.size() - 1] == '/')
{
trimmed.erase(trimmed.size() - 1);
}
if (trimmed.find("http://") != 0)
{
return "";
}
return trimmed;
}
std::string buildWifiPortalPage(const std::string &apSsid,
const std::string &portalIp,
const std::vector<WifiPortalNetwork> &networks,
const std::string &selectedSsid,
const std::string &remoteBaseUrl,
const std::string &deviceId,
const std::string &message)
{
std::ostringstream html;
html << "<!doctype html><html><head>"
<< "<meta name='viewport' content='width=device-width,initial-scale=1'>"
<< "<title>SmallDesktopDisplay WiFi Setup</title>"
<< "</head><body style='font-family:sans-serif;background:#111;color:#eee;padding:16px'>"
<< "<h2>SmallDesktopDisplay WiFi Setup</h2>";
if (!apSsid.empty())
{
html << "<p>Connect to AP <b>" << escapeHtml(apSsid) << "</b> and open <b>"
<< escapeHtml(portalIp) << "</b>.</p>";
}
else
{
html << "<p>Open <b>" << escapeHtml(portalIp)
<< "</b> from any device on the same LAN.</p>";
}
if (!message.empty())
{
html << "<p><b>" << escapeHtml(message) << "</b></p>";
}
html << "<h3>Nearby WiFi</h3><ul>";
if (networks.empty())
{
html << "<li>No scan results yet</li>";
}
else
{
for (const auto &network : networks)
{
html << "<li><button type='button' data-ssid='" << escapeHtml(network.ssid)
<< "' onclick=\"document.getElementById('ssid').value=this.dataset.ssid\">"
<< escapeHtml(network.ssid) << "</button> "
<< "(" << network.rssi << " dBm";
if (network.encrypted)
{
html << ", lock";
}
else
{
html << ", open";
}
html << ")</li>";
}
}
html << "</ul>";
html << "<form method='post' action='/save'>"
<< "<p><label>SSID<br><input id='ssid' name='ssid' maxlength='31' style='width:100%;padding:10px' value='"
<< escapeHtml(selectedSsid) << "'></label></p>"
<< "<p><label>Password<br><input name='psk' type='password' maxlength='63' style='width:100%;padding:10px'></label></p>"
<< "<p><label>Render server<br><input name='RemoteBaseUrl' maxlength='95' style='width:100%;padding:10px' placeholder='http://192.168.1.20:8080' value='"
<< escapeHtml(remoteBaseUrl) << "'></label></p>"
<< "<p><label>Device ID<br><input name='DeviceId' maxlength='23' style='width:100%;padding:10px' value='"
<< escapeHtml(deviceId) << "'></label></p>"
<< "<p><button type='submit' style='padding:10px 14px'>Save and Restart</button></p>"
<< "</form></body></html>";
return html.str();
}
} // namespace app