add ctl script
This commit is contained in:
68
diyvpnctl.sh
Executable file
68
diyvpnctl.sh
Executable file
@@ -0,0 +1,68 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
diyvpn_cfg="${DIYVPN_CFG:-$HOME/.config/diyvpn/servers}"
|
||||
|
||||
add() {
|
||||
read -rp "Server name [e.g. foobar] [required] : " name
|
||||
if [[ -d "$diyvpn_cfg/$name" ]]; then
|
||||
echo "Warning: $diyvpn_cfg/$name already exists. Exising files will be overwritten. [Ctrl+C to quit]"
|
||||
fi
|
||||
|
||||
read -rp "Listen address [e.g. 127.0.0.1:9090] [required] : " listen_address
|
||||
read -rp "Idle Timeout [e.g. 5min] [5min] : " idle_timeout
|
||||
: "${idle_timeout:=5min}"
|
||||
|
||||
read -rp "Remote server Ip [e.g. 1.2.3.4] [required] : " hostname
|
||||
read -rp "Remote server ssh port [e.g. 2222] [22] : " port
|
||||
: "${port:=22}"
|
||||
read -rp "Remote server username [e.g. dave] [required] : " username
|
||||
read -rp "SSH private key [e.g. ~/.ssh/id_ed25519] [required] : " identityfile
|
||||
|
||||
mkdir -p "$diyvpn_cfg/$name"
|
||||
|
||||
cat >"$diyvpn_cfg/$name/config.rc" <<-EOF
|
||||
LISTEN_ADDRESS=$listen_address
|
||||
IDLE_TIMEOUT=$idle_timeout
|
||||
EOF
|
||||
|
||||
cat >"$diyvpn_cfg/$name/ssh_config" <<-EOF
|
||||
Hostname $hostname
|
||||
Port $port
|
||||
User $username
|
||||
IdentityFile $identityfile
|
||||
EOF
|
||||
|
||||
head -100 "$diyvpn_cfg/$name/"*
|
||||
|
||||
systemctl --user daemon-reload
|
||||
}
|
||||
|
||||
action="${1:-none}"
|
||||
|
||||
case "$action" in
|
||||
|
||||
add)
|
||||
|
||||
add
|
||||
;;
|
||||
|
||||
list)
|
||||
|
||||
echo "config path: $diyvpn_cfg"
|
||||
paste <(
|
||||
echo "servers"
|
||||
cd "$diyvpn_cfg"
|
||||
basename ./*
|
||||
) <(
|
||||
echo "ListenAddress"
|
||||
sed -n '/LISTEN/s/.*=\(.*\)/\1/p' "$diyvpn_cfg"/*/config.rc
|
||||
) | column -t
|
||||
;;
|
||||
|
||||
*)
|
||||
echo "Usage: diyvpnctl.sh [add|list]"
|
||||
;;
|
||||
|
||||
esac
|
||||
Reference in New Issue
Block a user