原發於http://www.linux-ch.com/post-25.html
現有一檔案分享系統,利用nginx平臺直接開啟目錄功能,可自由瀏覽並選擇檔案進行下載.同時開通ftp,可以上傳檔案與大家分享.這一切實現起 來並不困難,但在一段時間後發現分享系統的流量較高,後通過分析日誌發現竟然是來自各地迅雷的連結.在這之後我曾將迅雷的agent給過濾掉,效果還不 錯,但隨之而來的問題是:平時我們也在使用迅雷,對於小檔案沒什麼影響,但對於動不動就上G的檔案,對於瀏覽器的下載功能是個嚴酷的考驗. 現在利用ngx_http_accesskey_module模組,並配合php,一個能防止下載工具我連結分享功能的檔案分享系統復活了~
nginx的編譯配置請參考http://wiki.nginx.org/NginxHttpAccessKeyModule
下面分享下關於我的配置:
nginx:
# autoindex on;
# autoindex_localtime on;
# autoindex_exact_size off;
accesskey on;
accesskey_hashmethod md5;
accesskey_arg “key”;
accesskey_signature “mypass$remote_addr”;
}
php原始碼:
$downfile=$_GET[“downfile”];
$dir0=`/web/html/downloads`;
$dir1=str_replace(`..`,“,$_GET[`dir1`]);
$dir=$dir0.$dir1;
if ($downfile) {
$filename =basename($downfile);
$file=fopen($dir.”/”.$filename,”rb”);
Header(“Content-type: application/octet-stream”);
Header(“Accept-Ranges: bytes”);
Header(“Accept-Length: “.filesize($dir.”/”.$filenamee));
Header(“Content-Disposition: p_w_upload; filename=”.$filename);
echo fread($file,filesize($dir.”/”.$filename));
fclose($file);
exit;
}
?>
<title>SZA</title>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″;>
<STYLE type=”text/css”>
body {font-family: “sans-serif”, “serif”; font-size: 12px;}
BODY { background-color:#A2B5CD }
a:link {color: #BFEFFF; text-decoration: none}
a:visited {color: #080808; text-decoration: none}
a:hover {color: #FFFFFF; text-decoration: underline}
input {font-family: “sans-serif”, “serif”;font-size: 12px;}
td {font-family: “sans-serif”, “Verdana”; font-size: 12px;}
.title {font-family: “Verdana”, “Tahoma”;font-size: 20px;font-weight: bold; color=black}
</STYLE>
</head>
<body>
<table width=”100%” cellspacing=”1″ cellpadding=”3″>
<tr>
<td class=”title” align=”center”>神州IT架構檔案分享系統</td>
</tr>
<tr><td align=”right”>上傳請轉至ftp,帳號:ftp 密碼:ftpkey </td></tr>
</table>
<hr>
<table width=”100%” cellspacing=”1″ cellpadding=”3″>
<?php
$ipkey= md5(“mypass”.$_SERVER[`REMOTE_ADDR`]);
echo `<form action=”” method=”post”>`;
echo `<tr>`;
echo `<td>例如你現在的一個連結是:”https://www.linux-ch.com/downloads/iso/rhel- server-6.0-i386-dvd.iso<font color=”red”>?key=d85b4aa593bkdkfia450ce6a55766e6b”</font></td>`;
echo `<tr><td>只要將後面的”<font color=”red”>?key=d85b4aa593bkdkfia450ce6a55766e6b</font>”刪除掉,再提 交到下面進行更新>就行了</td></tr>`;
echo `<td>`;
echo “輸入需要更新的連結:”;
echo `<input type=”text” name=”url” style=”font-family:Verdana,Arial; font-size: 9pt; width:60%”>`;
echo `<input type=”submit” value=”更新” style=”font-family:Verdana,Arial; font-size: 9pt;background-color:#A2B5CD “>`;
echo `</td>`;
echo `</tr>`;
echo `<tr>`;
$durl=$_POST[`url`];
if (!isset($durl) or empty($durl)) {
echo ” <td> </td></tr>
“;
} else {
echo “<tr><td>新的下載地址是: </td></tr>
“;
echo “<tr><td><a href=””.($durl).”?key=”.$ipkey.””>”.($durl).”?key=”.$ipkey.” </a></td></tr>
“;
}
echo “</tr>
“;
echo `</form>`;
echo `<tr>`;
echo `<td><b>`;
echo “檔案列表:”;
echo `</b></td>`;
echo `</tr>`;
?>
</table>
<table width=”100%” border=”0″ cellpadding=”3″ cellspacing=”1″>
<?php
$dirs=@opendir($dir);
while ($file=@readdir($dirs)) {
$b=”$dir/$file”;
$a=@is_dir($b);
if($a==”1″){
if($file!=”..”&&$file!=”.”) {
echo “<tr>
“;
echo ” <td><a href=”?dir1=”.rawurlencode($dir1).”/”.rawurlencode($file).””>$file</a></td>
“;
echo “</tr>
“;
} else {
echo ”
“;
}
}
}
@closedir($dirs);
$dirs=@opendir($dir);
while ($file=@readdir($dirs)) {
$b=”$dir/$file”;
$a=@is_dir($b);
if($a==”0″){
// $size=@filesize(“$dir/$file”);
$size = exec (`stat -c %s `. escapeshellarg (“$dir/$file”));
if($size >= 1073741824) {
$size = round($size / 1073741824 * 100) / 100 . ” GB”;
} elseif($size >= 1048576) {
$size = round($size / 1048576 * 100) / 100 . ” MB”;
} elseif($size >= 1024) {
$size = round($size / 1024 * 100) / 100 . ” KB”;
} else {
$size = $size . ” B”;
}
// $lastsave=@date(“Y-n-d H:i:s”,filemtime(“$dir/$file”));
$lastsave=@date(“Y-n-d H:i:s”,exec(`date +%s -r`. escapeshellarg (“$dir/$file”)));
echo “<tr>
“;
echo “<td>$file</td>
“;
echo “<td>$lastsave</td>
“;
echo “<td>$size</td>
“;
echo “<td><a href=” https://www.linux-ch.com/downloads”.rawurlencode($dir1).” /”.rawurlencode($file).”?key=”.$ipkey.””>[下載]</a></td>
“;
echo “</tr>
“;
}
}
@closedir($dirs);
?>
</table>
</body>
</html>
原始碼原來是使用urlencode這個引數對連結進行轉換的,遇到空格就出問題,後改用rawurlencode將問題解決.
php的原始碼是在網上找回來修改加工的,在此我要感謝3t,有他的幫助我才能改好這些檔案,當然也要感謝將原始碼釋出出來前輩,這樣我才有機會去修改這些,謝謝.