Skip to content

Commit

Permalink
Add 'ucsf vpn reconnect'
Browse files Browse the repository at this point in the history
  • Loading branch information
HenrikBengtsson committed May 20, 2024
1 parent 20b967e commit 5e6860d
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 5 deletions.
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ ucsf-vpn

* Add `ucsf vpn install-vpnc`, which is required before using
`--flavor=<flavor>`.

* Add `ucsf vpn reconnect`, which signals `SIGUSR2` to the
OpenConnect process and thereby "forces an immediate disconnection
and reconnection; this can be used to quickly recover from LAN IP
address changes."

### Deprecated and Defunct

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ Usage:
Commands:
start Connect to VPN
stop Disconnect from VPN
reconnect Reconnect to VPN
restart Disconnect and reconnect to VPN
toggle Connect to or disconnect from VPN
status Display VPN connection status
Expand Down Expand Up @@ -202,7 +203,7 @@ Useful resources:
* UCSF Managing Your Passwords:
- https://it.ucsf.edu/services/managing-your-passwords
Version: 5.8.0-9007
Version: 5.8.0-9008
Copyright: Henrik Bengtsson (2016-2024)
License: GPL (>= 2.1) [https://www.gnu.org/licenses/gpl.html]
Source: https://github.com/HenrikBengtsson/ucsf-vpn
Expand Down
1 change: 1 addition & 0 deletions WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ dns
PID
www
YubiKey
reconnection
56 changes: 54 additions & 2 deletions bin/ucsf-vpn
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
### Commands:
### start Connect to VPN
### stop Disconnect from VPN
### reconnect Reconnect to VPN
### restart Disconnect and reconnect to VPN
### toggle Connect to or disconnect from VPN
### status Display VPN connection status
Expand Down Expand Up @@ -109,7 +110,7 @@
### * UCSF Managing Your Passwords:
### - https://it.ucsf.edu/services/managing-your-passwords
###
### Version: 5.8.0-9007
### Version: 5.8.0-9008
### Copyright: Henrik Bengtsson (2016-2024)
### License: GPL (>= 2.1) [https://www.gnu.org/licenses/gpl.html]
### Source: https://github.com/HenrikBengtsson/ucsf-vpn
Expand Down Expand Up @@ -929,6 +930,9 @@ function openconnect_start() {
_exit 0
fi

rm "$(logfile)"
log "openconnect_start() ..."

## Record IP routing table before connecting to the VPN
ip route show > "${ip_route_novpn_file}"

Expand Down Expand Up @@ -1050,6 +1054,8 @@ function openconnect_start() {
default_route_before=$(grep -E '^default[[:space:]]' "${ip_route_novpn_file}" | sed 's/default //' | sed -E 's/ +$//')
minfo "Default IP routing was changed from '${default_route_before}' to '${default_route_after}'"
fi

log "openconnect_start() ... done"

minfo "Connected to VPN server"
}
Expand All @@ -1060,6 +1066,8 @@ function openconnect_stop() {

mdebug "openconnect_stop() ..."

log "openconnect_stop() ..."

pid=$(openconnect_pid)
if [[ $pid == -1 ]]; then
mwarn "Could not detect a VPN ('openconnect') process. Skipping."
Expand All @@ -1082,6 +1090,7 @@ function openconnect_stop() {
## session off, disconnecting from the gateway, and running the vpnc-script
## to restore the network configuration.
mdebug "Killing OpenConnect process: sudo kill -s INT \"$pid\" 2> /dev/null"
log "- sudo kill -s INT $pid"
sudo kill -s INT $pid 2> /dev/null

## Wait for process to terminate
Expand Down Expand Up @@ -1132,11 +1141,46 @@ function openconnect_stop() {
default_route_before=$(grep -E '^default[[:space:]].*tun' "${ip_route_vpn_file}" | sed 's/default //' | sed -E 's/ +$//')
minfo "Default IP routing was changed from '${default_route_before}' to '${default_route_after}'"
fi


log "openconnect_stop() ... done"

minfo "Disconnected from VPN server"
}


function openconnect_reconnect() {
local kill_timeout
local -i kk pid

mdebug "openconnect_reconnect() ..."

log "openconnect_reconnect() ..."

pid=$(openconnect_pid)
if [[ $pid == -1 ]]; then
mwarn "Could not detect a VPN ('openconnect') process. Skipping."
return
fi

minfo "Reconnecting to VPN server"

assert_sudo "stop"

## From 'man openconnect': SIGUSR2 forces an immediate disconnection and
## reconnection; this can be used to quickly recover from LAN IP address
## changes.
mdebug "sudo kill -s USR2 $pid"
log "- sudo kill -s USR2 $pid"
sudo kill -s USR2 $pid 2> /dev/null

status "connected"

log "openconnect_reconnect() ... done"

minfo "Reconnected to VPN server"
}


# -------------------------------------------------------------------------
# XDG config utility functions
# -------------------------------------------------------------------------
Expand Down Expand Up @@ -1360,6 +1404,10 @@ function logfile() {
echo "${file}"
}

log() {
echo "[$(date --iso-8601=seconds)] $*" >> "$(logfile)"
}


# -------------------------------------------------------------------------
# Deprecated and defunct
Expand Down Expand Up @@ -1415,6 +1463,8 @@ while [[ $# -gt 0 ]]; do
action=$1
elif [[ "$1" == "stop" ]]; then
action=$1
elif [[ "$1" == "reconnect" ]]; then
action=$1
elif [[ "$1" == "toggle" ]]; then
action=$1
force=true
Expand Down Expand Up @@ -1674,6 +1724,8 @@ elif [[ $action == "start" ]]; then
elif [[ $action == "stop" ]]; then
openconnect_stop
status "disconnected"
elif [[ $action == "reconnect" ]]; then
openconnect_reconnect
elif [[ $action == "restart" ]]; then
if $force || is_connected; then
openconnect_stop
Expand Down
45 changes: 44 additions & 1 deletion src/incl/openconnect.sh
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ function openconnect_start() {
_exit 0
fi

rm "$(logfile)"
log "openconnect_start() ..."

## Record IP routing table before connecting to the VPN
ip route show > "${ip_route_novpn_file}"

Expand Down Expand Up @@ -275,6 +278,8 @@ function openconnect_start() {
default_route_before=$(grep -E '^default[[:space:]]' "${ip_route_novpn_file}" | sed 's/default //' | sed -E 's/ +$//')
minfo "Default IP routing was changed from '${default_route_before}' to '${default_route_after}'"
fi

log "openconnect_start() ... done"

minfo "Connected to VPN server"
}
Expand All @@ -285,6 +290,8 @@ function openconnect_stop() {

mdebug "openconnect_stop() ..."

log "openconnect_stop() ..."

pid=$(openconnect_pid)
if [[ $pid == -1 ]]; then
mwarn "Could not detect a VPN ('openconnect') process. Skipping."
Expand All @@ -307,6 +314,7 @@ function openconnect_stop() {
## session off, disconnecting from the gateway, and running the vpnc-script
## to restore the network configuration.
mdebug "Killing OpenConnect process: sudo kill -s INT \"$pid\" 2> /dev/null"
log "- sudo kill -s INT $pid"
sudo kill -s INT $pid 2> /dev/null

## Wait for process to terminate
Expand Down Expand Up @@ -357,6 +365,41 @@ function openconnect_stop() {
default_route_before=$(grep -E '^default[[:space:]].*tun' "${ip_route_vpn_file}" | sed 's/default //' | sed -E 's/ +$//')
minfo "Default IP routing was changed from '${default_route_before}' to '${default_route_after}'"
fi


log "openconnect_stop() ... done"

minfo "Disconnected from VPN server"
}


function openconnect_reconnect() {
local kill_timeout
local -i kk pid

mdebug "openconnect_reconnect() ..."

log "openconnect_reconnect() ..."

pid=$(openconnect_pid)
if [[ $pid == -1 ]]; then
mwarn "Could not detect a VPN ('openconnect') process. Skipping."
return
fi

minfo "Reconnecting to VPN server"

assert_sudo "stop"

## From 'man openconnect': SIGUSR2 forces an immediate disconnection and
## reconnection; this can be used to quickly recover from LAN IP address
## changes.
mdebug "sudo kill -s USR2 $pid"
log "- sudo kill -s USR2 $pid"
sudo kill -s USR2 $pid 2> /dev/null

status "connected"

log "openconnect_reconnect() ... done"

minfo "Reconnected to VPN server"
}
11 changes: 10 additions & 1 deletion src/ucsf-vpn.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### Commands:
### start Connect to VPN
### stop Disconnect from VPN
### reconnect Reconnect to VPN
### restart Disconnect and reconnect to VPN
### toggle Connect to or disconnect from VPN
### status Display VPN connection status
Expand Down Expand Up @@ -106,7 +107,7 @@
### * UCSF Managing Your Passwords:
### - https://it.ucsf.edu/services/managing-your-passwords
###
### Version: 5.8.0-9007
### Version: 5.8.0-9008
### Copyright: Henrik Bengtsson (2016-2024)
### License: GPL (>= 2.1) [https://www.gnu.org/licenses/gpl.html]
### Source: https://github.com/HenrikBengtsson/ucsf-vpn
Expand Down Expand Up @@ -429,6 +430,10 @@ function logfile() {
echo "${file}"
}

log() {
echo "[$(date --iso-8601=seconds)] $*" >> "$(logfile)"
}


# -------------------------------------------------------------------------
# Deprecated and defunct
Expand Down Expand Up @@ -484,6 +489,8 @@ while [[ $# -gt 0 ]]; do
action=$1
elif [[ "$1" == "stop" ]]; then
action=$1
elif [[ "$1" == "reconnect" ]]; then
action=$1
elif [[ "$1" == "toggle" ]]; then
action=$1
force=true
Expand Down Expand Up @@ -743,6 +750,8 @@ elif [[ $action == "start" ]]; then
elif [[ $action == "stop" ]]; then
openconnect_stop
status "disconnected"
elif [[ $action == "reconnect" ]]; then
openconnect_reconnect
elif [[ $action == "restart" ]]; then
if $force || is_connected; then
openconnect_stop
Expand Down

0 comments on commit 5e6860d

Please sign in to comment.