Skip to content

Commit a0ca1cf

Browse files
committed
Removed single instance check
1 parent 33302a2 commit a0ca1cf

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## v1.1.0
4+
5+
- Removed single instance check
6+
- Added a real world example to the readme file
7+
- Fixed a bug in regex pattern in raw mode
8+
39
## v1.0.0
410

511
- Initial release

README.md

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Automatic CDN and bogon IP list updater for firewall and server configurations
66
* Downloads bogon IP lists to blacklist in firewalls
77
* Downloads CDN/trusted proxy/reverse proxy IP ranges to whitelist in firewalls, update server configurations
88
* Supports Ipset/Iptables mode, Nginx ngx_http_realip_module, Apache mod_remoteip module, raw mode (for any firewall, server or daemon)
9+
* Supports many input files with "IP address/netmask" format including raw IP lists, jsonp, xml, etc...
910
* Downloads multiple lists and merge
1011
* IP address and list validation just in case
1112
* Compatible with any daemon, server or firewall
@@ -90,11 +91,11 @@ Doing some magic with bash and raw list. /etc/myscript.sh contents:
9091

9192
This example demonstrates how to whitelist your CDN/reverse proxy IP range through ipset and iptables.
9293

93-
Create a whitelist set, create iptables rule to accept whitelist set for http/https ports, add Cloudflare IPv4 range to whitelist set.
94+
Create a proxylist set, create iptables rule to accept proxylist set for http/https ports, add Cloudflare IPv4 range to proxylist set.
9495

95-
$ ipset create whitelist hash:net family inet hashsize 1024 maxelem 131072
96-
$ iptables -I INPUT -p tcp -m multiport --dports 80,443 -m set --match-set whitelist src -j ACCEPT
97-
$ ip-list-updater.php --update --mode="ipset" --setname="whitelist" --ipv=4 --output="/etc/whitelist.txt" --sources="cloudflare"
96+
$ ipset create proxylist hash:net family inet hashsize 1024 maxelem 131072
97+
$ iptables -I INPUT -p tcp -m multiport --dports 80,443 -m set --match-set proxylist src -j ACCEPT
98+
$ ip-list-updater.php --update --mode="ipset" --setname="proxylist" --ipv=4 --output="/etc/proxylist.txt" --sources="cloudflare"
9899

99100
This example demonstrates how to block a bogonlist through ipset and iptables.
100101

@@ -138,7 +139,17 @@ Update ip list and create Apache (module mod_remoteip) trusted proxy list file t
138139
Make sure Apache reload success command is correct which may be OS specific.
139140

140141
$ ip-list-updater.php --update --mode="apache" --ipv=4 --output="/etc/apache-cloudflare.lst" --sources="cloudflare" --success="apachectl -k graceful"
141-
142+
143+
### Examples (A real world example !!!)
144+
145+
In the following crontab entries, the first line downloads Spamhaus bogon IPv4 list daily at 03:15 AM, updates Ipset named "bogonlist", which is used by dtables (my firewall script), and only logs error output. [dtables](https://github.com/vkucukcakar/dtables)
146+
The second line downloads the Cloudflare IPv4 range, updates Ipset named "proxylist", which is used by the firewall. (There should be another line if we had IPv6 set support as IPv4 sets are not compatible with IPv6 sets.)
147+
The third line downloads the Cloudflare IP range, updates the server configuration and reloads Nginx with zero downtime by sending a HUP signal to the container by Docker.
148+
149+
15 3 * * * root /usr/local/bin/ip-list-updater.php --update --mode="ipset" --setname="bogonlist" --ipv=4 --output="/etc/bogonlist.txt" --sources="spamhaus" --success="ipset save bogonlist -f /etc/dtables/data/bogonlist.save" >/dev/null 2>/var/log/ip-list-updater.log
150+
45 3 * * * root /usr/local/bin/ip-list-updater.php --update --mode="ipset" --setname="proxylist" --ipv=4 --output="/etc/proxylist.txt" --sources="cloudflare" --success="ipset save proxylist -f /etc/dtables/data/proxylist.save" >/dev/null 2>/var/log/ip-list-updater.log
151+
30 3 * * * root /usr/local/bin/ip-list-updater.php --update --mode="nginx" --ipv=all --output="/lemp/configurations/cdn.conf" --sources="cloudflare" --success="docker kill --signal=HUP server-proxy" >/dev/null 2>/var/log/ip-list-updater.log
152+
142153
## Caveats
143154

144155
* In ipset mode, IPv4 and IPv6 sets have different structure and must be handled on separate lines with --ipv parameter.

ip-list-updater.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ class ip_list_updater {
2626
// Short name
2727
private static $app_name = "ip-list-updater";
2828
// Version
29-
private static $app_version = "1.0.0";
29+
private static $app_version = "1.1.0";
3030
// Description
3131
private static $app_description = "Automatic CDN and bogon IP list updater for firewall and server configurations";
32+
/*
3233
// PID file
3334
private static $pid_file = "/var/run/ip-list-updater.pid";
35+
*/
3436
// URLs of some pre-defined sources of bogon IP lists and CDN IP ranges
3537
public static $pd_sources = array(
3638

@@ -92,9 +94,11 @@ class ip_list_updater {
9294
/*
9395
* Shutdown callback
9496
*/
97+
/*
9598
static function shutdown() {
9699
unlink( self::$pid_file );
97100
}// function
101+
*/
98102

99103
/*
100104
* Custom error exit function that writes error string to STDERR and exits with 1
@@ -197,7 +201,7 @@ static function update() {
197201
// Read old list from file
198202
$old_list = @file_get_contents( self::$output );
199203
// Check current output file data to avoid overwriting arbitrary files or configuration data
200-
if ( false !== $old_list && !preg_match( '~(?:^\s*### ip-list-updater ###|^[\s\d\.:/]*$)~is', $old_list ) ){
204+
if ( false !== $old_list && !preg_match( '~(?:^\s*### ip-list-updater ###|^[\s\d\.:/a-f]*$)~is', $old_list ) ){
201205
self::error( "Error: Current output file contains unknown data. Please clear or delete output file after checking to avoid overwriting arbitrary files or configuration data.\n" );
202206
}
203207
// Replace keywords with real urls of pre-defined sources
@@ -482,6 +486,12 @@ static function run() {
482486
if ( version_compare( PHP_VERSION, '5.3.0', '<' ) ) {
483487
self::error( "Error: This application requires PHP 5.3.0 or later to run. PHP " . PHP_VERSION . " found. Please update PHP-CLI.\n" );
484488
}
489+
/*
490+
* Single instance check that I have implemented in predecessors of ip-list-updater is no longer required
491+
* as memory consumption is slightly decreased after some optimizations in array usage
492+
* and it also has disadvantages because of the multiple mode nature of the new script.
493+
*/
494+
/*
485495
// Single instance check
486496
$pid = @file_get_contents( self::$pid_file );
487497
if ( false !== $pid ) {
@@ -496,6 +506,7 @@ static function run() {
496506
}
497507
file_put_contents( self::$pid_file, getmypid() );
498508
register_shutdown_function( array( __CLASS__, 'shutdown' ) );
509+
*/
499510
// Load "openssl" required for file_get_contents() from "https"
500511
if ( ( !extension_loaded( 'openssl' ) ) && ( function_exists( 'dl' ) ) ) {
501512
dl( 'openssl.so' );

0 commit comments

Comments
 (0)