代码之家  ›  专栏  ›  技术社区  ›  maddingl

linux上的VPN设置

  •  -1
  • maddingl  · 技术社区  · 7 年前

    (下面是我自己的实现,请随时评论或提出改进建议!)

    编辑:当我写这篇文章时,我不知道NordVPN确实引入了 command line tool for linux 最近。

    1 回复  |  直到 7 年前
        1
  •  0
  •   maddingl    7 年前

    我编写了一个小脚本,可以下载配置文件,重命名它们并启用自动身份验证。在中插入您的VPN登录凭据 generate authentification file 部分。

    #!/bin/bash
    # run as root!!!
    
    # install openvpn. I'm running arch, this might be different on your system.
    pacman -S openvpn
    
    # go to openvpn config folder
    cd /etc/openvpn
    
    # download config files, extract and clean up
    wget https://downloads.nordcdn.com/configs/archives/servers/ovpn.zip
    unzip ovpn.zip
    rm ovpn.zip
    
    # rename tcp config files and put them in /etc/openvpn/client
    cd ovpn_tcp
    for file in *; do mv "${file}" "${file/.nordvpn.com.tcp.ovpn/}tcp.conf"; done
    cp * ../client
    
    # rename udp config files and put them in /etc/openvpn/client
    cd ../ovpn_udp
    for file in *; do mv "${file}" "${file/.nordvpn.com.udp.ovpn/}udp.conf"; done
    cp * ../client
    
    # generate authentification file
    cd ../client
    printf "<your email>\n<your password>" > auth.txt
    
    # make all configs use authentification file
    find . -name '*.conf' -exec sed -i -e 's/auth-user-pass/auth-user-pass\ auth.txt/g' {} \;
    
    # clean up
    cd ..
    rm -r ovpn_tcp/
    rm -r ovpn_udp
    

    systemctl start openvpn-client@de415tcp.service
    

    systemctl stop openvpn-client@de415tcp.service
    

    $PATH . 传递国家代码(如 us de uk )作为命令行参数 start-vpn 如果你想选择一个特定的国家。它会自动选择 tcp 连接。你可以改成 udp 如果你愿意的话。

    #!/usr/bin/python
    import sys
    
    import requests
    import os
    import time
    
    # you don't necessarily need the following. It's for monitoring via i3blocks.
    
    def notify_i3blocks():
        os.system('pkill -RTMIN+12 i3blocks')
    
    
    def fork_and_continue_notifying_in_background():
        newpid = os.fork()
        if newpid == 0:  # if this is the child process
            for i in range(60):
                notify_i3blocks()
                time.sleep(1)
    
    
    if __name__ == '__main__':
    
        notify_i3blocks()
    
        # below is what you do need.
    
        suffix = ''
        if len(sys.argv) > 1:
            countries = requests.get('https://nordvpn.com/wp-admin/admin-ajax.php?action=servers_countries').json()
            for country in countries:
                if country["code"].lower() == sys.argv[1].lower():
                    suffix = '&filters={"country_id":' + str(country["id"]) + '}'
    
        result = requests.get('https://nordvpn.com/wp-admin/admin-ajax.php?action=servers_recommendations' + suffix)
        profile = result.json()[0]['subdomain'] + 'tcp'
    
        command = 'systemctl start openvpn-client@' + profile + '.service'
    
        os.system(command)
    
        # the following is for i3blocks again.        
    
        fork_and_continue_notifying_in_background()
    

    stop-vpn

    #!/bin/bash
    
    function service {
        systemctl |
        grep openvpn |
        grep running |
        head -n1 |
        awk '{print $1;}'
    }
    
    while [[ $(service) ]]; do
        systemctl stop $(service)
    done
    
    # notify i3blocks
    pkill -RTMIN+12 i3blocks
    

    为了方便起见,我的字典里有两个别名 ~/.bashrc

    alias start-vpn='sudo start-vpn'
    alias stop-vpn='sudo stop-vpn'
    

    i3blocks ,把这个放在你的口袋里

    [vpn]
    interval=once
    signal=12
    

    vpn ):

    #!/bin/bash
    
    function name {
        systemctl |
        grep openvpn |
        grep running |
        head -n1 |
        awk '{print $1;}' |
        cut -d @ -f 2 |
        cut -d . -f 1
    }
    
    starting=$(pgrep -f start-vpn) # this might not be the most accurate, but it works for me. Improvement suggestions are welcomed.
    
    if [[ $(name) ]]; then
        echo $(name)
        echo && echo "#00FF00"
    else
        if [[ ${starting} ]]; then
            echo starting vpn...
            echo && echo "#FFFF00"
        else
            echo no vpn
            echo && echo "#FF0000"
        fi
    fi
    

    为了在网络接口上升/下降时自动启动和停止vpn,请在 /etc/NetworkManager/dispatcher.d/10-openvpn enable start NetworkManager-dispatcher.service . 更多信息 here .

    在我的大学,我连接到EDUROAM,它不允许VPN。这就是为什么我把它排除在外。

    /etc/NetworkManager/dispatcher.d/10-openvpn

    #!/bin/bash
    
    case "$2" in
        up)
            if ! nmcli -t connection | grep eduroam | grep wlp3s0 ; then
                start-vpn
            fi
            ;;
        down)
            stop-vpn
            ;;
    esac
    

    我希望这能帮助其他想在linux上使用NordVPN的人。同样,请随时发表评论并提出改进建议。