Skip to content

Instantly share code, notes, and snippets.

@walkure
Created May 15, 2023 16:12
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/2c76502f95673b47b31ce0c2d872bd75 to your computer and use it in GitHub Desktop.
Save walkure/2c76502f95673b47b31ce0c2d872bd75 to your computer and use it in GitHub Desktop.
Set Cloudflare IP Addresses to nginx real_ip
#!/usr/bin/env perl
use strict;
use warnings;
use HTTP::Tiny;
my $prefix = 'cloudflare-origin';
die "ERR: requires one argument: output file path" unless scalar @ARGV == 1;
my @addrs = fetch('v4');
my @v6 = fetch('v6');
push(@addrs,@v6);
open(my $fh,">$ARGV[0]") or die "cannot open $ARGV[0]:$!";
# ngx_http_realip_module
foreach my $addr(@addrs){
print $fh "set_real_ip_from $addr;\n";
}
print $fh "real_ip_header CF-Connecting-IP;\n";
close $fh;
sub fetch
{
my $type = shift;
my $client = HTTP::Tiny->new();
my $res = $client->get("https://www.cloudflare.com/ips-$type");
if(!$res->{success}){
die "failure to fecth $type:$res->{reason}\n";
}
split(/\n/,$res->{content});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment