From 63cd03faaaf2a33c99246e41eb0b0392eb49588f Mon Sep 17 00:00:00 2001 From: Jacob Tuz Poot Date: Mon, 11 Sep 2023 23:07:20 -0600 Subject: [PATCH] chore: improve code formatting --- src/list-sessions.sh | 4 +- src/openvpn3applet.sh | 140 +++++++++++++++++++----------------------- 2 files changed, 64 insertions(+), 80 deletions(-) diff --git a/src/list-sessions.sh b/src/list-sessions.sh index 11b4d8c..589f1bc 100755 --- a/src/list-sessions.sh +++ b/src/list-sessions.sh @@ -4,12 +4,10 @@ YAD_OPTIONS="--window-icon='dialog-information' --name=IxSysinfo" KEY=$RANDOM - # main dialog TXT="Current sessions:\\n\\n" TXT+="$(openvpn3 sessions-list)\\n" -openvpn3 sessions-list | grep -v "\-\-\-" | grep -v "Session name" | sed -r "s/^[^:]*:\s//" | sed -r "s/\s\s.*//"|\ +openvpn3 sessions-list | grep -v "\-\-\-" | grep -v "Session name" | sed -r "s/^[^:]*:\s//" | sed -r "s/\s\s.*//" | yad --notebook --width=1000 --height=450 --title="System info" --text="$TXT" --button=Close \ --key=$KEY --list --column="Path" --column="Created" --column="Owner" --column="Config name" --column="Status" - diff --git a/src/openvpn3applet.sh b/src/openvpn3applet.sh index 6cc99b8..b342f28 100755 --- a/src/openvpn3applet.sh +++ b/src/openvpn3applet.sh @@ -9,22 +9,20 @@ sleepTime=10 ################################################################################ # Functions # ################################################################################ -function display_help() -{ - # Display Help - echo "Helper to display openvpn3 connection state." - echo - echo "Syntax: ./openvpn3applet.sh [-h|-s n]" - echo "options:" - echo "-s [seconds] Time between state refresh in seconds, default: 10" - echo "-h Print this Help." - echo +function display_help() { + # Display Help + echo "Helper to display openvpn3 connection state." + echo + echo "Syntax: ./openvpn3applet.sh [-h|-s n]" + echo "options:" + echo "-s [seconds] Time between state refresh in seconds, default: 10" + echo "-h Print this Help." + echo } function set_sleep_time() { - if ! [[ $1 =~ $re ]] - then - echo $(date) " | " "Invalid number for sleepTime, using default (10s)" + if ! [[ $1 =~ $re ]]; then + echo $(date) " | " "Invalid number for sleepTime, using default (10s)" else sleepTime=$1 fi @@ -34,7 +32,7 @@ function select_and_save_configfile() { configfile=$(yad --width="500" --center --title="Select config file" --text="\nPlease select your openvpn3 config file:\n" --button="OK" --file --file-filter="*.ovpn") # create directory in case it does not exist mkdir -p $CONFIG_DIR - echo "OPENVPN_CONFIG_PATH="$configfile > $CONFIG_FILE_PATH + echo "OPENVPN_CONFIG_PATH="$configfile >$CONFIG_FILE_PATH OPENVPN_CONFIG_PATH=$configfile echo $(date) " | " "Selected configfile: " $OPENVPN_CONFIG_PATH } @@ -42,80 +40,76 @@ export -f select_and_save_configfile # add handler to manage process shutdown function on_exit() { - exec 3<> $PIPE + exec 3<>$PIPE echo $(date) " | " "Quitting.." - echo "quit" >&3 - rm -f $PIPE + echo "quit" >&3 + rm -f $PIPE } export -f on_exit # add handler for tray icon left click function on_click() { - update_state + update_state } export -f on_click function disconnect() { echo $(date) " | " "Disconnecting.." - exec 3<> $PIPE + exec 3<>$PIPE echo "icon:$RUNNING_DIR/icons/circle-lightblue.png" >&3 - sessionPath=$(openvpn3 sessions-list | grep Path | awk ' { print $2 } ') - openvpn3 session-manage --disconnect --session-path $sessionPath - update_state - echo $(date) " | " "Disconnected." + sessionPath=$(openvpn3 sessions-list | grep Path | awk ' { print $2 } ') + openvpn3 session-manage --disconnect --session-path $sessionPath + update_state + echo $(date) " | " "Disconnected." } export -f disconnect function connect() { echo $(date) " | " "Connecting..." - exec 3<> $PIPE + exec 3<>$PIPE echo "icon:$RUNNING_DIR/icons/circle-lightblue.png" >&3 - openvpn3 session-start --config $OPENVPN_CONFIG_PATH - update_state + openvpn3 session-start --config $OPENVPN_CONFIG_PATH + update_state } export -f connect function update_state() { - exec 3<> $PIPE + exec 3<>$PIPE defaultMenuEntries="|Select config!bash -c 'select_and_save_configfile'|Exit!bash -c 'on_exit'" statEntry="|Stats!bash -c 'display_session_stats'" - + detectedState=false output=$(openvpn3 sessions-list) while IFS= read -r line; do - if [[ $line = "No sessions available" || $line = *"Client authentication failed: Authentication failed" ]] - then + if [[ $line = "No sessions available" || $line = *"Client authentication failed: Authentication failed" ]]; then echo $(date) " | " "Not connected" echo "icon:$RUNNING_DIR/icons/circle-red.png" >&3 echo "menu:Connect!bash -c 'connect'" $defaultMenuEntries >&3 echo "tooltip:Not connected" >&3 detectedState=true - elif [[ $line = *"Client connected" ]] - then + elif [[ $line = *"Client connected" ]]; then echo $(date) " | " "Session found!" echo "icon:$RUNNING_DIR/icons/circle-green.png" >&3 echo "menu:Disconnect!bash -c 'disconnect'" $statEntry $defaultMenuEntries >&3 echo "tooltip:Connected to VPN" >&3 detectedState=true - elif [[ $line = *"Web authentication required to connect" ]] - then + elif [[ $line = *"Web authentication required to connect" ]]; then echo $(date) " | " "Waiting for web authentication" echo "icon:$RUNNING_DIR/icons/circle-lightblue.png" >&3 echo "menu:Disconnect!bash -c 'disconnect'" $statEntry $defaultMenuEntries >&3 echo "tooltip:Waiting for Web authentiction (check webbrowser)" >&3 detectedState=true fi - done <<< "$output" - + done <<<"$output" + # failsafe for unknown states - if [[ $detectedState = false ]] - then + if [[ $detectedState = false ]]; then echo $(date) " | " "Unknown state" echo "icon:$RUNNING_DIR/icons/circle-red.png" >&3 echo "menu:Connect!bash -c 'connect'" $defaultMenuEntries >&3 echo "tooltip:Not connected" >&3 fi - + } export -f update_state @@ -130,35 +124,33 @@ export -f display_session_stats # End of functions (make sure to export them) # ################################################################################ - ################################################################################ # Start of script # ################################################################################ -while getopts s:h flag -do - case "${flag}" in - s) echo "mklsdljf" ${OPTARG} - if ! [[ ${OPTARG} =~ ^[0-9]+$ ]] - then - echo $(date) " | " "Invalid number for sleepTime, using default ("$sleepTime"s)" - else - sleepTime=${OPTARG} - echo $(date) " | " "Using sleepTime:" $sleepTime - fi - ;; - h) display_help - exit;; - esac +while getopts s:h flag; do + case "${flag}" in + s) + echo "mklsdljf" ${OPTARG} + if ! [[ ${OPTARG} =~ ^[0-9]+$ ]]; then + echo $(date) " | " "Invalid number for sleepTime, using default ("$sleepTime"s)" + else + sleepTime=${OPTARG} + echo $(date) " | " "Using sleepTime:" $sleepTime + fi + ;; + h) + display_help + exit + ;; + esac done # check if there is a saved vpn config file, if not, ask for one and save it -if [ -f "$CONFIG_FILE_PATH" ] -then +if [ -f "$CONFIG_FILE_PATH" ]; then source $CONFIG_FILE_PATH fi -if [[ -z $OPENVPN_CONFIG_PATH || $OPENVPN_CONFIG_PATH = "" ]] -then +if [[ -z $OPENVPN_CONFIG_PATH || $OPENVPN_CONFIG_PATH = "" ]]; then select_and_save_configfile fi export OPENVPN_CONFIG_PATH @@ -169,32 +161,26 @@ export PIPE mkfifo $PIPE # attach a file descriptor to the file -exec 3<> $PIPE +exec 3<>$PIPE trap on_exit EXIT RUNNING_DIR=${BASH_SOURCE%/*} export RUNNING_DIR - # create the notification icon -yad --notification \ - --listen \ - --image="$RUNNING_DIR/icons/circle-red.png" \ - --text="openvpn3-applet" \ - --command="bash -c 'on_click'" <&3 & notifpid=$! - -while true -do - if ! ps -p $notifpid > /dev/null - then +yad --notification \ + --listen \ + --image="$RUNNING_DIR/icons/circle-red.png" \ + --text="openvpn3-applet" \ + --command="bash -c 'on_click'" <&3 & +notifpid=$! + +while true; do + if ! ps -p $notifpid >/dev/null; then # exit if yad process has been terminated exit fi - update_state - sleep $sleepTime + update_state + sleep $sleepTime done - - - -