Linux、nordvpn 和睡眠/恢复

Linux、nordvpn 和睡眠/恢复

我真的很想北VPN为了在睡眠/恢复事件中幸存下来,但是当然,如​​果远程端听不到我的笔记本电脑的声音,那么它就会断开连接。公平地说,我很乐意在睡觉前断开连接并在恢复时重新连接,只要我可以自动化它 - 我有通常的事情/usr/lib/systemd/system-sleep/sleep-stuff(以下)。

但这并不完全有效。恢复时,运行“nordvpn connect”,如果立即观察,路由表看起来不错(即从睡眠/恢复脚本中:

Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         10.8.1.1        128.0.0.0       UG        0 0          0 tun0
0.0.0.0         192.168.0.1     0.0.0.0         UG        0 0          0 wlp3s0
10.8.1.0        0.0.0.0         255.255.255.0   U         0 0          0 tun0
103.137.12.219  192.168.0.1     255.255.255.255 UGH       0 0          0 wlp3s0
128.0.0.0       10.8.1.1        128.0.0.0       UG        0 0          0 tun0
172.16.2.0      0.0.0.0         255.255.255.0   U         0 0          0 vmnet8
172.16.71.0     0.0.0.0         255.255.255.0   U         0 0          0 vmnet1
192.168.0.0     0.0.0.0         255.255.255.0   U         0 0          0 wlp3s0

不幸的是,当我登录时,路由表已被“某些东西”切换回非 VPN 模式,我不知道是什么,可能是 NetworkManager:

$ netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         192.168.0.1     0.0.0.0         UG        0 0          0 wlp3s0
172.16.2.0      0.0.0.0         255.255.255.0   U         0 0          0 vmnet8
172.16.71.0     0.0.0.0         255.255.255.0   U         0 0          0 vmnet1
192.168.0.0     0.0.0.0         255.255.255.0   U         0 0          0 wlp3s0

NordVPN 状态报告其正在运行:

$ nordvpn status
Status: Connected
Current server: au492.nordvpn.com
Country: Australia
City: Brisbane
Your new IP: 144.48.39.91
Current technology: OpenVPN
Current protocol: UDP
Transfer: 7.89 MiB received, 6.00 MiB sent
Uptime: 24 minutes 52 seconds

...但当然我必须进行额外的断开/连接,这很俗气。

有任何想法吗?

这是脚本(放入 /usr/lib/systemd/system-sleep/sleep-stuff 和 chmod +x 中):

#!/bin/sh
if [ "${1}" == "pre" ]; then
    # before suspend
    pgrep nordvpnd &> /dev/null && nordvpn status |grep -q 'Status: Connected' && {
        # This will run as root, so root needs to have been initialised by
        # nordvpn login at some time in the past.
        /bin/nordvpn disconnect
    }
elif [ "${1}" == "post" ]; then
    # after resume
    pgrep nordvpnd &> /dev/null && {
        # This will run as root, so root needs to have been initialised by
        # nordvpn login at some time in the past.
        (
            sleep 5 # I've tried sleep from 0 to 5
            /bin/nordvpn connect
            netstat -rn # routing table looks OK now but it gets zapped by something else later!!
        ) &
    }
fi

这是在 Fedora-31 上。

答案1

经过一番思考后,我得出的结论是,不可能使用脚本重新启动nordvpn/usr/lib/systemd/系统睡眠/

我已经满足于使用该脚本在睡眠时停止nordvpn,并且我必须记住在恢复时手动重新启动它。正如我正在使用的摇摆(1)、热键可以节省一些打字时间。

这是修改后的脚本:

#!/bin/sh
if [ "${1}" == "pre" ]; then
    # before suspend
    pgrep nordvpnd &> /dev/null && nordvpn status |grep -q 'Status: Connected' && {
        # This will run as root, so root needs to have been initialised by
        # 'nordvpn login' at some time in the past.
        nordvpn disconnect
    }
elif [ "${1}" == "post" ]; then
    # after resume
    :
fi

相关内容