GithubHelp home page GithubHelp logo

Comments (1)

intika avatar intika commented on August 24, 2024

Problem seems to be solved on the current release as of 11/09/2019...

Wireguard is stateless, the VPN connection is meant to be kept when after sleep/hibernate but because network manager is disconnecting on sleep/hibernate we loose the stateless feature, i did make a special setup to get back this feature, this setup also work with other VPN system like OpenVPN, for info and if any one need this solution here is what i did:

Restoring VPN connection after sleep/hibernate (if it was used)

Features:

  • Display current ip
  • Warn on wan ip change
  • Reconnect VPN if it was used before sleep

Setup:

  1. First under KDE i use a the widget "Command Output" and setup the widget to run "sh Ip.Vpn.Watcher.sh" every 15 sec (this watch for ip changes notify it, display current ip, and save current used network to /tmp/used-conn)
  2. Systemd is configured to run a script after sleep/hibernate: suspend-vpn is added to /usr/lib/systemd/system-sleep/
  3. suspend-vpn trigger Wait.Network.And.Restore.VPN.sh

That's it :)

**Ip.Vpn.Watcher.sh**
#!/bin/bash

wget -q http://internet.adress.that.return.myip.in.text/ip.php -O /tmp/ip --force-clobber

ip=$( cat /tmp/ip )
wantedip=$( cat /tmp/ip-old )

if [[ $ip = $wantedip ]]
then
    echo $ip
    nmcli --fields name connection show --active | tail -n +2 | head -n 1 > /tmp/used-conn
else
    if [[ $ip != "" ]]
    then
        echo $ip
        zenity --warning --title="VPN Watcher" --width=250 --text="\nWARNING : Wan IP Changed !"
        nmcli --fields name connection show --active | tail -n +2 | head -n 1 > /tmp/used-conn
        cp -f /tmp/ip /tmp/ip-old
    else
        echo $ip
        zenity --warning --title="VPN Watcher" --width=250 --text="\nWARNING : Wan Connection Gone !"
        cp -f /tmp/ip /tmp/ip-old
    fi
fi
**suspend-vpn**
#!/bin/bash

# $1 values ('pre' or 'post')
# $2 values ('suspend', 'hibernate', or 'hybrid-sleep')
# case "$1/$2" in

case $1 in
    pre)
        # Save connection state
        # Too late for this network is already gone... 
        # Implemented via check ip (plasmoid)
        # nmcli --fields name connection show --active | tail -n +2 | head -n 1 > /tmp/used-conn
    ;;
    post)
        # Restore connection 
        su username -c "export DISPLAY=:0; export XDG_RUNTIME_DIR='/run/user/2000'; /scripts/location/Wait.Network.And.Restore.VPN.sh &"
    ;;
esac
**Wait.Network.And.Restore.VPN.sh**
#!/bin/bash

timer=1
conn=$( cat /tmp/used-conn )

printf "%s" "Waiting For Online State ..."

while ! ping -c 1 -n -w 1 8.8.8.8 &> /dev/null
do
    printf "%c" "."
    sleep 1
    let "timer++"
    
    if [[ ( $timer -ge 300 ) ]] ; then
        printf "\n%s\n"  "Script Timeout"
        exit
    fi
    
done

printf "\n%s\n"  "Network Is Online"

if [[ $conn == VPNCONNECTIONAME* ]] || [[ $conn == VPNCONNECTIONAME2* ]] 
then
    # Need to wait network to be up 
    sleep 5
    nmcli connection up $conn
fi

from network-manager-wireguard.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.