Created
March 5, 2023 17:15
-
-
Save walkure/84b8529b032ada945882406c69f5bb77 to your computer and use it in GitHub Desktop.
Set Cloudflare IP Addresses to ipset
This file contains 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
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use HTTP::Tiny; | |
my $prefix = 'cloudflare-origin'; | |
updatelist('v4'); | |
updatelist('v6'); | |
sub updatelist | |
{ | |
my $type = shift; | |
my $family = $type eq 'v6' ? 'inet6' : 'inet'; | |
my @addrs = fetch($type); | |
return unless @addrs; | |
my $set = "$prefix-$type"; | |
my $newset = "$set-tmp"; | |
`ipset create $newset hash:net family $family`; | |
foreach my $addr(@addrs){ | |
`ipset add $newset $addr`; | |
} | |
`ipset swap -q $set $newset`; | |
`ipset rename $newset $set` if $?; | |
`ipset destroy -q $newset`; | |
} | |
sub fetch | |
{ | |
my $type = shift; | |
my $client = HTTP::Tiny->new(); | |
my $res = $client->get("https://www.cloudflare.com/ips-$type"); | |
if(!$res->{success}){ | |
print "failure to fecth $type:$res->{reason}\n"; | |
return; | |
} | |
split(/\n/,$res->{content}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment