将 Web 服务器流量限制为仅通过 SSH 隧道连接的客户端

将 Web 服务器流量限制为仅通过 SSH 隧道连接的客户端

注意:我不确定这是问这个问题的正确位置,还是我问的问题是否正确。如果我错了,请耐心等待,并告诉我如何纠正我的问题或将其移至正确的 StackExchange 站点。

我有一个 Web 服务器,我只希望一些客户端连接到(Ubuntu 主机 1 和 2),并且我希望这些客户端通过 SSH 隧道连接到 Web 服务器,并且所有其他客户端都应该被拒绝访问 Web 服务器。

设置应该如下所示,如果 Ubuntu 主机(或任何其他主机)尝试直接连接到 Web 服务器,它们会被拒绝访问(或者更好的是服务器根本不响应),但如果它们连接到服务器通过 SSH 隧道,只有这样他们才能访问 Web 服务器

                    ┌────────────────────────────────────────────────── ────────────────────┐
                    │ 拒绝访问 │
┌────────────────────┴─┐ │
│ │ ┌────────────────────────────┐ │
│ │ SSH 隧道 │ Web 服务器 │ │
│ Ubuntu主机1 ◄────────────────────────────────┤ │ │
│ ┌──────────────┼────────────────┼────────────── ──────┐ │ │
│ │ │ │ 已授予访问权限 │ │ │
└──────────────────────┘ │ │ │ │ │
                                       │ │ │ │ │
┌──────────────────────┐ │ │ ┌─▼────────────┤ │
│ │ │ │ 授予访问权限 │ Python │ │
│ ├──────────────┼────────────────┼────────────── ───► http.server │◄─┘
│ Ubuntu 主机 2 ◄──────────────┘ │ │ 端口=8080 │
│ │ │ │ 主机=0.0.0.0│◄─┐
│ │ └────────────────┴──────────────┘ │
└────────────────────┬─┘ │
                    │ 拒绝访问 │
                    └────────────────────────────────────────────────── ────────────────────┘

我可以通过执行以下操作来实现此目的:

在网络服务器上(web-server.example.com):

  1. 跑步python3 server.py
  2. 通过 SSH 连接到 Ubuntu 机器
ssh ubuntu-host-1.example.com -R 8080:web-server.example.com:8080
ssh ubuntu-host-2.example.com -R 8080:web-server.example.com:8080

代码在服务器.py(从pythonbasics.org):

# Python 3 server example
from http.server import BaseHTTPRequestHandler, HTTPServer
import time, logging, ipaddress

hostName = "0.0.0.0"
serverPort = 8080
allowed_net = ipaddress.ip_network('172.16.0.0/12')
priv_net = list(allowed_net.hosts())

class MyServer(BaseHTTPRequestHandler):
    def do_GET(self):
        client = ipaddress.ip_address(self.client_address[0])
        if client in priv_net:
            self.send_response(200)
            self.send_header("Content-type", "text/html")
            self.end_headers()
            self.wfile.write(bytes("<html><head><title>https://pythonbasics.org</title></head>", "utf-8"))
            self.wfile.write(bytes("<p>Request: %s</p>" % self.path, "utf-8"))
            self.wfile.write(bytes("<p>IP: %s</p>" % self.client_address[0], "utf-8"))
            self.wfile.write(bytes("<body>", "utf-8"))
            self.wfile.write(bytes("<p>This is an example web server.</p>", "utf-8"))
            self.wfile.write(bytes("</body></html>", "utf-8"))
        else:
            self.send_response(404)
            self.send_header("Content-type", "text/html")
            self.end_headers()
            self.wfile.write(bytes("<html><head><title>https://pythonbasics.org</title></head>", "utf-8"))
            self.wfile.write(bytes("<p>Request: %s</p>" % self.path, "utf-8"))
            self.wfile.write(bytes("<p>IP: %s</p>" % self.client_address[0], "utf-8"))
            self.wfile.write(bytes("<body>", "utf-8"))
            self.wfile.write(bytes("<p>Access Denied</p>", "utf-8"))
            self.wfile.write(bytes("</body></html>", "utf-8"))



if __name__ == "__main__":        
    webServer = HTTPServer((hostName, serverPort), MyServer)
    print("Server started http://%s:%s" % (hostName, serverPort))

    try:
        webServer.serve_forever()
    except KeyboardInterrupt:
        pass

    webServer.server_close()
    print("Server stopped.")

正如您所看到的,在我的 Web 服务器代码中,我必须处理客户端从 SSH 隧道外部连接的情况(即不属于 172.16.0.0/12 网络的一部分)

有没有一种方法可以实现这一点,而不需要服务器侦听所有接口(0.0.0.0),但仍然只为通过 SSH 隧道连接的客户端提供服务?

注意:我无法打开从 Ubuntu 主机到 Web 服务器的 SSH 隧道。一定是相反的

答案1

根据您的图表,我了解到 SSH 隧道和 Web 服务器正在同一台计算机上运行。在这种情况下,到 Python HTTP 服务器的所有有效连接都将从“localhost”到达。

如果是这种情况,仅不绑定在所有接口上,localhost将是您正在寻找的解决方案,因为来自 127.0.0.1 之外的任何 IP 的任何内容都不会到达 Web 服务器,因为它不会监听他们在那里。

相关内容