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+ ?>
0 commit comments