Skip to content

Instantly share code, notes, and snippets.

@lepture
Created April 4, 2019 07:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lepture/b73bb93f9e2b93de555fec8527781564 to your computer and use it in GitHub Desktop.
Save lepture/b73bb93f9e2b93de555fec8527781564 to your computer and use it in GitHub Desktop.
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