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}"
|
: "${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")")
|
script_dir=$(dirname "$(realpath "$0")")
|
||||||
common_ssh_cfg_path="${DIYVPN_SSH_CFG:-$script_dir/common_sshconfig}"
|
common_ssh_cfg_path="${DIYVPN_SSH_CFG:-$script_dir/common_sshconfig}"
|
||||||
diyvpn_cfg="${DIYVPN_CFG:-$HOME/.config/diyvpn/servers}"
|
diyvpn_cfg="${DIYVPN_CFG:-$HOME/.config/diyvpn/servers}"
|
||||||
|
|
||||||
|
check_file_exists "$common_ssh_cfg_path"
|
||||||
|
|
||||||
generate() {
|
generate() {
|
||||||
local cfgpath name server_ssh_cfg idle_timeout listen_address
|
local cfgpath name server_ssh_cfg idle_timeout listen_address
|
||||||
cfgpath="$1"
|
cfgpath="$1"
|
||||||
name=$(basename "$cfgpath" | tr -d '[:space:]')
|
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}"
|
idle_timeout="${IDLE_TIMEOUT:-10min}"
|
||||||
listen_address="${LISTEN_ADDRESS:?LISTEN_ADDRESS should be set}"
|
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}"
|
diyvpn_cfg="${DIYVPN_CFG:-$HOME/.config/diyvpn/servers}"
|
||||||
|
|
||||||
add() {
|
add() {
|
||||||
|
local name listen_address idle_timeout hostname port username identityfile
|
||||||
|
|
||||||
read -rp "Server name [e.g. foobar] [required] : " name
|
read -rp "Server name [e.g. foobar] [required] : " name
|
||||||
if [[ -d "$diyvpn_cfg/$name" ]]; then
|
if [[ -d "$diyvpn_cfg/$name" ]]; then
|
||||||
echo "Warning: $diyvpn_cfg/$name already exists. Exising files will be overwritten. [Ctrl+C to quit]"
|
echo "Warning: $diyvpn_cfg/$name already exists. Exising files will be overwritten. [Ctrl+C to quit]"
|
||||||
@@ -32,24 +34,19 @@ add() {
|
|||||||
Port $port
|
Port $port
|
||||||
User $username
|
User $username
|
||||||
IdentityFile $identityfile
|
IdentityFile $identityfile
|
||||||
|
|
||||||
|
# vim: set filetype=sshconfig:
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
|
echo "Diyvpn config for $name added. Edit below configs for further changes"
|
||||||
|
|
||||||
head -100 "$diyvpn_cfg/$name/"*
|
head -100 "$diyvpn_cfg/$name/"*
|
||||||
|
|
||||||
systemctl --user daemon-reload
|
systemctl --user daemon-reload
|
||||||
|
systemctl --user start "diyvpnact-$name.socket"
|
||||||
}
|
}
|
||||||
|
|
||||||
action="${1:-none}"
|
list() {
|
||||||
|
|
||||||
case "$action" in
|
|
||||||
|
|
||||||
add)
|
|
||||||
|
|
||||||
add
|
|
||||||
;;
|
|
||||||
|
|
||||||
list)
|
|
||||||
|
|
||||||
echo "config path: $diyvpn_cfg"
|
echo "config path: $diyvpn_cfg"
|
||||||
paste <(
|
paste <(
|
||||||
echo "servers"
|
echo "servers"
|
||||||
@@ -59,10 +56,12 @@ list)
|
|||||||
echo "ListenAddress"
|
echo "ListenAddress"
|
||||||
sed -n '/LISTEN/s/.*=\(.*\)/\1/p' "$diyvpn_cfg"/*/config.rc
|
sed -n '/LISTEN/s/.*=\(.*\)/\1/p' "$diyvpn_cfg"/*/config.rc
|
||||||
) | column -t
|
) | column -t
|
||||||
;;
|
}
|
||||||
|
|
||||||
*)
|
action="${1:-none}"
|
||||||
echo "Usage: diyvpnctl.sh [add|list]"
|
|
||||||
;;
|
|
||||||
|
|
||||||
|
case "$action" in
|
||||||
|
add) add ;;
|
||||||
|
list) list ;;
|
||||||
|
*) echo "Usage: diyvpnctl.sh [add|list]" ;;
|
||||||
esac
|
esac
|
||||||
|
|||||||
Reference in New Issue
Block a user