/* WEBSERVER MODULE Copyright (C) 2016-2017 by Xose PĂ©rez */ #if WEB_SUPPORT #include #include #include #include #include #include #include #if WEB_EMBEDDED #include "static/index.html.gz.h" #endif // ----------------------------------------------------------------------------- AsyncWebServer * _server; char _last_modified[50]; Ticker _web_defer; // ----------------------------------------------------------------------------- AsyncWebSocket _ws("/ws"); typedef struct { IPAddress ip; unsigned long timestamp = 0; } ws_ticket_t; ws_ticket_t _ticket[WS_BUFFER_SIZE]; // ----------------------------------------------------------------------------- typedef struct { char * url; char * key; apiGetCallbackFunction getFn = NULL; apiPutCallbackFunction putFn = NULL; } web_api_t; std::vector _apis; // ----------------------------------------------------------------------------- // WEBSOCKETS // ----------------------------------------------------------------------------- void _wsMQTTCallback(unsigned int type, const char * topic, const char * payload) { if (type == MQTT_CONNECT_EVENT) { wsSend_P(PSTR("{\"mqttStatus\": true}")); } if (type == MQTT_DISCONNECT_EVENT) { wsSend_P(PSTR("{\"mqttStatus\": false}")); } } void _wsParse(uint32_t client_id, uint8_t * payload, size_t length) { // Parse JSON input DynamicJsonBuffer jsonBuffer; JsonObject& root = jsonBuffer.parseObject((char *) payload); if (!root.success()) { DEBUG_MSG_P(PSTR("[WEBSOCKET] Error parsing data\n")); wsSend_P(client_id, PSTR("{\"message\": \"Error parsing data!\"}")); return; } // Check actions if (root.containsKey("action")) { String action = root["action"]; DEBUG_MSG_P(PSTR("[WEBSOCKET] Requested action: %s\n"), action.c_str()); if (action.equals("reset")) { customReset(CUSTOM_RESET_WEB); ESP.restart(); } #ifdef ITEAD_SONOFF_RFBRIDGE if (action.equals("rfblearn") && root.containsKey("data")) { JsonObject& data = root["data"]; rfbLearn(data["id"], data["status"]); } if (action.equals("rfbforget") && root.containsKey("data")) { JsonObject& data = root["data"]; rfbForget(data["id"], data["status"]); } if (action.equals("rfbsend") && root.containsKey("data")) { JsonObject& data = root["data"]; rfbStore(data["id"], data["status"], data["data"].as()); } #endif if (action.equals("restore") && root.containsKey("data")) { JsonObject& data = root["data"]; if (!data.containsKey("app") || (data["app"] != APP_NAME)) { wsSend_P(client_id, PSTR("{\"message\": \"The file does not look like a valid configuration backup.\"}")); return; } for (unsigned int i = EEPROM_DATA_END; i < SPI_FLASH_SEC_SIZE; i++) { EEPROM.write(i, 0xFF); } for (auto element : data) { if (strcmp(element.key, "app") == 0) continue; if (strcmp(element.key, "version") == 0) continue; setSetting(element.key, element.value.as()); } saveSettings(); wsSend_P(client_id, PSTR("{\"message\": \"Changes saved. You should reboot your board now.\"}")); } if (action.equals("reconnect")) { // Let the HTTP request return and disconnect after 100ms _web_defer.once_ms(100, wifiDisconnect); } if (action.equals("relay") && root.containsKey("data")) { JsonObject& data = root["data"]; if (data.containsKey("status")) { bool status = (strcmp(data["status"], "1") == 0); unsigned int relayID = 0; if (data.containsKey("id")) { String value = data["id"]; relayID = value.toInt(); } relayStatus(relayID, status); } } #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE if (lightHasColor()) { if (action.equals("color") && root.containsKey("data")) { lightColor(root["data"]); lightUpdate(true, true); } if (action.equals("brightness") && root.containsKey("data")) { lightBrightness(root["data"]); lightUpdate(true, true); } } if (action.equals("channel") && root.containsKey("data")) { JsonObject& data = root["data"]; if (data.containsKey("id") && data.containsKey("value")) { lightChannel(data["id"], data["value"]); lightUpdate(true, true); } } #endif }; // Check config if (root.containsKey("config") && root["config"].is()) { JsonArray& config = root["config"]; DEBUG_MSG_P(PSTR("[WEBSOCKET] Parsing configuration data\n")); unsigned char webMode = WEB_MODE_NORMAL; bool save = false; bool changed = false; bool changedMQTT = false; bool changedNTP = false; unsigned int network = 0; unsigned int dczRelayIdx = 0; String adminPass; for (unsigned int i=0; i= relayCount()) continue; key = key + String(dczRelayIdx); ++dczRelayIdx; } #else if (key.startsWith("dcz")) continue; #endif // Web portions if (key == "webPort") { if ((value.toInt() == 0) || (value.toInt() == 80)) { save = changed = true; delSetting(key); continue; } } if (key == "webMode") { webMode = value.toInt(); continue; } // Check password if (key == "adminPass1") { adminPass = value; continue; } if (key == "adminPass2") { if (!value.equals(adminPass)) { wsSend_P(client_id, PSTR("{\"message\": \"Passwords do not match!\"}")); return; } if (value.length() == 0) continue; wsSend_P(client_id, PSTR("{\"action\": \"reload\"}")); key = String("adminPass"); } if (key == "ssid") { key = key + String(network); } if (key == "pass") { key = key + String(network); } if (key == "ip") { key = key + String(network); } if (key == "gw") { key = key + String(network); } if (key == "mask") { key = key + String(network); } if (key == "dns") { key = key + String(network); ++network; } if (value != getSetting(key)) { //DEBUG_MSG_P(PSTR("[WEBSOCKET] Storing %s = %s\n", key.c_str(), value.c_str())); setSetting(key, value); save = changed = true; if (key.startsWith("mqtt")) changedMQTT = true; if (key.startsWith("ntp")) changedNTP = true; } } if (webMode == WEB_MODE_NORMAL) { // Clean wifi networks int i = 0; while (i < network) { if (getSetting("ssid" + String(i)).length() == 0) { delSetting("ssid" + String(i)); break; } if (getSetting("pass" + String(i)).length() == 0) delSetting("pass" + String(i)); if (getSetting("ip" + String(i)).length() == 0) delSetting("ip" + String(i)); if (getSetting("gw" + String(i)).length() == 0) delSetting("gw" + String(i)); if (getSetting("mask" + String(i)).length() == 0) delSetting("mask" + String(i)); if (getSetting("dns" + String(i)).length() == 0) delSetting("dns" + String(i)); ++i; } while (i < WIFI_MAX_NETWORKS) { if (getSetting("ssid" + String(i)).length() > 0) { save = changed = true; } delSetting("ssid" + String(i)); delSetting("pass" + String(i)); delSetting("ip" + String(i)); delSetting("gw" + String(i)); delSetting("mask" + String(i)); delSetting("dns" + String(i)); ++i; } } // Save settings if (save) { saveSettings(); wifiConfigure(); otaConfigure(); #if ALEXA_SUPPORT alexaConfigure(); #endif #if INFLUXDB_SUPPORT influxDBConfigure(); #endif #if DOMOTICZ_SUPPORT domoticzConfigure(); #endif mqttConfigure(); #if RF_SUPPORT rfBuildCodes(); #endif #if EMON_SUPPORT setCurrentRatio(getSetting("emonRatio").toFloat()); #endif // Check if we should reconfigure MQTT connection if (changedMQTT) { mqttDisconnect(); } // Check if we should reconfigure NTP connection if (changedNTP) { ntpConnect(); } } if (changed) { wsSend_P(client_id, PSTR("{\"message\": \"Changes saved\"}")); } else { wsSend_P(client_id, PSTR("{\"message\": \"No changes detected\"}")); } } } void _wsStart(uint32_t client_id) { char chipid[6]; snprintf_P(chipid, sizeof(chipid), PSTR("%06X"), ESP.getChipId()); DynamicJsonBuffer jsonBuffer; JsonObject& root = jsonBuffer.createObject(); bool changePassword = false; #if WEB_FORCE_PASS_CHANGE String adminPass = getSetting("adminPass", ADMIN_PASS); if (adminPass.equals(ADMIN_PASS)) changePassword = true; #endif if (changePassword) { root["webMode"] = WEB_MODE_PASSWORD; } else { root["webMode"] = WEB_MODE_NORMAL; root["app"] = APP_NAME; root["version"] = APP_VERSION; root["buildDate"] = __DATE__; root["buildTime"] = __TIME__; root["manufacturer"] = String(MANUFACTURER); root["chipid"] = chipid; root["mac"] = WiFi.macAddress(); root["device"] = String(DEVICE); root["hostname"] = getSetting("hostname"); root["network"] = getNetwork(); root["deviceip"] = getIP(); root["ntpStatus"] = ntpConnected(); root["ntpServer1"] = getSetting("ntpServer1", NTP_SERVER); root["ntpServer2"] = getSetting("ntpServer2"); root["ntpServer3"] = getSetting("ntpServer3"); root["ntpOffset"] = getSetting("ntpOffset", NTP_TIME_OFFSET).toInt(); root["ntpDST"] = getSetting("ntpDST", NTP_DAY_LIGHT).toInt() == 1; root["mqttStatus"] = mqttConnected(); root["mqttServer"] = getSetting("mqttServer", MQTT_SERVER); root["mqttPort"] = getSetting("mqttPort", MQTT_PORT); root["mqttUser"] = getSetting("mqttUser"); root["mqttPassword"] = getSetting("mqttPassword"); root["mqttTopic"] = getSetting("mqttTopic", MQTT_TOPIC); root["mqttUseJson"] = getSetting("mqttUseJson", MQTT_USE_JSON).toInt() == 1; JsonArray& relay = root.createNestedArray("relayStatus"); for (unsigned char relayID=0; relayID 1) { root["multirelayVisible"] = 1; root["relaySync"] = getSetting("relaySync", RELAY_SYNC); } root["btnDelay"] = getSetting("btnDelay", BUTTON_DBLCLICK_DELAY).toInt(); root["webPort"] = getSetting("webPort", WEB_PORT).toInt(); root["apiEnabled"] = getSetting("apiEnabled", API_ENABLED).toInt() == 1; root["apiKey"] = getSetting("apiKey"); root["tmpUnits"] = getSetting("tmpUnits", TMP_UNITS).toInt(); #if DOMOTICZ_SUPPORT root["dczVisible"] = 1; root["dczEnabled"] = getSetting("dczEnabled", DOMOTICZ_ENABLED).toInt() == 1; root["dczTopicIn"] = getSetting("dczTopicIn", DOMOTICZ_IN_TOPIC); root["dczTopicOut"] = getSetting("dczTopicOut", DOMOTICZ_OUT_TOPIC); JsonArray& dczRelayIdx = root.createNestedArray("dczRelayIdx"); for (byte i=0; iremoteIP(); unsigned long now = millis(); unsigned short index = 0; for (index = 0; index < WS_BUFFER_SIZE; index++) { if ((_ticket[index].ip == ip) && (now - _ticket[index].timestamp < WS_TIMEOUT)) break; } if (index == WS_BUFFER_SIZE) { DEBUG_MSG_P(PSTR("[WEBSOCKET] Validation check failed\n")); wsSend_P(client->id(), PSTR("{\"message\": \"Session expired, please reload page...\"}")); return false; } return true; } void _wsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len){ static uint8_t * message; // Authorize #ifndef NOWSAUTH if (!_wsAuth(client)) return; #endif if (type == WS_EVT_CONNECT) { IPAddress ip = client->remoteIP(); DEBUG_MSG_P(PSTR("[WEBSOCKET] #%u connected, ip: %d.%d.%d.%d, url: %s\n"), client->id(), ip[0], ip[1], ip[2], ip[3], server->url()); _wsStart(client->id()); } else if(type == WS_EVT_DISCONNECT) { DEBUG_MSG_P(PSTR("[WEBSOCKET] #%u disconnected\n"), client->id()); } else if(type == WS_EVT_ERROR) { DEBUG_MSG_P(PSTR("[WEBSOCKET] #%u error(%u): %s\n"), client->id(), *((uint16_t*)arg), (char*)data); } else if(type == WS_EVT_PONG) { DEBUG_MSG_P(PSTR("[WEBSOCKET] #%u pong(%u): %s\n"), client->id(), len, len ? (char*) data : ""); } else if(type == WS_EVT_DATA) { AwsFrameInfo * info = (AwsFrameInfo*)arg; // First packet if (info->index == 0) { message = (uint8_t*) malloc(info->len); } // Store data memcpy(message + info->index, data, len); // Last packet if (info->index + len == info->len) { _wsParse(client->id(), message, info->len); free(message); } } } // ----------------------------------------------------------------------------- bool wsConnected() { return (_ws.count() > 0); } void wsSend(const char * payload) { if (_ws.count() > 0) { _ws.textAll(payload); } } void wsSend_P(PGM_P payload) { if (_ws.count() > 0) { char buffer[strlen_P(payload)]; strcpy_P(buffer, payload); _ws.textAll(buffer); } } void wsSend(uint32_t client_id, const char * payload) { _ws.text(client_id, payload); } void wsSend_P(uint32_t client_id, PGM_P payload) { char buffer[strlen_P(payload)]; strcpy_P(buffer, payload); _ws.text(client_id, buffer); } void wsSetup() { _ws.onEvent(_wsEvent); mqttRegister(_wsMQTTCallback); _server->addHandler(&_ws); _server->on("/auth", HTTP_GET, _onAuth); } // ----------------------------------------------------------------------------- // API // ----------------------------------------------------------------------------- bool _authAPI(AsyncWebServerRequest *request) { if (getSetting("apiEnabled", API_ENABLED).toInt() == 0) { DEBUG_MSG_P(PSTR("[WEBSERVER] HTTP API is not enabled\n")); request->send(403); return false; } if (!request->hasParam("apikey", (request->method() == HTTP_PUT))) { DEBUG_MSG_P(PSTR("[WEBSERVER] Missing apikey parameter\n")); request->send(403); return false; } AsyncWebParameter* p = request->getParam("apikey", (request->method() == HTTP_PUT)); if (!p->value().equals(getSetting("apiKey"))) { DEBUG_MSG_P(PSTR("[WEBSERVER] Wrong apikey parameter\n")); request->send(403); return false; } return true; } bool _asJson(AsyncWebServerRequest *request) { bool asJson = false; if (request->hasHeader("Accept")) { AsyncWebHeader* h = request->getHeader("Accept"); asJson = h->value().equals("application/json"); } return asJson; } ArRequestHandlerFunction _bindAPI(unsigned int apiID) { return [apiID](AsyncWebServerRequest *request) { _webLog(request); if (!_authAPI(request)) return; web_api_t api = _apis[apiID]; // Check if its a PUT if (api.putFn != NULL) { if (request->hasParam("value", request->method() == HTTP_PUT)) { AsyncWebParameter* p = request->getParam("value", request->method() == HTTP_PUT); (api.putFn)((p->value()).c_str()); } } // Get response from callback char value[API_BUFFER_SIZE]; (api.getFn)(value, API_BUFFER_SIZE); char *p = ltrim(value); // The response will be a 404 NOT FOUND if the resource is not available if (!value) { DEBUG_MSG_P(PSTR("[API] Sending 404 response\n")); request->send(404); return; } DEBUG_MSG_P(PSTR("[API] Sending response '%s'\n"), p); // Format response according to the Accept header if (_asJson(request)) { char buffer[64]; snprintf_P(buffer, sizeof(buffer), PSTR("{ \"%s\": %s }"), api.key, p); request->send(200, "application/json", buffer); } else { request->send(200, "text/plain", p); } }; } void _onAPIs(AsyncWebServerRequest *request) { _webLog(request); if (!_authAPI(request)) return; bool asJson = _asJson(request); String output; if (asJson) { DynamicJsonBuffer jsonBuffer; JsonObject& root = jsonBuffer.createObject(); for (unsigned int i=0; i < _apis.size(); i++) { root[_apis[i].key] = _apis[i].url; } root.printTo(output); request->send(200, "application/json", output); } else { for (unsigned int i=0; i < _apis.size(); i++) { output += _apis[i].key + String(" -> ") + _apis[i].url + String("\n"); } request->send(200, "text/plain", output); } } void _onRPC(AsyncWebServerRequest *request) { _webLog(request); if (!_authAPI(request)) return; //bool asJson = _asJson(request); int response = 404; if (request->hasParam("action")) { AsyncWebParameter* p = request->getParam("action"); String action = p->value(); DEBUG_MSG_P(PSTR("[RPC] Action: %s\n"), action.c_str()); if (action.equals("reset")) { response = 200; _web_defer.once_ms(100, []() { customReset(CUSTOM_RESET_RPC); ESP.restart(); }); } } request->send(response); } // ----------------------------------------------------------------------------- void apiRegister(const char * url, const char * key, apiGetCallbackFunction getFn, apiPutCallbackFunction putFn) { // Store it web_api_t api; char buffer[40]; snprintf_P(buffer, sizeof(buffer), PSTR("/api/%s"), url); api.url = strdup(buffer); api.key = strdup(key); api.getFn = getFn; api.putFn = putFn; _apis.push_back(api); // Bind call unsigned int methods = HTTP_GET; if (putFn != NULL) methods += HTTP_PUT; _server->on(buffer, methods, _bindAPI(_apis.size() - 1)); } void apiSetup() { _server->on("/apis", HTTP_GET, _onAPIs); _server->on("/rpc", HTTP_GET, _onRPC); } // ----------------------------------------------------------------------------- // WEBSERVER // ----------------------------------------------------------------------------- void _webLog(AsyncWebServerRequest *request) { DEBUG_MSG_P(PSTR("[WEBSERVER] Request: %s %s\n"), request->methodToString(), request->url().c_str()); } bool _authenticate(AsyncWebServerRequest *request) { String password = getSetting("adminPass", ADMIN_PASS); char httpPassword[password.length() + 1]; password.toCharArray(httpPassword, password.length() + 1); return request->authenticate(WEB_USERNAME, httpPassword); } void _onAuth(AsyncWebServerRequest *request) { _webLog(request); if (!_authenticate(request)) return request->requestAuthentication(); IPAddress ip = request->client()->remoteIP(); unsigned long now = millis(); unsigned short index; for (index = 0; index < WS_BUFFER_SIZE; index++) { if (_ticket[index].ip == ip) break; if (_ticket[index].timestamp == 0) break; if (now - _ticket[index].timestamp > WS_TIMEOUT) break; } if (index == WS_BUFFER_SIZE) { request->send(429); } else { _ticket[index].ip = ip; _ticket[index].timestamp = now; request->send(204); } } void _onGetConfig(AsyncWebServerRequest *request) { _webLog(request); if (!_authenticate(request)) return request->requestAuthentication(); AsyncJsonResponse * response = new AsyncJsonResponse(); JsonObject& root = response->getRoot(); root["app"] = APP_NAME; root["version"] = APP_VERSION; unsigned int size = settingsKeyCount(); for (unsigned int i=0; iaddHeader("Content-Disposition", buffer); response->setLength(); request->send(response); } #if WEB_EMBEDDED void _onHome(AsyncWebServerRequest *request) { _webLog(request); if (request->header("If-Modified-Since").equals(_last_modified)) { request->send(304); } else { AsyncWebServerResponse *response = request->beginResponse_P(200, "text/html", index_html_gz, index_html_gz_len); response->addHeader("Content-Encoding", "gzip"); response->addHeader("Last-Modified", _last_modified); request->send(response); } } #endif void _onUpgrade(AsyncWebServerRequest *request) { AsyncWebServerResponse *response = request->beginResponse(200, "text/plain", Update.hasError() ? "FAIL" : "OK"); response->addHeader("Connection", "close"); if (!Update.hasError()) { _web_defer.once_ms(100, []() { customReset(CUSTOM_RESET_UPGRADE); ESP.restart(); }); } request->send(response); } void _onUpgradeData(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) { if (!index) { DEBUG_MSG_P(PSTR("[UPGRADE] Start: %s\n"), filename.c_str()); Update.runAsync(true); if (!Update.begin((ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000)) { #ifdef DEBUG_PORT Update.printError(DEBUG_PORT); #endif } } if (!Update.hasError()) { if (Update.write(data, len) != len) { #ifdef DEBUG_PORT Update.printError(DEBUG_PORT); #endif } } if (final) { if (Update.end(true)){ DEBUG_MSG_P(PSTR("[UPGRADE] Success: %u bytes\n"), index + len); } else { #ifdef DEBUG_PORT Update.printError(DEBUG_PORT); #endif } } else { DEBUG_MSG_P(PSTR("[UPGRADE] Progress: %u bytes\r"), index + len); } } // ----------------------------------------------------------------------------- void webSetup() { // Cache the Last-Modifier header value snprintf_P(_last_modified, sizeof(_last_modified), PSTR("%s %s GMT"), __DATE__, __TIME__); // Create server _server = new AsyncWebServer(getSetting("webPort", WEB_PORT).toInt()); // Setup websocket wsSetup(); // API setup apiSetup(); // Rewrites _server->rewrite("/", "/index.html"); // Serve home (basic authentication protection) #if WEB_EMBEDDED _server->on("/index.html", HTTP_GET, _onHome); #endif _server->on("/config", HTTP_GET, _onGetConfig); _server->on("/upgrade", HTTP_POST, _onUpgrade, _onUpgradeData); // Serve static files #if SPIFFS_SUPPORT _server->serveStatic("/", SPIFFS, "/") .setLastModified(_last_modified) .setFilter([](AsyncWebServerRequest *request) -> bool { _webLog(request); return true; }); #endif // 404 _server->onNotFound([](AsyncWebServerRequest *request){ request->send(404); }); // Run server _server->begin(); DEBUG_MSG_P(PSTR("[WEBSERVER] Webserver running on port %d\n"), getSetting("webPort", WEB_PORT).toInt()); } #endif // WEB_SUPPORT