将配置文件中的所有 ip 重定向到静态地址



目前使用Tornado作为几个WSGI应用程序(主要是Flask应用程序)的包装器。最近注意到很多黑客尝试,并且想知道是否有可能自动查看某个文件中定义的 IP 列表,然后将所有这些 IP 重定向到一个页面,上面写着"有人使用此 IP 试图入侵我们的网站,证明您不是机器人,我们将重新允许您的 IP"。

运行服务器的龙卷风代码在这里:

from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from Wrapper.app import application
http_server = HTTPServer(WSGIContainer(application))
http_server.listen(80)
IOLoop.instance().start()

wrapper.app 如下:

from werkzeug.wsgi import DispatcherMiddleware
from Splash import splash_app
from SentimentDemo import sentiment_app
from FERDemo import FER_app
application = DispatcherMiddleware(splash_app, {
    '/api/sentiment': sentiment_app,
    '/api/fer': FER_app
})

无法找到有关此类事情的任何文档,因此,如果这个问题似乎不知情,我提前表示歉意,但即使只是一个开始寻找的地方也会很壮观。

您希望对 WSGIContainer 进行子类化并覆盖其__call__方法。类似的东西

class MyWSGIContainer(WSGIContainer):
    def __call__(self, request):
        if request.remote_ip in blacklist:
            self.write_redirect()
        else:
            super(MyWSGIContainer, self)(request)

有关编写self.write_redirect()的一些提示,请在此处查看WSGIContainer的代码;您可以看到它如何格式化HTTP标头。您应该使用 HTTP 302 临时重定向。

然后将您的 MyWSGIContainer 实例传递到 HTTPServer,而不是默认的 WSGIContainer。

最新更新