对于Flash,WindowsMedia视频这种占用流量较大的服务无法实现防盗链,对一个依靠这类内容作为盈利点的网站来说是非常头疼的,俺通过一些研究以及测试实现了采用Cookie技术的防盗链解决方案,完美的解决了对Flash,WindowsMedia视频的防盗链。
首先发现虽然ActiveX插件不传递Referer,但是却忠实的传递Cookie。于是在显示ActiveX的页面的<head> </head>标签内嵌入一段代码:
external_acl_type download children=15 %{Cookie} /usr/local/squid/libexec/squid_cookie.pl
acl filetype url_regex -i \.wmv
acl filetype url_regex -i \.wma
acl filetype url_regex -i \.asf
acl filetype url_regex -i \.asx
acl filetype url_regex -i \.avi
acl filetype url_regex -i \.mp3
acl filetype url_regex -i \.smi
acl filetype url_regex -i \.rm
acl filetype url_regex -i \.ram
acl filetype url_regex -i \.rmvb
acl filetype url_regex -i \.swf
acl filetype url_regex -i \.mpg
acl filetype url_regex -i \.mpeg
acl filetype url_regex -i \.mov
acl filetype url_regex -i \.zip
acl filetype url_regex -i \.mid
如果仅仅只是禁止用户访问的话,就没意思了,要让盗链者帮我们宣传我们的网站,特别是发现盗链比较多的时候,这个时候,可以让任何盗链的网站帮我们免费宣传~~~那就是把盗链的url重定向到我们的网站宣传页~~
建立脚本:/usr/local/squid/libexec/squid_redir.pl
#
# Description: Direct all request to files who are in a local dir to
# this directory
#
use File::Basename;
use URI::URL;
# flush after every print
$| = 1;
# Process lines of the form 'URL ip-address/fqdn ident method'
# See release notes of Squid 1.1 for details
while ( <> ) {
$r302=0;
($url, $addr, $fqdn, $ident, $method) = m:(\S*) (\S*)/(\S*) (\S*) (\S*):;
$url = url $url;
$host = lc($url->host);
if ( $host !~ /\./ ) {
next;
}
if ( $host =~ /vod\.domain\.com/ ) {
$url->path("/ad.wmv");
$r302=1;
}
} continue {
if ( $r302 ) {
print "302:$url\n";
} else {
print "$url $addr/$fqdn $ident $method\n";
}
}
--------------------------
然后在squid.conf添加:
redirect_program /usr/local/squid/libexec/squid_redir.pl
redirect_children 5
acl superurl url_regex -i ^http://vod\.domain\.com/tom\.wmv$
redirector_access deny superurl
redirector_access allow filetype !dl
redirector_access deny all
设置superurl是因为宣传我们自己站点的视频是不做防盗链的,这样才能起到宣传的作用。现在大功告成啦!网站的流量大幅增加~~~PV是原来的三倍,Oh,Yeah~
WMS视频的MMS协议由于不是明文,无法实现防盗链,但是RTSP协议基本就是HTTP协议的变种,可以在BIGIP等可以进行Layer 7处理数据的设备上实现对Cookie的校验。但是WMS视频防盗链要禁止MMS协议,因为MMS协议不是明文,无法进行cookies的识别。但是可以在HTTP,RTSP上进行识别,WMS V9都是支持HTTP,RTSP的。下面是用于BIGIP的防盗链iRule:
--------------------------
if (http_uri matches_regex "vod.domain.com") {
if (http_cookie("Cache") == "vod") {
use pool vod-rtsp
}
else {
discard
}
}
else {
use pool vod-rtsp
}
--------------------------
其他的系统可以参考以上的规则。
采用Cookie的防盗链方式对于大多数的系统都可以轻易实现,Cookie一般都是用来传递认证信息,相信不会出现不传递的状况。