ESPHome 2026.11.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
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
24namespace esphome::wireguard {
25
27struct AllowedIP {
28 const char *ip;
29 const char *netmask;
30};
31
33class Wireguard final : public PollingComponent {
34 public:
35 void setup() override;
36 void loop() override;
37 void update() override;
38 void dump_config() override;
39 void on_shutdown() override;
40 bool can_proceed() override;
41
43
44 void set_address(const char *address) { this->address_ = address; }
45 void set_netmask(const char *netmask) { this->netmask_ = netmask; }
46 void set_private_key(const char *key) { this->private_key_ = key; }
47 void set_peer_endpoint(const char *endpoint) { this->peer_endpoint_ = endpoint; }
48 void set_peer_public_key(const char *key) { this->peer_public_key_ = key; }
49 void set_peer_port(uint16_t port) { this->peer_port_ = port; }
50 void set_preshared_key(const char *key) { this->preshared_key_ = key; }
51
53 void set_address(const std::string &address) = delete;
54 void set_netmask(const std::string &netmask) = delete;
55 void set_private_key(const std::string &key) = delete;
56 void set_peer_endpoint(const std::string &endpoint) = delete;
57 void set_peer_public_key(const std::string &key) = delete;
58 void set_preshared_key(const std::string &key) = delete;
59
61 void set_allowed_ips(const AllowedIP *ips) { this->allowed_ips_ = ips; }
62
63 void set_keepalive(const uint16_t seconds) { this->keepalive_ = seconds; }
64 void set_reboot_timeout(const uint32_t seconds) { this->reboot_timeout_ = seconds; }
65 void set_srctime(time::RealTimeClock *srctime) { this->srctime_ = srctime; }
66
67#ifdef USE_BINARY_SENSOR
70#endif
71
72#ifdef USE_SENSOR
73 void set_handshake_sensor(sensor::Sensor *sensor) { this->handshake_sensor_ = sensor; }
74#endif
75
76#ifdef USE_TEXT_SENSOR
78#endif
79
81 void disable_auto_proceed() { this->proceed_allowed_ = false; }
82
84 void enable();
85
87 void disable();
88
91
93 bool is_enabled() { return this->enabled_; }
94
95 bool is_peer_up() const;
96 time_t get_latest_handshake() const;
97
98 protected:
99 const char *address_{nullptr};
100 const char *netmask_{nullptr};
101 const char *private_key_{nullptr};
102 const char *peer_endpoint_{nullptr};
103 const char *peer_public_key_{nullptr};
104 const char *preshared_key_{nullptr};
105
106 const AllowedIP *allowed_ips_{nullptr};
107
108 uint16_t peer_port_;
109 uint16_t keepalive_;
111
113
114#ifdef USE_BINARY_SENSOR
117#endif
118
119#ifdef USE_SENSOR
121#endif
122
123#ifdef USE_TEXT_SENSOR
125#endif
126
128 bool proceed_allowed_ = true;
129
131 bool enabled_ = true;
132
133 wireguard_config_t wg_config_ = ESP_WIREGUARD_CONFIG_DEFAULT();
134 wireguard_ctx_t wg_ctx_ = ESP_WIREGUARD_CONTEXT_DEFAULT();
135
136 esp_err_t wg_initialized_ = ESP_FAIL;
137 esp_err_t wg_connected_ = ESP_FAIL;
138
142
150
151 void start_connection_();
152 void stop_connection_();
153};
154
155// These are used for possibly long DNS resolution to temporarily suspend the watchdog
158
160static constexpr size_t MASK_KEY_BUFFER_SIZE = 12;
161static constexpr uint32_t RECONNECT_INTERVAL_MS = 30000;
162
164void mask_key_to(char *buffer, size_t len, const char *key);
165
166} // namespace esphome::wireguard
167#endif
uint8_t address
Definition bl0906.h:4
This class simplifies creating components that periodically check a state.
Definition component.h:519
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.
Main Wireguard component class.
Definition wireguard.h:33
binary_sensor::BinarySensor * enabled_sensor_
Definition wireguard.h:116
void set_netmask(const char *netmask)
Definition wireguard.h:45
bool enabled_
When false the wireguard link will not be established.
Definition wireguard.h:131
binary_sensor::BinarySensor * status_sensor_
Definition wireguard.h:115
float get_setup_priority() const override
Definition wireguard.h:42
void set_status_sensor(binary_sensor::BinarySensor *sensor)
Definition wireguard.h:68
void set_srctime(time::RealTimeClock *srctime)
Definition wireguard.h:65
void publish_enabled_state()
Publish the enabled state if the enabled binary sensor is configured.
time_t get_latest_handshake() const
void set_reboot_timeout(const uint32_t seconds)
Definition wireguard.h:64
sensor::Sensor * handshake_sensor_
Definition wireguard.h:120
time::RealTimeClock * srctime_
Definition wireguard.h:112
void set_preshared_key(const char *key)
Definition wireguard.h:50
bool proceed_allowed_
Set to false to block the setup step until peer is connected.
Definition wireguard.h:128
void set_peer_endpoint(const std::string &endpoint)=delete
void set_peer_public_key(const std::string &key)=delete
void disable_auto_proceed()
Block the setup step until peer is connected.
Definition wireguard.h:81
void set_private_key(const char *key)
Definition wireguard.h:46
void set_address_sensor(text_sensor::TextSensor *sensor)
Definition wireguard.h:77
text_sensor::TextSensor * address_sensor_
Definition wireguard.h:124
uint32_t wg_peer_offline_time_
The last time the remote peer become offline.
Definition wireguard.h:140
void set_address(const char *address)
Definition wireguard.h:44
void disable()
Stop any running connection and disable the WireGuard component.
void set_enabled_sensor(binary_sensor::BinarySensor *sensor)
Definition wireguard.h:69
void set_address(const std::string &address)=delete
Prevent accidental use of std::string which would dangle.
void set_handshake_sensor(sensor::Sensor *sensor)
Definition wireguard.h:73
void set_keepalive(const uint16_t seconds)
Definition wireguard.h:63
void set_preshared_key(const std::string &key)=delete
bool is_enabled()
Return if the WireGuard component is or is not enabled.
Definition wireguard.h:93
void enable()
Enable the WireGuard component.
void set_peer_port(uint16_t port)
Definition wireguard.h:49
void set_allowed_ips(const AllowedIP *ips)
Table ended by an entry with a null ip; must outlive the component.
Definition wireguard.h:61
void set_peer_endpoint(const char *endpoint)
Definition wireguard.h:47
void set_netmask(const std::string &netmask)=delete
void set_private_key(const std::string &key)=delete
const AllowedIP * allowed_ips_
Definition wireguard.h:106
void set_peer_public_key(const char *key)
Definition wireguard.h:48
time_t latest_saved_handshake_
The latest saved handshake.
Definition wireguard.h:149
wireguard_config_t wg_config_
Definition wireguard.h:133
const char * key
uint32_t buffer
Definition gif.cpp:50
constexpr float BEFORE_CONNECTION
For components that should be initialized after WiFi and before API is connected.
Definition component.h:53
void mask_key_to(char *buffer, size_t len, const char *key)
Strip most part of the key only for secure printing.
const void size_t len
Definition hal.h:68
static void uint32_t
Allowed IP entry for WireGuard peer configuration.
Definition wireguard.h:27