2005-12-11 15:32:52 by: h4x0r

一段屏蔽通过代理服务器访问脚本的代码

Font Size: Large | Medium | Small
具体效果还没来得及测试…
使用的时候直接把此文件包含到要保护的文件的开头部分。
其他设置脚本里边的注释有说明。
<?php
 
/**
* 屏蔽通过代理访问脚本
* @author ToToDoDo <totododo(at)gmail[dot]com>
* @copyright Powered by NEATSTUDIO 2002 - 2005 <neatstudio(at)qq[dot]com>
* @link http://www.neatstudio.com NeatStudio
* Create 2005-11-26
*/
 
# 是否允许用户通过代理服务器访问?
define( 'NEAT_ALLOW_PROXY', FALSE );
 
# 是否记录通过代理服务器访问用户的真实IP地址?
define( 'NEAT_RECORD_REAL_IP', TRUE );
 
# 记录通过代理服务器访问用户真实IP地址的日志文件保存的路径(只有当NEAT_RECORD_REAL_IP为TRUE时本设置有效)
define( 'NEAT_RECORD_REAL_IP_PATH', '/records' );
 
# 服务器上保存的单个日志文件的大小
define( 'NEAT_LOG_MAX_SIZE', '102400' );
 
# 作为判断是否通过代理服务器访问的依据
$NEATProxyEnvArray = array(
 0 => 'HTTP_X_FORWARDED_FOR',
 1 => 'HTTP_X_FORWARDED',
 2 => 'HTTP_FORWARDED_FOR',
 3 => 'HTTP_FORWARDED',
 4 => 'HTTP_PRAGMA',
 5 => 'HTTP_VIA',
 6 => 'HTTP_XROXY_CONNECTION',
 7 => 'HTTP_X_COMING_FROM',
 8 => 'HTTP_COMING_FROM'
 );
 
// 错误提示的模板文件
$NEATTemplate = "<html>
<head>
<title>{NEATTitle}</title>
</head>
<body bgcolor=\"#FFFFFF\">
<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"700\" align=\"center\" height=\"85%\">
 <tr align=\"center\" valign=\"middle\">
 <td>
 <table cellpadding=\"10\" cellspacing=\"0\" border=\"0\" width=\"80%\" align=\"center\" style=\"font-family: Verdana, Tahoma; color: #666666; font-size: 11px\">
 <tr>
 <td valign=\"middle\" align=\"center\" bgcolor=\"#EBEBEB\">
 <br><b style=\"font-size: 16px\">{NEATTitle}</b>
 <br><br>{NEATContent}
 <br><br>
 </td>
 </tr>
 </table>
 </td>
 </tr>
</table>
</body>
</html>";
 
// 是否需要对通过代理访问用户执行操作?
if ( NEAT_ALLOW_PROXY == FALSE || NEAT_RECORD_REAL_IP == TRUE )
{
 foreach ( $NEATProxyEnvArray as $NEATKey => $NEATValue )
 {
 if ( NEATGetVar( $NEATValue ) )
 {
 // 如果需要记录通过代理访问的用户真实IP地址则开始记录到日志文件
 if ( NEAT_RECORD_REAL_IP == TRUE )
 {
 // 获取代理用户的真实IP地址
 if ( $NEATClientIP = NEATGetVar( 'HTTP_CLIENT_IP' ) )
 $NEATIPArray[] = $NEATClientIP;
 if ( $NEATXFF = NEATGetVar( 'HTTP_X_FORWARDED_FOR' ) )
 $NEATIPArray[] = explode( ', ', $NEATXFF );
 
 // 依次判断数组中IP地址是否为内网IP地址
 foreach ( $NEATIPArray as $NEATKey => $NEATValue )
 {
 if ( !eregi( "^(10|172\.16|192\.168)\.", $NEATValue ) )
 {
 $NEATRealIP = $NEATValue;
 break;
 }
 }
 
 // 如果通过代理方式获取不到IP则直接使用REMOTE_ADDR
 $NEATRealIP = $NEATRealIP ? $NEATRealIP : NEATGetVar( 'REMOTE_ADDR' );
 
 // 开始记录代理访问用户真实IP地址到本地日志文件
 if ( $NEATRealIP )
 {
 // 取得日志保存路径
 $NEATRecordPath = NEATGetPath( time() );
 
 // 判断路径是否可写
 if ( !is_writeable( $NEATRecordPath ) )
 {
 $NeatTitle = 'No Permission!';
 $NeatContent = "Can't write to the record path...";
 NEATOutput( $NeatTitle, $NeatContent );
 }
 else
 {
 // 判断日志记录文件是否存在
 if ( @file_exists( $NEATRecordPath . 'info.log' ) )
 {
 // 取得日志记录文件中的日志文件序列
 $NEATLogCount = NEATReadFromFile( $NEATRecordPath . 'info.log' );
 
 // 判断日志文件是否超过规定大小
 if ( @filesize( $NEATRecordPath . $NEATLogCount . '.log' ) > NEAT_LOG_MAX_SIZE )
 {
 // 如果大于规定大小则创建一个新的文件
 NEATWriteToFile( $NEATRecordPath . (string)($NEATLogCount + 1) . '.log', $NEATRealIP . '|' . date( 'Ymd-H:i:s' ) . "\n" );
 NEATWriteToFile( $NEATRecordPath . 'info.log', (string)( (int)NEATReadFromFile( $NEATRecordPath . 'info.log' ) + 1 ) );
 }
 else
 // 增加一行访问记录
 NEATAddTOFile( $NEATRecordPath . (string)($NEATLogCount) . '.log', $NEATRealIP . '|' . date( 'Ymd-H:i:s' ) . "\n" );
 }
 else
 {
 // 创建记录文件
 NEATWriteToFile( $NEATRecordPath . 'info.log', '1' );
 NEATWriteToFile( $NEATRecordPath . '1.log', $NEATRealIP . '|' . date( 'Ymd-H:i:s' ) . "\n" );
 }
 }
 }
 }
 
 // 如果禁止使用代理访问则直接退出
 if ( NEAT_ALLOW_PROXY == FALSE )
 {
 $NeatTitle = 'Forbidden!';
 $NeatContent = 'You are not allowed accessing via a proxy ...';
 NEATOutput( $NeatTitle, $NeatContent );
 }
 }
 }
}
 
# 获取环境变量
function NEATGetVar( $NEATVarType )
{
 if ( isset( $_SERVER[$NEATVarType] ) )
 return $_SERVER[$NEATVarType];
 elseif ( isset( $_ENV[$NEATVarType] ) )
 return $_ENV[$NEATVarType];
 else
 return getenv( $NEATVarType );
}
 
# 按日期创建日志存放目录
function NEATGetPath( $NEATTimeStamp )
{
 // 根据UNIX时间戳获取当前要创建的目录
 $NEATYear = date( 'Y', $NEATTimeStamp );
 $NEATMonth = date( 'n', $NEATTimeStamp );
 $NEATDay = date( 'j', $NEATTimeStamp );
 
 // 按 "年/月/日/" 目录结构创建目录
 $NEATDatePath = NEAT_RECORD_REAL_IP_PATH . '/' . $NEATYear;
 if ( ! @file_exists( $NEATDatePath ) )
 mkdir( $NEATDatePath );
 $NEATDatePath .= '/' . $NEATMonth;
 if ( ! @file_exists( $NEATDatePath ) )
 mkdir( $NEATDatePath );
 $NEATDatePath .= '/' . $NEATDay;
 if ( ! @file_exists( $NEATDatePath ) )
 mkdir( $NEATDatePath );
 $NEATDatePath .= '/';
 
 return $NEATDatePath;
}
 
# 从文件读取内容
function NEATReadFromFile( $NEATFile )
{
 if ( $NEATfp = @fopen( $NEATRecordPath, 'rb' ) )
 {
 @flock( $NEATfp, LOCK_SH );
 $NEATReadData = fread( $NEATfp, @filesize( $NEATFile ) );
 fclose( $NEATfp );
 return $NEATReadData;
 }
}
 
# 向文件写入内容
function NEATWriteToFile( $NEATFile, $NEATData )
{
 @touch( $NEATFile );
 $NEATfp = @fopen( $NEATFile, 'rb+' );
 @flock( $NEATfp, LOCK_EX );
 @fwrite( $NEATfp, $NEATData );
 @ftruncate( $NEATfp, strlen( $NEATData ) );
 @fclose( $NEATfp );
}
 
# 向文件末尾添加内容
function NEATAddTOFile( $NEATFile, $NEATData )
{
 @touch( $NEATFile );
 $NEATfp = @fopen( $NEATFile, 'ab' );
 @flock( $NEATfp, LOCK_EX );
 @fwrite( $NEATfp, $NEATData );
 @fclose( $NEATfp );
}
 
# 输出错误提示并退出程序
function NEATOutput( $title, $content )
{
 global $NEATTemplate;
 
 // 对模板标签进行替换
 $outputs = str_replace( '{NEATTitle}', $title, $NEATTemplate );
 $outputs = str_replace( '{NEATContent}', $content, $outputs );
 
 // 退出并显示错误提示
 die( $outputs );
}
 
?>
Comments Feed Comments Feed: http://www.4evil.org/feed.asp?q=comment&id=345

There is no comment on this article.

Post Comment
Smilies
[smile] [confused] [cool] [cry]
[eek] [angry] [wink] [sweat]
[lol] [stun] [razz] [redface]
[rolleyes] [sad] [yes] [no]
[heart] [star] [music] [idea]
Enable UBB Codes
Auto Convert URL
Show Smilies
Hidden Comment
Username:   Password:   Register Now?
Security Code * Please Enter the Security Code