reresolve-dns.sh: Add support for wireguard-tools on Mac OS X (interface `utunX`), debugging, bash 3.2, and improved logging for cron jobs
On Mac OS X and while using wg-quick it is possible that the config base filename be wg1.conf while the actual interface name is something like utun4. In this case, this improvement allows us to enter something like:
sudo ./reresolve-dns.sh /opt/homebrew/etc/wireguard/wg1.conf utun4
(on Mac OS X, when installing wireguard-tools using homebrew, elevated privileges are needed when running wg)
In the second commit I added better logging when using a cron job:
*/2 * * * * sudo $HOME/repos/wireguard/reresolve-dns.sh /opt/homebrew/etc/wireguard/wg1.conf utun4 >> /tmp/wireguard-wg1.log
I noticed that the script was not doing what it is supposed to do on Mac OS X. After some investigations I realised it was the shebang line which refers to /bin/bash, an outdated interpreter (bash 3.2) on many systems.
I have fixed the issue by polyfilling the EPOCHSECONDS ENV variable. A change of the shebang is recommended but I have not made that change.
Recommended cronjob spec:
* * * * SUDO_ASKPASS="$HOME"/.config/askpass sudo -A \
PATH="$PATH:/opt/homebrew/bin" DEBUG=1 \
"$HOME"/.config/wireguard/reresolve-dns.sh \
/opt/homebrew/etc/wireguard/wgX.conf utunX \
>>/tmp/wireguard.log 2>/tmp/wireguard.debug || \
cp /tmp/wireguard.debug /tmp/wireguard.debug."$?"
This cron job will:
- Let you know when the endpoint is updated in a log file
- Maintain an xtrace log for the last successful invocation
- Maintain the xtrace log for the last failed invocation, for each error code
- Attach useful information to xtrace logs including the current date, the user used for the invocation, and the
PATHenv variable.