|
@ -27,6 +27,57 @@ std::vector<ws_on_receive_callback_f> _ws_on_receive_callbacks; |
|
|
// Private methods
|
|
|
// Private methods
|
|
|
// -----------------------------------------------------------------------------
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct { |
|
|
|
|
|
IPAddress ip; |
|
|
|
|
|
unsigned long timestamp = 0; |
|
|
|
|
|
} ws_ticket_t; |
|
|
|
|
|
ws_ticket_t _ticket[WS_BUFFER_SIZE]; |
|
|
|
|
|
|
|
|
|
|
|
void _onAuth(AsyncWebServerRequest *request) { |
|
|
|
|
|
|
|
|
|
|
|
webLog(request); |
|
|
|
|
|
if (!webAuthenticate(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(200, "text/plain", "OK"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool _wsAuth(AsyncWebSocketClient * client) { |
|
|
|
|
|
|
|
|
|
|
|
IPAddress ip = client->remoteIP(); |
|
|
|
|
|
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\": 10}")); |
|
|
|
|
|
return false; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
#if MQTT_SUPPORT
|
|
|
#if MQTT_SUPPORT
|
|
|
void _wsMQTTCallback(unsigned int type, const char * topic, const char * payload) { |
|
|
void _wsMQTTCallback(unsigned int type, const char * topic, const char * payload) { |
|
|
if (type == MQTT_CONNECT_EVENT) wsSend_P(PSTR("{\"mqttStatus\": true}")); |
|
|
if (type == MQTT_CONNECT_EVENT) wsSend_P(PSTR("{\"mqttStatus\": true}")); |
|
@ -205,9 +256,7 @@ void _wsParse(AsyncWebSocketClient *client, uint8_t * payload, size_t length) { |
|
|
if (save) { |
|
|
if (save) { |
|
|
|
|
|
|
|
|
// Callbacks
|
|
|
// Callbacks
|
|
|
for (unsigned char i = 0; i < _ws_on_after_parse_callbacks.size(); i++) { |
|
|
|
|
|
(_ws_on_after_parse_callbacks[i])(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
wsReload(); |
|
|
|
|
|
|
|
|
// This should got to callback as well
|
|
|
// This should got to callback as well
|
|
|
// but first change management has to be in place
|
|
|
// but first change management has to be in place
|
|
@ -317,6 +366,11 @@ void _wsStart(uint32_t client_id) { |
|
|
void _wsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len){ |
|
|
void _wsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len){ |
|
|
|
|
|
|
|
|
if (type == WS_EVT_CONNECT) { |
|
|
if (type == WS_EVT_CONNECT) { |
|
|
|
|
|
|
|
|
|
|
|
#ifndef NOWSAUTH
|
|
|
|
|
|
if (!_wsAuth(client)) return; |
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
IPAddress ip = client->remoteIP(); |
|
|
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()); |
|
|
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()); |
|
|
_wsStart(client->id()); |
|
@ -423,27 +477,24 @@ void wsSend_P(uint32_t client_id, PGM_P payload) { |
|
|
_ws.text(client_id, buffer); |
|
|
_ws.text(client_id, buffer); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void wsConfigure() { |
|
|
|
|
|
#if USE_PASSWORD
|
|
|
|
|
|
bool auth = getSetting("wsAuth", WS_AUTHENTICATION).toInt() == 1; |
|
|
|
|
|
if (auth) { |
|
|
|
|
|
_ws.setAuthentication(WEB_USERNAME, (const char *) getSetting("adminPass", ADMIN_PASS).c_str()); |
|
|
|
|
|
} else { |
|
|
|
|
|
_ws.setAuthentication("", ""); |
|
|
|
|
|
} |
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
// This method being public makes
|
|
|
|
|
|
// _ws_on_after_parse_callbacks strange here,
|
|
|
|
|
|
// it should belong somewhere else.
|
|
|
|
|
|
void wsReload() { |
|
|
|
|
|
for (unsigned char i = 0; i < _ws_on_after_parse_callbacks.size(); i++) { |
|
|
|
|
|
(_ws_on_after_parse_callbacks[i])(); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void wsSetup() { |
|
|
void wsSetup() { |
|
|
_ws.onEvent(_wsEvent); |
|
|
_ws.onEvent(_wsEvent); |
|
|
wsConfigure(); |
|
|
|
|
|
webServer()->addHandler(&_ws); |
|
|
webServer()->addHandler(&_ws); |
|
|
|
|
|
webServer()->on("/auth", HTTP_GET, _onAuth); |
|
|
#if MQTT_SUPPORT
|
|
|
#if MQTT_SUPPORT
|
|
|
mqttRegister(_wsMQTTCallback); |
|
|
mqttRegister(_wsMQTTCallback); |
|
|
#endif
|
|
|
#endif
|
|
|
wsOnSendRegister(_wsOnStart); |
|
|
wsOnSendRegister(_wsOnStart); |
|
|
wsOnReceiveRegister(_wsOnReceive); |
|
|
wsOnReceiveRegister(_wsOnReceive); |
|
|
wsOnAfterParseRegister(wsConfigure); |
|
|
|
|
|
espurnaRegisterLoop(_wsLoop); |
|
|
espurnaRegisterLoop(_wsLoop); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|