ESPHome 2025.5.0
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 <vector>
6#include <tuple>
7
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 {
26namespace wireguard {
27
30 public:
31 void setup() override;
32 void loop() override;
33 void update() override;
34 void dump_config() override;
35 void on_shutdown() override;
36 bool can_proceed() override;
37
39
40 void set_address(const std::string &address);
41 void set_netmask(const std::string &netmask);
42 void set_private_key(const std::string &key);
43 void set_peer_endpoint(const std::string &endpoint);
44 void set_peer_public_key(const std::string &key);
45 void set_peer_port(uint16_t port);
46 void set_preshared_key(const std::string &key);
47
48 void add_allowed_ip(const std::string &ip, const std::string &netmask);
49
50 void set_keepalive(uint16_t seconds);
51 void set_reboot_timeout(uint32_t seconds);
52 void set_srctime(time::RealTimeClock *srctime);
53
54#ifdef USE_BINARY_SENSOR
57#endif
58
59#ifdef USE_SENSOR
61#endif
62
63#ifdef USE_TEXT_SENSOR
65#endif
66
69
71 void enable();
72
74 void disable();
75
78
80 bool is_enabled();
81
82 bool is_peer_up() const;
83 time_t get_latest_handshake() const;
84
85 protected:
86 std::string address_;
87 std::string netmask_;
88 std::string private_key_;
89 std::string peer_endpoint_;
90 std::string peer_public_key_;
91 std::string preshared_key_;
92
93 std::vector<std::tuple<std::string, std::string>> allowed_ips_;
94
95 uint16_t peer_port_;
96 uint16_t keepalive_;
98
100
101#ifdef USE_BINARY_SENSOR
104#endif
105
106#ifdef USE_SENSOR
108#endif
109
110#ifdef USE_TEXT_SENSOR
112#endif
113
115 bool proceed_allowed_ = true;
116
118 bool enabled_ = true;
119
120 wireguard_config_t wg_config_ = ESP_WIREGUARD_CONFIG_DEFAULT();
121 wireguard_ctx_t wg_ctx_ = ESP_WIREGUARD_CONTEXT_DEFAULT();
122
123 esp_err_t wg_initialized_ = ESP_FAIL;
124 esp_err_t wg_connected_ = ESP_FAIL;
125
128
136
137 void start_connection_();
138 void stop_connection_();
139};
140
141// These are used for possibly long DNS resolution to temporarily suspend the watchdog
144
146std::string mask_key(const std::string &key);
147
149template<typename... Ts> class WireguardPeerOnlineCondition : public Condition<Ts...>, public Parented<Wireguard> {
150 public:
151 bool check(Ts... x) override { return this->parent_->is_peer_up(); }
152};
153
155template<typename... Ts> class WireguardEnabledCondition : public Condition<Ts...>, public Parented<Wireguard> {
156 public:
157 bool check(Ts... x) override { return this->parent_->is_enabled(); }
158};
159
161template<typename... Ts> class WireguardEnableAction : public Action<Ts...>, public Parented<Wireguard> {
162 public:
163 void play(Ts... x) override { this->parent_->enable(); }
164};
165
167template<typename... Ts> class WireguardDisableAction : public Action<Ts...>, public Parented<Wireguard> {
168 public:
169 void play(Ts... x) override { this->parent_->disable(); }
170};
171
172} // namespace wireguard
173} // namespace esphome
174#endif
uint8_t address
Definition bl0906.h:4
Base class for all automation conditions.
Definition automation.h:75
Helper class to easily give an object a parent of type T.
Definition helpers.h:538
This class simplifies creating components that periodically check a state.
Definition component.h:301
Base class for all binary_sensor-type classes.
Base-class for all sensors.
Definition sensor.h:57
The RealTimeClock class exposes common timekeeping functions via the device's local real-time clock.
Action to disable Wireguard component.
Definition wireguard.h:167
Action to enable Wireguard component.
Definition wireguard.h:161
Condition to check if Wireguard component is enabled.
Definition wireguard.h:155
Main Wireguard component class.
Definition wireguard.h:29
binary_sensor::BinarySensor * enabled_sensor_
Definition wireguard.h:103
void set_keepalive(uint16_t seconds)
bool enabled_
When false the wireguard link will not be established.
Definition wireguard.h:118
binary_sensor::BinarySensor * status_sensor_
Definition wireguard.h:102
float get_setup_priority() const override
Definition wireguard.h:38
void set_peer_public_key(const std::string &key)
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
void add_allowed_ip(const std::string &ip, const std::string &netmask)
sensor::Sensor * handshake_sensor_
Definition wireguard.h:107
time::RealTimeClock * srctime_
Definition wireguard.h:99
bool proceed_allowed_
Set to false to block the setup step until peer is connected.
Definition wireguard.h:115
std::vector< std::tuple< std::string, std::string > > allowed_ips_
Definition wireguard.h:93
void set_reboot_timeout(uint32_t seconds)
void set_peer_endpoint(const std::string &endpoint)
void set_private_key(const std::string &key)
void disable_auto_proceed()
Block the setup step until peer is connected.
void set_preshared_key(const std::string &key)
void set_address_sensor(text_sensor::TextSensor *sensor)
text_sensor::TextSensor * address_sensor_
Definition wireguard.h:111
uint32_t wg_peer_offline_time_
The last time the remote peer become offline.
Definition wireguard.h:127
void disable()
Stop any running connection and disable the WireGuard component.
void set_enabled_sensor(binary_sensor::BinarySensor *sensor)
void set_handshake_sensor(sensor::Sensor *sensor)
void set_address(const std::string &address)
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)
void set_netmask(const std::string &netmask)
time_t latest_saved_handshake_
The latest saved handshake.
Definition wireguard.h:135
wireguard_config_t wg_config_
Definition wireguard.h:120
Condition to check if remote peer is online.
Definition wireguard.h:149
const float BEFORE_CONNECTION
For components that should be initialized after WiFi and before API is connected.
Definition component.cpp:25
std::string mask_key(const std::string &key)
Strip most part of the key only for secure printing.
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint16_t x
Definition tt21100.cpp:5