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

Commit 4a11d88

Browse files
committed
use mDNS to access web UI with usb.nugg
1 parent edcf5a0 commit 4a11d88

File tree

2 files changed

+89
-47
lines changed

2 files changed

+89
-47
lines changed

src/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ RUN tar -xf arduino-cli_${ARDUINO_CLI_VERSION}_Linux_64bit.tar.gz
1717
# TODO: version-lock these
1818
COPY arduino-cli.yaml .
1919
RUN ./arduino-cli core update-index --config-file arduino-cli.yaml
20-
RUN ./arduino-cli core install esp32:esp32
20+
RUN ./arduino-cli core install esp32:esp32@2.0.4
2121
RUN ./arduino-cli lib install \
2222
"ESP8266 and ESP32 OLED driver for SSD1306 displays" \
2323
"Adafruit NeoPixel"

src/RubberNugget/RubberNugget.ino

Lines changed: 88 additions & 46 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
#include "SH1106Wire.h"
1316
#include "Nugget_Interface.h"
@@ -38,67 +41,80 @@ WebServer server(80);
3841
TaskHandle_t webapp;
3942
TaskHandle_t nuggweb;
4043

41-
void getPayloads() {
42-
String* payloadPaths = RubberNugget::allPayloadPaths();
44+
void getPayloads()
45+
{
46+
String *payloadPaths = RubberNugget::allPayloadPaths();
4347
Serial.printf("[SERVER][getPayloads] %s\n", payloadPaths->c_str());
4448
server.send(200, "text/plain", *payloadPaths);
4549
}
4650

47-
void handleRoot() {
51+
void handleRoot()
52+
{
4853
Serial.println("handling root!");
4954
server.send(200, "text/html", String(INDEX));
5055
}
5156

52-
void delpayload() {
57+
void delpayload()
58+
{
5359
String path(server.arg("path"));
5460
FRESULT res = f_unlink(path.c_str());
55-
if (res == FR_OK){
61+
if (res == FR_OK)
62+
{
5663
server.send(200);
57-
} else {
64+
}
65+
else
66+
{
5867
server.send(500);
5968
}
6069
}
6170

62-
void websave() {
71+
void websave()
72+
{
6373
// path should be corrected, i.e. by index.html's pathCorrector function
6474
// Each path should start with '/' and not end with '/'
6575
String path = (server.arg("path"));
66-
const char* constPath = path.c_str();
76+
const char *constPath = path.c_str();
6777

6878
// construct directories as needed
6979
FILINFO filinfo;
7080
int pathSoFarIdx = 1;
71-
while(true) {
81+
while (true)
82+
{
7283
int nextDir = path.indexOf("/", pathSoFarIdx);
73-
if (nextDir == -1){
84+
if (nextDir == -1)
85+
{
7486
break;
7587
}
7688
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+
{
7993
server.send(500, "text/plain", "Could not create directory");
8094
return;
8195
}
8296
}
83-
pathSoFarIdx = nextDir+1;
97+
pathSoFarIdx = nextDir + 1;
8498
}
8599

86100
// Create file
87101
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+
{
89104
server.send(500, "text/plain", "Could not open file for writing");
90105
return;
91106
}
92107

93108
// Write to file
94109
String content = (server.arg("payloadText"));
95110
content.replace(" ", "/"); // why
96-
const char* contentBase64 = content.c_str();
111+
const char *contentBase64 = content.c_str();
97112
size_t payloadLength = BASE64::decodeLength(contentBase64);
98113
uint8_t payloadContent[payloadLength];
99114
BASE64::decode(contentBase64, payloadContent);
100115
UINT written = 0;
101-
if (FR_OK != f_write(&file, payloadContent, payloadLength, &written)){
116+
if (FR_OK != f_write(&file, payloadContent, payloadLength, &written))
117+
{
102118
server.send(500, "text/plain", "Could not write to file");
103119
f_close(&file);
104120
return;
@@ -108,15 +124,18 @@ void websave() {
108124
}
109125

110126
// decode base64 and run
111-
void webrunlive() {
127+
void webrunlive()
128+
{
112129
server.send(200, "text/plain", "Running payload...");
113-
if (server.hasArg("plain")) {
130+
if (server.hasArg("plain"))
131+
{
114132
Serial.print("Decoding: ");
115133
String decoded = (server.arg("plain"));
116134
char tab2[decoded.length() + 1];
117135
strcpy(tab2, decoded.c_str());
118136

119-
for (int i = 0; i < decoded.length(); i++) {
137+
for (int i = 0; i < decoded.length(); i++)
138+
{
120139
Serial.print(tab2[i]);
121140
}
122141

@@ -128,15 +147,16 @@ void webrunlive() {
128147
Serial.println(BASE64::decodeLength(tab2));
129148
BASE64::decode(tab2, raw);
130149

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));
133152
RubberNugget::runLivePayload(meow);
134153
Serial.println();
135154
Serial.println("-------");
136155
}
137156
}
138157

139-
void webget() {
158+
void webget()
159+
{
140160
FRESULT fr;
141161
FIL file;
142162
uint16_t size;
@@ -145,40 +165,49 @@ void webget() {
145165
String path = server.arg("path");
146166
fr = f_open(&file, path.c_str(), FA_READ);
147167

148-
if (fr != FR_OK) {
168+
if (fr != FR_OK)
169+
{
149170
// TODO: most likely file not found, but we need to check why fr != OK.
150171
// Marking 500 until resolved
151172
server.send(500, "plain/text", String("Error loading script"));
152173
return;
153174
}
154175

155176
size = f_size(&file);
156-
char * data = NULL;
177+
char *data = NULL;
157178

158-
data = (char*) malloc(size);
179+
data = (char *)malloc(size);
159180

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+
{
162184
String payload = String(data);
163185
payload = payload.substring(0, bytesRead);
164186
payload = base64::encode(payload);
165187
server.send(200, "plain/text", payload);
166-
} else {
188+
}
189+
else
190+
{
167191
server.send(500, "plain/text", String("Error reading script"));
168192
}
169193
f_close(&file);
170-
171194
}
172195

173196
// run payload with get request path
174-
void webrun() {
197+
void webrun()
198+
{
175199
server.send(200, "text/html", "Running payload...");
176200
String path = server.arg("path");
177201
RubberNugget::runPayload(path.c_str(), 1); // provide parameter triggered from webpage
178202
}
179203

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();
182211
server.handleClient();
183212
vTaskDelay(2);
184213
}
@@ -187,32 +216,44 @@ void webserverInit(void *p) {
187216
extern String netPassword;
188217
extern String networkName;
189218

190-
void setup() {
191-
pinMode(12, OUTPUT);
192-
strip.begin();
219+
220+
221+
void setup()
222+
{
223+
pinMode(12, OUTPUT);
224+
strip.begin();
193225
delay(500);
194226

195227
Serial.begin(115200);
196228

197229
RubberNugget::init();
198-
199-
if (networkName.length() >0) {
230+
231+
if (networkName.length() > 0)
232+
{
200233
Serial.println(networkName);
201-
const char * c = networkName.c_str();
202-
ssid=c;
234+
const char *c = networkName.c_str();
235+
ssid = c;
203236
}
204-
if (netPassword.length() >=8) {
237+
if (netPassword.length() >= 8)
238+
{
205239
Serial.println(netPassword);
206-
const char * d = netPassword.c_str();
207-
password=d;
240+
const char *d = netPassword.c_str();
241+
password = d;
208242
}
209243

210244
WiFi.softAP(ssid, password);
211-
// }
245+
// }
212246
IPAddress myIP = WiFi.softAPIP();
213247
Serial.print("AP IP address: ");
214248
Serial.println(myIP);
215249

250+
251+
dns.setErrorReplyCode(DNSReplyCode::NoError);
252+
dns.start(53, "*", myIP);
253+
254+
MDNS.begin("usb.nugg");
255+
Serial.println("mDNS responder started");
256+
216257
server.on("/", handleRoot);
217258
server.on("/payloads", getPayloads);
218259
server.on("/savepayload", HTTP_POST, websave);
@@ -223,13 +264,14 @@ void setup() {
223264

224265
server.begin();
225266

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));
228270
strip.show();
229271
strip.show();
230272

231273
// initialize & launch payload selector
232-
274+
233275
xTaskCreate(webserverInit, "webapptask", 12 * 1024, NULL, 5, &webapp); // create task priority 1
234276
RubberNugget::selectPayload();
235277
}

0 commit comments

Comments
 (0)