Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

os-wireguard: add script to enable cron renewal of DNS for stale connections #2956

Merged
merged 5 commits into from May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion net/wireguard/Makefile
@@ -1,5 +1,5 @@
PLUGIN_NAME= wireguard
PLUGIN_VERSION= 1.10
PLUGIN_VERSION= 1.11
PLUGIN_COMMENT= WireGuard VPN service
PLUGIN_DEPENDS= wireguard-go wireguard-tools
PLUGIN_MAINTAINER= m.muenz@gmail.com
Expand Down
4 changes: 4 additions & 0 deletions net/wireguard/pkg-descr
Expand Up @@ -16,6 +16,10 @@ WWW: https://www.wireguard.com/
Changelog
---------

1.11

* Add script for renewal of Wireguard DNS-based entries for stale connections

1.10

* Remove instance limit
Expand Down
@@ -0,0 +1,49 @@
#!/usr/local/bin/bash
# SPDX-License-Identifier: GPL-2.0
Comment on lines +1 to +2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be any more Linux-y :D

#
# Copyright (C) 2015-2020 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.

set -e
shopt -s nocasematch
shopt -s extglob
export LC_ALL=C

for CONFIG_FILE in /usr/local/etc/wireguard/*.conf
do

[[ $CONFIG_FILE =~ /?([a-zA-Z0-9_=+.-]{1,15})\.conf$ ]]
INTERFACE="${BASH_REMATCH[1]}"

process_peer() {
[[ $PEER_SECTION -ne 1 || -z $PUBLIC_KEY || -z $ENDPOINT ]] && return 0
[[ $(wg show "$INTERFACE" latest-handshakes) =~ ${PUBLIC_KEY//+/\\+}\ ([0-9]+) ]
] || return 0
(( ($EPOCHSECONDS - ${BASH_REMATCH[1]}) > 135 )) || return 0
wg set "$INTERFACE" peer "$PUBLIC_KEY" endpoint "$ENDPOINT"
reset_peer_section
}

reset_peer_section() {
PEER_SECTION=0
PUBLIC_KEY=""
ENDPOINT=""
}

reset_peer_section
while read -r line || [[ -n $line ]]; do
stripped="${line%%\#*}"
key="${stripped%%=*}"; key="${key##*([[:space:]])}"; key="${key%%*([[:space:]])}"
value="${stripped#*=}"; value="${value##*([[:space:]])}"; value="${value%%*([[:spa
ce:]])}"
[[ $key == "["* ]] && { process_peer; reset_peer_section; }
[[ $key == "[Peer]" ]] && PEER_SECTION=1
if [[ $PEER_SECTION -eq 1 ]]; then
case "$key" in
PublicKey) PUBLIC_KEY="$value"; continue ;;
Endpoint) ENDPOINT="$value"; continue ;;
esac
fi
done < "$CONFIG_FILE"
process_peer

done
Expand Up @@ -23,6 +23,13 @@ type:script
message:Restarting WireGuard
description: Restart WireGuard

[renew]
command:/usr/local/opnsense/scripts/OPNsense/Wireguard/resolve-dns.sh
parameters:
type:script
message:Renew DNS for Wireguard
description:Renew DNS for Wireguard on stale connections

[genkey]
command:/usr/local/opnsense/scripts/OPNsense/Wireguard/genkey.sh
parameters: %s
Expand Down