Last active
November 25, 2022 02:42
-
-
Save vdwijngaert/7dc5011dd24e598745a20838ea303415 to your computer and use it in GitHub Desktop.
Helper function to automate openvpn3 connections that need SSO capabilities
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Helper function to automate openvpn3 connections that need SSO capabilities. | |
# Add it to your ~/.bashrc file using following code (and make sure to edit $OPENVPN3_CONFIG_NAME): | |
# source /path/to/openvpn3_sso.sh | |
# If using zsh, you can also save this to the `$ZSH/custom/openvpn3_sso.zsh` file. | |
# You can automatically have this run by calling ovpn3_sso_connect in your ~/.bashrc or ~/.zshrc file. | |
# WARNING: This has currently only been tested on zsh and bash. | |
# (c) Koen Van den Wijngaert <[email protected]> | |
# OVPN3 config name. This can be set once using following command: | |
# openvpn3 config-import -c ./path-to-ovpn-profile.ovpn -p -n ovpn-profile-name | |
export OPENVPN3_CONFIG_NAME=ovpn-profile-name | |
# Prompt for confirmation. | |
function ovpn3_sso_prompt_confirmation() { | |
# Ask confirmation. | |
is_bash=$(ps -p $$ | grep "bash") | |
if [[ $? -eq 0 ]]; then | |
read -e -p "$1 [Y/n] " YN; # bash-version | |
else | |
read YN"?$1 [Y/n] "; # zsh-version | |
fi | |
# Start session if confirmation is positive or default. | |
[[ $YN == "y" || $YN == "Y" || $YN == "" ]] && return 0 || return 1; | |
} | |
# Check VPN connection, if not connected, try and initiate it. | |
# Experiencing issues? Enable output with VPN_DEBUG_MODE=1 ovpn3_sso_connect. | |
function ovpn3_sso_connect() { | |
if [[ -z "$VPN_DEBUG_MODE" || "$VPN_DEBUG_MODE" == 0 ]]; then | |
redir_stderr='/dev/null'; | |
redir_stdout='/dev/null'; | |
else | |
redir_stderr='/dev/stderr'; | |
redir_stdout='/dev/stdout'; | |
fi | |
# Check whether a session is already ongoing. | |
session_stats=$(openvpn3 session-stats -c $OPENVPN3_CONFIG_NAME > $redir_stdout 2> $redir_stderr); | |
if [[ $? -eq 0 ]]; then | |
# session might be paused. | |
is_session_connected=$(openvpn3 sessions-list | grep -A3 "$OPENVPN3_CONFIG_NAME" | grep "Client connected"); | |
if [[ $? -eq 0 ]]; then | |
return 0; | |
fi | |
# Resume session if confirmed. | |
ovpn3_sso_prompt_confirmation "Resume ovpn3 session for \"$OPENVPN3_CONFIG_NAME\"?" | |
if [[ $? -eq 0 ]]; then | |
echo 'Session resuming...'; | |
session_resume=$(openvpn3 session-manage -R -c "$OPENVPN3_CONFIG_NAME" > $redir_stdout 2> $redir_stderr); | |
test $? -eq 0 && echo 'Session resumed.' || echo 'Failed to resume session.'; | |
fi | |
return 0; | |
fi | |
# Start session if confirmed. | |
ovpn3_sso_prompt_confirmation "Initiate ovpn3 session for \"$OPENVPN3_CONFIG_NAME\"?" | |
if [[ $? -eq 0 ]]; then | |
echo 'Initiating session...'; | |
session_resume=$(openvpn3 session-start -c "$OPENVPN3_CONFIG_NAME" > $redir_stdout 2> $redir_stderr); | |
test $? -eq 0 && echo 'Session initiated.' || echo 'Failed to initiate session.'; | |
fi | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment