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

81 lines
1.6 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 ""
2025-03-09 01:33:34 -08:00
return $"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-08-26 16:50:49 -07:00
def te [exit_node: string = ""] {
2025-02-20 14:55:55 -08:00
if ($exit_node | is-empty) and (ps | find deluge | is-not-empty) {
2025-05-07 17:17:37 -07:00
print "stop summoning first!"
return false
2025-02-20 14:55:55 -08:00
}
2025-05-07 17:17:37 -07:00
tailscale set --exit-node $exit_node
2025-02-20 14:55:55 -08:00
if ($exit_node | is-not-empty) {
2025-06-04 22:16:53 -07:00
check-mullvad
2025-03-09 01:33:34 -08:00
} else {
2025-05-07 17:17:37 -07:00
print "exit node set"
return true
2025-02-20 14:55:55 -08:00
}
}
2025-03-06 00:20:30 -08:00
# list all mullvad exit nodes
2025-08-26 16:50:49 -07:00
def tx [] {
2025-03-06 00:20:30 -08:00
tailscale exit-node list
2025-03-06 17:44:40 -08:00
| lines
| drop 4
2025-03-06 00:20:30 -08:00
| skip 1
2025-03-06 17:44:40 -08:00
| to text
| detect columns --guess
2025-03-06 00:20:30 -08:00
| where HOSTNAME =~ mullvad
| reject STATUS
}
# sort mullvad exit nodes by fastest ping
2025-02-20 14:55:55 -08:00
def tsp [] {
2025-08-26 16:50:49 -07:00
tx
2025-03-06 00:20:30 -08:00
| 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-08-26 16:50:49 -07:00
def tr [] {
tx
2025-06-04 22:16:53 -07:00
| get (random int 0..($in | length))
2025-08-26 16:50:49 -07:00
| te $in.IP
2025-06-04 22:16:53 -07:00
| return $in
2025-02-20 14:55:55 -08:00
}
2025-03-06 00:20:30 -08:00
2025-08-26 16:50:49 -07:00
alias t = tailscale
alias ts = tailscale status
alias tu = tailscale up
alias td = tailscale down
alias ta = tailscale exit-node suggest