Skip to content

Instantly share code, notes, and snippets.

@rafaeltovar
Created May 10, 2016 10:50
Show Gist options
  • Save rafaeltovar/c7f691e827dd8318e031d1e88dde399b to your computer and use it in GitHub Desktop.
Save rafaeltovar/c7f691e827dd8318e031d1e88dde399b to your computer and use it in GitHub Desktop.
CloudFlare whitelist rule for Nginx
"""
@authors: Rafael Tovar <hola AT rafaeltovar.info>
https://github.com/rafaeltovar/cloudflare2nginx/blob/master/cloudflare2nginx.py
"""
OUTPUT_FILE = "/etc/nginx/cloudflare_allow"
# from # https://www.cloudflare.com/ips
CLOUDFLARE_IPv4 = "https://www.cloudflare.com/ips-v4"
CLOUDFLARE_IPv6 = "https://www.cloudflare.com/ips-v6"
import time, urllib2
# get cloudflare ips
contentIPv4 = urllib2.urlopen(CLOUDFLARE_IPv4).read()
contentIPv6 = urllib2.urlopen(CLOUDFLARE_IPv6).read()
# write file
# TODO control permission?
f = open(OUTPUT_FILE,'w')
f.write('# By Cloudflare2Nginx script\n')
f.write('# UPDATE: ' + time.strftime("%d/%m/%Y")+'\n\n')
f.write('# IPv4\n')
for line in contentIPv4.splitlines():
f.write('allow ' + line + ';\n')
f.write('\n# IPv6\n')
for line in contentIPv6.splitlines():
f.write('allow ' + line + ';\n')
f.write('\n# Deny all petition\n')
f.write('deny all;\n')
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment