aboutsummaryrefslogtreecommitdiff
path: root/scripts/util/vpn.sh
blob: b994e5a1cf7f6a3bb69356ade649dccb6c03d7e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/bin/sh

vpn_stat()
{
	stat=$(mullvad status)

	if  echo "$stat" | grep -q "Disconnected"; then
		echo 0
	elif echo "$stat" | grep -q "Connected"; then
		echo 1
	elif echo "$stat" | grep -q "Connecting"; then
		echo "Connecting"
	else
		echo "Unkown Status"
	fi
}

vpn_stat_verbose()
{
	mullvad status
}

rand_conn()
{
relays=$(mullvad relay list | awk 'NR>1 {gsub(/[()]/, "", $2); print $1 "-" $2}' | grep -v '^-$')

    if [ -z "$relays" ]; then
        echo "No valid relays found."
        return 1
    fi

    random_relay=$(echo "$relays" | shuf -n 1)

    country=$(echo "$random_relay" | cut -d'-' -f1)
    city=$(echo "$random_relay" | cut -d'-' -f2)

    mullvad relay set location $country $city
    mullvad connect
    echo "Connected to $country $city"
}



if [ "$#" -eq 0 ] || [ "$1" = "-s" ]; then
    if [ "$#" -eq 2 ] && [ "$2" = "-v" ]; then
        vpn_stat_verbose
    else
        vpn_stat
    fi
elif [ "$1" = "-c" ]; then
    if [ "$2" = "--rand" ]; then
        rand_conn
    elif [ "$#" -eq 3 ]; then
        country=$2
        city=$3
        mullvad relay set location $country $city
        mullvad connect
        echo "Connected to $country $city"
    else
        echo "Invalid usage. Use ./vpn.sh -c [country] [city] or ./vpn.sh -c --rand"
        exit 1
    fi
fi