55 lines
No EOL
1.3 KiB
Bash
55 lines
No EOL
1.3 KiB
Bash
#!/bin/bash
|
|
# Author: Martijn de Boer
|
|
|
|
if [[ $EUID -ne 0 ]]; then
|
|
echo "You must be a root user" 2>&1
|
|
exit 1
|
|
fi
|
|
|
|
if [[ ! -f /etc/debian_version ]]; then
|
|
echo "This script only works on Debian"
|
|
exit 1
|
|
fi
|
|
|
|
set -e
|
|
|
|
DOMAINS_CONFIG="/etc/tunnel-manager/domains.json"
|
|
|
|
add_domain() {
|
|
local domain="$1"
|
|
local target_ips=("${@:2}")
|
|
|
|
[[ -z "$domain" ]] && { echo "Error: Domain required"; exit 1; }
|
|
[[ ${#target_ips[@]} -eq 0 ]] && { echo "Error: At least one target IP required"; exit 1; }
|
|
|
|
jq --arg domain "$domain" \
|
|
--arg ipv4 "${target_ips[0]}" \
|
|
--arg ipv6 "${target_ips[1]:-}" \
|
|
'.domains[$domain] = {
|
|
"ipv4": $ipv4,
|
|
"ipv6": $ipv6,
|
|
"added_at": now
|
|
}' "$DOMAINS_CONFIG" > tmp.json && mv tmp.json "$DOMAINS_CONFIG"
|
|
|
|
/root/tunnel-manager/update-tunnels.sh
|
|
}
|
|
|
|
list_domains() {
|
|
jq '.' "$DOMAINS_CONFIG"
|
|
}
|
|
|
|
remove_domain() {
|
|
local domain="$1"
|
|
jq "del(.domains[\"$domain\"])" "$DOMAINS_CONFIG" > tmp.json && mv tmp.json "$DOMAINS_CONFIG"
|
|
/root/tunnel-manager/update-tunnels.sh
|
|
}
|
|
|
|
case "$1" in
|
|
add) add_domain "${@:2}" ;;
|
|
list) list_domains ;;
|
|
remove) remove_domain "$2" ;;
|
|
*)
|
|
echo "Usage: $0 {add|list|remove} [domain] [ipv4] [ipv6]"
|
|
exit 1
|
|
;;
|
|
esac |