ESPHome  2024.4.2
wireguard.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <ctime>
4 #include <vector>
5 #include <tuple>
6 
9 
10 #ifdef USE_BINARY_SENSOR
12 #endif
13 
14 #ifdef USE_SENSOR
16 #endif
17 
18 #ifdef USE_TEXT_SENSOR
20 #endif
21 
22 #include <esp_wireguard.h>
23 
24 namespace esphome {
25 namespace wireguard {
26 
28 class Wireguard : public PollingComponent {
29  public:
30  void setup() override;
31  void loop() override;
32  void update() override;
33  void dump_config() override;
34  void on_shutdown() override;
35  bool can_proceed() override;
36 
38 
39  void set_address(const std::string &address);
40  void set_netmask(const std::string &netmask);
41  void set_private_key(const std::string &key);
42  void set_peer_endpoint(const std::string &endpoint);
43  void set_peer_public_key(const std::string &key);
44  void set_peer_port(uint16_t port);
45  void set_preshared_key(const std::string &key);
46 
47  void add_allowed_ip(const std::string &ip, const std::string &netmask);
48 
49  void set_keepalive(uint16_t seconds);
50  void set_reboot_timeout(uint32_t seconds);
51  void set_srctime(time::RealTimeClock *srctime);
52 
53 #ifdef USE_BINARY_SENSOR
56 #endif
57 
58 #ifdef USE_SENSOR
60 #endif
61 
62 #ifdef USE_TEXT_SENSOR
64 #endif
65 
67  void disable_auto_proceed();
68 
70  void enable();
71 
73  void disable();
74 
76  void publish_enabled_state();
77 
79  bool is_enabled();
80 
81  bool is_peer_up() const;
82  time_t get_latest_handshake() const;
83 
84  protected:
85  std::string address_;
86  std::string netmask_;
87  std::string private_key_;
88  std::string peer_endpoint_;
89  std::string peer_public_key_;
90  std::string preshared_key_;
91 
92  std::vector<std::tuple<std::string, std::string>> allowed_ips_;
93 
94  uint16_t peer_port_;
95  uint16_t keepalive_;
96  uint32_t reboot_timeout_;
97 
99 
100 #ifdef USE_BINARY_SENSOR
103 #endif
104 
105 #ifdef USE_SENSOR
107 #endif
108 
109 #ifdef USE_TEXT_SENSOR
111 #endif
112 
114  bool proceed_allowed_ = true;
115 
117  bool enabled_ = true;
118 
119  wireguard_config_t wg_config_ = ESP_WIREGUARD_CONFIG_DEFAULT();
120  wireguard_ctx_t wg_ctx_ = ESP_WIREGUARD_CONTEXT_DEFAULT();
121 
122  esp_err_t wg_initialized_ = ESP_FAIL;
123  esp_err_t wg_connected_ = ESP_FAIL;
124 
126  uint32_t wg_peer_offline_time_ = 0;
127 
135 
136  void start_connection_();
137  void stop_connection_();
138 };
139 
140 // These are used for possibly long DNS resolution to temporarily suspend the watchdog
141 void suspend_wdt();
142 void resume_wdt();
143 
145 std::string mask_key(const std::string &key);
146 
148 template<typename... Ts> class WireguardPeerOnlineCondition : public Condition<Ts...>, public Parented<Wireguard> {
149  public:
150  bool check(Ts... x) override { return this->parent_->is_peer_up(); }
151 };
152 
154 template<typename... Ts> class WireguardEnabledCondition : public Condition<Ts...>, public Parented<Wireguard> {
155  public:
156  bool check(Ts... x) override { return this->parent_->is_enabled(); }
157 };
158 
160 template<typename... Ts> class WireguardEnableAction : public Action<Ts...>, public Parented<Wireguard> {
161  public:
162  void play(Ts... x) override { this->parent_->enable(); }
163 };
164 
166 template<typename... Ts> class WireguardDisableAction : public Action<Ts...>, public Parented<Wireguard> {
167  public:
168  void play(Ts... x) override { this->parent_->disable(); }
169 };
170 
171 } // namespace wireguard
172 } // namespace esphome
void set_status_sensor(binary_sensor::BinarySensor *sensor)
Definition: wireguard.cpp:188
wireguard_config_t wg_config_
Definition: wireguard.h:119
binary_sensor::BinarySensor * status_sensor_
Definition: wireguard.h:101
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:126
text_sensor::TextSensor * address_sensor_
Definition: wireguard.h:110
sensor::Sensor * handshake_sensor_
Definition: wireguard.h:106
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:98
bool proceed_allowed_
Set to false to block the setup step until peer is connected.
Definition: wireguard.h:114
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:148
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:160
float get_setup_priority() const override
Definition: wireguard.h:37
void set_address_sensor(text_sensor::TextSensor *sensor)
Definition: wireguard.cpp:197
time_t latest_saved_handshake_
The latest saved handshake.
Definition: wireguard.h:134
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:117
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:28
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:102
Action to disable Wireguard component.
Definition: wireguard.h:166
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:154
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
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
Helper class to easily give an object a parent of type T.
Definition: helpers.h:515
std::vector< std::tuple< std::string, std::string > > allowed_ips_
Definition: wireguard.h:92