Created
November 25, 2022 20:01
-
-
Save KumaTea/c2e447fe9b3722a71c576ce0b1170175 to your computer and use it in GitHub Desktop.
Update Cloudflare IPs for Nginx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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