From 9e41d03b7184e2d3ec36333627cb6d21c2cb5738 Mon Sep 17 00:00:00 2001 From: Daniil Firsov Date: Tue, 8 Nov 2022 15:02:19 +0300 Subject: [PATCH] dnk: tune shm_actions_script.sh --- shm_actions_script.sh | 49 ++++++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/shm_actions_script.sh b/shm_actions_script.sh index 0c5399c..8cd88cc 100755 --- a/shm_actions_script.sh +++ b/shm_actions_script.sh @@ -1,7 +1,19 @@ -#!/bin/bash -e +#!/bin/bash + +set -e EVENT="{{ event_name }}" WG_MANAGER="/etc/wireguard/wg-manager.sh" +SESSION_ID="{{ user.gen_session.id }}" + +# We need the --fail-with-body option for curl. +# It has been added since curl 7.76.0, but almost all Linux distributions do not support it yet. +# If your distribution has an older version of curl, you can use it (just comment CURL_REPO) +CURL_REPO="https://github.com/moparisthebest/static-curl/releases/download/v7.86.0/curl-amd64" +CURL="/opt/curl/curl-amd64" +#CURL="curl" + +echo "EVENT=$EVENT" case $EVENT in INIT) @@ -16,30 +28,43 @@ case $EVENT in wireguard \ wireguard-tools \ qrencode \ - curl + wget + + if [[ $CURL_REPO && ! -f $CURL ]]; then + mkdir -p /opt/curl + cd /opt/curl + wget $CURL_REPO + chmod 755 $CURL + fi + cd /etc/wireguard - curl -s https://danuk.github.io/wg-manager/wg-manager.sh > $WG_MANAGER + $CURL -s --fail-with-body https://danuk.github.io/wg-manager/wg-manager.sh > $WG_MANAGER chmod 700 $WG_MANAGER $WG_MANAGER -i -s $SERVER_HOST ;; CREATE) - SESSION_ID="{{ user.gen_session.id }}" USER_CFG=$($WG_MANAGER -c "{{ us.id }}" -p) - if [ $? -ne 0]; then - echo "ERROR: can't create user" - exit 2 - fi - - curl -s -XPUT \ + $CURL -s --fail-with-body -XPUT \ -H "session-id: $SESSION_ID" \ -H "Content-Type: text/plain" \ {{ config.api.url }}/shm/v1/storage/manage/vpn \ - --data-binary $USER_CFG + --data-binary @"/etc/wireguard/keys/{{ us.id }}/{{ us.id }}.conf" + echo "done" + ;; + ACTIVATE) + $WG_MANAGER -u "{{ us.id }}" -U + echo "done" + ;; + BLOCK) + $WG_MANAGER -u "{{ us.id }}" -L echo "done" ;; REMOVE) - $WG_MANAGER -c "{{ us.id }}" -d + $WG_MANAGER -u "{{ us.id }}" -d + $CURL -s --fail-with-body -XDELETE \ + -H "session-id: $SESSION_ID" \ + {{ config.api.url }}/shm/v1/storage/manage/vpn echo "done" ;; *)