#use Apache2::RequestIO (); # 预加载的类库
use Apache2::RequestRec ();
use Apache2::Connection ();
use Apache2::RequestUtil ();
use Apache2::ServerUtil ();
use Apache2::Log ();
use Apache2::Request ();
1; # 不要丢了这个1,表示返回真给调用者
DLAuth2.pm内容类似如下:
package DLAuth2;
use strict;
use warnings;
use Socket qw(inet_aton);
use POSIX qw(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 $q = Apache2::Request->new($r);
my $s = Apache2::ServerUtil->server;
my $ip = $r->connection->remote_ip; # 获取访问者IP
my $fid = $q->param('fid') || get_fileid(); # 获取目标文件ID
my $ip_int = ip2int($ip); # 将IP转换成大整数
my $shareKey = $r->dir_config('ShareKey') || ''; # 从配置文件获取shareKey
my @passip = $r->dir_config->get('PassAuthIPs'); # 从配置文件获取白名单IP
my @passip_int; # 白名单IP数组
for (@passip) { # 将白名单IP转换成整数数组
if (/-/) {
my ($start,$end) = split/-/;
my $start_int = ip2int($start);
my $end_int = ip2int($end);
for (my $i=$start_int;$i<=$end_int;$i++) {
push @passip_int,$i;
}
} else {
push @passip_int, ip2int($_);
}
}
for (@passip_int) { # 如果请求IP位于白名单里,则允许访问
return Apache2::Const::OK if $ip_int == $_;
}
# 日期这里取的是当前天,为防止时间不一致,在我的产品程序里,时间设置比较宽松,当前天的前后2天都是可以的。
my $date = strftime("%Y%m%d",localtime);
# 基于相关条件产生验证串。
my $auth_string = generate_auth_string($ip_int, $fid, $date, $shareKey);
# 获取请求URL的验证串,该串由前台PHP程序产生
my $str = $q->param('a') || '';
if ($str eq $auth_string) {
return Apache2::Const::OK; # 如果2串一致,则允许访问