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

Commit 68de015

Browse files
AlexLyndBrandon Paiz
authored and
Brandon Paiz
committed
use mDNS to access web UI with usb.nugg
1 parent ef9d483 commit 68de015

File tree

1 file changed

+173
-52
lines changed

1 file changed

+173
-52
lines changed

src/RubberNugget/RubberNugget.ino

Lines changed: 173 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
#include <WiFiClient.h>
99
#include <WebServer.h>
1010

11+
#include <ESPmDNS.h>
12+
#include <DNSServer.h>
13+
1114
#include "webUI/index.h"
1215

1316
#include "src/utils.h"
@@ -24,82 +27,175 @@ WebServer server(80);
2427
TaskHandle_t webapp;
2528
TaskHandle_t nuggweb;
2629

27-
void getPayloads() {
28-
String* payloadPaths = RubberNugget::allPayloadPaths();
30+
void getPayloads()
31+
{
32+
String *payloadPaths = RubberNugget::allPayloadPaths();
2933
Serial.printf("[SERVER][getPayloads] %s\n", payloadPaths->c_str());
3034
server.send(200, "text/plain", *payloadPaths);
3135
}
3236

33-
void handleRoot() {
37+
void handleRoot()
38+
{
3439
Serial.println("handling root!");
3540
server.send(200, "text/html", String(INDEX));
3641
}
3742

38-
void delpayload() {
43+
void delpayload()
44+
{
3945
String path(server.arg("path"));
4046
FRESULT res = f_unlink(path.c_str());
41-
if (res == FR_OK){
47+
if (res == FR_OK)
48+
{
4249
server.send(200);
43-
} else {
50+
}
51+
else
52+
{
4453
server.send(500);
4554
}
4655
}
4756

48-
void websave() {
49-
fileOp decodeOp = base64Decode(server.arg("payloadText"));
50-
if (!decodeOp.ok){
51-
server.send(500, "text/plain", decodeOp.result);
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");
5291
return;
5392
}
54-
fileOp saveOp = saveFile(server.arg("path"), decodeOp.result);
55-
if (!saveOp.ok){
56-
server.send(500, "text/plain", saveOp.result);
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);
57106
return;
58107
}
59-
server.send(200, "text/plain", "payload saved successfully");
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+
}
60142
}
61143

62-
void webget() {
144+
void webget()
145+
{
146+
FRESULT fr;
147+
FIL file;
148+
uint16_t size;
149+
UINT bytesRead;
150+
63151
String path = server.arg("path");
64-
fileOp op = readFile(path);
65-
if (!op.ok) {
66-
// TODO: send 500/4XX depending on file existence vs internal error
67-
server.send(500, "text/plain", String("error getting payload: ") + op.result);
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"));
68159
return;
69160
}
70-
String payload = base64::encode(op.result);
71-
server.send(200, "text/plain", payload);
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);
72180
}
73181

74182
NuggetInterface* nuggetInterface;
75183

76184
// run payload with get request path
77-
void webrun() {
78-
fileOp op = readFile(server.arg("path"));
79-
if (op.ok) {
80-
server.send(200, "text/html", "Running payload...");
81-
NuggetScreen* runner = new ScriptRunnerScreen(op.result);
82-
bool ok = nuggetInterface->injectScreen(runner);
83-
return;
84-
}
85-
server.send(500, "text/html", "couldn't run payload: " + op.result);
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
86190
}
87191

88-
void webrunlive() {
89-
// TODO: use server.arg "content" or "payload" instead of "plain"
90-
fileOp op = base64Decode(server.arg("plain"));
91-
if (op.ok) {
92-
server.send(200, "text/plain", "running live payload");
93-
NuggetScreen* runner = new ScriptRunnerScreen(op.result);
94-
bool ok = nuggetInterface->injectScreen(runner);
95-
// TODO: send 503 when device is busy
96-
return;
97-
}
98-
server.send(500, "text/html", "Device busy");
99-
}
192+
DNSServer dns;
100193

101-
void webserverInit(void *p) {
102-
while (1) {
194+
void webserverInit(void *p)
195+
{
196+
while (1)
197+
{
198+
dns.processNextRequest();
103199
server.handleClient();
104200
vTaskDelay(2);
105201
}
@@ -108,27 +204,44 @@ void webserverInit(void *p) {
108204
extern String netPassword;
109205
extern String networkName;
110206

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

114217
RubberNugget::init();
115-
116-
if (networkName.length() >0) {
218+
219+
if (networkName.length() > 0)
220+
{
117221
Serial.println(networkName);
118-
const char * c = networkName.c_str();
119-
ssid=c;
222+
const char *c = networkName.c_str();
223+
ssid = c;
120224
}
121-
if (netPassword.length() >=8) {
225+
if (netPassword.length() >= 8)
226+
{
122227
Serial.println(netPassword);
123-
const char * d = netPassword.c_str();
124-
password=d;
228+
const char *d = netPassword.c_str();
229+
password = d;
125230
}
126231

127232
WiFi.softAP(ssid, password);
233+
// }
128234
IPAddress myIP = WiFi.softAPIP();
129235
Serial.print("AP IP address: ");
130236
Serial.println(myIP);
131237

238+
239+
dns.setErrorReplyCode(DNSReplyCode::NoError);
240+
dns.start(53, "*", myIP);
241+
242+
MDNS.begin("usb.nugg");
243+
Serial.println("mDNS responder started");
244+
132245
server.on("/", handleRoot);
133246
server.on("/payloads", getPayloads);
134247
server.on("/savepayload", HTTP_POST, websave);
@@ -139,6 +252,14 @@ void setup() {
139252

140253
server.begin();
141254

255+
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+
142263
xTaskCreate(webserverInit, "webapptask", 12 * 1024, NULL, 5, &webapp); // create task priority 1
143264
nuggetInterface = new NuggetInterface;
144265
NuggetScreen* dirScreen = new DirScreen("/");

0 commit comments

Comments
 (0)