首页 > php教程 > PHP源码 > 正文

EasySite FireWall 防火墙模块

PHP中文网
发布: 2016-05-25 17:08:36
原创
1725人浏览过

easysite firewall 防火墙模块

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

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

$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['fwkey']) || isset($_COOKIE['es_admin_fwkey'])){
	$fwkey = isset($_GET['fwkey']) ? trim($_GET['fwkey']) : 
	(isset($_COOKIE['es_admin_fwkey']) ? $_COOKIE['es_admin_fwkey'] : '');
    if($fwkey === FW_ADMIN_KEY) $FW_DEFEND_IP_ON  = $FW_DEFEND_CC_ON  = false;
	setcookie('es_admin_fwkey', $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['whitelist']);
		foreach($MYFW_LIST as $ip){
			if(preg_match($ip, $client_ip)){
				$allowed = true;
				break;
			}
		}
		if(!$allowed){
			header('HTTP/1.1 403 Forbidden');
			exit('HTTP/1.1 403 ES FireWall Forbidden :  Not allowed IP');
		}
	}else{
		$MYFW_LIST = parse_ip_list($MYFW_LIST['blacklist']);
		foreach($MYFW_LIST as $ip){
			if(preg_match($ip, $client_ip)){
				header('HTTP/1.1 403 Forbidden');
				exit('HTTP/1.1 403 ES FireWall Forbidden :  Not allowed IP');
			}
		}
	}

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


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

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

	unset($nowtime, $lasttime, $fwtimes);
}
?>
登录后复制

 2. [PHP]代码

<?php

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

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

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

           

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

MacsMind
MacsMind

电商AI超级智能客服

MacsMind 131
查看详情 MacsMind

       

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
热门推荐
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号