ESPHome 2026.2.4
Loading...
Searching...
No Matches
wireguard.h
Go to the documentation of this file.
1#pragma once
3#ifdef USE_WIREGUARD
4#include <ctime>
5#include <initializer_list>
6
10
11#ifdef USE_BINARY_SENSOR
13#endif
14
15#ifdef USE_SENSOR
17#endif
18
19#ifdef USE_TEXT_SENSOR
21#endif
22
23#include <esp_wireguard.h>
24
25namespace esphome::wireguard {
26
28struct AllowedIP {
29 const char *ip;
30 const char *netmask;
31};
32
35 public:
36 void setup() override;
37 void loop() override;
38 void update() override;
39 void dump_config() override;
40 void on_shutdown() override;
41 bool can_proceed() override;
42
44
45 void set_address(const char *address) { this->address_ = address; }
46 void set_netmask(const char *netmask) { this->netmask_ = netmask; }
47 void set_private_key(const char *key) { this->private_key_ = key; }
48 void set_peer_endpoint(const char *endpoint) { this->peer_endpoint_ = endpoint; }
49 void set_peer_public_key(const char *key) { this->peer_public_key_ = key; }
50 void set_peer_port(uint16_t port) { this->peer_port_ = port; }
51 void set_preshared_key(const char *key) { this->preshared_key_ = key; }
52
54 void set_address(const std::string &address) = delete;
55 void set_netmask(const std::string &netmask) = delete;
56 void set_private_key(const std::string &key) = delete;
57 void set_peer_endpoint(const std::string &endpoint) = delete;
58 void set_peer_public_key(const std::string &key) = delete;
59 void set_preshared_key(const std::string &key) = delete;
60
61 void set_allowed_ips(std::initializer_list<AllowedIP> ips) { this->allowed_ips_ = ips; }
63 void set_allowed_ips(std::initializer_list<std::tuple<std::string, std::string>> ips) = delete;
64
65 void set_keepalive(uint16_t seconds);
66 void set_reboot_timeout(uint32_t seconds);
67 void set_srctime(time::RealTimeClock *srctime);
68
69#ifdef USE_BINARY_SENSOR
72#endif
73
74#ifdef USE_SENSOR
76#endif
77
78#ifdef USE_TEXT_SENSOR
80#endif
81
84
86 void enable();
87
89 void disable();
90
93
95 bool is_enabled();
96
97 bool is_peer_up() const;
98 time_t get_latest_handshake() const;
99
100 protected:
101 const char *address_{nullptr};
102 const char *netmask_{nullptr};
103 const char *private_key_{nullptr};
104 const char *peer_endpoint_{nullptr};
105 const char *peer_public_key_{nullptr};
106 const char *preshared_key_{nullptr};
107
109
110 uint16_t peer_port_;
111 uint16_t keepalive_;
113
115
116#ifdef USE_BINARY_SENSOR
119#endif
120
121#ifdef USE_SENSOR
123#endif
124
125#ifdef USE_TEXT_SENSOR
127#endif
128
130 bool proceed_allowed_ = true;
131
133 bool enabled_ = true;
134
135 wireguard_config_t wg_config_ = ESP_WIREGUARD_CONFIG_DEFAULT();
136 wireguard_ctx_t wg_ctx_ = ESP_WIREGUARD_CONTEXT_DEFAULT();
137
138 esp_err_t wg_initialized_ = ESP_FAIL;
139 esp_err_t wg_connected_ = ESP_FAIL;
140
143
151
152 void start_connection_();
153 void stop_connection_();
154};
155
156// These are used for possibly long DNS resolution to temporarily suspend the watchdog
159
161static constexpr size_t MASK_KEY_BUFFER_SIZE = 12;
162
164void mask_key_to(char *buffer, size_t len, const char *key);
165
167template<typename... Ts> class WireguardPeerOnlineCondition : public Condition<Ts...>, public Parented<Wireguard> {
168 public:
169 bool check(const Ts &...x) override { return this->parent_->is_peer_up(); }
170};
171
173template<typename... Ts> class WireguardEnabledCondition : public Condition<Ts...>, public Parented<Wireguard> {
174 public:
175 bool check(const Ts &...x) override { return this->parent_->is_enabled(); }
176};
177
179template<typename... Ts> class WireguardEnableAction : public Action<Ts...>, public Parented<Wireguard> {
180 public:
181 void play(const Ts &...x) override { this->parent_->enable(); }
182};
183
185template<typename... Ts> class WireguardDisableAction : public Action<Ts...>, public Parented<Wireguard> {
186 public:
187 void play(const Ts &...x) override { this->parent_->disable(); }
188};
189
190} // namespace esphome::wireguard
191#endif
uint8_t address
Definition bl0906.h:4
Base class for all automation conditions.
Definition automation.h:258
Fixed-capacity vector - allocates once at runtime, never reallocates This avoids std::vector template...
Definition helpers.h:227
Helper class to easily give an object a parent of type T.
Definition helpers.h:1471
This class simplifies creating components that periodically check a state.
Definition component.h:512
Base class for all binary_sensor-type classes.
Base-class for all sensors.
Definition sensor.h:43
The RealTimeClock class exposes common timekeeping functions via the device's local real-time clock.
Action to disable Wireguard component.
Definition wireguard.h:185
void play(const Ts &...x) override
Definition wireguard.h:187
Action to enable Wireguard component.
Definition wireguard.h:179
void play(const Ts &...x) override
Definition wireguard.h:181
Condition to check if Wireguard component is enabled.
Definition wireguard.h:173
bool check(const Ts &...x) override
Definition wireguard.h:175
Main Wireguard component class.
Definition wireguard.h:34
binary_sensor::BinarySensor * enabled_sensor_
Definition wireguard.h:118
void set_netmask(const char *netmask)
Definition wireguard.h:46
FixedVector< AllowedIP > allowed_ips_
Definition wireguard.h:108
void set_keepalive(uint16_t seconds)
bool enabled_
When false the wireguard link will not be established.
Definition wireguard.h:133
void set_allowed_ips(std::initializer_list< AllowedIP > ips)
Definition wireguard.h:61
binary_sensor::BinarySensor * status_sensor_
Definition wireguard.h:117
float get_setup_priority() const override
Definition wireguard.h:43
void set_status_sensor(binary_sensor::BinarySensor *sensor)
void set_srctime(time::RealTimeClock *srctime)
void publish_enabled_state()
Publish the enabled state if the enabled binary sensor is configured.
time_t get_latest_handshake() const
sensor::Sensor * handshake_sensor_
Definition wireguard.h:122
time::RealTimeClock * srctime_
Definition wireguard.h:114
void set_preshared_key(const char *key)
Definition wireguard.h:51
bool proceed_allowed_
Set to false to block the setup step until peer is connected.
Definition wireguard.h:130
void set_peer_endpoint(const std::string &endpoint)=delete
void set_allowed_ips(std::initializer_list< std::tuple< std::string, std::string > > ips)=delete
Prevent accidental use of std::string which would dangle.
void set_peer_public_key(const std::string &key)=delete
void set_reboot_timeout(uint32_t seconds)
void disable_auto_proceed()
Block the setup step until peer is connected.
void set_private_key(const char *key)
Definition wireguard.h:47
void set_address_sensor(text_sensor::TextSensor *sensor)
text_sensor::TextSensor * address_sensor_
Definition wireguard.h:126
uint32_t wg_peer_offline_time_
The last time the remote peer become offline.
Definition wireguard.h:142
void set_address(const char *address)
Definition wireguard.h:45
void disable()
Stop any running connection and disable the WireGuard component.
void set_enabled_sensor(binary_sensor::BinarySensor *sensor)
void set_address(const std::string &address)=delete
Prevent accidental use of std::string which would dangle.
void set_handshake_sensor(sensor::Sensor *sensor)
void set_preshared_key(const std::string &key)=delete
bool is_enabled()
Return if the WireGuard component is or is not enabled.
void enable()
Enable the WireGuard component.
void set_peer_port(uint16_t port)
Definition wireguard.h:50
void set_peer_endpoint(const char *endpoint)
Definition wireguard.h:48
void set_netmask(const std::string &netmask)=delete
void set_private_key(const std::string &key)=delete
void set_peer_public_key(const char *key)
Definition wireguard.h:49
time_t latest_saved_handshake_
The latest saved handshake.
Definition wireguard.h:150
wireguard_config_t wg_config_
Definition wireguard.h:135
Condition to check if remote peer is online.
Definition wireguard.h:167
const float BEFORE_CONNECTION
For components that should be initialized after WiFi and before API is connected.
Definition component.cpp:90
void mask_key_to(char *buffer, size_t len, const char *key)
Strip most part of the key only for secure printing.
std::string size_t len
Definition helpers.h:692
Allowed IP entry for WireGuard peer configuration.
Definition wireguard.h:28
uint16_t x
Definition tt21100.cpp:5