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

Adding Wireguard Tunnel #524

Open
wants to merge 49 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
ee20151
quick & dirty wireguard version for openDTU
janchri Jan 15, 2023
f69ecb5
Plaformio_override.ini update
janchri Jan 15, 2023
b8376cb
Fix timezone/configTime() in beginWireGuard()
janchri Jan 15, 2023
245cdb0
Add wireguard to Configuration.h & WebApi
janchri Jan 16, 2023
cddde6c
Fix linking error -> class name was wrong in WebApi_wireguard.cpp
janchri Jan 16, 2023
177fa4d
fix typo
janchri Jan 16, 2023
1cf3abc
Minor fixes due to code quality
janchri Jan 16, 2023
1147b34
Create new Class Files for Wireguard; improve encapsulation
janchri Jan 16, 2023
3e0a9da
wireguardclass created; better encapsulation
janchri Jan 16, 2023
f71f8c0
changed gitignore for debugging, added private keys
janchri Jan 16, 2023
3497dcb
minor fix: typo + defaults_wireguard.h added for storing secret keys/…
janchri Jan 16, 2023
5fb1789
Merge branch 'tbnobody:master' into master
janchri Jan 17, 2023
71591aa
fix array length of key variable to [45]
janchri Jan 17, 2023
aeec268
v3.2.2
janchri Jan 18, 2023
e3e3c87
Minor fixes in backend + first frontend integration for wireguard
janchri Jan 18, 2023
5c52cad
Fix language files; added EN, FR missing!
janchri Jan 18, 2023
269075d
restart needed for wireguard setup changes; working on a fix
janchri Jan 19, 2023
4ec13fb
Merge branch 'tbnobody:master' into master
janchri Jan 19, 2023
0294f2e
Update vuejs frontend
janchri Jan 19, 2023
92ad765
fix .gitignore
janchri Jan 21, 2023
0cb91b7
quick & dirty wireguard version for openDTU
janchri Jan 15, 2023
9ec6a3a
Plaformio_override.ini update
janchri Jan 15, 2023
e8e10f7
Fix timezone/configTime() in beginWireGuard()
janchri Jan 15, 2023
21c6e5b
Add wireguard to Configuration.h & WebApi
janchri Jan 16, 2023
ff21d4d
Fix linking error -> class name was wrong in WebApi_wireguard.cpp
janchri Jan 16, 2023
d54c1ed
fix typo
janchri Jan 16, 2023
f7f939a
Minor fixes due to code quality
janchri Jan 16, 2023
ffebfaa
Create new Class Files for Wireguard; improve encapsulation
janchri Jan 16, 2023
9f62701
wireguardclass created; better encapsulation
janchri Jan 16, 2023
e970e52
changed gitignore for debugging, added private keys
janchri Jan 16, 2023
9b76496
minor fix: typo + defaults_wireguard.h added for storing secret keys/…
janchri Jan 16, 2023
46bcfb5
fix array length of key variable to [45]
janchri Jan 17, 2023
d1e9ab7
v3.2.2
janchri Jan 18, 2023
7732e07
Minor fixes in backend + first frontend integration for wireguard
janchri Jan 18, 2023
3cc7f05
Fix language files; added EN, FR missing!
janchri Jan 18, 2023
b8ce458
restart needed for wireguard setup changes; working on a fix
janchri Jan 19, 2023
52c55b9
Update vuejs frontend
janchri Jan 19, 2023
5743df3
fix .gitignore
janchri Jan 21, 2023
7db01c9
merge upstream/master and wireguard
janchri Jan 21, 2023
7ca107c
merge frontend app.js.gz
janchri Jan 21, 2023
bd201b9
Merge remote-tracking branch 'upstream/master'
janchri Jan 23, 2023
bb996fc
merge apps.js.gz with upstream/master
janchri Jan 23, 2023
de21805
Merge remote-tracking branch 'upstream/master'
janchri Feb 4, 2023
76b730b
Merge branch 'master' of https://github.com/tbnobody/OpenDTU
janchri Feb 23, 2023
a3a06cc
Merge upstream/master
janchri Feb 23, 2023
29ad6b2
Merge upstream/master webapp
janchri Feb 23, 2023
d8440f6
added allowedIp/allowedMask + Shelly3em support
janchri Feb 25, 2023
c23218e
set shelly to 3sec polling
janchri Feb 28, 2023
01a7b2e
Shelly 3sec polling
janchri Feb 28, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"C_Cpp.clang_format_style": "WebKit"
"C_Cpp.clang_format_style": "WebKit",
"files.associations": {
"tcpip_adapter.h": "c"
}
}
11 changes: 11 additions & 0 deletions include/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ struct CONFIG_T {
bool WiFi_Dhcp;
char WiFi_Hostname[WIFI_MAX_HOSTNAME_STRLEN + 1];

bool Wg_Enabled;
bool Wg_Refresh;
char Wg_Endpoint_Address[15];
uint Wg_Endpoint_Port;
char Wg_Endpoint_Public_Key[45];
char Wg_Opendtu_Public_Key[45];
byte Wg_Opendtu_Allowed_Ip[4];
byte Wg_Opendtu_Allowed_Mask[4];
byte Wg_Opendtu_Local_Ip[4];
char Wg_Opendtu_Private_Key[45];

char Ntp_Server[NTP_MAX_SERVER_STRLEN + 1];
char Ntp_Timezone[NTP_MAX_TIMEZONE_STRLEN + 1];
char Ntp_TimezoneDescr[NTP_MAX_TIMEZONEDESCR_STRLEN + 1];
Expand Down
17 changes: 17 additions & 0 deletions include/MqttHandleShelly3EM.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once

#include <cstdint>
#include <HTTPClient.h>

class MqttHandleShelly3EMClass {
public:
void init();
void loop();

private:
uint32_t _lastPublish;
HTTPClient http;
};

extern MqttHandleShelly3EMClass MqttHandleShelly3EM;
2 changes: 2 additions & 0 deletions include/WebApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "WebApi_security.h"
#include "WebApi_sysstatus.h"
#include "WebApi_webapp.h"
#include "WebApi_wireguard.h"
#include "WebApi_ws_console.h"
#include "WebApi_ws_live.h"
#include <ESPAsyncWebServer.h>
Expand Down Expand Up @@ -52,6 +53,7 @@ class WebApiClass {
WebApiSecurityClass _webApiSecurity;
WebApiSysstatusClass _webApiSysstatus;
WebApiWebappClass _webApiWebapp;
WebApiWireguardClass _webApiWireguard;
WebApiWsConsoleClass _webApiWsConsole;
WebApiWsLiveClass _webApiWsLive;
};
Expand Down
17 changes: 17 additions & 0 deletions include/WebApi_wireguard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once

#include <ESPAsyncWebServer.h>

class WebApiWireguardClass {
public:
void init(AsyncWebServer* server);
void loop();

private:
void onWireguardStatus(AsyncWebServerRequest* request);
void onWireguardAdminGet(AsyncWebServerRequest* request);
void onWireguardAdminPost(AsyncWebServerRequest* request);

AsyncWebServer* _server;
};
18 changes: 18 additions & 0 deletions include/WireguardSettings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once

#include <WireGuard-ESP32.h>

class WireguardSettingsClass {
public:
WireguardSettingsClass();
void init();
void loop();

private:
WireGuard wg;
void endWireGuard();
void beginWireGuard();
};

extern WireguardSettingsClass WireguardSettings;
10 changes: 10 additions & 0 deletions include/defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@
#define WIFI_PASSWORD ""
#define WIFI_DHCP true

#define WG_ENABLED false
#define WG_ENDPOINT_ADDRESS ""
#define WG_ENDPOINT_PORT 51820
#define WG_ENDPOINT_PUBLIC_KEY ""
#define WG_OPENDTU_LOCAL_IP ""
#define WG_OPENDTU_ALLOWED_IP ""
#define WG_OPENDTU_ALLOWED_MASK ""
#define WG_OPENDTU_PUBLIC_KEY ""
#define WG_OPENDTU_PRIVATE_KEY ""

#define NTP_SERVER "pool.ntp.org"
#define NTP_TIMEZONE "CET-1CEST,M3.5.0,M10.5.0/3"
#define NTP_TIMEZONEDESCR "Europe/Berlin"
Expand Down
1 change: 1 addition & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ lib_deps =
nrf24/RF24 @ ^1.4.5
olikraus/U8g2 @ ^2.34.13
buelowp/sunset @ ^1.1.7
https://github.com/janchri/WireGuard-ESP32-Arduino
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to pin the exact version so this stays reproducible

Copy link
Contributor

@stefan123t stefan123t Sep 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ESPHome uses this in their platformio.ini:

droscy/[email protected]            ; wireguard

@janchri would it be better to use the Library that Tomoyuki Sakurai created for ESP-IDF and Simone Rosetto already maintains for ESPHOME ?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And that is also fork because of some esphome optimizations, but works perfectly.
Maybe it make sense to start new attempt with this "esphome" fork...


extra_scripts =
pre:auto_firmware_version.py
Expand Down
4 changes: 2 additions & 2 deletions platformio_override.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
; Under Linux, the ports are in the format /dev/tty***, typically /dev/ttyUSB0
; To look up the port, execute ls /dev/tty*, then connect the device
; then execute ls /dev/tty* again and check which device was added
monitor_port = COM4
upload_port = COM4
; monitor_port = COM4
; upload_port = COM4


; you can define your personal board and/or settings here
Expand Down
38 changes: 38 additions & 0 deletions src/Configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ bool ConfigurationClass::write()
wifi["dhcp"] = config.WiFi_Dhcp;
wifi["hostname"] = config.WiFi_Hostname;

JsonObject wg = doc.createNestedObject("wg");
wg["enabled"] = config.Wg_Enabled;
wg["endpoint_address"] = config.Wg_Endpoint_Address;
wg["endpoint_port"] = config.Wg_Endpoint_Port;
wg["endpoint_public_key"] = config.Wg_Endpoint_Public_Key;
wg["opendtu_local_ip"] = IPAddress(config.Wg_Opendtu_Local_Ip).toString();
wg["opendtu_allowed_ip"] = IPAddress(config.Wg_Opendtu_Allowed_Ip).toString();
wg["opendtu_allowed_mask"] = IPAddress(config.Wg_Opendtu_Allowed_Mask).toString();
wg["opendtu_public_key"] = config.Wg_Opendtu_Public_Key;
wg["opendtu_private_key"] = config.Wg_Opendtu_Private_Key;

JsonObject ntp = doc.createNestedObject("ntp");
ntp["server"] = config.Ntp_Server;
ntp["timezone"] = config.Ntp_Timezone;
Expand Down Expand Up @@ -177,6 +188,33 @@ bool ConfigurationClass::read()

config.WiFi_Dhcp = wifi["dhcp"] | WIFI_DHCP;

JsonObject wg = doc["wg"];
config.Wg_Enabled = wg["enabled"] | WG_ENABLED;
config.Wg_Refresh = false;
strlcpy(config.Wg_Opendtu_Public_Key, wg["opendtu_public_key"] | WG_OPENDTU_PUBLIC_KEY, sizeof(config.Wg_Opendtu_Public_Key));
strlcpy(config.Wg_Opendtu_Private_Key, wg["opendtu_private_key"] | WG_OPENDTU_PRIVATE_KEY, sizeof(config.Wg_Opendtu_Private_Key));
strlcpy(config.Wg_Endpoint_Address, wg["endpoint_address"] | WG_ENDPOINT_ADDRESS, sizeof(config.Wg_Endpoint_Address));
strlcpy(config.Wg_Endpoint_Public_Key, wg["endpoint_public_key"] | WG_ENDPOINT_PUBLIC_KEY, sizeof(config.Wg_Endpoint_Public_Key));
config.Wg_Endpoint_Port = wg["endpoint_port"] | WG_ENDPOINT_PORT;
IPAddress Wg_Opendtu_Local_Ip;
Wg_Opendtu_Local_Ip.fromString(wg["opendtu_local_ip"] | WG_OPENDTU_LOCAL_IP);
config.Wg_Opendtu_Local_Ip[0] = Wg_Opendtu_Local_Ip[0];
config.Wg_Opendtu_Local_Ip[1] = Wg_Opendtu_Local_Ip[1];
config.Wg_Opendtu_Local_Ip[2] = Wg_Opendtu_Local_Ip[2];
config.Wg_Opendtu_Local_Ip[3] = Wg_Opendtu_Local_Ip[3];
IPAddress Wg_Opendtu_Allowed_Ip;
Wg_Opendtu_Allowed_Ip.fromString(wg["opendtu_allowed_ip"] | WG_OPENDTU_ALLOWED_IP);
config.Wg_Opendtu_Allowed_Ip[0] = Wg_Opendtu_Allowed_Ip[0];
config.Wg_Opendtu_Allowed_Ip[1] = Wg_Opendtu_Allowed_Ip[1];
config.Wg_Opendtu_Allowed_Ip[2] = Wg_Opendtu_Allowed_Ip[2];
config.Wg_Opendtu_Allowed_Ip[3] = Wg_Opendtu_Allowed_Ip[3];
IPAddress Wg_Opendtu_Allowed_Mask;
Wg_Opendtu_Allowed_Mask.fromString(wg["opendtu_allowed_mask"] | WG_OPENDTU_ALLOWED_MASK);
config.Wg_Opendtu_Allowed_Mask[0] = Wg_Opendtu_Allowed_Mask[0];
config.Wg_Opendtu_Allowed_Mask[1] = Wg_Opendtu_Allowed_Mask[1];
config.Wg_Opendtu_Allowed_Mask[2] = Wg_Opendtu_Allowed_Mask[2];
config.Wg_Opendtu_Allowed_Mask[3] = Wg_Opendtu_Allowed_Mask[3];

JsonObject ntp = doc["ntp"];
strlcpy(config.Ntp_Server, ntp["server"] | NTP_SERVER, sizeof(config.Ntp_Server));
strlcpy(config.Ntp_Timezone, ntp["timezone"] | NTP_TIMEZONE, sizeof(config.Ntp_Timezone));
Expand Down
71 changes: 71 additions & 0 deletions src/MqttHandleShelly3EM.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2023 Christoph Jans and others
*/
#include "MqttHandleShelly3EM.h"
#include "Configuration.h"
#include "MqttSettings.h"
#include "NetworkSettings.h"
#include <Hoymiles.h>
#include "MessageOutput.h"
#include <ArduinoJson.h>

MqttHandleShelly3EMClass MqttHandleShelly3EM;

void MqttHandleShelly3EMClass::init()
{
http.setReuse(true);
}

void MqttHandleShelly3EMClass::loop()
{
if ( !MqttSettings.getConnected() ) {
return;
}

//const CONFIG_T& config = Configuration.get();
//if (millis() - _lastPublish > (config.Mqtt_PublishInterval * 1000)) {
if (millis() - _lastPublish > (3 * 999)) {
// Send request
MessageOutput.println(F("Polling SHEM-3... "));
http.begin("http://192.168.1.103/status");
int httpCode = http.GET();

if (httpCode == 200) {
// Parse response
DynamicJsonDocument doc(2048);
DeserializationError error = deserializeJson(doc, http.getStream());

// Check for errors
if (error) {
MessageOutput.print(F("deserializeJson() failed: "));
MessageOutput.println(error.c_str());
} else{
// Publish values
MqttSettings.publishGeneric("shelly3EM/type", "SHEM-3", 0, Configuration.get().Mqtt_Retain);
MqttSettings.publishGeneric("shelly3EM/status/unixtime", doc["unixtime"], 0, Configuration.get().Mqtt_Retain);
MqttSettings.publishGeneric("shelly3EM/status/total_power", doc["total_power"], 0, Configuration.get().Mqtt_Retain);

int i = 0;
for (JsonObject emeter : doc["emeters"].as<JsonArray>()) {
MqttSettings.publishGeneric("shelly3EM/status/"+String(i)+"/power", emeter["power"], 0, Configuration.get().Mqtt_Retain);
MqttSettings.publishGeneric("shelly3EM/status/"+String(i)+"/pf", emeter["pf"], 0, Configuration.get().Mqtt_Retain);
MqttSettings.publishGeneric("shelly3EM/status/"+String(i)+"/current", emeter["current"], 0, Configuration.get().Mqtt_Retain);
MqttSettings.publishGeneric("shelly3EM/status/"+String(i)+"/voltage", emeter["voltage"], 0, Configuration.get().Mqtt_Retain);
MqttSettings.publishGeneric("shelly3EM/status/"+String(i)+"/is_valid", emeter["is_valid"], 0, Configuration.get().Mqtt_Retain);
MqttSettings.publishGeneric("shelly3EM/status/"+String(i)+"/total", emeter["total"], 0, Configuration.get().Mqtt_Retain);
MqttSettings.publishGeneric("shelly3EM/status/"+String(i)+"/total_returned", emeter["total_returned"], 0, Configuration.get().Mqtt_Retain);
i++;
}
}
}
else{
// Falls HTTP-Error
MessageOutput.println("Shelly 3EM Connection Error: " + String(httpCode));
}
_lastPublish = millis();

// Disconnect
http.end();
}
}
2 changes: 2 additions & 0 deletions src/WebApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ void WebApiClass::init()
_webApiSecurity.init(&_server);
_webApiSysstatus.init(&_server);
_webApiWebapp.init(&_server);
_webApiWireguard.init(&_server);
_webApiWsConsole.init(&_server);
_webApiWsLive.init(&_server);

Expand All @@ -58,6 +59,7 @@ void WebApiClass::loop()
_webApiSecurity.loop();
_webApiSysstatus.loop();
_webApiWebapp.loop();
_webApiWireguard.loop();
_webApiWsConsole.loop();
_webApiWsLive.loop();
}
Expand Down
Loading