Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

latest platform and libraries and add security to OTA update #281

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,6 @@ ubuntu-xenial-16.04-cloudimg-console.log
lib/ConfigJson
lib/ESPAL
lib/OpenEVSE

# gui static files
src/web_static/
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "gui"]
path = gui
url = https://github.com/openevse/openevse_wifi_gui
url = https://github.com/chaseadam/openevse_wifi_gui
2 changes: 1 addition & 1 deletion gui
Submodule gui updated from bb089a to ae95ac
26 changes: 17 additions & 9 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ default_envs = openevse
[common]
version = -DBUILD_TAG=2.9.1
monitor_speed=115200
# AsyncWebServer 3.3.18+ does not enter static file handler
lib_deps =
PubSubClient@2.6
ESP Async WebServer@1.2.3
ESPAsyncTCP@1.2.2
ArduinoJson@6.15.1
PubSubClient@2.8
mathieucarbou/ESPAsyncWebServer @ 3.3.23
ESP32Async/ESPAsyncTCP@2.0.0
ArduinoJson@6.21.5
Micro Debug@0.0.3
ConfigJson@0.0.6
OpenEVSE@0.0.14
Expand All @@ -48,9 +49,15 @@ debug_flags =
# -DENABLE_DEBUG_RAPI
-DENABLE_PROFILE
-DDEBUG_PORT=Serial1
# -DDEBUG_ESP_PORT=Serial
# -DDEBUG_ESP_MDNS_RESPONDER
# -DDEBUG_ESP_WIFI
# -DDEBUG_ESP_CORE
ota_flags =
-DENABLE_OTA
-DWIFI_LED=0
# TODO switch ota password to env variable
# -DOTA_PASSWORD="123"
src_build_flags =
# -DENABLE_ASYNC_WIFI_SCAN

Expand All @@ -59,7 +66,7 @@ build_flags =
# specify exact Arduino ESP SDK version, requires platformio 3.5+ (curently dev version)
# http://docs.platformio.org/en/latest/projectconf/section_env_general.html#platform
#platform = https://github.com/platformio/platform-espressif8266.git#release/v1.6.0
platform = espressif8266@2.5.2
platform = espressif8266@4.2.1
platform_stage = https://github.com/platformio/platform-espressif8266.git#develop

[env:openevse]
Expand Down Expand Up @@ -91,8 +98,11 @@ framework = arduino
lib_deps = ${common.lib_deps}
build_flags = ${common.build_flags} ${common.debug_flags}
src_build_flags = ${common.version}.dev ${common.src_build_flags} ${common.ota_flags} ${common.debug_flags}
#upload_protocol = espota
#upload_port = openevse.local
# uncomment speed and comment ota lines for initial upload via serial
#upload_speed = 921600
upload_protocol = espota
upload_port = openevse.local
upload_flags = --auth="123"
monitor_speed = ${common.monitor_speed}
extra_scripts = ${common.extra_scripts}

Expand Down Expand Up @@ -130,8 +140,6 @@ board = esp12e
framework = arduino
lib_deps =
https://github.com/knolleary/pubsubclient
https://github.com/me-no-dev/ESPAsyncWebServer.git
https://github.com/me-no-dev/ESPAsyncTCP.git
https://github.com/bblanchon/ArduinoJson.git
https://github.com/jeremypoulter/MicroDebug.git
https://github.com/jeremypoulter/ConfigJson.git
Expand Down
22 changes: 11 additions & 11 deletions src/divert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ void divertmode_update(byte newmode)
{
case DIVERT_MODE_NORMAL:
// Restore the max charge current
rapiSender.sendCmdSync(String(F("$SC ")) + String(max_charge_current));
DBUGF("Restore max I: %d", max_charge_current);
//rapiSender.sendCmdSync(String(F("$SC ")) + String(max_charge_current));
//DBUGF("Restore max I: %d", max_charge_current);
break;

case DIVERT_MODE_ECO:
Expand Down Expand Up @@ -208,12 +208,12 @@ void divert_update_state()
// Set charge rate via RAPI
bool chargeRateSet = false;
// Try and set current with new API with volatile flag (don't save the current rate to EEPROM)
if(0 == rapiSender.sendCmdSync(String(F("$SC ")) + String(charge_rate) + String(F(" V")))) {
chargeRateSet = true;
} else if(0 == rapiSender.sendCmdSync(String(F("$SC ")) + String(charge_rate))) {
// Fallback to old API
chargeRateSet = true;
}
//if(0 == rapiSender.sendCmdSync(String(F("$SC ")) + String(charge_rate) + String(F(" V")))) {
// chargeRateSet = true;
//} else if(0 == rapiSender.sendCmdSync(String(F("$SC ")) + String(charge_rate))) {
// // Fallback to old API
// chargeRateSet = true;
//}

if(true == chargeRateSet)
{
Expand Down Expand Up @@ -269,9 +269,9 @@ void divert_update_state()
DBUGLN(F("Charge Stopped"));
event["divert_active"] = divert_active = false;

if(0 == rapiSender.sendCmdSync(String(F("$SC ")) + String(max_charge_current))) {
DBUGF("Restore max I: %d", max_charge_current);
}
//if(0 == rapiSender.sendCmdSync(String(F("$SC ")) + String(max_charge_current))) {
// DBUGF("Restore max I: %d", max_charge_current);
//}
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <ESP8266HTTPClient.h>

WiFiClientSecure client; // Create class for HTTPS TCP connections get_https()
WiFiClient client_insecure;
HTTPClient http; // Create class for HTTP TCP connections get_http()

// -------------------------------------------------------------------
Expand All @@ -20,7 +21,7 @@ get_https(const char *fingerprint, const char *host, String url,
DEBUG.print(host + httpsPort); //debug
return ("Connection error");
}
if (client.verify(fingerprint, host)) {
//if (client.verify(fingerprint, host)) {
client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host +
"\r\n" + "Connection: close\r\n\r\n");
// Handle wait for reply and timeout
Expand All @@ -39,9 +40,9 @@ get_https(const char *fingerprint, const char *host, String url,
return ("ok");
}
}
} else {
return ("HTTPS fingerprint no match");
}
//} else {
// return ("HTTPS fingerprint no match");
//}
return ("error " + String(host));
}

Expand All @@ -51,7 +52,7 @@ get_https(const char *fingerprint, const char *host, String url,
// -------------------------------------------------------------------
String
get_http(const char *host, String url) {
http.begin(String("http://") + host + String(url));
http.begin(client_insecure,String("http://") + host + String(url));
int httpCode = http.GET();
if ((httpCode > 0) && (httpCode == HTTP_CODE_OK)) {
String payload = http.getString();
Expand Down
2 changes: 1 addition & 1 deletion src/mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void mqttmsg_callback(char *topic, byte * payload, unsigned int length) {
// Print RAPI command from mqtt-sub topic e.g $SC
// ASSUME RAPI COMMANDS ARE ALWAYS PREFIX BY $ AND TWO CHARACTERS LONG)
String cmd = String(topic + rapi_character_index);
if (payload[0] != 0); { // If MQTT msg contains a payload e.g $SC 13. Not all rapi commands have a payload e.g. $GC
if (payload[0] != 0) { // If MQTT msg contains a payload e.g $SC 13. Not all rapi commands have a payload e.g. $GC
cmd += " ";
// print RAPI value received via MQTT serial
for (unsigned int i = 0; i < length; i++) {
Expand Down
10 changes: 5 additions & 5 deletions src/ohm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ void ohm_loop()
DBUGLN(F("ERROR Ohm Connect - connection failed"));
return;
}
if (client.verify(ohm_fingerprint, ohm_host))
{
//if (client.verify(ohm_fingerprint, ohm_host))
//{
client.print(String("GET ") + ohm_url + ohm + " HTTP/1.1\r\n" +
"Host: " + ohm_host + "\r\n" +
"User-Agent: OpenEVSE\r\n" + "Connection: close\r\n\r\n");
Expand Down Expand Up @@ -93,9 +93,9 @@ void ohm_loop()
}
}
}
} else {
DBUGLN(F("ERROR Ohm Connect - Certificate Invalid"));
}
//} else {
// DBUGLN(F("ERROR Ohm Connect - Certificate Invalid"));
//}
}

Profile_End(ohm_loop, 5);
Expand Down
1 change: 1 addition & 0 deletions src/ota.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ void ota_setup()
{
// Start local OTA update server
ArduinoOTA.setHostname(esp_hostname.c_str());
ArduinoOTA.setPassword("123");
ArduinoOTA.begin();

ArduinoOTA.onStart([]() {
Expand Down
4 changes: 2 additions & 2 deletions src/urlencode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ String urlencode(String str)
char c;
char code0;
char code1;
char code2;
//char code2;
for (unsigned int i =0; i < str.length(); i++){
c=str.charAt(i);
if (c == ' '){
Expand All @@ -71,7 +71,7 @@ String urlencode(String str)
if (c > 9){
code0=c - 10 + 'A';
}
code2='\0';
//code2='\0';
encodedString+='%';
encodedString+=code0;
encodedString+=code1;
Expand Down
14 changes: 5 additions & 9 deletions src/web_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ void dumpRequest(AsyncWebServerRequest *request) {
int headers = request->headers();
int i;
for(i=0; i<headers; i++) {
AsyncWebHeader* h = request->getHeader(i);
const AsyncWebHeader* h = request->getHeader(i);
DBUGF("_HEADER[%s]: %s", h->name().c_str(), h->value().c_str());
}

int params = request->params();
for(i = 0; i < params; i++) {
AsyncWebParameter* p = request->getParam(i);
const AsyncWebParameter* p = request->getParam(i);
if(p->isFile()){
DBUGF("_FILE[%s]: %s, size: %u", p->name().c_str(), p->value().c_str(), p->size());
} else if(p->isPost()){
Expand Down Expand Up @@ -292,7 +292,7 @@ handleSaveMqtt(AsyncWebServerRequest *request) {
String pass = request->arg("pass");

int port = 1883;
AsyncWebParameter *portParm = request->getParam("port");
const AsyncWebParameter *portParm = request->getParam("port");
if(nullptr != portParm) {
port = portParm->value().toInt();
}
Expand Down Expand Up @@ -926,15 +926,11 @@ void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient *client, AwsEventTy

void
web_server_setup() {
// SPIFFS.begin(); // mount the fs

// Setup the static files
// server.serveStatic("/", SPIFFS, "/")
// .setDefaultFile("index.html");

// Add the Web Socket server
ws.onEvent(onWsEvent);
server.addHandler(&ws);

// static file handler
server.addHandler(&staticFile);

// Start server & server root html /
Expand Down
20 changes: 14 additions & 6 deletions src/web_server_static.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ StaticFileWebHandler::StaticFileWebHandler()
{
}

bool StaticFileWebHandler::_getFile(AsyncWebServerRequest *request, StaticFile **file)
bool StaticFileWebHandler::_getFile(AsyncWebServerRequest *request, StaticFile **file) const
{
DBUGF("[StaticFileWebHandler::_getFile] entry");
// Remove the found uri
String path = request->url();
if(path == "/") {
Expand All @@ -53,11 +54,15 @@ bool StaticFileWebHandler::_getFile(AsyncWebServerRequest *request, StaticFile *
return false;
}

bool StaticFileWebHandler::canHandle(AsyncWebServerRequest *request)
bool StaticFileWebHandler::canHandle(AsyncWebServerRequest *request) const
{
StaticFile *file = NULL;
if (request->method() == HTTP_GET &&
_getFile(request, &file))
DBUGF("[StaticFileWebHandler::canHandle] entry");
if (request->isHTTP() &&
request->method() == HTTP_GET &&
//request->url().startsWith(_uri) &&
_getFile(request, &file)
)
{
request->_tempObject = file;
DBUGF("[StaticFileWebHandler::canHandle] TRUE");
Expand All @@ -73,22 +78,24 @@ void StaticFileWebHandler::handleRequest(AsyncWebServerRequest *request)

// Are we authenticated
if(wifi_mode_is_sta() &&
_username != "" && _password != "" &&
false == request->authenticate(_username.c_str(), _password.c_str()))
www_username != "" && www_password != "" &&
false == request->authenticate(www_username.c_str(), www_password.c_str()))
{
request->requestAuthentication(esp_hostname.c_str());
return;
}

// Get the filename from request->_tempObject and free it
StaticFile *file = (StaticFile *)request->_tempObject;
DBUGF("[StaticFileWebHandler::handleRequest] checking if file set");
if (file)
{
request->_tempObject = NULL;
AsyncWebServerResponse *response = new StaticFileResponse(200, file);
request->send(response);
} else {
request->send(404);
DBUGF("[StaticFileWebHandler::handleRequest] returning 404");
}
}

Expand Down Expand Up @@ -119,6 +126,7 @@ size_t StaticFileResponse::write(AsyncWebServerRequest *request)
// How should failures to send be handled?
request->client()->send();
}
return 0;
}

size_t StaticFileResponse::writeData(AsyncWebServerRequest *request)
Expand Down
4 changes: 2 additions & 2 deletions src/web_server_static.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ struct StaticFile
class StaticFileWebHandler: public AsyncWebHandler
{
private:
bool _getFile(AsyncWebServerRequest *request, StaticFile **file = NULL);
bool _getFile(AsyncWebServerRequest *request, StaticFile **file = NULL) const;
protected:
public:
StaticFileWebHandler();
virtual bool canHandle(AsyncWebServerRequest *request) override final;
virtual bool canHandle(AsyncWebServerRequest *request) const override final;
virtual void handleRequest(AsyncWebServerRequest *request) override final;
};

Expand Down
15 changes: 0 additions & 15 deletions src/web_static/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,6 @@

Do not edit these files, these are all auto generated from the files in `gui`.

To update do the following:

1. Make sure the content of `gui` is up-to-date

```shell
cd gui
git checkout master
git pull
```

2. 'Build' the UI

```shell
npm install
npm run build
```

3. Build/Upload the project as normal
Loading