Skip to content

Instantly share code, notes, and snippets.

@KumaTea
Created November 25, 2022 20:01
Show Gist options
  • Select an option

  • Save KumaTea/c2e447fe9b3722a71c576ce0b1170175 to your computer and use it in GitHub Desktop.

Select an option

Save KumaTea/c2e447fe9b3722a71c576ce0b1170175 to your computer and use it in GitHub Desktop.
Update Cloudflare IPs for Nginx
import requests
CONF = '/etc/nginx/cloudflare_ips.conf'
IPV4 = 'https://www.cloudflare.com/ips-v4'
IPV6 = 'https://www.cloudflare.com/ips-v6'
def get_ips(url):
return requests.get(url).text
def parse_ips(text):
result = '\n'.join(
[f'allow {i.strip()};' for i in text.splitlines() if i]
)
return result
def update():
config = ''
config += f'# {IPV4}\n'
config += parse_ips(get_ips(IPV4))
config += '\n\n'
config += f'# {IPV6}\n'
config += parse_ips(get_ips(IPV6))
# print(config)
with open(CONF, 'w') as f:
f.write(config)
if __name__ == '__main__':
update()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment