我應該將 OpenWrt 上的伺服器暴露給 WAN 還是僅暴露給 LAN(透過 WAN 進行連接埠轉送)?

我應該將 OpenWrt 上的伺服器暴露給 WAN 還是僅暴露給 LAN(透過 WAN 進行連接埠轉送)?

如果我在路由器後面的計算機上安裝了伺服器,並且想要允許從Internet 連接到它,我必須做兩件事:第一,打開伺服器電腦上的防火牆以允許傳入連接;第二,打開伺服器計算機上的防火牆以允許傳入連線;第二,開啟伺服器電腦上的防火牆以允許傳入連線。第二,在路由器上設定連接埠轉送規則,以便連線可以從 WAN 到 LAN(特別是到伺服器電腦)。

但是,如果有問題的伺服器機器是運行在 OpenWrt 上的路由器本身,我該怎麼辦?

我可以想到兩個選擇。

選項 A. 像對待 LAN 中的任何其他電腦一樣對待路由器。在路由器上安裝伺服器後,開啟防火牆以允許從 LAN 內部到伺服器的傳入連線。 (我相信這是在 LuCI > 網路 > 防火牆 > 流量規則中完成的。)然後,設定從 WAN 到 LAN(特別是伺服器)的連接埠轉送規則。 (LuCI > 網路 > 防火牆 > 連接埠轉送。)

選項 B. 使用 LuCI 的「流量規則」標籤開啟防火牆,以便從 WAN 直接連線到伺服器。

問題是:

  1. 我認為這是我可能考慮的兩個選擇,對嗎?

  2. 雙方的優點和缺點是什麼?

  3. 標準做法是什麼?

有問題的伺服器可能是 VPN 伺服器(例如 Wireguard)或 OpenSSH,我可以安裝它來取代 Dropbear。但是如果您在 OpenWrt 上安裝了 A/V 流伺服器(假設這是可能的),也會出現相同的問題。換句話說,我希望這個問題仍然是上述兩個選項的通用問題,而不是僅限於任何特定軟體(Wireguard 等)。

我對 OpenWrt 和 Linux 都是新手。直到幾天前我才知道 OpenWrt 的存在。選項 A 可能很瘋狂(從來沒有人做過),我的大腦之所以想到它,是因為它以前從未見過像 OpenWrt 這樣的東西,只能用「常規路由器」術語來思考。

事實上,這讓我認為選項 A 可能有這個優勢。其實我以前也做過連接埠轉發,但Linux防火牆對我來說還是陌生的。所以我可能會集中精力研究交通規則,如果混亂只發生在區域網路中會更好。

請指教。謝謝。

附錄

這些是說明選項 A(左)和 B(右)的 LuCI 螢幕截圖。左側面板假定連接埠 12000 已向 LAN 開啟(透過預設政策或特定流量規則)。 192.168.1.1。是路由器的 LAN IP 位址。

在此輸入影像描述

答案1

(雖然在某些瀏覽器上不明顯,但每個檔案路徑都是連結)

對於 SSH,標準做法是指定一個DNAT規則[連接埠轉送]到它所在的內部網路;而對於 VPN 伺服器,您需要建立一條規則以允許 WAN 存取其伺服器連接埠:

  • 線衛維基
  • SSH:
    • /etc/config/firewall:
      #
      
          ##::[[---  OpenWrt WAN Firewall Config  ---]]::##
      
      #===========================================================
                    ##----- NAT Redirects -----##
      #===========================================================
      
      # SSH #
      #-----------------------------------------------------------
      config redirect
          option  target          'DNAT'
          option  proto           'tcp'
          option  src             'wan'
          option  src_dport       60501
          option  dest            'lan'
          option  dest_ip         192.168.1.1
          option  dest_port       22
          option  name            'Allow Redirect WAN → LAN (SSH)'
      
      config redirect
          option  target          'DNAT'
          option  proto           'tcp'
          option  src             'vpn'
          option  src_dport       60502
          option  dest            'lan'
          option  dest_ip         192.168.1.1
          option  dest_port       22
          option  name            'Allow Redirect Wireguard → LAN (SSH)'
      
      #===========================================================
                        ##----- VPN Zones -----##
      #===========================================================
      
      # Wireguard #
      #-----------------------------------------------------------
      config zone
          option  name            'vpn'
          option  network         'vpn'
          option  input           'ACCEPT'
          option  forward         'ACCEPT'
          option  output          'ACCEPT'
          option  log             1
      
      # Rules:
      #-----------------------------------------------------------
      config rule
          option  target          'ACCEPT'
          option  proto           'tcp udp'
          option  src             'wan'
          option  dest            '*'
          option  dest_port       51820
          option  name            'Allow Forwarded Wireguard → Router'
      
      config rule
          option  target          'ACCEPT'
          option  proto           'tcp'
          option  src             'vpn'
          option  dest            '*'
          option  dest_port       60502
          option  name            'Allow Wireguard → Router (SSH)'
      

    • /etc/config/dropbear:
      #
      
            ##::[[---  OpenWrt DropBear Config  ---]]::##
      
      #===========================================================
                       ##----- Default -----##
      #===========================================================
      
      # Server #
      #-----------------------------------------------------------
      config dropbear
          option  enable            1
          option  Interface         'lan'
          option  PasswordAuth      'off'
          option  RootPasswordAuth  'off'
          option  IdleTimeout       0
          option  Port              22
          option  RootLogin         1
          option  SSHKeepAlive      300
          option  verbose           1
      

    • /etc/ssh/sshd_config: 客製化
      #
      
          ##::[[---  OpenWrt OpenSSH SSHd Config  ---]]::##
      
      #===========================================================
                    ##----- Global Options -----##
      #===========================================================
      
        # Please verify, as all algorithms may not supported:
      
          # Ciphers:                  # HostKey & KeyTypes
            ## ssh -Q cipher            ## ssh -Q key
      
          # Kex [Key Exchange]        # MAC [Message Authentication Code]
            ## ssh -Q kex               ## ssh -Q mac
      
      
      # Connection #
      #-----------------------------------------------------------
      AddressFamily                     = inet
      ListenAddress                     = 192.168.1.1:22
      
      
      # Encryption #
      #-----------------------------------------------------------
      AuthorizedKeysFile                = /root/.ssh/authorized_keys
      
      HostKey                           = /etc/ssh/ssh_host_ed25519_key
      HostKey                           = /etc/ssh/ssh_host_rsa_key
      
      
      # Authentication #
      #-----------------------------------------------------------
      AllowUsers                        = root
      AllowGroups                       = root
      
      ChallengeResponseAuthentication   = no
      PasswordAuthentication            = no
      PermitEmptyPasswords              = no
      
      StrictModes                       = yes
      PubkeyAuthentication              = yes
      
      LoginGraceTime                    = 30
      
      MaxAuthTries                      = 3
      MaxSessions                       = 10
      MaxStartups                       = 3:30:10
      
      PermitRootLogin                   = prohibit-password
      
      
      # Reliability #
      #-----------------------------------------------------------
      ClientAliveCountMax               = 3
      ClientAliveInterval               = 600
      
      TCPKeepAlive                      = yes
      
      UseDNS                            = yes
      
      
      # Security #
      #-----------------------------------------------------------
      AllowAgentForwarding              = yes
      AllowTcpForwarding                = yes
      
      GatewayPorts                      = clientspecified
      PermitTunnel                      = yes
      
      Subsystem   sftp                  = /usr/lib/sftp-server
      
      RekeyLimit                        = 500M 60m
      
      
      # Logging #
      #-----------------------------------------------------------
      SyslogFacility                    = AUTH
      LogLevel                          = VERBOSE
      
      PidFile                           = /tmp/run/sshd.pid
      
      
      # Ciphers and ReKeying #
      #-----------------------------------------------------------
      FingerprintHash                   = sha256
      
      Ciphers                           = [email protected],[email protected],aes128-ctr,aes128-cbc,aes192-ctr,aes192-cbc,aes256-ctr,aes256-cbc,[email protected]
      
      HostKeyAlgorithms                 = ssh-ed25519,ssh-rsa,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
      
      HostbasedAcceptedKeyTypes         = ssh-ed25519,ssh-rsa,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
      
      KexAlgorithms                     = [email protected],curve25519-sha256,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256
      
      MACs                              = [email protected],[email protected],hmac-sha2-256,hmac-sha2-512
      
      PubkeyAcceptedKeyTypes            = ssh-ed25519,ssh-rsa,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
      

  • 魯西:
    1. https://<router IP>網路 (左側選單)防火牆
    2. 交通規則
      1. 添加:
        姓名: Allow Forwarded Wireguard → Router
        協定: TCP UDP
        來源區: wan
        目的地區域: Any zone (forward)
        目的端口: 51820
        行動: accept
        節省
      2. 添加:
        姓名: Allow Wireguard → Router (SSH)
        協定: TCP
        來源區: vpn
        目的地區域: Any zone (forward)
        目的端口: 60502
        行動: accept
        節省

    3. 連接埠轉送
      1. 添加:
        姓名: Allow Redirect WAN → LAN (SSH)
        協定: TCP
        來源區: wan
        外部連接埠: 60501
        目的地區域: lan
        內部IP位址: 192.168.1.1
        內部連接埠: 22
        行動: accept
        節省
      2. 添加:
        姓名: Allow Redirect VPN → LAN (SSH)
        協定: TCP
        來源區: vpn
        外部連接埠: 60502
        目的地區域: lan
        內部IP位址: 192.168.1.1
        內部連接埠: 22
        行動: accept
        節省

    4. 儲存並應用

相關內容