NOflake/users/tao/nushell/extras/tailscale.nu

76 lines
1.5 KiB
Text
Raw Normal View History

2025-03-06 00:20:30 -08:00
# see if others see us connecting from a mullvad exit node
2025-02-20 14:55:55 -08:00
def check-mullvad [] {
2025-03-06 00:20:30 -08:00
print -n "checking mullvad status"
mut check = false
mut j = null
while not $check {
print -n "."
$j = (http get https://am.i.mullvad.net/json)
$check = $j.mullvad_exit_ip
2025-02-20 14:55:55 -08:00
}
2025-03-06 00:20:30 -08:00
print ""
print $"connected to ($j.city), ($j.country)"
2025-02-20 14:55:55 -08:00
}
2025-03-06 00:20:30 -08:00
# switch to a specific exit node, or none
2025-02-20 14:55:55 -08:00
def tse [exit_node: string = ""] {
if ($exit_node | is-empty) and (ps | find deluge | is-not-empty) {
return "stop summoning first!"
} else {
tailscale set --exit-node $exit_node
}
if ($exit_node | is-not-empty) {
check-mullvad
}
return "exit node set"
}
2025-03-06 00:20:30 -08:00
# list all mullvad exit nodes
def tsx [] {
tailscale exit-node list
| detect columns --guess
| drop 3
| skip 1
| where HOSTNAME =~ mullvad
| reject STATUS
}
# sort mullvad exit nodes by fastest ping
2025-02-20 14:55:55 -08:00
def tsp [] {
2025-03-06 00:20:30 -08:00
tsx
| where COUNTRY == USA
| par-each {
insert ping {
$in.HOSTNAME
| str replace "mullvad.ts.net" "relays.mullvad.net"
| try {
print $"pinging ($in)"
ping -c5 -q $in
| lines
| last
| split row ' '
| get 3
| split row '/'
| get 1
| into float
}
2025-02-20 14:55:55 -08:00
}
2025-03-06 00:20:30 -08:00
}
| sort-by ping
}
# switch to a random mullvad exit node
2025-02-20 14:55:55 -08:00
def tsr [] {
2025-03-06 00:20:30 -08:00
tsx
| get (random int 0..($in | length))
| tse $in.IP
2025-02-20 14:55:55 -08:00
}
2025-03-06 00:20:30 -08:00
2025-02-20 14:55:55 -08:00
alias ts = tailscale
alias tss = tailscale status
alias tsu = tailscale up
alias tsd = tailscale down
alias tsa = tailscale exit-node suggest