c08e232339
A NodeMCU v3 (ESP8266) that speaks the Dingtian wire contract (binary UDP 60000 with relay_pw, string UDP 60001, config CGI incl. the setconfig gotcha, userset.cgi, Digest input push) so the booth's existing driver needs no change, plus a heartbeat/command/ watchdog path to an ephemeral cloud stand-in (poc/cloud-standin.mjs). Verified at park-lab: the booth's assign flow configured it like real hardware and a button press reached the booth (401→Digest→200) and the cloud with the booth's status. Watchdog probes /health and power-cycles the PC via a spare relay with backoff. IO behind one interface for the ESP32/ Dingtian port. wiki: relay-proxy-firmware page; esp32-flashability-test runbook (esptool lock test); dingtian-relay "can we flash our own firmware" (vendor locks the standard SKU); site-device-installation: booth ufw must allow device-net → 3000 (push targets PORT, not Caddy's 80); open-questions: cloud takeover vs signed ledger (parked). .gitignore: firmware venv/.pio/secrets.h.
63 lines
2.0 KiB
C++
63 lines
2.0 KiB
C++
#include "digest.h"
|
|
#include <ESP8266WiFi.h>
|
|
#include <ESP8266HTTPClient.h>
|
|
#include <MD5Builder.h>
|
|
|
|
static String md5(const String& s) {
|
|
MD5Builder m;
|
|
m.begin();
|
|
m.add(s);
|
|
m.calculate();
|
|
return m.toString();
|
|
}
|
|
|
|
// Pull key="value" or key=value out of a challenge header.
|
|
static String param(const String& h, const char* key) {
|
|
String k = String(key) + "=";
|
|
int i = h.indexOf(k);
|
|
if (i < 0) return "";
|
|
i += k.length();
|
|
if (h[i] == '"') {
|
|
int j = h.indexOf('"', i + 1);
|
|
return j < 0 ? "" : h.substring(i + 1, j);
|
|
}
|
|
int j = h.indexOf(',', i);
|
|
return j < 0 ? h.substring(i) : h.substring(i, j);
|
|
}
|
|
|
|
int digestGet(const String& host, uint16_t port, const String& path, const String& user, const String& pass) {
|
|
WiFiClient client;
|
|
HTTPClient http;
|
|
http.setTimeout(2000);
|
|
if (!http.begin(client, host, port, path)) return -1;
|
|
const char* keys[] = {"WWW-Authenticate"};
|
|
http.collectHeaders(keys, 1);
|
|
int code = http.GET();
|
|
if (code != 401 || user.isEmpty()) {
|
|
http.end();
|
|
return code;
|
|
}
|
|
String ch = http.header("WWW-Authenticate");
|
|
http.end();
|
|
if (!ch.startsWith("Digest")) return 401;
|
|
|
|
String realm = param(ch, "realm"), nonce = param(ch, "nonce"), qop = param(ch, "qop");
|
|
String nc = "00000001";
|
|
String cnonce = String(ESP.random(), HEX);
|
|
String ha1 = md5(user + ":" + realm + ":" + pass);
|
|
String ha2 = md5("GET:" + path);
|
|
String resp = qop.length() ? md5(ha1 + ":" + nonce + ":" + nc + ":" + cnonce + ":" + qop + ":" + ha2)
|
|
: md5(ha1 + ":" + nonce + ":" + ha2);
|
|
String auth = "Digest username=\"" + user + "\", realm=\"" + realm + "\", nonce=\"" + nonce +
|
|
"\", uri=\"" + path + "\", algorithm=MD5, response=\"" + resp + "\"";
|
|
if (qop.length()) auth += ", qop=" + qop + ", nc=" + nc + ", cnonce=\"" + cnonce + "\"";
|
|
|
|
HTTPClient h2;
|
|
h2.setTimeout(2000);
|
|
if (!h2.begin(client, host, port, path)) return -1;
|
|
h2.addHeader("Authorization", auth);
|
|
code = h2.GET();
|
|
h2.end();
|
|
return code;
|
|
}
|