Skip to content

Commit 77f67a3

Browse files
committed
added new blocklist plugin
1 parent 0f40d22 commit 77f67a3

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

src/Plugin/BlockListPlugin.php

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
use Proxy\Plugin\AbstractPlugin;
4+
use Proxy\Event\ProxyEvent;
5+
use Proxy\Config;
6+
7+
// https://proxylist.hidemyass.com/upload/
8+
// TODO: this file is not found to be existant in ./plugins/ when namespace is specified
9+
class BlockListPlugin extends AbstractPlugin {
10+
11+
function onBeforeRequest(ProxyEvent $event){
12+
13+
$user_ip = $_SERVER['REMOTE_ADDR'];
14+
$user_ip_long = sprintf('%u', ip2long($user_ip));
15+
16+
$url = $event['request']->getUrl();
17+
$url_host = parse_url($url, PHP_URL_HOST);
18+
19+
$fnc_custom = Config::get('blocklist.custom');
20+
if(is_callable($fnc_custom)){
21+
22+
$ret = call_user_func($fnc_custom, compact('user_ip', 'user_ip_long', 'url', 'url_host') );
23+
if(!$ret){
24+
throw new \Exception("Error: Access Denied!");
25+
}
26+
27+
return;
28+
}
29+
30+
/*
31+
1. Wildcard format: 1.2.3.*
32+
2. CIDR format: 1.2.3/24 OR 1.2.3.4/255.255.255.0
33+
3. Start-End IP format: 1.2.3.0-1.2.3.255
34+
*/
35+
$ip_match = false;
36+
$action_block = true;
37+
38+
if(Config::has('blocklist.ip_allow')){
39+
$ip_match = Config::get('blocklist.ip_allow');
40+
$action_block = false;
41+
} else if(Config::has('blocklist.ip_block')){
42+
$ip_match = Config::get('blocklist.ip_block');
43+
}
44+
45+
if($ip_match){
46+
$m = re_match($ip_match, $user_ip);
47+
48+
// ip matched and we are in block_mode
49+
// ip NOT matched and we are in allow mode
50+
if( ($m && $action_block) || (!$m && !$action_block)){
51+
throw new \Exception("Error: Access denied!");
52+
}
53+
}
54+
}
55+
}
56+
57+
?>

src/helpers.php

+11
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@ function in_arrayi($needle, $haystack){
1515
return in_array(strtolower($needle), array_map('strtolower', $haystack));
1616
}
1717

18+
function re_match($pattern, $string){
19+
20+
$quoted = preg_quote($pattern, '#');
21+
$translated = strtr($quoted, array(
22+
'\*' => '.*',
23+
'\?' => '.'
24+
));
25+
26+
return preg_match("#^".$translated."$#i", $string) === 1;
27+
}
28+
1829
// regular array_merge does not work if arrays have numeric keys...
1930
function array_merge_custom(){
2031

0 commit comments

Comments
 (0)