Skip to content
This repository was archived by the owner on Dec 25, 2023. It is now read-only.

Commit 3a77e73

Browse files
author
Brandon Paiz
committed
remove unrelated formatting changes
1 parent 68de015 commit 3a77e73

File tree

1 file changed

+54
-162
lines changed

1 file changed

+54
-162
lines changed

src/RubberNugget/RubberNugget.ino

Lines changed: 54 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -23,178 +23,87 @@ const char *ssid = "Nugget AP";
2323
const char *password = "nugget123";
2424

2525
WebServer server(80);
26+
DNSServer dns;
2627

2728
TaskHandle_t webapp;
2829
TaskHandle_t nuggweb;
2930

30-
void getPayloads()
31-
{
32-
String *payloadPaths = RubberNugget::allPayloadPaths();
31+
void getPayloads() {
32+
String* payloadPaths = RubberNugget::allPayloadPaths();
3333
Serial.printf("[SERVER][getPayloads] %s\n", payloadPaths->c_str());
3434
server.send(200, "text/plain", *payloadPaths);
3535
}
3636

37-
void handleRoot()
38-
{
37+
void handleRoot() {
3938
Serial.println("handling root!");
4039
server.send(200, "text/html", String(INDEX));
4140
}
4241

43-
void delpayload()
44-
{
42+
void delpayload() {
4543
String path(server.arg("path"));
4644
FRESULT res = f_unlink(path.c_str());
47-
if (res == FR_OK)
48-
{
45+
if (res == FR_OK){
4946
server.send(200);
50-
}
51-
else
52-
{
47+
} else {
5348
server.send(500);
5449
}
5550
}
5651

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);
9156
return;
9257
}
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);
10661
return;
10762
}
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");
14264
}
14365

144-
void webget()
145-
{
146-
FRESULT fr;
147-
FIL file;
148-
uint16_t size;
149-
UINT bytesRead;
150-
66+
void webget() {
15167
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);
15972
return;
16073
}
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);
18076
}
18177

18278
NuggetInterface* nuggetInterface;
18379

18480
// 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);
19090
}
19191

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+
}
193104

194-
void webserverInit(void *p)
195-
{
196-
while (1)
197-
{
105+
void webserverInit(void *p) {
106+
while (1) {
198107
dns.processNextRequest();
199108
server.handleClient();
200109
vTaskDelay(2);
@@ -204,42 +113,31 @@ void webserverInit(void *p)
204113
extern String netPassword;
205114
extern String networkName;
206115

207-
208-
209-
void setup()
210-
{
211-
pinMode(12, OUTPUT);
212-
strip.begin();
213-
delay(500);
214-
116+
void setup() {
215117
Serial.begin(115200);
216118

217119
RubberNugget::init();
218-
219-
if (networkName.length() > 0)
220-
{
120+
121+
if (networkName.length() >0) {
221122
Serial.println(networkName);
222-
const char *c = networkName.c_str();
223-
ssid = c;
123+
const char * c = networkName.c_str();
124+
ssid=c;
224125
}
225-
if (netPassword.length() >= 8)
226-
{
126+
if (netPassword.length() >=8) {
227127
Serial.println(netPassword);
228-
const char *d = netPassword.c_str();
229-
password = d;
128+
const char * d = netPassword.c_str();
129+
password=d;
230130
}
231131

232132
WiFi.softAP(ssid, password);
233-
// }
234133
IPAddress myIP = WiFi.softAPIP();
235134
Serial.print("AP IP address: ");
236135
Serial.println(myIP);
237136

238-
239137
dns.setErrorReplyCode(DNSReplyCode::NoError);
240138
dns.start(53, "*", myIP);
241139

242-
MDNS.begin("usb.nugg");
140+
MDNS.begin("usb.nugget");
243141
Serial.println("mDNS responder started");
244142

245143
server.on("/", handleRoot);
@@ -253,12 +151,6 @@ void setup()
253151
server.begin();
254152

255153
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
262154

263155
xTaskCreate(webserverInit, "webapptask", 12 * 1024, NULL, 5, &webapp); // create task priority 1
264156
nuggetInterface = new NuggetInterface;

0 commit comments

Comments
 (0)