nginx 整合 ngx_http_accesskey_module 模塊的應用

原發於http://www.linux-ch.com/post-25.htmlphp

現有一文件分享系統,利用nginx平臺直接打開目錄功能,可自由瀏覽並選擇文件進行下載.同時開通ftp,能夠上傳文件與你們分享.這一切實現起 來並不困難,但在一段時間後發現分享系統的流量較高,後經過分析日誌發現居然是來自各地迅雷的連接.在這以後我曾將迅雷的agent給過濾掉,效果還不 錯,但隨之而來的問題是:平時咱們也在使用迅雷,對於小文件沒什麼影響,但對於動不動就上G的文件,對於瀏覽器的下載功能是個嚴酷的考驗. 如今利用ngx_http_accesskey_module模塊,並配合php,一個能防止下載工具我連接分享功能的文件分享系統復活了~css

    nginx的編譯配置請參考http://wiki.nginx.org/NginxHttpAccessKeyModulehtml

    下面分享下關於個人配置:linux

     nginx:nginx

        location /downloads {
#                autoindex on;
#                autoindex_localtime on;
#                autoindex_exact_size off;
                accesskey             on;
                accesskey_hashmethod  md5;
                accesskey_arg         "key";
                accesskey_signature   "mypass$remote_addr";
        }
 

 

php源碼:web

<?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>\n";
        } else {
                echo "<tr><td>新的下載地址是: </td></tr>\n";
                echo "<tr><td><a href=\"".($durl)."?key=".$ipkey."\">".($durl)."?key=".$ipkey." </a></td></tr>\n";
        }
        echo "</tr>\n";
        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>\n";
                        echo "  <td><a href=\"?dir1=".rawurlencode($dir1)."/".rawurlencode($file)."\">$file</a></td>\n";
                        echo "</tr>\n";
                } else {
                        echo "\n";
                }
        }
}
@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>\n";
        echo "<td>$file</td>\n";
        echo "<td>$lastsave</td>\n";
        echo "<td>$size</td>\n";
        echo "<td><a href=\" https://www.linux-ch.com/downloads".rawurlencode($dir1)." /".rawurlencode($file)."?key=".$ipkey."\">[下載]</a></td> \n";
        echo "</tr>\n";
        }
}
@closedir($dirs);
?>
</table>
</body>
</html>

    源碼原來是使用urlencode這個參數對連接進行轉換的,遇到空格就出問題,後改用rawurlencode將問題解決.shell

    php的源碼是在網上找回來修改加工的,在此我要感謝3t,有他的幫助我才能改好這些文件,固然也要感謝將源碼發佈出來前輩,這樣我纔有機會去修改這些,謝謝.瀏覽器

相關文章
相關標籤/搜索