python - 带有扭曲 python HTTPS 服务器的反向代理
问题描述
我正在尝试构建一个反向代理来过滤所有到达本地服务器的请求。
服务器:
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)
推荐阅读
- json.net - 使用 JSONPath 和 Json.Net 获取属性名称
- python - “顺序”对象没有属性“Conv2DTranspose”
- javascript - 如何使用 rxjs 在 Angular 中嵌套 api 调用?
- java - JMeter 5.4打开测试计划时出现意外错误
- git - GIT - 从 Jenkins 获取文件夹的最后一次提交
- selenium - 运行 selenium 脚本时出错。未使用 chrome 浏览打开会话
- excel - 使用 ISNUMBER SEARCH 在 Excel 中的多个单元格范围内查找文本值时出现问题
- firebase - 如何检查 Firebase 规则中的相同值
- javascript - 使可编辑元素溢出到新元素
- php - 将数字转换为字符串 php