Skip to content

Instantly share code, notes, and snippets.

@alectrocute
Last active January 9, 2025 12:19
Show Gist options
  • Select an option

  • Save alectrocute/e5ee3c7d533b32a07623e6d59abf9616 to your computer and use it in GitHub Desktop.

Select an option

Save alectrocute/e5ee3c7d533b32a07623e6d59abf9616 to your computer and use it in GitHub Desktop.

Revisions

  1. alectrocute renamed this gist Oct 23, 2024. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. alectrocute revised this gist Oct 23, 2024. 1 changed file with 9 additions and 2 deletions.
    11 changes: 9 additions & 2 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ log() {
    echo "wg-watchdog $(date '+%Y-%m-%d %H:%M:%S') $1"
    }

    log "Attempting to turn off and on WireGuard client..."
    log "Attempting to turn off and on WireGuard client (if needed)..."

    # make sure dependencies are installed
    if opkg status jq | grep -q 'Installed-Time'; then
    @@ -37,6 +37,8 @@ else
    log "VPN is not working correctly, IP address is $CURRENT_IP which doesn't match $WG_PEER_IP. Restarting WireGuard..."
    fi

    log "Attempting to authenticate with GL.iNet web application..."

    CHALLENGE_RESPONSE=$(curl -X POST \
    -H 'Content-Type: application/json' \
    -d '{"jsonrpc":"2.0","method":"challenge","params": {"username": "'$USERNAME'"},"id": 0}' \
    @@ -59,6 +61,9 @@ SID=$(curl -X POST \
    jq '.result.sid' |
    tr -d '"')

    log "Finished authenticating with GL.iNet web application!"
    log "Attempting to turn off WireGuard client..."

    WIREGUARD_OFF_RESPONSE=$(curl -X POST \
    -H 'Content-Type: application/json' \
    -d '{"jsonrpc":"2.0","method":"call","params":["'$SID'","wg-client","stop", {}],"id": 0}' \
    @@ -67,6 +72,8 @@ WIREGUARD_OFF_RESPONSE=$(curl -X POST \

    log $WIREGUARD_OFF_RESPONSE

    log "Attempting to turn on WireGuard client..."

    WIREGUARD_ON_RESPONSE=$(curl -X POST \
    -H 'Content-Type: application/json' \
    -d '{"jsonrpc":"2.0","method":"call","params":["'$SID'","wg-client","start",{"group_id":'$WG_GROUP_ID',"peer_id":'$WG_PEER_ID'}],"id":0}' \
    @@ -75,4 +82,4 @@ WIREGUARD_ON_RESPONSE=$(curl -X POST \

    log $WIREGUARD_ON_RESPONSE

    log "Finished attempting to turn off and on WireGuard client!"
    log "Complete!"
  3. alectrocute revised this gist Oct 23, 2024. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -30,9 +30,11 @@ IPIFY_RESPONSE=$(curl -s 'https://api.ipify.org?format=json')
    CURRENT_IP=$(jq -n "$IPIFY_RESPONSE" | jq '.ip' | tr -d '"')

    if [ "$CURRENT_IP" == "$WG_PEER_IP" ]; then
    log "VPN is working correctly, IP address is $CURRENT_IP"
    log "VPN is working correctly, IP address is $CURRENT_IP!"
    log "Exiting watchdog script..."
    exit 1
    else
    log "VPN is not working correctly, IP address is $CURRENT_IP which doesn't match $WG_PEER_IP. Restarting WireGuard..."
    fi

    CHALLENGE_RESPONSE=$(curl -X POST \
  4. alectrocute created this gist Oct 23, 2024.
    76 changes: 76 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,76 @@
    # cron example (wg-watchdog.sh being this file):
    # 58 0-7 * * * /root/wg-watchdog.sh

    # start config
    USERNAME='root'
    PASSWORD='mypassword'
    HOST='192.168.8.1'

    WG_PEER_IP='123.456.5.4'
    WG_GROUP_ID=4910 # find in browser devtools
    WG_PEER_ID=6132 # find in browser devtools
    # end config

    log() {
    echo "wg-watchdog $(date '+%Y-%m-%d %H:%M:%S') $1"
    }

    log "Attempting to turn off and on WireGuard client..."

    # make sure dependencies are installed
    if opkg status jq | grep -q 'Installed-Time'; then
    continue
    else
    log "Installing jq package..."
    opkg install jq
    log "Finished installing jq package!"
    fi

    IPIFY_RESPONSE=$(curl -s 'https://api.ipify.org?format=json')
    CURRENT_IP=$(jq -n "$IPIFY_RESPONSE" | jq '.ip' | tr -d '"')

    if [ "$CURRENT_IP" == "$WG_PEER_IP" ]; then
    log "VPN is working correctly, IP address is $CURRENT_IP"
    log "Exiting watchdog script..."
    exit 1
    fi

    CHALLENGE_RESPONSE=$(curl -X POST \
    -H 'Content-Type: application/json' \
    -d '{"jsonrpc":"2.0","method":"challenge","params": {"username": "'$USERNAME'"},"id": 0}' \
    http://$HOST/rpc \
    -s)

    ALG=$(jq -n "$CHALLENGE_RESPONSE" | jq '.result.alg' | tr -d '"')
    SALT=$(jq -n "$CHALLENGE_RESPONSE" | jq '.result.salt' | tr -d '"')
    NONCE=$(jq -n "$CHALLENGE_RESPONSE" | jq '.result.nonce' | tr -d '"')

    CIPHER_PASSWORD=$(openssl passwd -1 -salt "$SALT" "$PASSWORD")

    HASH=$(echo -n "$USERNAME:$CIPHER_PASSWORD:$NONCE" | md5sum | cut -d' ' -f1)

    SID=$(curl -X POST \
    -H 'Content-Type: application/json' \
    -d '{"jsonrpc":"2.0","method":"login","params": {"username": "'$USERNAME'", "hash": "'$HASH'"},"id": 0}' \
    http://$HOST/rpc \
    -s |
    jq '.result.sid' |
    tr -d '"')

    WIREGUARD_OFF_RESPONSE=$(curl -X POST \
    -H 'Content-Type: application/json' \
    -d '{"jsonrpc":"2.0","method":"call","params":["'$SID'","wg-client","stop", {}],"id": 0}' \
    http://$HOST/rpc \
    -s)

    log $WIREGUARD_OFF_RESPONSE

    WIREGUARD_ON_RESPONSE=$(curl -X POST \
    -H 'Content-Type: application/json' \
    -d '{"jsonrpc":"2.0","method":"call","params":["'$SID'","wg-client","start",{"group_id":'$WG_GROUP_ID',"peer_id":'$WG_PEER_ID'}],"id":0}' \
    http://$HOST/rpc \
    -s)

    log $WIREGUARD_ON_RESPONSE

    log "Finished attempting to turn off and on WireGuard client!"