ESPHome 2026.3.0-dev
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
11
12#ifdef USE_BINARY_SENSOR
14#endif
15
16#ifdef USE_SENSOR
18#endif
19
20#ifdef USE_TEXT_SENSOR
22#endif
23
24#include <esp_wireguard.h>
25
26namespace esphome::wireguard {
27
29struct AllowedIP {
30 const char *ip;
31 const char *netmask;
32};
33
36 public:
37 void setup() override;
38 void loop() override;
39 void update() override;
40 void dump_config() override;
41 void on_shutdown() override;
42 bool can_proceed() override;
43
45
46 void set_address(const char *address) { this->address_ = address; }
47 void set_netmask(const char *netmask) { this->netmask_ = netmask; }
48 void set_private_key(const char *key) { this->private_key_ = key; }
49 void set_peer_endpoint(const char *endpoint) { this->peer_endpoint_ = endpoint; }
50 void set_peer_public_key(const char *key) { this->peer_public_key_ = key; }
51 void set_peer_port(uint16_t port) { this->peer_port_ = port; }
52 void set_preshared_key(const char *key) { this->preshared_key_ = key; }
53
55 void set_address(const std::string &address) = delete;
56 void set_netmask(const std::string &netmask) = delete;
57 void set_private_key(const std::string &key) = delete;
58 void set_peer_endpoint(const std::string &endpoint) = delete;
59 void set_peer_public_key(const std::string &key) = delete;
60 void set_preshared_key(const std::string &key) = delete;
61
62 void set_allowed_ips(std::initializer_list<AllowedIP> ips) { this->allowed_ips_ = ips; }
64 void set_allowed_ips(std::initializer_list<std::tuple<std::string, std::string>> ips) = delete;
65
66 void set_keepalive(uint16_t seconds);
67 void set_reboot_timeout(uint32_t seconds);
68 void set_srctime(time::RealTimeClock *srctime);
69
70#ifdef USE_BINARY_SENSOR
73#endif
74
75#ifdef USE_SENSOR
77#endif
78
79#ifdef USE_TEXT_SENSOR
81#endif
82
85
87 void enable();
88
90 void disable();
91
94
96 bool is_enabled();
97
98 bool is_peer_up() const;
99 time_t get_latest_handshake() const;
100
101 protected:
102 const char *address_{nullptr};
103 const char *netmask_{nullptr};
104 const char *private_key_{nullptr};
105 const char *peer_endpoint_{nullptr};
106 const char *peer_public_key_{nullptr};
107 const char *preshared_key_{nullptr};
108
110
111 uint16_t peer_port_;
112 uint16_t keepalive_;
114
116
117#ifdef USE_BINARY_SENSOR
120#endif
121
122#ifdef USE_SENSOR
124#endif
125
126#ifdef USE_TEXT_SENSOR
128#endif
129
131 bool proceed_allowed_ = true;
132
134 bool enabled_ = true;
135
136 wireguard_config_t wg_config_ = ESP_WIREGUARD_CONFIG_DEFAULT();
137 wireguard_ctx_t wg_ctx_ = ESP_WIREGUARD_CONTEXT_DEFAULT();
138
139 esp_err_t wg_initialized_ = ESP_FAIL;
140 esp_err_t wg_connected_ = ESP_FAIL;
141
144
152
153 void start_connection_();
154 void stop_connection_();
155};
156
157// These are used for possibly long DNS resolution to temporarily suspend the watchdog
160
162static constexpr size_t MASK_KEY_BUFFER_SIZE = 12;
163
165void mask_key_to(char *buffer, size_t len, const char *key);
166
168template<typename... Ts> class WireguardPeerOnlineCondition : public Condition<Ts...>, public Parented<Wireguard> {
169 public:
170 bool check(const Ts &...x) override { return this->parent_->is_peer_up(); }
171};
172
174template<typename... Ts> class WireguardEnabledCondition : public Condition<Ts...>, public Parented<Wireguard> {
175 public:
176 bool check(const Ts &...x) override { return this->parent_->is_enabled(); }
177};
178
180template<typename... Ts> class WireguardEnableAction : public Action<Ts...>, public Parented<Wireguard> {
181 public:
182 void play(const Ts &...x) override { this->parent_->enable(); }
183};
184
186template<typename... Ts> class WireguardDisableAction : public Action<Ts...>, public Parented<Wireguard> {
187 public:
188 void play(const Ts &...x) override { this->parent_->disable(); }
189};
190
191} // namespace esphome::wireguard
192#endif
uint8_t address
Definition bl0906.h:4
Base class for all automation conditions.
Definition automation.h:304
Fixed-capacity vector - allocates once at runtime, never reallocates This avoids std::vector template...
Definition helpers.h:299
Helper class to easily give an object a parent of type T.
Definition helpers.h:1618
This class simplifies creating components that periodically check a state.
Definition component.h:527
Base class for all binary_sensor-type classes.
Base-class for all sensors.
Definition sensor.h:47
The RealTimeClock class exposes common timekeeping functions via the device's local real-time clock.
Action to disable Wireguard component.
Definition wireguard.h:186
void play(const Ts &...x) override
Definition wireguard.h:188
Action to enable Wireguard component.
Definition wireguard.h:180
void play(const Ts &...x) override
Definition wireguard.h:182
Condition to check if Wireguard component is enabled.
Definition wireguard.h:174
bool check(const Ts &...x) override
Definition wireguard.h:176
Main Wireguard component class.
Definition wireguard.h:35
binary_sensor::BinarySensor * enabled_sensor_
Definition wireguard.h:119
void set_netmask(const char *netmask)
Definition wireguard.h:47
FixedVector< AllowedIP > allowed_ips_
Definition wireguard.h:109
void set_keepalive(uint16_t seconds)
bool enabled_
When false the wireguard link will not be established.
Definition wireguard.h:134
void set_allowed_ips(std::initializer_list< AllowedIP > ips)
Definition wireguard.h:62
binary_sensor::BinarySensor * status_sensor_
Definition wireguard.h:118
float get_setup_priority() const override
Definition wireguard.h:44
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:123
time::RealTimeClock * srctime_
Definition wireguard.h:115
void set_preshared_key(const char *key)
Definition wireguard.h:52
bool proceed_allowed_
Set to false to block the setup step until peer is connected.
Definition wireguard.h:131
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:48
void set_address_sensor(text_sensor::TextSensor *sensor)
text_sensor::TextSensor * address_sensor_
Definition wireguard.h:127
uint32_t wg_peer_offline_time_
The last time the remote peer become offline.
Definition wireguard.h:143
void set_address(const char *address)
Definition wireguard.h:46
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:51
void set_peer_endpoint(const char *endpoint)
Definition wireguard.h:49
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:50
time_t latest_saved_handshake_
The latest saved handshake.
Definition wireguard.h:151
wireguard_config_t wg_config_
Definition wireguard.h:136
Condition to check if remote peer is online.
Definition wireguard.h:168
constexpr float BEFORE_CONNECTION
For components that should be initialized after WiFi and before API is connected.
Definition component.h:39
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:817
Allowed IP entry for WireGuard peer configuration.
Definition wireguard.h:29
uint16_t x
Definition tt21100.cpp:5