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