403 AccessDenied
前不久一套代码里的样式突然全掉了,看了下控制台发现 css/js 和 图片文件全都403了
返回提示如下
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<Error>
<Code>AccessDenied</Code>
<Message>You are denied by bucket referer policy.</Message>
<RequestId>5E72380661ECA1559CCB04F6</RequestId>
<HostId>shepay.xxxx.com</HostId>
<BucketName>shepay</BucketName>
</Error>
看了下是阿里云的CDN,拒绝访问了,联系不到CDN的主人,甚至也来不及保存到本地了,相当难受。
怎么办
于是网上搜寻解决办法,发现并没有很多比较详细的提到如何绕过防盗链的,我真的只是想保存到本地而已~~
后来在一位老哥这里看到了相关的功能,但是实测了下可以部分绕过防盗链,但是只限于图片格式,而我需要的不仅仅是图片,还要有文件格式。
于是根据这位老哥的方法,做了修改和完善,图片和文字通过不同方式直接展示到浏览器,然后Command + S
保存即可。
测试
调用方式如下(链接随时可能失效,仅供演示):
https://guotianyu.cn/fdl.php?url= 要绕过防盗链的文件/图片链接
如https://guotianyu.cn/fdl.php?url=http://shepay.ayunx.com/user/js/popper.min.js
图片效果演示(上面是防盗链链接,下面是绕过防盗链的链接):
文字效果演示:
就当长个教训吧,特异性文件以后一定不放别人的CDN上...
当然啦,不经许可使用即为盗,这种方法还是适合应急用,这里还是不鼓励这种行为啦~
代码实现
最后附上修改后的代码:
<?php
class ImgBridge{
private $water='';
private $imgUrl='';
private $referer='';
private $ua='MQQBrowser/26 Mozilla/5.0 (Linux; U; Android 2.3.7; zh-cn; MB200 Build/GRJ22; CyanogenMod-7) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1';
private $imgCode='';
private $imgHeader='';
private $imgBody='';
private $imgType='';
private $response = '';
public function __construct($config=array()){
foreach($config as $key=>$value){
$this->$key=$value;
}
}
public function getImg($imgUrl){
$this->imgUrl=$imgUrl;
/*\* 处理url \*/
if(substr($this->imgUrl,0,7)!=='http://' && substr($this->imgUrl,0,8)!=='https://'){
$this->imgUrl='http://'.$this->imgUrl;
}
/*\* 解析url中的host \*/
$url_array=parse_url($this->imgUrl);
/*\* 设置referer \*/
$this->referer=$this->referer==""?'http://'.$url_array['host']:$this->referer;
/*\*开始获取 \*/
$this->urlOpen();
$this->imgBody;
/*\*处理错误 \*/
if($this->imgCode!=200){
$this->error(1);
exit();
}
/*\*获取图片格式 \*/
preg_match("/Content-Type: image\/(.+?)\\n/sim",$this->imgHeader,$result);
/*\*看看是不是图片 \*/
if(!isset($result[1])){
// 如果不是图片就输出成文件
echo $this->response;
// 也可以选择保存成文件,直接重命名即可使用
//file_put_contents('./test.txt',$this->response);die();
die();
$this->error(2);
exit();
}else{
$this->imgType=$result[1];
}
/*\* 输出内容 \*/
$this->out();
}
private function out(){
/*\* gif 不处理,直接出图 \*/
if($this->imgType=='gif'){
header("Content-Type: image/gif");
echo $this->imgBody;
exit();
}
header("Content-Type: image/png");
/*\* 其他类型的,加水印 \*/
$im=imagecreatefromstring($this->imgBody);
$white = imagecolorallocate($im, 255, 255, 255);
/*加上水印\*/
if($this->water){
imagettftext($im, 12, 0, 20, 20, $white, "/fonts/hwxh.ttf", $this->water);
}
imagepng($im);
}
private function error($err){
header("Content-Type: image/jpeg");
$im=imagecreatefromstring(file_get_contents('./default.jpg'));
imagejpeg($im);
}
private function urlOpen()
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->imgUrl);
curl_setopt($ch, CURLOPT_USERAGENT, $this->ua);
curl_setopt ($ch,CURLOPT_REFERER,$this->referer);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
/*\*跳转也要 \*/
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
/*\* 支持https \*/
$opt[CURLOPT_SSL_VERIFYHOST] = 2;
$opt[CURLOPT_SSL_VERIFYPEER] = FALSE;
curl_setopt_array($ch, $opt);
$response = curl_exec($ch);
$this->response = $response;
$this->imgCode=curl_getinfo($ch, CURLINFO_HTTP_CODE) ;
if ($this->imgCode == '200') {
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$this->imgHeader = substr($response, 0, $headerSize);
$this->imgBody = substr($response, $headerSize);
$this->response = $this->imgBody;
return ;
}
curl_close($ch);
}
private function download()
{
ob_start();
$filename='down.txt';
$title=substr($filename,strrpos($filename,'/')+1);
//$size=readfile($filename);
//var_dump($size);exit;
Header( "Content-type:application/octet-stream");
Header( "Accept-Ranges:bytes");
Header( "Accept-Length:");
header( "Content-Disposition: attachment; filename= $title");
//echo file_get_contents($size);
exit;
}
}
$img=new ImgBridge(array('water'=>''));
$img->getImg(strstr($_SERVER["QUERY_STRING"], "http"));
评论 (0)