【Anychat音视频开发】apache防盗链之mod_perl
客戶需求如下:
在web請求視頻時,按算法生成密文和明文串,然后依規則組成最終的url請求;
算法規則——用如下三個關鍵詞生成MD5密文:
1、自定義密鑰:abcde.;
2、視頻文件真實路徑,即/path/to/file.rmvb;
3、請求時間,以當前UNIX時間換算為十六進制字符串,并作為明文;
最終url格式是http://www.test.com/path/to/file.rmvb?key=1234567890abcdefghijklmnopqrstuy&t=1234abcd這樣。
要求失效時間為8小時。
這個需求和之前一次相當類似,不過上回是squid,這次是apache。同樣采用perl腳本進行防盜鏈設置,apache需要使用mod_perl模塊。
首先安裝perl模塊:
wget http://perl.apache.org/dist/mod_perl-2.0-current.tar.gz
tar zxvf mod_perl-2.0-current.tar.gz
cd mod_perl-2.0-current.tar.gz
perl Makefile.PL MP_APXS=/home/apache2/bin/apxs
make && make install
echo "LoadModule perl_module modules/mod_perl.so" >> /home/apache2/conf/httpd.conf
perl -MCPAN -e shell
>install Apache2::Request
>look Apache2::Request
rm -f configure
rm -f apreq2-config
./buildconf
perl Makefile.PL
make && make install
exit
(因為64位系統的libexpat.so有問題,編譯libapreq2會出問題,只好如此強制安裝)
echo "LoadModule apreq_module modules/mod_apreq2.so" >> /home/apache2/conf/httpd.conf
因為libapreq2.so安裝在/home/apache2/lib/下了,所以需要echo "/home/apache2/lib">/etc/lo.so.conf.d/apache.conf,然后ldconfig。
修改httpd.conf,加入如下設置:
PerlPostConfigRequire /home/apache2/perl/start.pl
<Location /smg>
SetHandler modperl
PerlAccessHandler DLAuth
PerlSetVar ShareKey abcde.
</Location>
然后mkdir /home/apache2/perl/,在其中創建start.pl和DLAuth.pm兩個文件。start.pl文件內容如下:
use strict;
use lib qw(/home/apache2/perl);
use Apache2::RequestIO ();
use Apache2::RequestRec ();
use Apache2::Connection ();
use Apache2::RequestUtil ();
use Apache2::ServerUtil ();
use Apache2::Log ();
use Apache2::Request ();
1;
DLAuth.pm文件內容如下:
package DLAuth;
use strict;
use warnings;
use Socket qw(inet_aton);
use POSIX qw(difftime strftime);
use Digest::MD5 qw(md5_hex);
use Apache2::RequestIO ();
use Apache2::RequestRec ();
use Apache2::Connection ();
use Apache2::RequestUtil ();
use Apache2::ServerUtil ();
use Apache2::Log ();
use Apache2::Request ();
use Apache2::Const -compile => qw(OK FORBIDDEN);
sub handler {
??? my $r = shift;
??? my $s = Apache2::ServerUtil->server;
??? my $shareKey = $r->dir_config('ShareKey') || '';
??? my $uri = $r->uri() || '';
??? my $args = $r->args() || '';
??? my $expire = 8 * 3600;
??? if ($args =~ m#^key=(\w{32})\&t=(\w{8})$#i){
??? my ($key, $date) = ($1, $2);
??? my $str = md5_hex($shareKey . $uri . $date)
??? my $reqtime = hex($date);
??? my $now = time;
??? if ( $now - $reqtime < $expire){
??? ??? if ($str eq $key) {
??? ??? ??? return Apache2::Const::OK;
??? ??? } else {
??????? ??? ??? $s->log_error("[$uri FORBIDDEN] Auth failed");
??? ??? ??? return Apache2::Const::FORBIDDEN;
??? ??? }
??? }
??? }
??? $s->log_error("[$uri FORBIDDEN] Auth failed");
??? return Apache2::Const::FORBIDDEN;
}
1;
就可以了。
apachectl restart。測試一下,先用perl自己生成一個測試鏈接:
#!/usr/bin/perl -w
use Digest::MD5 qw(md5_hex);
my $key = "bestv.";
$path = shift(@ARGV);
my $date = sprintf("%x",time);
$result = md5_hex($key . $path . $date);
my $uri = "http://127.0.0.1$path\?key=$result\&t=$date";
print $uri;
運行./url.pl /smg/abc.rmvb生成http://127.0.0.1/smg/abc.rmvb?key=4fb6b4e6a0ec484aea98fa727fc7149d&t=4bc7dd5a,然后wget -S -O /dev/null "http://127.0.0.1/smg/abc.rmvb?key=4fb6b4e6a0ec484aea98fa727fc7149d&t=4bc7dd5a",返回200 OK;任意修改t為12345678,再wget,返回403 Forbidden。error_log顯示如下:
[Fri Apr 16 11:47:06 2010] [error] [/smg/abc.rmvbkey=4fb6b4e6a0ec484aea98fa727fc7149d&t=12345678 FORBIDDEN] Auth failed
?
Anychat專業即時通訊方案,專注六年的研發。能夠給你提供最高清實時的音視頻即時通訊。
如有需要下載體驗可以訪問http://www.bairuitech.com/
詳細了解可以致電020-38109065/ 020-38103410或者加Q1816573263。
轉載于:https://blog.51cto.com/6278039/1103014
總結
以上是生活随笔為你收集整理的【Anychat音视频开发】apache防盗链之mod_perl的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 清除应用程序所有缓存
- 下一篇: 高性能分布式计算与存储系统设计概要——暨