This commit is contained in:
parent
7e5d880156
commit
d93f63cf82
40 changed files with 448 additions and 649 deletions
|
|
@ -1,92 +1,49 @@
|
|||
#!/bin/bash
|
||||
# shellcheck disable=SC2034
|
||||
|
||||
die() {
|
||||
[ -n "$oc_pid" ] && kill -s TERM "$oc_pid"
|
||||
echo "exitting..."
|
||||
rm -f $PIPE
|
||||
exit 0
|
||||
}
|
||||
|
||||
trap 'die' SIGTERM SIGQUIT SIGINT
|
||||
|
||||
PIPE="/var/run/vpnd.sock"
|
||||
oc_pid=
|
||||
up_name=
|
||||
|
||||
[ -p $PIPE ] && exit 1
|
||||
[ "$(id -u)" != "0" ] && exit 1
|
||||
|
||||
trap 'die' SIGTERM SIGQUIT SIGINT
|
||||
|
||||
declare -a CONFIGS
|
||||
for config in /etc/openconnect/config_*; do
|
||||
config="$(basename "$config")"
|
||||
CONFIGS+=("${config#config_}")
|
||||
for config in /etc/amnezia/amneziawg/*; do
|
||||
config="$(basename "$config")"
|
||||
CONFIGS+=("${config%.conf}")
|
||||
done
|
||||
|
||||
COMMANDS=("up" "down" "status")
|
||||
COMMANDS=("up" "down")
|
||||
|
||||
is_really_up() {
|
||||
[ -n "$oc_pid" ] && if ! kill -0 "$oc_pid"; then
|
||||
oc_pid=
|
||||
up_name=
|
||||
fi
|
||||
die() {
|
||||
rm $PIPE
|
||||
exit 0
|
||||
}
|
||||
|
||||
in_arr() {
|
||||
declare -n arr="$2"
|
||||
declare -n arr="$2"
|
||||
|
||||
for value in "${arr[@]}"; do
|
||||
[ "$value" = "$1" ] && return 0
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
down() {
|
||||
is_really_up
|
||||
[ -z "$oc_pid" ] && return
|
||||
kill -s TERM "$oc_pid"
|
||||
wait "$oc_pid"
|
||||
oc_pid=
|
||||
up_name=
|
||||
}
|
||||
|
||||
up() {
|
||||
is_really_up
|
||||
[ -n "$oc_pid" ] && down
|
||||
openconnect --config "/etc/openconnect/config_$1" &
|
||||
oc_pid="$!"
|
||||
up_name="$1"
|
||||
}
|
||||
|
||||
status() {
|
||||
is_really_up
|
||||
echo "$up_name" > $PIPE
|
||||
for value in "${arr[@]}"; do
|
||||
[ "$value" = "$1" ] && return 0
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
main() {
|
||||
mkfifo $PIPE -m666
|
||||
mkfifo $PIPE -m666
|
||||
|
||||
while :; do
|
||||
read -r cmd ifname < $PIPE
|
||||
if ! in_arr "$cmd" "COMMANDS"; then
|
||||
echo "ERROR: Invalid command $cmd" > $PIPE
|
||||
else
|
||||
case "$cmd" in
|
||||
"up")
|
||||
if ! in_arr "$ifname" "CONFIGS"; then
|
||||
echo "ERROR: Invalid interface $ifname" > $PIPE
|
||||
else
|
||||
up "$ifname"
|
||||
echo "$ifname" > $PIPE
|
||||
fi
|
||||
;;
|
||||
"down") down; echo "down" > $PIPE;;
|
||||
"status") status;;
|
||||
esac
|
||||
while :; do
|
||||
read -r cmd ifname < $PIPE
|
||||
|
||||
fi
|
||||
done
|
||||
if ! in_arr "$ifname" "CONFIGS"; then
|
||||
echo "ERROR: Invalid interface $ifname" > $PIPE
|
||||
elif ! in_arr "$cmd" "COMMANDS"; then
|
||||
echo "ERROR: Invalid command $cmd" > $PIPE
|
||||
else
|
||||
awg-quick "$cmd" "$ifname" > $PIPE 2>&1
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
echo "Running..."
|
||||
main
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue