Skip to content

Instantly share code, notes, and snippets.

@drmalex07
Created May 15, 2024 16:29
Show Gist options
  • Save drmalex07/c313e34bdfcb0941de9d4f25028a9c77 to your computer and use it in GitHub Desktop.
Save drmalex07/c313e34bdfcb0941de9d4f25028a9c77 to your computer and use it in GitHub Desktop.
An up hook for openvpn clients to configure routing for private DNS domains. #openvpn #resolvectl
#!/bin/bash
set -u -e
# On systems with `systemd-resolved.service`, this script can be used as an "up" hook in client ovpn file.
# For example:
# ...
# script-security 2
# up /etc/openvpn/add-routing-domain.bash
# ...
function _add_routing_domain() {
declare -l -r domain=${1}
resolvectl domain ${dev} ${domain}
declare -l dns_server
declare var_name=""
declare -i i=0
while true; do
let i=i+1
var_name="foreign_option_${i}"
v=${!var_name:-}
[[ -n "$v" ]] || break;
dns_server=$(grep -Po -e 'dhcp-option\s+DNS\s+\K(.+)' <<<"${v}")
if [[ -n "${dns_server}" ]]; then
resolvectl dns ${dev} ${dns_server}
break;
fi
done
}
if [[ -n "${OPENVPN_internal_domain:-}" ]]; then
_add_routing_domain ${OPENVPN_internal_domain}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment