8
8
#include < WiFiClient.h>
9
9
#include < WebServer.h>
10
10
11
+ #include < ESPmDNS.h>
12
+ #include < DNSServer.h>
13
+
11
14
#include " webUI/index.h"
12
15
#include " SH1106Wire.h"
13
16
#include " Nugget_Interface.h"
@@ -38,67 +41,80 @@ WebServer server(80);
38
41
TaskHandle_t webapp;
39
42
TaskHandle_t nuggweb;
40
43
41
- void getPayloads () {
42
- String* payloadPaths = RubberNugget::allPayloadPaths ();
44
+ void getPayloads ()
45
+ {
46
+ String *payloadPaths = RubberNugget::allPayloadPaths ();
43
47
Serial.printf (" [SERVER][getPayloads] %s\n " , payloadPaths->c_str ());
44
48
server.send (200 , " text/plain" , *payloadPaths);
45
49
}
46
50
47
- void handleRoot () {
51
+ void handleRoot ()
52
+ {
48
53
Serial.println (" handling root!" );
49
54
server.send (200 , " text/html" , String (INDEX));
50
55
}
51
56
52
- void delpayload () {
57
+ void delpayload ()
58
+ {
53
59
String path (server.arg (" path" ));
54
60
FRESULT res = f_unlink (path.c_str ());
55
- if (res == FR_OK){
61
+ if (res == FR_OK)
62
+ {
56
63
server.send (200 );
57
- } else {
64
+ }
65
+ else
66
+ {
58
67
server.send (500 );
59
68
}
60
69
}
61
70
62
- void websave () {
71
+ void websave ()
72
+ {
63
73
// path should be corrected, i.e. by index.html's pathCorrector function
64
74
// Each path should start with '/' and not end with '/'
65
75
String path = (server.arg (" path" ));
66
- const char * constPath = path.c_str ();
76
+ const char * constPath = path.c_str ();
67
77
68
78
// construct directories as needed
69
79
FILINFO filinfo;
70
80
int pathSoFarIdx = 1 ;
71
- while (true ) {
81
+ while (true )
82
+ {
72
83
int nextDir = path.indexOf (" /" , pathSoFarIdx);
73
- if (nextDir == -1 ){
84
+ if (nextDir == -1 )
85
+ {
74
86
break ;
75
87
}
76
88
String pathSoFar = path.substring (0 , nextDir);
77
- if (FR_OK != f_stat (pathSoFar.c_str (), &filinfo)){
78
- if (f_mkdir (pathSoFar.c_str ()) != FR_OK) {
89
+ if (FR_OK != f_stat (pathSoFar.c_str (), &filinfo))
90
+ {
91
+ if (f_mkdir (pathSoFar.c_str ()) != FR_OK)
92
+ {
79
93
server.send (500 , " text/plain" , " Could not create directory" );
80
94
return ;
81
95
}
82
96
}
83
- pathSoFarIdx = nextDir+ 1 ;
97
+ pathSoFarIdx = nextDir + 1 ;
84
98
}
85
99
86
100
// Create file
87
101
FIL file;
88
- if (FR_OK != f_open (&file, constPath, FA_WRITE | FA_CREATE_ALWAYS)){
102
+ if (FR_OK != f_open (&file, constPath, FA_WRITE | FA_CREATE_ALWAYS))
103
+ {
89
104
server.send (500 , " text/plain" , " Could not open file for writing" );
90
105
return ;
91
106
}
92
107
93
108
// Write to file
94
109
String content = (server.arg (" payloadText" ));
95
110
content.replace (" " , " /" ); // why
96
- const char * contentBase64 = content.c_str ();
111
+ const char * contentBase64 = content.c_str ();
97
112
size_t payloadLength = BASE64::decodeLength (contentBase64);
98
113
uint8_t payloadContent[payloadLength];
99
114
BASE64::decode (contentBase64, payloadContent);
100
115
UINT written = 0 ;
101
- if (FR_OK != f_write (&file, payloadContent, payloadLength, &written)){
116
+ if (FR_OK != f_write (&file, payloadContent, payloadLength, &written))
117
+ {
102
118
server.send (500 , " text/plain" , " Could not write to file" );
103
119
f_close (&file);
104
120
return ;
@@ -108,15 +124,18 @@ void websave() {
108
124
}
109
125
110
126
// decode base64 and run
111
- void webrunlive () {
127
+ void webrunlive ()
128
+ {
112
129
server.send (200 , " text/plain" , " Running payload..." );
113
- if (server.hasArg (" plain" )) {
130
+ if (server.hasArg (" plain" ))
131
+ {
114
132
Serial.print (" Decoding: " );
115
133
String decoded = (server.arg (" plain" ));
116
134
char tab2[decoded.length () + 1 ];
117
135
strcpy (tab2, decoded.c_str ());
118
136
119
- for (int i = 0 ; i < decoded.length (); i++) {
137
+ for (int i = 0 ; i < decoded.length (); i++)
138
+ {
120
139
Serial.print (tab2[i]);
121
140
}
122
141
@@ -128,15 +147,16 @@ void webrunlive() {
128
147
Serial.println (BASE64::decodeLength (tab2));
129
148
BASE64::decode (tab2, raw);
130
149
131
- String meow = (char *) raw;
132
- meow = meow.substring (0 , (int ) BASE64::decodeLength (tab2));
150
+ String meow = (char *) raw;
151
+ meow = meow.substring (0 , (int )BASE64::decodeLength (tab2));
133
152
RubberNugget::runLivePayload (meow);
134
153
Serial.println ();
135
154
Serial.println (" -------" );
136
155
}
137
156
}
138
157
139
- void webget () {
158
+ void webget ()
159
+ {
140
160
FRESULT fr;
141
161
FIL file;
142
162
uint16_t size;
@@ -145,40 +165,49 @@ void webget() {
145
165
String path = server.arg (" path" );
146
166
fr = f_open (&file, path.c_str (), FA_READ);
147
167
148
- if (fr != FR_OK) {
168
+ if (fr != FR_OK)
169
+ {
149
170
// TODO: most likely file not found, but we need to check why fr != OK.
150
171
// Marking 500 until resolved
151
172
server.send (500 , " plain/text" , String (" Error loading script" ));
152
173
return ;
153
174
}
154
175
155
176
size = f_size (&file);
156
- char * data = NULL ;
177
+ char *data = NULL ;
157
178
158
- data = (char *) malloc (size);
179
+ data = (char *) malloc (size);
159
180
160
- fr = f_read (&file, data, (UINT) size, &bytesRead);
161
- if (fr == FR_OK) {
181
+ fr = f_read (&file, data, (UINT)size, &bytesRead);
182
+ if (fr == FR_OK)
183
+ {
162
184
String payload = String (data);
163
185
payload = payload.substring (0 , bytesRead);
164
186
payload = base64::encode (payload);
165
187
server.send (200 , " plain/text" , payload);
166
- } else {
188
+ }
189
+ else
190
+ {
167
191
server.send (500 , " plain/text" , String (" Error reading script" ));
168
192
}
169
193
f_close (&file);
170
-
171
194
}
172
195
173
196
// run payload with get request path
174
- void webrun () {
197
+ void webrun ()
198
+ {
175
199
server.send (200 , " text/html" , " Running payload..." );
176
200
String path = server.arg (" path" );
177
201
RubberNugget::runPayload (path.c_str (), 1 ); // provide parameter triggered from webpage
178
202
}
179
203
180
- void webserverInit (void *p) {
181
- while (1 ) {
204
+ DNSServer dns;
205
+
206
+ void webserverInit (void *p)
207
+ {
208
+ while (1 )
209
+ {
210
+ dns.processNextRequest ();
182
211
server.handleClient ();
183
212
vTaskDelay (2 );
184
213
}
@@ -187,32 +216,44 @@ void webserverInit(void *p) {
187
216
extern String netPassword;
188
217
extern String networkName;
189
218
190
- void setup () {
191
- pinMode (12 , OUTPUT);
192
- strip.begin ();
219
+
220
+
221
+ void setup ()
222
+ {
223
+ pinMode (12 , OUTPUT);
224
+ strip.begin ();
193
225
delay (500 );
194
226
195
227
Serial.begin (115200 );
196
228
197
229
RubberNugget::init ();
198
-
199
- if (networkName.length () >0 ) {
230
+
231
+ if (networkName.length () > 0 )
232
+ {
200
233
Serial.println (networkName);
201
- const char * c = networkName.c_str ();
202
- ssid= c;
234
+ const char *c = networkName.c_str ();
235
+ ssid = c;
203
236
}
204
- if (netPassword.length () >=8 ) {
237
+ if (netPassword.length () >= 8 )
238
+ {
205
239
Serial.println (netPassword);
206
- const char * d = netPassword.c_str ();
207
- password= d;
240
+ const char *d = netPassword.c_str ();
241
+ password = d;
208
242
}
209
243
210
244
WiFi.softAP (ssid, password);
211
- // }
245
+ // }
212
246
IPAddress myIP = WiFi.softAPIP ();
213
247
Serial.print (" AP IP address: " );
214
248
Serial.println (myIP);
215
249
250
+
251
+ dns.setErrorReplyCode (DNSReplyCode::NoError);
252
+ dns.start (53 , " *" , myIP);
253
+
254
+ MDNS.begin (" usb.nugg" );
255
+ Serial.println (" mDNS responder started" );
256
+
216
257
server.on (" /" , handleRoot);
217
258
server.on (" /payloads" , getPayloads);
218
259
server.on (" /savepayload" , HTTP_POST, websave);
@@ -223,13 +264,14 @@ void setup() {
223
264
224
265
server.begin ();
225
266
226
- strip.clear ();
227
- strip.setPixelColor (0 , strip.Color (0 , 0 , 0 ));
267
+ MDNS.addService (" http" , " tcp" , 80 );
268
+ strip.clear ();
269
+ strip.setPixelColor (0 , strip.Color (0 , 0 , 0 ));
228
270
strip.show ();
229
271
strip.show ();
230
272
231
273
// initialize & launch payload selector
232
-
274
+
233
275
xTaskCreate (webserverInit, " webapptask" , 12 * 1024 , NULL , 5 , &webapp); // create task priority 1
234
276
RubberNugget::selectPayload ();
235
277
}
0 commit comments