diff options
Diffstat (limited to '.config')
| -rw-r--r-- | .config/nushell/vpn.nu | 61 |
1 files changed, 42 insertions, 19 deletions
diff --git a/.config/nushell/vpn.nu b/.config/nushell/vpn.nu index 63ab2e0..cb72845 100644 --- a/.config/nushell/vpn.nu +++ b/.config/nushell/vpn.nu @@ -1,16 +1,24 @@ -# Connect to one of the Mullvad VPN Servers via Wireguard and optionally filter by name +use post-vpn-connect.nu +use pre-vpn-disconnect.nu + +# Connect to one of the Mullvad VPN Servers via Wireguard and optionally filter by name. export def connect [ --filter: string # Filter by VPN server config name, e.g. 'de' to filter German servers ] { try { - let current_status = (status) - if ($current_status | is-not-empty) { - let connected_server_name = ($current_status | get name | get 0) - print $"You are already connected to ($connected_server_name)." + let active_interface = (active-interface) + if ($active_interface | is-not-empty) { + print $"You are already connected to ($active_interface)." return } + let servers = (sudo ls /etc/wireguard/ | lines | str replace '.conf' '') | where { |name| $name =~ ($filter | default '') } - sudo wg-quick up ($servers | select (random int 0..($servers | length)) | get 0) + sudo wg-quick up ($servers | select (random int 0..(($servers | length) - 1)) | get 0) + + print "Executing Post-VPN Connect Tasks..." + post-vpn-connect + print "Finished Post-VPN Connect Tasks..." + } catch { |err| print $"Failed to connect to VPN...\n\nError: ($err.msg)" @@ -18,34 +26,49 @@ export def connect [ } # Check if you are connected to a Mullvad VPN server. -# Return: -# If connected: table<name: string, ip: string> -# If not: nothing -export def status [] { +export def test-connection [] { try { let response = (http get https://am.i.mullvad.net/connected --max-time 5sec) - let result = ($response | parse "You are connected to Mullvad (server {name}). Your IP address is {ip}") - if ($result | is-not-empty) { - return $result - } + return $response } catch { |err| print $"Failed to get response from status...\n\nError: ($err.msg)" } } -# Disconnect the active VPN server connection +# Check if a Wireguard interface is up and running. +# If yes: string (name of interface) +# If not: nothing +export def active-interface [] { + try { + let wg_show_output = (sudo wg show | lines) + if ($wg_show_output | is-not-empty) { + $wg_show_output | get 0 | parse "interface: {interface}" | get 0 | return $in.interface + } + } catch { + |err| + print $"Failed to check active Wireguard interface...\n\nError: ($err.msg)" + } +} + +# Disconnect the active VPN server connection. export def disconnect [] { try { - let current_status = (status) - if ($current_status | is-empty) { + let active_interface = (active-interface) + if ($active_interface | is-empty) { print "You are not connected to any Mullvad VPN server." return } - let current_server_name = ($current_status | get name | get 0) - sudo wg-quick down $current_server_name + + print "Executing Pre-VPN Disconnect Tasks..." + pre-vpn-disconnect + print "Finished Pre-VPN Disconnect Tasks..." + + sudo wg-quick down $active_interface } catch { |err| print $"Failed to disconnect active VPN connection...\n\n Error: ($err.msg)" } } + + |
