From 614f694fbd4152c1263b8e5fd8c75714e653d047 Mon Sep 17 00:00:00 2001 From: Balakrishnan Balasubramanian Date: Tue, 9 Dec 2025 12:29:54 -0500 Subject: [PATCH] Start the socket file. Add file validation and cleanup --- diyvpn.sh | 20 ++++++++++++++++++-- diyvpnctl.sh | 29 ++++++++++++++--------------- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/diyvpn.sh b/diyvpn.sh index 7bdd645..3398ed0 100755 --- a/diyvpn.sh +++ b/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}" diff --git a/diyvpnctl.sh b/diyvpnctl.sh index f00296b..4f91f4f 100755 --- a/diyvpnctl.sh +++ b/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