Skip to content

Commit 1c84692

Browse files
committed
Add PHP script that updates module's config with latest CF IPs
1 parent ae04957 commit 1c84692

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

mod_cloudflare_conf_ip_update.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
// A simple PHP script automatically updating Cloudflare IPs
4+
// in mod_cloudflare's config file at /etc/apache2/mods-available/cloudflare.conf
5+
// Works in Debian and will work in all OSs provided they have PHP and you optionally adjust config file location
6+
// You can uncomment DenyAllButCloudFlare option in config content if you desire too
7+
// Can be added to cron like so (remember about having correct permissions, so the executing user can write the config file):
8+
// 0 0 * * * php /<path>/mod_cloudflare_conf_ip_update.php
9+
//
10+
// Created by p0358
11+
12+
$list = '';
13+
$arr = [];
14+
15+
$ipv4 = file_get_contents('https://www.cloudflare.com/ips-v4');
16+
$ipv6 = file_get_contents('https://www.cloudflare.com/ips-v6');
17+
18+
$ipv4_arr = explode("\n", trim($ipv4));
19+
foreach ($ipv4_arr as $line) {
20+
$arr[] = trim($line);
21+
}
22+
23+
$ipv6_arr = explode("\n", trim($ipv6));
24+
foreach ($ipv6_arr as $line) {
25+
$arr[] = trim($line);
26+
}
27+
28+
//$arr[] = ''; -- you can append your public server's IP(s) here too
29+
$arr[] = '127.0.0.1';
30+
$arr[] = '::1';
31+
32+
$list = implode(' ', $arr);
33+
34+
$datestring = date('Y-m-d H:i:s');
35+
36+
$content = <<<CONTENT
37+
<IfModule mod_cloudflare.c>
38+
CloudFlareRemoteIPHeader CF-Connecting-IP
39+
CloudFlareRemoteIPTrustedProxy $list
40+
#DenyAllButCloudFlare
41+
</IfModule>
42+
# Updated using mod_cloudflare_conf_ip_update.php by p0358 at $datestring
43+
CONTENT;
44+
45+
file_put_contents('/etc/apache2/mods-available/cloudflare.conf', $content);

0 commit comments

Comments
 (0)