ESPHome  2024.10.2
wireguard.h
Go to the documentation of this file.
1 #pragma once
2 #include "esphome/core/defines.h"
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 
25 namespace esphome {
26 namespace wireguard {
27 
29 class Wireguard : public PollingComponent {
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 
68  void disable_auto_proceed();
69 
71  void enable();
72 
74  void disable();
75 
77  void publish_enabled_state();
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_;
97  uint32_t reboot_timeout_;
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 
127  uint32_t wg_peer_offline_time_ = 0;
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
142 void suspend_wdt();
143 void resume_wdt();
144 
146 std::string mask_key(const std::string &key);
147 
149 template<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 
155 template<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 
161 template<typename... Ts> class WireguardEnableAction : public Action<Ts...>, public Parented<Wireguard> {
162  public:
163  void play(Ts... x) override { this->parent_->enable(); }
164 };
165 
167 template<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
void set_status_sensor(binary_sensor::BinarySensor *sensor)
Definition: wireguard.cpp:188
wireguard_config_t wg_config_
Definition: wireguard.h:120
binary_sensor::BinarySensor * status_sensor_
Definition: wireguard.h:102
void set_preshared_key(const std::string &key)
Definition: wireguard.cpp:177
const float BEFORE_CONNECTION
For components that should be initialized after WiFi and before API is connected. ...
Definition: component.cpp:25
uint32_t wg_peer_offline_time_
The last time the remote peer become offline.
Definition: wireguard.h:127
text_sensor::TextSensor * address_sensor_
Definition: wireguard.h:111
sensor::Sensor * handshake_sensor_
Definition: wireguard.h:107
The RealTimeClock class exposes common timekeeping functions via the device&#39;s local real-time clock...
uint16_t x
Definition: tt21100.cpp:17
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
void disable_auto_proceed()
Block the setup step until peer is connected.
Definition: wireguard.cpp:200
Condition to check if remote peer is online.
Definition: wireguard.h:149
void set_address(const std::string &address)
Definition: wireguard.cpp:171
void set_keepalive(uint16_t seconds)
Definition: wireguard.cpp:183
std::string mask_key(const std::string &key)
Strip most part of the key only for secure printing.
Definition: wireguard.cpp:288
This class simplifies creating components that periodically check a state.
Definition: component.h:283
void enable()
Enable the WireGuard component.
Definition: wireguard.cpp:202
Action to enable Wireguard component.
Definition: wireguard.h:161
float get_setup_priority() const override
Definition: wireguard.h:38
void set_address_sensor(text_sensor::TextSensor *sensor)
Definition: wireguard.cpp:197
time_t latest_saved_handshake_
The latest saved handshake.
Definition: wireguard.h:135
void set_private_key(const std::string &key)
Definition: wireguard.cpp:173
bool enabled_
When false the wireguard link will not be established.
Definition: wireguard.h:118
void set_srctime(time::RealTimeClock *srctime)
Definition: wireguard.cpp:185
Base class for all automation conditions.
Definition: automation.h:74
Main Wireguard component class.
Definition: wireguard.h:29
void add_allowed_ip(const std::string &ip, const std::string &netmask)
Definition: wireguard.cpp:179
void set_reboot_timeout(uint32_t seconds)
Definition: wireguard.cpp:184
void set_netmask(const std::string &netmask)
Definition: wireguard.cpp:172
binary_sensor::BinarySensor * enabled_sensor_
Definition: wireguard.h:103
Action to disable Wireguard component.
Definition: wireguard.h:167
void disable()
Stop any running connection and disable the WireGuard component.
Definition: wireguard.cpp:208
void publish_enabled_state()
Publish the enabled state if the enabled binary sensor is configured.
Definition: wireguard.cpp:215
void set_peer_endpoint(const std::string &endpoint)
Definition: wireguard.cpp:174
time_t get_latest_handshake() const
Definition: wireguard.cpp:163
Condition to check if Wireguard component is enabled.
Definition: wireguard.h:155
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
uint8_t address
Definition: bl0906.h:211
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:37
bool is_enabled()
Return if the WireGuard component is or is not enabled.
Definition: wireguard.cpp:223
void set_handshake_sensor(sensor::Sensor *sensor)
Definition: wireguard.cpp:193
Base-class for all sensors.
Definition: sensor.h:57
void set_peer_public_key(const std::string &key)
Definition: wireguard.cpp:175
void set_enabled_sensor(binary_sensor::BinarySensor *sensor)
Definition: wireguard.cpp:189
void set_peer_port(uint16_t port)
Definition: wireguard.cpp:176
esphome::sensor::Sensor * sensor
Definition: statsd.h:37
Helper class to easily give an object a parent of type T.
Definition: helpers.h:521
std::vector< std::tuple< std::string, std::string > > allowed_ips_
Definition: wireguard.h:93