23#define LOGMSG_PEER_STATUS "Remote peer is %s (latest handshake %s)"
25static const char *
const LOGMSG_ONLINE =
"online";
26static const char *
const LOGMSG_OFFLINE =
"offline";
40 this->publish_enabled_state();
48 ESP_LOGI(TAG,
"Initialized");
54 PUBLISH_STATE(this->address_text_sensor_, this->
address_);
57 ESP_LOGE(TAG,
"Cannot initialize: error code %d", this->
wg_initialized_);
62void Wireguard::loop() {
68 ESP_LOGV(TAG,
"Local network connection has been lost, stopping");
73void Wireguard::update() {
74 bool peer_up = this->is_peer_up();
75 time_t lhs = this->get_latest_handshake();
78 ESP_LOGV(TAG,
"enabled=%d, connected=%d, peer_up=%d, handshake: current=%.0f latest=%.0f updated=%d",
83 this->latest_saved_handshake_ = lhs;
86 std::string latest_handshake =
87 (this->latest_saved_handshake_ > 0)
89 :
"timestamp not available";
93 ESP_LOGI(TAG, LOGMSG_PEER_STATUS, LOGMSG_ONLINE, latest_handshake.c_str());
96 ESP_LOGD(TAG, LOGMSG_PEER_STATUS, LOGMSG_ONLINE, latest_handshake.c_str());
100 ESP_LOGW(TAG, LOGMSG_PEER_STATUS, LOGMSG_OFFLINE, latest_handshake.c_str());
103 }
else if (this->enabled_) {
104 ESP_LOGD(TAG, LOGMSG_PEER_STATUS, LOGMSG_OFFLINE, latest_handshake.c_str());
115 ESP_LOGE(TAG,
"Remote peer is unreachable; rebooting");
121#ifdef USE_BINARY_SENSOR
122 PUBLISH_STATE(this->status_binary_sensor_, peer_up);
126 if (this->handshake_sensor_ !=
nullptr && lhs_updated) {
127 this->handshake_sensor_->publish_state((
double) this->latest_saved_handshake_);
132void Wireguard::dump_config() {
133 char private_key_masked[MASK_KEY_BUFFER_SIZE];
134 char preshared_key_masked[MASK_KEY_BUFFER_SIZE];
143 " Private Key: " LOG_SECRET(
"%s")
"\n"
144 " Peer Endpoint: " LOG_SECRET(
"%s")
"\n"
145 " Peer Port: " LOG_SECRET(
"%d")
"\n"
146 " Peer Public Key: " LOG_SECRET(
"%s")
"\n"
147 " Peer Pre-shared Key: " LOG_SECRET(
"%s"),
150 (this->
preshared_key_ !=
nullptr ? preshared_key_masked : LOG_STR_LITERAL(
"NOT IN USE")));
152 ESP_LOGCONFIG(TAG,
" Peer Allowed IPs:");
153 for (
const AllowedIP *it = this->
allowed_ips_; it !=
nullptr && it->
ip !=
nullptr; it++) {
154 ESP_LOGCONFIG(TAG,
" - %s/%s", it->ip, it->netmask);
156 ESP_LOGCONFIG(TAG,
" Peer Persistent Keepalive: %d%s", this->
keepalive_,
157 (this->
keepalive_ > 0 ? LOG_STR_LITERAL(
"s") : LOG_STR_LITERAL(
" (DISABLED)")));
158 ESP_LOGCONFIG(TAG,
" Reboot Timeout: %" PRIu32
"%s", (this->
reboot_timeout_ / 1000),
159 (this->
reboot_timeout_ != 0 ? LOG_STR_LITERAL(
"s") : LOG_STR_LITERAL(
" (DISABLED)")));
161 ESP_LOGCONFIG(TAG,
" Require Connection to Proceed: %s",
163 LOG_UPDATE_INTERVAL(
this);
168bool Wireguard::can_proceed() {
return (this->
proceed_allowed_ || this->is_peer_up() || !this->enabled_); }
170bool Wireguard::is_peer_up()
const {
172 (esp_wireguardif_peer_is_up(&(this->
wg_ctx_)) == ESP_OK);
175time_t Wireguard::get_latest_handshake()
const {
177 if (esp_wireguard_latest_handshake(&(this->
wg_ctx_), &result) != ESP_OK) {
183void Wireguard::enable() {
184 this->enabled_ =
true;
185 ESP_LOGI(TAG,
"Enabled");
186 this->publish_enabled_state();
189void Wireguard::disable() {
190 this->enabled_ =
false;
192 ESP_LOGI(TAG,
"Disabled");
193 this->publish_enabled_state();
196void Wireguard::publish_enabled_state() {
197#ifdef USE_BINARY_SENSOR
198 PUBLISH_STATE(this->enabled_binary_sensor_, this->enabled_);
203 if (!this->enabled_) {
204 ESP_LOGV(TAG,
"Disabled, cannot start connection");
214 ESP_LOGD(TAG,
"Waiting for local network connection to be available");
219 ESP_LOGD(TAG,
"Waiting for system time to be synchronized");
224 ESP_LOGV(TAG,
"Connection already started");
228 ESP_LOGD(TAG,
"Starting connection");
235 ESP_LOGI(TAG,
"Connection started");
237 ESP_LOGD(TAG,
"Waiting for endpoint IP address to be available");
240 ESP_LOGW(TAG,
"Cannot start connection, error code %d", this->
wg_connected_);
244 ESP_LOGD(TAG,
"Configuring allowed IPs list");
245 bool allowed_ips_ok =
true;
247 allowed_ips_ok &= (esp_wireguard_add_allowed_ip(&(this->
wg_ctx_), it->ip, it->netmask) == ESP_OK);
250 if (allowed_ips_ok) {
251 ESP_LOGD(TAG,
"Allowed IPs list configured correctly");
253 ESP_LOGE(TAG,
"Cannot configure allowed IPs list, aborting");
261 ESP_LOGD(TAG,
"Stopping connection");
264 esp_wireguard_disconnect(&(this->
wg_ctx_));
272 if (
len < MASK_KEY_BUFFER_SIZE ||
key ==
nullptr) {
279 for (; i < 5 &&
key[i] !=
'\0'; ++i) {
283 const char *suffix =
"[...]=";
284 for (
size_t j = 0; suffix[j] !=
'\0' && (i + j) <
len - 1; ++j) {
285 buffer[i + j] = suffix[j];
size_t strftime(char *buffer, size_t buffer_len, const char *format)
Convert this ESPTime struct to a null-terminated c string buffer as specified by the format argument.