forked from tbnobody/OpenDTU
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebApi.cpp
102 lines (90 loc) · 2.58 KB
/
WebApi.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2022 Thomas Basler and others
*/
#include "WebApi.h"
#include "Configuration.h"
#include "defaults.h"
#include <AsyncJson.h>
WebApiClass::WebApiClass()
: _server(HTTP_PORT)
, _events("/events")
{
}
void WebApiClass::init()
{
_server.addHandler(&_events);
_webApiConfig.init(&_server);
_webApiDevice.init(&_server);
_webApiDevInfo.init(&_server);
_webApiDtu.init(&_server);
_webApiEventlog.init(&_server);
_webApiFirmware.init(&_server);
_webApiGridprofile.init(&_server);
_webApiInverter.init(&_server);
_webApiLimit.init(&_server);
_webApiMaintenance.init(&_server);
_webApiMqtt.init(&_server);
_webApiNetwork.init(&_server);
_webApiNtp.init(&_server);
_webApiPower.init(&_server);
_webApiPrometheus.init(&_server);
_webApiSecurity.init(&_server);
_webApiSysstatus.init(&_server);
_webApiWebapp.init(&_server);
_webApiWsConsole.init(&_server);
_webApiWsLive.init(&_server);
_server.begin();
}
void WebApiClass::loop()
{
_webApiConfig.loop();
_webApiDevice.loop();
_webApiDevInfo.loop();
_webApiDtu.loop();
_webApiEventlog.loop();
_webApiFirmware.loop();
_webApiGridprofile.loop();
_webApiInverter.loop();
_webApiLimit.loop();
_webApiMaintenance.loop();
_webApiMqtt.loop();
_webApiNetwork.loop();
_webApiNtp.loop();
_webApiPower.loop();
_webApiSecurity.loop();
_webApiSysstatus.loop();
_webApiWebapp.loop();
_webApiWsConsole.loop();
_webApiWsLive.loop();
}
bool WebApiClass::checkCredentials(AsyncWebServerRequest* request)
{
CONFIG_T& config = Configuration.get();
if (request->authenticate(AUTH_USERNAME, config.Security_Password)) {
return true;
}
AsyncWebServerResponse* r = request->beginResponse(401);
// WebAPI should set the X-Requested-With to prevent browser internal auth dialogs
if (!request->hasHeader("X-Requested-With")) {
r->addHeader("WWW-Authenticate", "Basic realm=\"Login Required\"");
}
request->send(r);
return false;
}
bool WebApiClass::checkCredentialsReadonly(AsyncWebServerRequest* request)
{
CONFIG_T& config = Configuration.get();
if (config.Security_AllowReadonly) {
return true;
} else {
return checkCredentials(request);
}
}
void WebApiClass::sendTooManyRequests(AsyncWebServerRequest* request)
{
auto response = request->beginResponse(429, "text/plain", "Too Many Requests");
response->addHeader("Retry-After", "60");
request->send(response);
}
WebApiClass WebApi;