Skip to content

Instantly share code, notes, and snippets.

@lepture

lepture/cloudflare-nginx.py Secret

Created Apr 4, 2019
Embed
What would you like to do?
import requests
ipv4 = 'https://www.cloudflare.com/ips-v4'
ipv6 = 'https://www.cloudflare.com/ips-v6'
def gen_set_real_ip(url):
resp = requests.get(url)
lines = resp.text.splitlines()
return '\n'.join(['set_real_ip_from {};'.format(ip) for ip in lines])
map_schema = '''map $http_cf_visitor $cf_scheme {
'{"scheme":"http"}' http;
'{"scheme":"https"}' https;
default $http_x_forwarded_proto;
}
'''
conf = '\n\n'.join([
map_schema,
'# https://www.cloudflare.com/ips/',
gen_set_real_ip(ipv4),
gen_set_real_ip(ipv6),
'real_ip_header CF-Connecting-IP;'
])
print(conf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.