133 lines
2.3 KiB
Plaintext
133 lines
2.3 KiB
Plaintext
use str
|
||
use framagit.org/benjamin.vaudour/elv-lib/mods/list
|
||
|
||
var vpnType = [
|
||
&wireguard=$nil
|
||
&vpn=$nil
|
||
]
|
||
|
||
var shortcut = [
|
||
&ca=ca.kaosx.cf
|
||
&fr=fr.kaosx.cf
|
||
&lu=luence-wg
|
||
&lu2=luence-ovpn
|
||
&cli=lunce-client
|
||
&nl=proton-nl5
|
||
&jp=proton-jp2
|
||
&us=proton-us3
|
||
]
|
||
|
||
var next = [
|
||
&ca.kaosx.cf=fr.kaosx.cf
|
||
&fr.kaosx.cf=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 | eawk {|_ @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.cf
|
||
}
|
||
}
|
||
|
||
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
|
||
}
|
||
}
|
||
}
|