@@ -84,19 +84,11 @@ void NetworkHandler::setupWebServer() {
84
84
85
85
std::istringstream deviceStream (deviceMacs);
86
86
std::string device;
87
- std::string devices;
88
87
while (std::getline (deviceStream, device, ' \n ' )) {
89
88
if (device.length () > 0 ) {
90
89
NetworkHandler::deviceMacs.push_back (device);
91
- devices += " <option value=\" " ;
92
- devices += device;
93
- devices += " \" >" ;
94
- devices += device;
95
- devices += " </option>\n " ;
96
90
}
97
91
}
98
- indexHtml = std::regex_replace (indexHtml, std::regex (" \\ $devices" ),
99
- devices.c_str ());
100
92
101
93
webServer.on (" /" , HTTP_GET, onIndexGet);
102
94
webServer.on (" /" , HTTP_POST, onIndexPost);
@@ -151,19 +143,68 @@ void NetworkHandler::setupOTA() {
151
143
152
144
void NetworkHandler::onIndexGet (AsyncWebServerRequest *request) {
153
145
std::string response = indexHtml;
146
+
147
+ std::string devices;
148
+ for (std::string device : deviceMacs) {
149
+ devices += " <option value=\" " ;
150
+ devices += device;
151
+ devices += " \" >\n " ;
152
+ }
153
+ response = std::regex_replace (response, std::regex (" \\ $devices" ),
154
+ devices.c_str ());
155
+
156
+ std::string targets = " <option value=\" " ;
157
+ targets += targetBroadcast.toString ().c_str ();
158
+ targets += " \" >\n " ;
159
+ IPAddress broadcast (255 , 255 , 255 , 255 );
160
+ if (targetBroadcast != broadcast) {
161
+ targets += " <option value=\" " ;
162
+ targets += broadcast.toString ().c_str ();
163
+ targets += " \" >\n " ;
164
+ }
165
+ response = std::regex_replace (response, std::regex (" \\ $targets" ),
166
+ targets.c_str ());
167
+
168
+ response = std::regex_replace (response, std::regex (" \\ $device" ), " " );
154
169
response = std::regex_replace (response, std::regex (" \\ $target" ),
155
170
targetBroadcast.toString ().c_str ());
171
+
156
172
request->send (200 , " text/html" , response.c_str ());
157
173
}
158
174
159
175
void NetworkHandler::onIndexPost (AsyncWebServerRequest *request) {
160
176
if (request->hasParam (" device" , true ) && request->hasParam (" target" , true )) {
161
177
String device = request->getParam (" device" , true )->value ();
162
178
String target = request->getParam (" target" , true )->value ();
163
-
164
179
std::string response = indexHtml;
180
+
181
+ std::string devices;
182
+ for (std::string device : deviceMacs) {
183
+ devices += " <option value=\" " ;
184
+ devices += device;
185
+ devices += " \" >\n " ;
186
+ }
187
+ response = std::regex_replace (response, std::regex (" \\ $devices" ),
188
+ devices.c_str ());
189
+
190
+ std::string targets = " <option value=\" " ;
191
+ targets += targetBroadcast.toString ().c_str ();
192
+ targets += " \" >\n " ;
193
+ IPAddress broadcast (255 , 255 , 255 , 255 );
194
+ if (targetBroadcast != broadcast) {
195
+ targets += " <option value=\" " ;
196
+ targets += broadcast.toString ().c_str ();
197
+ targets += " \" >\n " ;
198
+ }
199
+ response = std::regex_replace (response, std::regex (" \\ $targets" ),
200
+ targets.c_str ());
201
+
165
202
response = std::regex_replace (response, std::regex (" \\ $target" ),
166
203
target.c_str ());
204
+ response = std::regex_replace (response, std::regex (" \\ $device" ),
205
+ device.c_str ());
206
+
207
+ IPAddress originalBroadcastTarget = targetBroadcast;
167
208
168
209
if (!WakeOnLanGenerator::isValidMac (device.c_str ())) {
169
210
std::string message = " error\" >Received invalid device mac address \" " ;
@@ -173,6 +214,7 @@ void NetworkHandler::onIndexPost(AsyncWebServerRequest *request) {
173
214
response = std::regex_replace (response, std::regex (" \\ $message_type\" hidden>\\ $message" ), message.c_str ());
174
215
request->send (400 , " text/html" , response.c_str ());
175
216
} else if (!targetBroadcast.fromString (target)) {
217
+ targetBroadcast = originalBroadcastTarget;
176
218
std::string message = " error\" >Received invalid target IP address \" " ;
177
219
message += target.c_str ();
178
220
message += " \" ." ;
0 commit comments