python拦截网卡_在python中拦截并过滤对HTTPS服务器的请求?

该博客介绍了一个Python类`HTTPSReverseProxyResource`,它用于拦截并对HTTPS服务器的请求进行过滤。当请求匹配到禁止的URL时,返回'LocalResource',否则通过代理转发。文章还展示了如何使用`argparse`参数解析端口和SSL证书,并利用Twisted库创建SSL服务器监听指定端口。
摘要由CSDN通过智能技术生成

class LocalResource(Resource):

def render(self, request):

return "Banned"

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 any([re.match(url, path) for url in banned_urls]):

return LocalResource()

else:

child = super(HTTPSReverseProxyResource, self).getChild(path, request)

return HTTPSReverseProxyResource(child.host, child.port, child.path,

child.reactor)

if __name__ == '__main__':

import argparse

ap = argparse.ArgumentParser()

ap.add_argument('port', default=8080, nargs='?', type=int)

ap.add_argument('--ssl-cert', type=str)

ap.add_argument('--ssl-key', type=str)

ns = ap.parse_args()

if ns.ssl_cert:

from twisted.internet import ssl

with open(ns.ssl_cert, 'rb') as fp:

ssl_cert = fp.read()

if ns.ssl_key:

from OpenSSL import crypto

with open(ns.ssl_key, 'rb') as fp:

ssl_key = fp.read()

ftype = crypto.FILETYPE_PEM

k = ssl.KeyPair.load(ssl_key, ftype)

certificate = ssl.PrivateCertificate.load(ssl_cert, k, ftype)

else:

certificate = ssl.PrivateCertificate.loadPEM(ssl_cert)

srv = HTTPSReverseProxyResource('your_main_server', 443 , '')

reactor.listenSSL(ns.port, srv, certificate.options())

reactor.run()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值