Skip to content

Instantly share code, notes, and snippets.

@walkure
Created March 5, 2023 17:15
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 walkure/84b8529b032ada945882406c69f5bb77 to your computer and use it in GitHub Desktop.
Save walkure/84b8529b032ada945882406c69f5bb77 to your computer and use it in GitHub Desktop.
Set Cloudflare IP Addresses to ipset
#!/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