Add functions to start/stop and ssh socks proxy
This commit is contained in:
parent
9106aeac69
commit
4eba89e95d
1 changed files with 63 additions and 0 deletions
|
@ -66,6 +66,69 @@ function pr-checkout() {
|
|||
fi
|
||||
}
|
||||
|
||||
# tail, but better
|
||||
function watch-file() {
|
||||
tail -f $1 | bat --paging=never -l log
|
||||
}
|
||||
|
||||
# Get a temporary directory in a cross-platform manner
|
||||
# See https://unix.stackexchange.com/a/685873
|
||||
function get-temporary-directory() {
|
||||
local temporary_directory=${XDG_RUNTIME_DIR:-${TMPDIR:-${TMP:-${TEMP:-/tmp}}}}
|
||||
|
||||
echo $temporary_directory
|
||||
}
|
||||
|
||||
# Start an SSH SOCKS tunnel
|
||||
function start-proxy() {
|
||||
if [ -z "$SSH_PROXY_HOST" ]; then
|
||||
echo "SSH_PROXY_HOST is not set or empty"
|
||||
|
||||
return
|
||||
fi
|
||||
|
||||
local socket_dir=$(get-temporary-directory)
|
||||
|
||||
# Check if the last character of socket_dir is a directory separator character
|
||||
if [[ $socket_dir[-1] == / ]]; then
|
||||
# If yes, remove the directory separator character
|
||||
socket_dir="${socket_dir%?}"
|
||||
fi
|
||||
|
||||
echo "Opening proxy connection"
|
||||
|
||||
# Below we use -S and -M to help make closing the connection more reliable
|
||||
# See this Stack Overflow answer for more info
|
||||
# https://unix.stackexchange.com/a/525388
|
||||
|
||||
# -D 1337 opens up the SOXKS tunnel on localhost:1337
|
||||
# -f Tells `ssh` to fork the ssh process in to the background
|
||||
# -C Enables compression of data on the connections
|
||||
# -q Uses quiet mode
|
||||
# -N Do not execute a remote command on this connection
|
||||
# -S Set the ControlPath for this connection
|
||||
# -M Place the client into `master` mode
|
||||
ssh -D 1337 -f -C -q -N -S $socket_dir/ssh-proxy-control -M $SSH_PROXY_HOST
|
||||
}
|
||||
|
||||
# Stop an open proxy connection
|
||||
function stop-proxy() {
|
||||
if [ -z "$SSH_PROXY_HOST" ]; then
|
||||
echo "SSH_PROXY_HOST is not set or empty"
|
||||
|
||||
return
|
||||
fi
|
||||
|
||||
local socket_dir=$(get-temporary-directory)
|
||||
|
||||
# Check if the last character of socket_dir is a directory separator character
|
||||
if [[ $socket_dir[-1] == / ]]; then
|
||||
# If yes, remove the directory separator character
|
||||
socket_dir="${socket_dir%?}"
|
||||
fi
|
||||
|
||||
echo "Closing proxy connection"
|
||||
|
||||
# See the comments in `start-proxy()` for more info
|
||||
ssh -S $socket_dir/ssh-proxy-control -O exit $SSH_PROXY_HOST
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue