Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/ MacOS show vpn name and tailscale exit node if active #279

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 69 additions & 5 deletions scripts/network_vpn.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,90 @@ export LC_ALL=en_US.UTF-8
current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source $current_dir/utils.sh



vpn_function() {
case $(uname -s) in
Linux)

verbose=$(get_tmux_option "@dracula-network-vpn-verbose" false)

#Show IP of tun0 if connected
vpn=$(ip -o -4 addr show dev tun0 | awk '{print $4}' | cut -d/ -f1)

which -s tailscale > /dev/null
tailscale_installed=$?

if [[ $vpn =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; then
echo $vpn
elif [ $tailscale_installed ]; then
# if tailscale is installed
#
# https://www.reddit.com/r/Tailscale/comments/18dirro/is_there_a_way_i_can_tell_which_exit_node_i_am/
node=$(tailscale status --peers --json | jq '.ExitNodeStatus')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm hesitant to rely on jq as a dependency.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

understandable. im planning to rework this entirely if i end up making it "production ready" and i will keep that in mind then 👍

if [[ -z $node ]] || [[ "$node" == 'null' ]]; then
# no tailscale exit node, no output, since trafic isnt actually rerouted
echo ""
else
exitnode=$(tailscale status | grep "; exit node" | awk '{print $2}')

if $verbose; then
vpn_label=$(get_tmux_option "@dracula-network-vpn-label" "󰌘 ")
echo "$vpn_label$exitnode"
else
vpn_label=$(get_tmux_option "@dracula-network-vpn-label" "Tailscale")
echo "$vpn_label"
fi
fi
else
echo "NO VPN"
echo ""
fi
;;

Darwin)
vpn=$(scutil --nc list | grep Connected)

if [ -z $vpn ]; then
verbose=$(get_tmux_option "@dracula-network-vpn-verbose" false)

vpn="$(scutil --nc list | sed "s/\*//g" | grep Connected)"

is_not_tailscale=$(echo "$vpn" | grep -v Tailscale)

which -s tailscale > /dev/null
tailscale_installed=$?

if [ -z "$vpn" ]; then
echo ""

elif [ $tailscale_installed ] && [ -z "$is_not_tailscale" ]; then
# if tailscale is installed and no other vpn is connected. this is because tailscale will
# always show as connected for some reason.
#
# https://www.reddit.com/r/Tailscale/comments/18dirro/is_there_a_way_i_can_tell_which_exit_node_i_am/
node=$(tailscale status --peers --json | jq '.ExitNodeStatus')
if [[ -z $node ]] || [[ "$node" == 'null' ]]; then
# no tailscale exit node, no output, since trafic isnt actually rerouted
echo ""
else
exitnode=$(tailscale status | grep "; exit node" | cut -w -f 2)

if $verbose; then
vpn_label=$(get_tmux_option "@dracula-network-vpn-label" "󰌘 ")
echo "$vpn_label$exitnode"
else
vpn_label=$(get_tmux_option "@dracula-network-vpn-label" "Tailscale")
echo "$vpn_label"
fi
fi

else
echo "VPN"
if $verbose; then
vpn_name=$(echo $is_not_tailscale | sed "s/.*\"\(.*\)\".*/\1/g")
vpn_label=$(get_tmux_option "@dracula-network-vpn-label" "󰌘 ")
echo "$vpn_label$vpn_name"
else
vpn_label=$(get_tmux_option "@dracula-network-vpn-label" "VPN")
echo "$vpn_label"
fi
fi
;;

Expand Down