Start the socket file. Add file validation and cleanup
This commit is contained in:
20
diyvpn.sh
20
diyvpn.sh
@@ -5,17 +5,33 @@ opdir="${1?Output dir missing}"
|
||||
|
||||
: "${HOME?HOME is not set}"
|
||||
|
||||
check_file_exists() {
|
||||
if [[ -f "$1" ]]; then
|
||||
return 0
|
||||
fi
|
||||
echo "Warning: File does not exist- $1"
|
||||
return 1
|
||||
}
|
||||
|
||||
script_dir=$(dirname "$(realpath "$0")")
|
||||
common_ssh_cfg_path="${DIYVPN_SSH_CFG:-$script_dir/common_sshconfig}"
|
||||
diyvpn_cfg="${DIYVPN_CFG:-$HOME/.config/diyvpn/servers}"
|
||||
|
||||
check_file_exists "$common_ssh_cfg_path"
|
||||
|
||||
generate() {
|
||||
local cfgpath name server_ssh_cfg idle_timeout listen_address
|
||||
cfgpath="$1"
|
||||
name=$(basename "$cfgpath" | tr -d '[:space:]')
|
||||
server_ssh_cfg="$cfgpath"/ssh_config # TODO validate
|
||||
|
||||
source "$cfgpath/config.rc"
|
||||
server_ssh_cfg="$cfgpath"/ssh_config
|
||||
server_config_rc="$cfgpath"/config.rc
|
||||
|
||||
check_file_exists "$server_ssh_cfg" || return 0
|
||||
check_file_exists "$server_config_rc" || return 0
|
||||
|
||||
# shellcheck disable=SC1091
|
||||
source "server_config_rc"
|
||||
|
||||
idle_timeout="${IDLE_TIMEOUT:-10min}"
|
||||
listen_address="${LISTEN_ADDRESS:?LISTEN_ADDRESS should be set}"
|
||||
|
||||
29
diyvpnctl.sh
29
diyvpnctl.sh
@@ -5,6 +5,8 @@ set -euo pipefail
|
||||
diyvpn_cfg="${DIYVPN_CFG:-$HOME/.config/diyvpn/servers}"
|
||||
|
||||
add() {
|
||||
local name listen_address idle_timeout hostname port username identityfile
|
||||
|
||||
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]"
|
||||
@@ -32,24 +34,19 @@ add() {
|
||||
Port $port
|
||||
User $username
|
||||
IdentityFile $identityfile
|
||||
|
||||
# vim: set filetype=sshconfig:
|
||||
EOF
|
||||
|
||||
echo "Diyvpn config for $name added. Edit below configs for further changes"
|
||||
|
||||
head -100 "$diyvpn_cfg/$name/"*
|
||||
|
||||
systemctl --user daemon-reload
|
||||
systemctl --user start "diyvpnact-$name.socket"
|
||||
}
|
||||
|
||||
action="${1:-none}"
|
||||
|
||||
case "$action" in
|
||||
|
||||
add)
|
||||
|
||||
add
|
||||
;;
|
||||
|
||||
list)
|
||||
|
||||
list() {
|
||||
echo "config path: $diyvpn_cfg"
|
||||
paste <(
|
||||
echo "servers"
|
||||
@@ -59,10 +56,12 @@ list)
|
||||
echo "ListenAddress"
|
||||
sed -n '/LISTEN/s/.*=\(.*\)/\1/p' "$diyvpn_cfg"/*/config.rc
|
||||
) | column -t
|
||||
;;
|
||||
}
|
||||
|
||||
*)
|
||||
echo "Usage: diyvpnctl.sh [add|list]"
|
||||
;;
|
||||
action="${1:-none}"
|
||||
|
||||
case "$action" in
|
||||
add) add ;;
|
||||
list) list ;;
|
||||
*) echo "Usage: diyvpnctl.sh [add|list]" ;;
|
||||
esac
|
||||
|
||||
Reference in New Issue
Block a user