elvish_config/aliases/vpn.elv

134 lines
2.4 KiB
Plaintext
Raw Normal View History

use re
use str
use framagit.org/benjamin.vaudour/elv-lib/mods/list
var vpnType = [
&wireguard=$nil
&vpn=$nil
]
var shortcut = [
&ca=ca.kaosx.ovh
&fr=fr.kaosx.ovh
&lu=luence-wg
&lu2=luence-ovpn
2022-09-02 13:53:03 +00:00
&cli=luence-client
&nl=proton_nl5
&jp=proton_jp2
&us=proton_us3
]
var next = [
&ca.kaosx.ovh=fr.kaosx.ovh
&fr.kaosx.ovh=luence-wg
&luence-wg=luence-client
&luence-ovpn=luence-client
# &luence-client=proton_nl5
# &proton_nl5=proton_us3
# &proton_us3=proton_jp2
]
fn -list {|&active=$false|
var @argv = connection show
if $active {
set @argv = $@argv --active
}
nmcli $@argv | re:awk {|_ @name uuid tpe dev|
if (has-key $vpnType $tpe) {
str:join ' ' $name
}
}
}
fn -up {|name|
nmcli connection up $name
}
fn -down {|name|
nmcli connection down $name
}
fn -active {||
-list &active=$true
}
fn -is-active {|name|
-active | list:contains $name
}
fn -next {||
var @n = (-active | list:first {|e| has-key $next $e })
if (== 1 (count $n)) {
put $next[$n[0]]
} else {
put ca.kaosx.ovh
}
}
fn -vpn-stop {||
var @a = (-active)
if (== (count $a) 0) {
echo 'Aucune connexion active'
} else {
-active | each $-down~
}
}
fn -vpn-start {|name|
if (has-key $shortcut $name) {
set name = $shortcut[$name]
}
if (not (-list | list:contains $name)) {
printf "Le VPN “%s” nèxiste pas\n" $name
} else {
-active | each $-down~
-up $name
}
}
fn -vpn-list {||
var @act = (-active)
-list | each {|n|
if (list:contains $n $act) {
echo (styled $n green)
} else {
echo $n
}
}
}
fn -vpn-help {||
echo 'vpn: Active ou désactive les connections VPN'
echo 'Usage:'
echo ' help Affiche cette aide'
echo ' list Liste les VPN disponibles'
echo ' stop Arrête toutes les connexions VPN actives'
echo ' <vpn> Active la connexion VPN donnée et désactive les autres'
echo ' (sans argument) Active la prochaine connexion VPN et désactive les autres'
}
fn -vpn {||
-vpn-start (-next)
}
edit:add-var vpn~ {|@argv|
var c = (count $argv)
if (== $c 0) {
-vpn
} elif (!= $c 1) {
-vpn-help
fail 'Arguments invalides'
} else {
var e = $argv[0]
if (eq $e help) {
-vpn-help
} elif (eq $e list) {
-vpn-list
} elif (eq $e stop) {
-vpn-stop
} else {
-vpn-start $e
}
}
}