This repository has been archived by the owner on Sep 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
proxyblock.php
189 lines (156 loc) · 4.27 KB
/
proxyblock.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
/*
--------------------
Proxy Block Script
--------------------
Created by NewEraCracker
Date 2013/08/04
Version 1.1.0
Requirements:
= PHP 5.2 or higher
= MySQL 5 or higher
License: CC BY-SA 3.0
*/
function check_proxy()
{
/*---------------------
* Configuration start
*--------------------*/
// Database information
$db_hostname = 'localhost';
$db_database = 'proxydb';
$db_username = 'username';
$db_password = 'password';
$db_installed = false; // change to true after executing 1st time
// Ports to check
$check_ports = true;
$ports = array(3128,8080);
// Proxy headers
$check_headers = true;
$headers = array('HTTP_VIA', 'HTTP_X_FORWARDED_FOR', 'HTTP_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_FORWARDED', 'HTTP_CLIENT_IP', 'HTTP_FORWARDED_FOR_IP', 'VIA', 'X_FORWARDED_FOR', 'FORWARDED_FOR', 'X_FORWARDED', 'FORWARDED', 'CLIENT_IP', 'FORWARDED_FOR_IP', 'HTTP_PROXY_CONNECTION');
// Banned
$banned_ips = array('193.200.150.');
$banned_useragents = array();
// Allowed
$allowed_ips = array('127.0.0.');
$allowed_useragents = array('Googlebot','msnbot','Slurp');
// Notes:
// You are able to ban/allow an IP range such as 1.0.0.0 -> 1.0.0.255
// by banning/allowing the IP "1.0.0."
/*---------------------
* Configuration end
*--------------------*/
// Init
error_reporting(0);
$userip = (string) $_SERVER['REMOTE_ADDR'];
$useragent = (string) $_SERVER['HTTP_USER_AGENT'];
$proxy = false;
// Fix configuration
if(!$check_ports)
$ports = array();
if(!$check_headers)
$headers = array();
// Ban certain IPs
if( count($banned_ips) )
{
foreach($banned_ips as $ip)
{
$test = strpos($userip,$ip);
if($test !== false && $test == 0)
return true;
}
unset($ip);
}
// Ban certain User-Agents
if( count($banned_useragents) )
{
foreach($banned_useragents as $ua)
{
$test = strpos($useragent,$ua);
if($test !== false)
return true;
}
unset($ua);
}
// Allow certain IPs
if( count($allowed_ips) )
{
foreach($allowed_ips as $ip)
{
$test = strpos($userip,$ip);
if($test !== false && $test == 0)
return false;
}
unset($ip);
}
// Allow certain User-Agents
if( count($allowed_useragents) )
{
foreach($allowed_useragents as $ua)
{
$test = strpos($useragent,$ua);
if($test !== false)
return false;
}
unset($ua);
}
// Check for proxy
if( count($ports) || count($headers) )
{
// Connect and select database
$db_link = mysql_connect($db_hostname,$db_username,$db_password) or die(mysql_error());
mysql_select_db($db_database) or die(mysql_error());
$db_setup = 'CREATE TABLE IF NOT EXISTS `proxyblock` ( `ip` varchar(40) CHARACTER SET latin1 NOT NULL, `proxy` tinyint(1) unsigned NOT NULL, `time` DATETIME NOT NULL, UNIQUE KEY `ip` (`ip`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1;';
$db_query = sprintf( "SELECT * FROM `proxyblock` WHERE `ip`='%s'",mysql_real_escape_string($userip) );
// To select records created in the last 30 minutes
$db_query .= " AND `time` > DATE_SUB( NOW(), INTERVAL 30 MINUTE)";
// Has database been initialized?
if( !$db_installed )
mysql_query($db_setup) or die(mysql_error());
// Now query for the IP address
$db_result = mysql_query($db_query) or die(mysql_error());
// Have we found it?
while ($row = mysql_fetch_assoc($db_result))
{
// No need for a port scan or check for headers here
return $row['proxy'];
}
// Check for proxy headers
if( count($headers) )
{
foreach ($headers as $header)
{
if( isset($_SERVER[$header]) )
{
$proxy = true;
break;
}
}
}
// Do a port scan
if( !$proxy && count($ports) )
{
foreach($ports as $port)
{
if($test = @fsockopen($userip,$port,$errno,$errstr,0.5))
{
fclose($test);
$proxy = true;
break;
}
}
}
// Delete older result and insert new
$proxy = intval($proxy);
$db_delete_ip = sprintf( "DELETE FROM `proxyblock` WHERE `ip`='%s'",mysql_real_escape_string($userip) );
$db_insert_ip = sprintf( "INSERT INTO `proxyblock` VALUES ('%s','{$proxy}',NOW())",mysql_real_escape_string($userip) );
mysql_query($db_delete_ip) or die(mysql_error());
mysql_query($db_insert_ip) or die(mysql_error());
}
// Return result
return $proxy;
}
if( check_proxy() )
{
die("<title>403: Forbidden</title>Oops... A proxy");
}