python - 带有扭曲 python HTTPS 服务器的反向代理

标签 python https server twisted reverse-proxy

我正在尝试构建一个反向代理来过滤所有到达本地服务器的请求。

服务器:

import BaseHTTPServer, SimpleHTTPServer
import ssl
import os

def main():
    httpd = BaseHTTPServer.HTTPServer(('192.168.58.1', 4443), SimpleHTTPServer.SimpleHTTPRequestHandler)
    os.chdir('./content')
    # httpd.socket = ssl.wrap_socket(httpd.socket, certfile='../server.pem', server_side=True)
    httpd.serve_forever()

if __name__ == '__main__':
    main()

代理:

from twisted.web import proxy, server
from twisted.internet import ssl, reactor
import argparse
from OpenSSL import crypto

class BadURL():
    def render(self, request):
        return "Banned"

class HTTPSReverseProxyResource(proxy.ReverseProxyResource, object):

    def getChild(self, path, request):

        if "fmm" in path:
            return BadURL()
        child = super(HTTPSReverseProxyResource, self).getChild(path, request)
        return HTTPSReverseProxyResource(child.host, child.port, child.path,
                                     child.reactor)


if __name__ == '__main__':

    ap = argparse.ArgumentParser()
    ap.add_argument('-c', type=str)
    ap.add_argument('-k', type=str)
    ns = ap.parse_args()

    myProxy = HTTPSReverseProxyResource('192.168.58.1', 4443, '')

    site = server.Site(myProxy)

    if ns.c:
        with open(ns.c, 'rb') as fp:
            ssl_cert = fp.read()
        if ns.k:

            with open(ns.k, 'rb') as fp:
                ssl_key = fp.read()
            certificate = ssl.PrivateCertificate.load(
                    ssl_cert,
                    ssl.KeyPair.load(ssl_key, crypto.FILETYPE_PEM),
                    crypto.FILETYPE_PEM)
        else:
            certificate = ssl.PrivateCertificate.loadPEM(ssl_cert)
        reactor.listenSSL(8080, site, certificate.options())
    else:
        reactor.listenTCP(8080, site)
    reactor.run()

问题和我的问题是,当我使用 HTTPS 服务器时(取消注释服务器中的“httpd.socket = ssl.wrap_socket ...”行)代理返回的所有页面都是空白的。

最佳答案

您没有将 proxyClientFactoryClass 方法添加到您的反向代理类 (Python-Twisted: Reverse Proxy to HTTPS API: Could not connect)。试试这个:

class HTTPSReverseProxyResource(proxy.ReverseProxyResource, object):
    def proxyClientFactoryClass(self, *args, **kwargs):
        """
        Make all connections using HTTPS.
        """
        return TLSMemoryBIOFactory(
            ssl.optionsForClientTLS(self.host.decode("ascii")), True,
            super(HTTPSReverseProxyResource, self)
            .proxyClientFactoryClass(*args, **kwargs))

    def getChild(self, path, request):

        if "fmm" in path:
            return BadURL()
        child = super(HTTPSReverseProxyResource, self).getChild(path, request)
        return HTTPSReverseProxyResource(child.host, child.port, child.path,
                                     child.reactor)

关于python - 带有扭曲 python HTTPS 服务器的反向代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52029569/

相关文章:

python - 为什么 Python Tkinter 单选按钮左对齐?

Python 3 简单的 HTTPS 服务器

android - SSLHandshakeException - Chain链验证失败,如何解决?

ubuntu - 在 VPS 服务器上安装 Kubernetes

ios - 无法在 iOS 上的 Swift 中将 base64 字符串发送到服务器

powershell - Cim_PhysicalMemory 和 Win32_ComputerSystem 返回不同的内存量

python - Conda PackageNotFoundError - 无法安装包

python - Numba 在频率计数方面比纯 Python 慢

python - 在 python 文件夹中导入图表/结构(清理代码)

ssl - 通过 SSL 的 WCF - 404 错误