跳至主要內容

如何在 OpenWRT 路由器上設定 OpenVPN

感謝 ulmwind 撰寫這些操作說明!

本教學將說明如何在 OpenWRT 路由器上設定 OpenVPN 通道。 

注意:如果您偏好透過路由器介面設定 OpenVPN,請參閱如何透過路由器介面在 OpenWRT 上設定 OpenVPN。


若要繼續,您需要一台搭載 OpenWRT 韌體的路由器,以及有效的 Surfshark 訂閱方案。您可以前往我們的定價頁面購買訂閱方案。

憑證與伺服器選擇

設定路由器前,您需要先取得手動設定所需的登入憑證,並選擇要連線的伺服器。
 

注意: 這些憑證並非您平常登入帳戶時使用的電子郵件地址和密碼。
 

  1. 前往 Surfshark 登入頁面 並登入。接著,按一下 VPN -> 手動設定。



  2. 選擇 手動設定。



  3. 選擇 OpenVPN 通訊協定。



  4. 進入該頁面後,請確認您位於憑證分頁,並按一下 產生憑證。 



    注意:請保持此分頁開啟,或儲存憑證,以便稍後使用。

 

選擇 Surfshark VPN 伺服器

  1. 前往 位置分頁,並找到您想要連線的伺服器。


     
  2. 按一下伺服器名稱右側的下載圖示。



  3. 按一下下載 UDP。 



    注意:在本範例中,我們將使用 UDP,但您也可以選擇 TCP。您可以參閱 TCP 與 UDP 之間的差異。

設定 OpenVPN 用戶端

  1. 在瀏覽器中開啟下列任一網站,以存取路由器:
    http://openwrt.org
    http://lede-project.org

    最初安裝 OpenWRT 韌體映像檔的路由器僅接受透過 telnet 通訊協定建立的連線。嘗試使用以下 IP 位址連線至 telnet:192.168.1.1,並使用指令「passwd」變更 root 密碼。

  2. 執行此指令後,路由器將會接受透過 SSH 的連線。預設情況下,韌體映像檔不包含 OpenVPN,因此您需要使用 opkg 安裝 OpenVPN 用戶端:
    opkg update
    opkg install openvpn-openssl

    您也可以安裝用於設定 OpenVPN 的 LuCI 元件,但此步驟並非必要:
    opkg install luci-app-openvpn

  3. 您也可以自行建置包含 OpenVPN 的韌體映像檔。這個版本將不需要安裝用戶端。請參閱 如何將 OpenWRT 路由器設定為 OpenVPN 用戶端。

    我們將依照該指南的步驟操作,並針對 Surfshark 進行適當調整。

  4. 安裝 OpenVPN 後,您可以輸入以下指令將其設定為在路由器開機時自動啟動:
    /etc/init.d/openvpn enable
  5. 現在您需要 Surfshark 伺服器設定檔(請參閱本文中的選擇 Surfshark 伺服器章節)。 在 Windows 上使用 PSCP 或 WinSCP 程式,或在 Linux 上使用 scp 指令,將您下載的檔案複製到路由器檔案系統的 /etc/openvpn/ 資料夾中。

    如果複製檔案時遇到問題,請強制使用 scp 通訊協定(也可以使用 sftp)。

  6. 在檔案內容中找到 auth-user-pass 所在的那一行,並在後面加上 "cred.txt",使該行變成 "auth-user-pass cred.txt"。

  7. 現在,您需要輸入憑證(請參閱本文的取得您的憑證章節)。

    在 /etc/openvpn/ 資料夾中建立名為 cred.txt 的檔案,第一行輸入 Surfshark 服務使用者名稱,第二行輸入 Surfshark 服務密碼:

    Surfshark 服務使用者名稱
    Surfshark 服務密碼

  8. 現在可以採用您偏好的 OpenVPN 設定檔。這可以透過兩種方式完成:

    1) 將檔案的副檔名從「ovpn」變更為「conf"。在這種情況下,OpenVPN 會根據副檔名自動找到該檔案。

    2) 使用 uci 在 /etc/config/openvpn 中指定檔案名稱:
    uci set openvpn.surfshark=openvpn
    uci set openvpn.surfshark.enabled='1'
    uci set openvpn.surfshark.config='/etc/openvpn/ae-dub.prod.surfshark.com_tcp.ovpn'
    uci commit openvpn

    檔案 /etc/config/openvpn 應包含以下附加字串:
    config openvpn 'surfshark'
            option enabled '1'   
            option config '/etc/openvpn/ae-dub.prod.surfshark.com_tcp.ovpn'

  9. 現在,建立新的網路介面。

    注意:有兩種方法可以執行此操作。我們不建議同時使用這兩種方法。

    uci set network.surfsharktun=interface
    uci set network.surfsharktun.proto='none'
    uci set network.surfsharktun.ifname='tun0'
    uci commit network


    /etc/config/network 檔案應包含以下附加字串:

    config interface 'surfsharktun'
            option proto 'none'  
            option ifname 'tun0'

  10. 如下所示建立新的防火牆區域,並新增從 LAN 到 VPN 的轉送規則:
    uci add firewall zone
    uci set firewall.@zone[-1].name='vpnfirewall'
    uci set firewall.@zone[-1].input='REJECT'
    uci set firewall.@zone[-1].output='ACCEPT'
    uci set firewall.@zone[-1].forward='REJECT'
    uci set firewall.@zone[-1].masq='1'
    uci set firewall.@zone[-1].mtu_fix='1'
    uci add_list firewall.@zone[-1].network='surfsharktun'
    uci add firewall forwarding
    uci set firewall.@forwarding[-1].src='lan'
    uci set firewall.@forwarding[-1].dest='vpnfirewall'
    uci commit firewall


    /etc/config/firewall 檔案應包含以下附加字串:

    config zone
            option name 'vpnfirewall'
            option input 'REJECT'
            option output 'ACCEPT'
            option forward 'REJECT'
            option masq '1'       
            option mtu_fix '1' 
            list network 'surfsharktun'      
    config forwarding       
            option src 'lan'    
            option dest 'vpnfirewall'

     

  11. 現在,您需要設定 DNS 伺服器。將路由器的 WAN 介面設定為使用 Surfshark DNS。您可以依照下方所示新增 Surfshark DNS:
    uci set network.wan.peerdns='0'
    uci del network.wan.dns
    uci add_list network.wan.dns='162.252.172.57'
    uci add_list network.wan.dns='149.154.159.92'
    uci commit


    /etc/config/network 檔案應包含「wan」區段及以下字串(最後三行字串為新增內容):

    config interface 'wan'                        
            option ifname 'eth0.2'                
            option force_link '1'                 
            option proto 'dhcp'                    
            option peerdns '0'                    
            list dns '162.252.172.57'                    
            list dns '149.154.159.92' 

     

  12. (選用)為避免流量透過 VPN 通道以外的路徑傳輸,您需要移除從 LAN 到 WAN 的轉送規則。預設設定中只有一條轉送規則,指令如下:

    uci del firewall.@forwarding[0]


    您也可以將 wan 區域的「masquerading」選項設為 '0';該區域位於 lan 區域之後。指令如下:

    uci set firewall.@zone[1].masq=0


    設定完成後,您應提交變更:

    uci commit firewall

     

  13. 您也可以修改/etc/firewall.user 檔案,停用未指定介面的流量轉送功能:

    # This file is interpreted as shell script.
    # Put your custom iptables rules here, they will
    # be executed with each firewall (re-)start.
    # Internal uci firewall chains are flushed and recreated on reload, so
    # put custom rules into the root chains e.g. INPUT or FORWARD or into the
    # special user chains, e.g. input_wan_rule or postrouting_lan_rule.

    if (! iptables -C forwarding_lan_rule ! -o tun+ -j REJECT); then
            iptables -I forwarding_lan_rule ! -o tun+ -j REJECT
    fi

     

  14. 此外,您還可以執行下列步驟。將以下內容附加到檔案 /etc/firewall.user:

    if (! ip a s tun0 up) && (! iptables -C forwarding_rule -j REJECT); then
            iptables -I forwarding_rule -j REJECT
    fi

    在 /etc/hotplug.d/iface/ 資料夾中建立名為 99-prevent-leak 的檔案,內容如下:

    #!/bin/sh
    if [ "$ACTION" = ifup ] && (ip a s tun0 up) && (iptables -C forwarding_rule -j REJECT); then
            iptables -D forwarding_rule -j REJECT
    fi
    if [ "$ACTION" = ifdown ] && (! ip a s tun0 up) && (! iptables -C forwarding_rule -j REJECT); then
            iptables -I forwarding_rule -j REJECT
    fi

     

  15. 設定完成後,使用以下指令重新啟動路由器:
    #reboot

  16. 在某些情況下,OpenVPN 可能會停止回應,並在記錄中出現類似(couldn't resolve host ……). 在此情況下,通道仍維持啟用狀態,但實際連線已中斷。您應使用以下指令碼 /etc/openvpn/reconnect.sh 手動重新連線,並將其新增至 /etc/rc.local,如下所示:

    /etc/openvpn/reconnect.sh &

    指令碼 reconnect.sh 的內容如下:

    #!/bin/sh
    n=10
    while sleep 50; do
            t=$(ping -c $n 8.8.8.8 | grep -o -E '\d+ packets r' | grep -o -E '\d+')
            if [ "$t" -eq 0 ]; then
                    /etc/init.d/openvpn restart
            fi
    done

     

確認連線成功

首次完成設定後,我們建議您確認 Surfshark VPN 是否正常運作。您只需執行 Surfshark IP 洩漏測試 和 DNS 洩漏測試,即可輕鬆確認。為了方便您使用,這兩項測試均可在我們的網站上進行。

 



您可能也對以下內容感興趣:

 

這篇文章是否有幫助?
感謝您的意見回饋!