首頁 php教程 PHP源码 EasySite FireWall 防火墙模块

EasySite FireWall 防火墙模块

May 25, 2016 pm 05:08 PM

EasySite FireWall 防火墙模块

<?php
/**
	EasySite FireWall 防火墙模块
	13:25 2012/7/23
*/

define(&#39;FW_ADMIN_KEY&#39;,   &#39;21232f297a57a5a743894a0e4a801fc3&#39;);  // 超级管理员密钥
define(&#39;FW_IP_RULE_FILE&#39;, APP_PATH.&#39;Runtime/Conf/Config.Iprule.php&#39;);

$FW_DEFEND_IP_ON = false; 	// 开启IP规则过滤
$FW_DEFEND_IP_TP = 1; 	  	// 开设置IP过滤模式 0-IP黑名单过滤  1-IP白名单过滤
$FW_DEFEND_CC_ON = false; 	// 开启防恶意刷新
$FW_DEFEND_CC_TL = 5; 		// 每五次请求最小间隔时间/S

if(isset($_GET[&#39;fwkey&#39;]) || isset($_COOKIE[&#39;es_admin_fwkey&#39;])){
	$fwkey = isset($_GET[&#39;fwkey&#39;]) ? trim($_GET[&#39;fwkey&#39;]) : 
	(isset($_COOKIE[&#39;es_admin_fwkey&#39;]) ? $_COOKIE[&#39;es_admin_fwkey&#39;] : &#39;&#39;);
    if($fwkey === FW_ADMIN_KEY) $FW_DEFEND_IP_ON  = $FW_DEFEND_CC_ON  = false;
	setcookie(&#39;es_admin_fwkey&#39;, $fwkey, time()+3600*24, SITE_PATH);
}

if(true === $FW_DEFEND_IP_ON){
	$client_ip = get_client_ip2();
	$MYFW_LIST = (include FW_IP_RULE_FILE);

	if(1 === $FW_DEFEND_IP_TP){
		$allowed = false;
		$MYFW_LIST = parse_ip_list($MYFW_LIST[&#39;whitelist&#39;]);
		foreach($MYFW_LIST as $ip){
			if(preg_match($ip, $client_ip)){
				$allowed = true;
				break;
			}
		}
		if(!$allowed){
			header(&#39;HTTP/1.1 403 Forbidden&#39;);
			exit(&#39;HTTP/1.1 403 ES FireWall Forbidden :  Not allowed IP&#39;);
		}
	}else{
		$MYFW_LIST = parse_ip_list($MYFW_LIST[&#39;blacklist&#39;]);
		foreach($MYFW_LIST as $ip){
			if(preg_match($ip, $client_ip)){
				header(&#39;HTTP/1.1 403 Forbidden&#39;);
				exit(&#39;HTTP/1.1 403 ES FireWall Forbidden :  Not allowed IP&#39;);
			}
		}
	}

	unset($allowed, $client_ip, $MYFW_LIST);
}


if(true === $FW_DEFEND_CC_ON){
	if(!session_id()) session_start();

	$nowtime = $lasttime = $_SERVER[&#39;REQUEST_TIME&#39;];
	if(isset($_SESSION[&#39;FireWall&#39;])){
		$lasttime = intval($_SESSION[&#39;FireWall&#39;][&#39;lasttime&#39;]);
$fwtimes  = intval($_SESSION[&#39;FireWall&#39;][&#39;fwtimes&#39;]) + 
(isset($_SERVER[&#39;HTTP_X_REQUESTED_WITH&#39;]) ? 0 : 1);
		$_SESSION[&#39;FireWall&#39;][&#39;fwtimes&#39;] = $fwtimes;
		
		
		if(($nowtime - $lasttime) < $FW_DEFEND_CC_TL){
			if($fwtimes >= 5){
				header(&#39;HTTP/1.1 403 Forbidden&#39;);
				$_SESSION[&#39;FireWall&#39;][&#39;lasttime&#39;] = $nowtime;
				exit(&#39;HTTP/1.1 403 ES FireWall Forbidden :  Not allowed CC&#39;);
			}
		}else{
			$_SESSION[&#39;FireWall&#39;][&#39;fwtimes&#39;]  = 0;
			$_SESSION[&#39;FireWall&#39;][&#39;lasttime&#39;] = $nowtime;
		}
	
	}else{
		$_SESSION[&#39;FireWall&#39;][&#39;fwtimes&#39;]  = 1;
		$_SESSION[&#39;FireWall&#39;][&#39;lasttime&#39;] = $nowtime;
	}

	unset($nowtime, $lasttime, $fwtimes);
}
?>
登入後複製

2. [PHP]代码

<?php

/**
 * 获取客户端IP
 * @param  void
 * @return String 客户端IP
 */
function get_client_ip2(){
	if(getenv(&#39;HTTP_CLIENT_IP&#39;)){
		$client_ip = getenv(&#39;HTTP_CLIENT_IP&#39;);
	}elseif(getenv(&#39;HTTP_X_FORWARDED_FOR&#39;)){
		$client_ip = getenv(&#39;HTTP_X_FORWARDED_FOR&#39;);
	}elseif(getenv(&#39;REMOTE_ADDR&#39;)) {
		$client_ip = getenv(&#39;REMOTE_ADDR&#39;);
	}else{
		$client_ip = $HTTP_SERVER_VARS[&#39;REMOTE_ADDR&#39;];
	}
	return $client_ip;
}

/**
 * 解析IP规则列表
 * @param  void
 * @return Array IP规则列表
 */
function parse_ip_list($rules){
	$arr = array();
	foreach($rules as $rule){
		if($rule[&#39;start_time&#39;] > $_SERVER[&#39;REQUEST_TIME&#39;] || $rule[&#39;end_time&#39;] 
		< $_SERVER[&#39;REQUEST_TIME&#39;]) continue;

		$ip = str_replace(&#39;.&#39;, &#39;\.&#39;, $rule[&#39;ip&#39;]);
		if($start = strstr($ip, &#39;-&#39;)){
			$start = substr($ip, 0, - strlen(strrchr($ip, &#39;.&#39;)) + 1);
			$pos = explode(&#39;-&#39;, trim(strrchr($ip, &#39;.&#39;), &#39;.&#39;));
			for($i=intval($pos[0]),$a=intval($pos[1])+1; $i < $a; $i++ ){
				$arr[] = &#39;#^&#39;.$start.$i.&#39;$#i&#39;;
			}
		}elseif($start = strstr($ip, &#39;[&#39;)){
			$_ips  = explode(&#39;|&#39;, substr($start, 1, -1));
		$arr[] = &#39;#^&#39;.substr($ip, 0, - strlen($start)).&#39;((&#39;.implode(&#39;)|(&#39;,$_ips ).&#39;))&#39;.&#39;$#i&#39;;
		}elseif(strpos($ip, &#39;*&#39;)){
	$arr[] = &#39;#^&#39;.str_replace(&#39;*&#39;, &#39;((25[0-5])|(2[0-4]\\d)|(1\\d{2})|(\\d{1,2}))&#39;, $ip).&#39;$#i&#39;;
		}else{
			$arr[] = &#39;#^&#39;.$ip.&#39;$#i&#39;;
		}
	}
	return $arr;
}
?>
登入後複製

           

 以上就是EasySite FireWall 防火墙模块的内容,更多相关内容请关注PHP中文网(www.php.cn)!


       

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)