Created
May 15, 2023 16:12
-
-
Save walkure/2c76502f95673b47b31ce0c2d872bd75 to your computer and use it in GitHub Desktop.
Set Cloudflare IP Addresses to nginx real_ip
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'; | |
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