Browse Source

fix support for Cloudflare

pull/12/merge
lilydjwg 2 years ago
parent
commit
f5b73aae74
2 changed files with 18 additions and 15 deletions
  1. +16
    -11
      cloudflare.py
  2. +2
    -4
      main.py

+ 16
- 11
cloudflare.py View File

@@ -35,14 +35,19 @@ async def updater():
logger.exception('error when update cloudflare ips')
await asyncio.sleep(24 * 3600)

class CfApplication(tornado.web.Application):
def __call__(self, request):
cfip = request.headers.get('Cf-Connecting-IP')
if cfip:
ip = request.remote_ip
for net in CLOUDFLARE_IPS:
if ip in net:
request.remote_ip = cfip
request.protocol = request.headers.get('X-Forwarded-Proto', 'http')
break
return super().__call__(request)
def install():
RH = tornado.web.RequestHandler
RH.prepare = _my_prepare

def _my_prepare(self):
logger.info('entering')
request = self.request
cfip = request.headers.get('Cf-Connecting-IP')
if cfip:
ip = ipaddress.ip_address(request.remote_ip)
for net in CLOUDFLARE_IPS:
logger.info('checking %s', net)
if ip in net:
request.remote_ip = cfip
request.protocol = request.headers.get('X-Forwarded-Proto', 'http')
break

+ 2
- 4
main.py View File

@@ -207,13 +207,11 @@ def main():

if options.cloudflare:
import cloudflare
Application = cloudflare.CfApplication
cloudflare.install()
loop = asyncio.get_event_loop()
loop.create_task(cloudflare.updater())
else:
Application = tornado.web.Application

application = Application([
application = tornado.web.Application([
(r"/", IndexHandler),
(r"/" + SCRIPT_PATH, ToolHandler),
(r"/([a-fA-F0-9]{2}/[a-fA-F0-9]{38})(\.\w*)?", MyStaticFileHandler, {


Loading…
Cancel
Save