ESPHome  2024.3.2
wireguard.h
Go to the documentation of this file.
1 #pragma once
2 
3 #ifdef USE_ESP32
4 
5 #include <ctime>
6 #include <vector>
7 #include <tuple>
8 
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 
26 namespace esphome {
27 namespace wireguard {
28 
30 class Wireguard : public PollingComponent {
31  public:
32  void setup() override;
33  void loop() override;
34  void update() override;
35  void dump_config() override;
36  void on_shutdown() override;
37  bool can_proceed() override;
38 
40 
41  void set_address(const std::string &address);
42  void set_netmask(const std::string &netmask);
43  void set_private_key(const std::string &key);
44  void set_peer_endpoint(const std::string &endpoint);
45  void set_peer_public_key(const std::string &key);
46  void set_peer_port(uint16_t port);
47  void set_preshared_key(const std::string &key);
48 
49  void add_allowed_ip(const std::string &ip, const std::string &netmask);
50 
51  void set_keepalive(uint16_t seconds);
52  void set_reboot_timeout(uint32_t seconds);
53  void set_srctime(time::RealTimeClock *srctime);
54 
55 #ifdef USE_BINARY_SENSOR
58 #endif
59 
60 #ifdef USE_SENSOR
62 #endif
63 
64 #ifdef USE_TEXT_SENSOR
66 #endif
67 
69  void disable_auto_proceed();
70 
72  void enable();
73 
75  void disable();
76 
78  void publish_enabled_state();
79 
81  bool is_enabled();
82 
83  bool is_peer_up() const;
84  time_t get_latest_handshake() const;
85 
86  protected:
87  std::string address_;
88  std::string netmask_;
89  std::string private_key_;
90  std::string peer_endpoint_;
91  std::string peer_public_key_;
92  std::string preshared_key_;
93 
94  std::vector<std::tuple<std::string, std::string>> allowed_ips_;
95 
96  uint16_t peer_port_;
97  uint16_t keepalive_;
98  uint32_t reboot_timeout_;
99 
101 
102 #ifdef USE_BINARY_SENSOR
105 #endif
106 
107 #ifdef USE_SENSOR
109 #endif
110 
111 #ifdef USE_TEXT_SENSOR
113 #endif
114 
116  bool proceed_allowed_ = true;
117 
119  bool enabled_ = true;
120 
121  wireguard_config_t wg_config_ = ESP_WIREGUARD_CONFIG_DEFAULT();
122  wireguard_ctx_t wg_ctx_ = ESP_WIREGUARD_CONTEXT_DEFAULT();
123 
124  esp_err_t wg_initialized_ = ESP_FAIL;
125  esp_err_t wg_connected_ = ESP_FAIL;
126 
128  uint32_t wg_peer_offline_time_ = 0;
129 
137 
138  void start_connection_();
139  void stop_connection_();
140 };
141 
142 // These are used for possibly long DNS resolution to temporarily suspend the watchdog
143 void suspend_wdt();
144 void resume_wdt();
145 
147 std::string mask_key(const std::string &key);
148 
150 template<typename... Ts> class WireguardPeerOnlineCondition : public Condition<Ts...>, public Parented<Wireguard> {
151  public:
152  bool check(Ts... x) override { return this->parent_->is_peer_up(); }
153 };
154 
156 template<typename... Ts> class WireguardEnabledCondition : public Condition<Ts...>, public Parented<Wireguard> {
157  public:
158  bool check(Ts... x) override { return this->parent_->is_enabled(); }
159 };
160 
162 template<typename... Ts> class WireguardEnableAction : public Action<Ts...>, public Parented<Wireguard> {
163  public:
164  void play(Ts... x) override { this->parent_->enable(); }
165 };
166 
168 template<typename... Ts> class WireguardDisableAction : public Action<Ts...>, public Parented<Wireguard> {
169  public:
170  void play(Ts... x) override { this->parent_->disable(); }
171 };
172 
173 } // namespace wireguard
174 } // namespace esphome
175 
176 #endif // USE_ESP32
void set_status_sensor(binary_sensor::BinarySensor *sensor)
Definition: wireguard.cpp:196
wireguard_config_t wg_config_
Definition: wireguard.h:121
binary_sensor::BinarySensor * status_sensor_
Definition: wireguard.h:103
void set_preshared_key(const std::string &key)
Definition: wireguard.cpp:185
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:128
text_sensor::TextSensor * address_sensor_
Definition: wireguard.h:112
sensor::Sensor * handshake_sensor_
Definition: wireguard.h:108
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:100
bool proceed_allowed_
Set to false to block the setup step until peer is connected.
Definition: wireguard.h:116
void disable_auto_proceed()
Block the setup step until peer is connected.
Definition: wireguard.cpp:208
Condition to check if remote peer is online.
Definition: wireguard.h:150
void set_address(const std::string &address)
Definition: wireguard.cpp:179
void set_keepalive(uint16_t seconds)
Definition: wireguard.cpp:191
std::string mask_key(const std::string &key)
Strip most part of the key only for secure printing.
Definition: wireguard.cpp:338
This class simplifies creating components that periodically check a state.
Definition: component.h:283
void enable()
Enable the WireGuard component.
Definition: wireguard.cpp:210
Action to enable Wireguard component.
Definition: wireguard.h:162
float get_setup_priority() const override
Definition: wireguard.h:39
void set_address_sensor(text_sensor::TextSensor *sensor)
Definition: wireguard.cpp:205
time_t latest_saved_handshake_
The latest saved handshake.
Definition: wireguard.h:136
void set_private_key(const std::string &key)
Definition: wireguard.cpp:181
bool enabled_
When false the wireguard link will not be established.
Definition: wireguard.h:119
void set_srctime(time::RealTimeClock *srctime)
Definition: wireguard.cpp:193
Base class for all automation conditions.
Definition: automation.h:74
Main Wireguard component class.
Definition: wireguard.h:30
void add_allowed_ip(const std::string &ip, const std::string &netmask)
Definition: wireguard.cpp:187
void set_reboot_timeout(uint32_t seconds)
Definition: wireguard.cpp:192
void set_netmask(const std::string &netmask)
Definition: wireguard.cpp:180
binary_sensor::BinarySensor * enabled_sensor_
Definition: wireguard.h:104
Action to disable Wireguard component.
Definition: wireguard.h:168
void disable()
Stop any running connection and disable the WireGuard component.
Definition: wireguard.cpp:216
void publish_enabled_state()
Publish the enabled state if the enabled binary sensor is configured.
Definition: wireguard.cpp:223
void set_peer_endpoint(const std::string &endpoint)
Definition: wireguard.cpp:182
time_t get_latest_handshake() const
Definition: wireguard.cpp:171
Condition to check if Wireguard component is enabled.
Definition: wireguard.h:156
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:231
void set_handshake_sensor(sensor::Sensor *sensor)
Definition: wireguard.cpp:201
Base-class for all sensors.
Definition: sensor.h:57
void set_peer_public_key(const std::string &key)
Definition: wireguard.cpp:183
void set_enabled_sensor(binary_sensor::BinarySensor *sensor)
Definition: wireguard.cpp:197
void set_peer_port(uint16_t port)
Definition: wireguard.cpp:184
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:94