Last active
August 9, 2022 06:58
-
-
Save samson4649/1a66188518135e36483d26c9e1cfce52 to your computer and use it in GitHub Desktop.
Tailscale wrapper to quickly change exit node configuration
This file contains 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
#!/bin/bash | |
# colours | |
GREEN='\033[0;32m' | |
NC='\033[0m' | |
CONFIG_PATH=~/.config/.tsen | |
function _usage(){ | |
echo -e "\nUsage: ${0} [--exit-node <tailscale_node_name>] [--clear | --none]\n" | |
} | |
function _critical(){ | |
echo "[CRIT] $@" | |
} | |
function _list(){ | |
echo -e " Nodes\n-------" | |
anode=$(tailscale status | grep '; exit node;' | awk '{print $2}') | |
for i in $(tailscale status | grep 'exit node;' | awk '{print $2}'); do | |
if [ "${i}" = "${anode}" ]; then | |
echo -e "${GREEN}*${i}${NC}" | |
else | |
echo "${i}" | |
fi | |
done | |
} | |
function _last(){ | |
# $0 --exit-node $(head -n 1 ${CONFIG_PATH} | cut -d' ' -f1) | |
head -n 1 ${CONFIG_PATH} | cut -d' ' -f1 | |
} | |
POSITIONAL_ARGS=() | |
while [[ $# -gt 0 ]]; do | |
case $1 in | |
ls|--list) | |
_list | |
exit 80 | |
;; | |
--none|--clear) | |
EXIT_NODE_CLEAR="true" | |
shift | |
;; | |
--exit-node|--node) | |
EXIT_NODE="${2}" | |
shift | |
shift | |
;; | |
--last) | |
EXIT_NODE="$(_last)" | |
shift | |
;; | |
*) | |
POSITIONAL_ARGS+=("$1") # save positional arg | |
shift # past argument | |
;; | |
esac | |
done | |
# echo "pos args: ${#POSITIONAL_ARGS[@]}" | |
if [[ -z "${EXIT_NODE}" ]] && [[ -z "${EXIT_NODE_CLEAR}" ]]; then | |
_critical "Need to provide something to do..." | |
_usage && exit 99 | |
fi | |
stdout=$(tailscale up --exit-node=${EXIT_NODE} 2>&1) | |
if echo "${stdout}" | grep "invalid value" &>/dev/null; then | |
_critical "Exit node doesnt exist: ${EXIT_NODE}. Exiting..." | |
exit 98 | |
fi | |
cmd=$( echo "${stdout}" | grep -P '^\ttailscale up ' | sed -E 's/^\t(.*)$/\1/g') | |
cmd="$(echo ${cmd} | sed -E 's/--exit-node-allow-lan-access=(false|true)//g')" | |
# set lan access if enabling exit node | |
if [[ ! -z "${EXIT_NODE}" ]]; then | |
cmd="${cmd} --exit-node-allow-lan-access=true" | |
elif [[ ! -z "${EXIT_NODE_CLEAR}" ]]; then | |
cmd="${cmd} --exit-node-allow-lan-access=false" | |
fi | |
echo "CMD: ${cmd}" | |
echo "Exit node: ${EXIT_NODE}" | |
$cmd | |
# save last if successful | |
if [ "$?" -eq "0" ] && [[ ! -z "${EXIT_NODE}" ]]; then | |
echo "${EXIT_NODE}" > ${CONFIG_PATH} | |
fi | |
echo "[+] Complete" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment