ESPHome 2026.11.0b6
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#ifdef USE_BINARY_SENSOR
35 SUB_BINARY_SENSOR(status)
36 SUB_BINARY_SENSOR(enabled)
37#endif
38#ifdef USE_SENSOR
39 SUB_SENSOR(handshake)
40#endif
41#ifdef USE_TEXT_SENSOR
42 SUB_TEXT_SENSOR(address)
43#endif
44
45 public:
46 void setup() override;
47 void loop() override;
48 void update() override;
49 void dump_config() override;
50 void on_shutdown() override;
51 bool can_proceed() override;
52
53 float get_setup_priority() const override { return esphome::setup_priority::BEFORE_CONNECTION; }
54
55 void set_address(const char *address) { this->address_ = address; }
56 void set_netmask(const char *netmask) { this->netmask_ = netmask; }
57 void set_private_key(const char *key) { this->private_key_ = key; }
58 void set_peer_endpoint(const char *endpoint) { this->peer_endpoint_ = endpoint; }
59 void set_peer_public_key(const char *key) { this->peer_public_key_ = key; }
60 void set_peer_port(uint16_t port) { this->peer_port_ = port; }
61 void set_preshared_key(const char *key) { this->preshared_key_ = key; }
62
64 void set_address(const std::string &address) = delete;
65 void set_netmask(const std::string &netmask) = delete;
66 void set_private_key(const std::string &key) = delete;
67 void set_peer_endpoint(const std::string &endpoint) = delete;
68 void set_peer_public_key(const std::string &key) = delete;
69 void set_preshared_key(const std::string &key) = delete;
70
72 void set_allowed_ips(const AllowedIP *ips) { this->allowed_ips_ = ips; }
73
74 void set_keepalive(const uint16_t seconds) { this->keepalive_ = seconds; }
75 void set_reboot_timeout(const uint32_t seconds) { this->reboot_timeout_ = seconds; }
76 void set_srctime(time::RealTimeClock *srctime) { this->srctime_ = srctime; }
77
79 void disable_auto_proceed() { this->proceed_allowed_ = false; }
80
82 void enable();
83
85 void disable();
86
88 void publish_enabled_state();
89
91 bool is_enabled() { return this->enabled_; }
92
93 bool is_peer_up() const;
94 time_t get_latest_handshake() const;
95
96 protected:
97 const char *address_{nullptr};
98 const char *netmask_{nullptr};
99 const char *private_key_{nullptr};
100 const char *peer_endpoint_{nullptr};
101 const char *peer_public_key_{nullptr};
102 const char *preshared_key_{nullptr};
103
104 const AllowedIP *allowed_ips_{nullptr};
105
106 uint16_t peer_port_;
107 uint16_t keepalive_;
109
111
113 bool proceed_allowed_ = true;
114
116 bool enabled_ = true;
117
118 wireguard_config_t wg_config_ = ESP_WIREGUARD_CONFIG_DEFAULT();
119 wireguard_ctx_t wg_ctx_ = ESP_WIREGUARD_CONTEXT_DEFAULT();
120
121 esp_err_t wg_initialized_ = ESP_FAIL;
122 esp_err_t wg_connected_ = ESP_FAIL;
123
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
143
145static constexpr size_t MASK_KEY_BUFFER_SIZE = 12;
146static constexpr uint32_t RECONNECT_INTERVAL_MS = 30000;
147
149void mask_key_to(char *buffer, size_t len, const char *key);
150
151} // namespace esphome::wireguard
152#endif
uint8_t status
Definition bl0942.h:8
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
This class simplifies creating components that periodically check a state.
Definition component.h:532
The RealTimeClock class exposes common timekeeping functions via the device's local real-time clock.
Main Wireguard component class.
Definition wireguard.h:33
bool enabled_
When false the wireguard link will not be established.
Definition wireguard.h:116
time::RealTimeClock * srctime_
Definition wireguard.h:110
bool proceed_allowed_
Set to false to block the setup step until peer is connected.
Definition wireguard.h:113
uint32_t wg_peer_offline_time_
The last time the remote peer become offline.
Definition wireguard.h:125
const AllowedIP * allowed_ips_
Definition wireguard.h:104
time_t latest_saved_handshake_
The latest saved handshake.
Definition wireguard.h:134
wireguard_config_t wg_config_
Definition wireguard.h:118
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