一个php文件实现反代jsdelivr,并缓存文件到本地服务器

2022-05-17 16:50:55 浏览数 (1)

之前写了shell脚本,可以手动下载jsdelivr的单文件,已经满足的我需求了(在这里 ),但评论区表示没有一步到位。 那就用最好的语言之php配合伪静态再搞一个吧!话不多说,直接上代码!

image.pngimage.png

将下面的代码保存为jsd.php

代码语言:php复制
<?php
$file = $_SERVER['REQUEST_URI'];
$self_path = pathinfo($_SERVER['PHP_SELF'], PATHINFO_DIRNAME);
$query = '';
if($_SERVER['QUERY_STRING'])
{
    $file = explode('?', $file)[0];
    $query = '?' . $_SERVER['QUERY_STRING'];
}
$file_info = pathinfo($file);
$path = $file_info['dirname'];
if($path=='/') exit('/* ??? */');
$mimetype = get_mimetype($file_info['extension']);

$cdn_file = $file;
if(strlen($self_path)>1){
   $cdn_file = substr($file, strlen($self_path));  
}

$local_path = substr(pathinfo( $cdn_file, PATHINFO_DIRNAME), 1);
if($local_path && !is_dir($local_path)){
    @mkdir($local_path, 755, true);
}

$url = 'https://cdn.jsdelivr.net' . $cdn_file . $query;

$content = curl($url);
if($content){
    header('content-type:'. $mimetype .';charset=utf-8');
    echo '/* ' . $url .'*/';
    file_put_contents(substr($cdn_file, 1), $content);
    exit($content);
}else{
    header('location: ' .$url );
}


function curl($url)
{
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 3000);
	curl_setopt($ch, CURLOPT_TIMEOUT_MS, 3000);
	if (strpos($url, 'https') !== false) {
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
	}
	curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36');
	$result = curl_exec($ch);
	curl_close($ch);
	return $result;
}
/**
 * 根据文件后缀获取其mine类型
 * @param string $extension
 * @return string
 */
 function get_mimetype($extension) {
	$ct['htm'] = 'text/html';
	$ct['html'] = 'text/html';
	$ct['txt'] = 'text/plain';
	$ct['asc'] = 'text/plain';
	$ct['bmp'] = 'image/bmp';
	$ct['gif'] = 'image/gif';
	$ct['jpeg'] = 'image/jpeg';
	$ct['jpg'] = 'image/jpeg';
	$ct['jpe'] = 'image/jpeg';
	$ct['png'] = 'image/png';
	$ct['ico'] = 'image/vnd.microsoft.icon';
	$ct['mpeg'] = 'video/mpeg';
	$ct['mpg'] = 'video/mpeg';
	$ct['mpe'] = 'video/mpeg';
	$ct['qt'] = 'video/quicktime';
	$ct['mov'] = 'video/quicktime';
	$ct['avi'] = 'video/x-msvideo';
	$ct['wmv'] = 'video/x-ms-wmv';
	$ct['mp2'] = 'audio/mpeg';
	$ct['mp3'] = 'audio/mpeg';
	$ct['rm'] = 'audio/x-pn-realaudio';
	$ct['ram'] = 'audio/x-pn-realaudio';
	$ct['rpm'] = 'audio/x-pn-realaudio-plugin';
	$ct['ra'] = 'audio/x-realaudio';
	$ct['wav'] = 'audio/x-wav';
	$ct['css'] = 'text/css';
	$ct['zip'] = 'application/zip';
	$ct['pdf'] = 'application/pdf';
	$ct['doc'] = 'application/msword';
	$ct['bin'] = 'application/octet-stream';
	$ct['exe'] = 'application/octet-stream';
	$ct['class'] = 'application/octet-stream';
	$ct['dll'] = 'application/octet-stream';
	$ct['xls'] = 'application/vnd.ms-excel';
	$ct['ppt'] = 'application/vnd.ms-powerpoint';
	$ct['wbxml'] = 'application/vnd.wap.wbxml';
	$ct['wmlc'] = 'application/vnd.wap.wmlc';
	$ct['wmlsc'] = 'application/vnd.wap.wmlscriptc';
	$ct['dvi'] = 'application/x-dvi';
	$ct['spl'] = 'application/x-futuresplash';
	$ct['gtar'] = 'application/x-gtar';
	$ct['gzip'] = 'application/x-gzip';
	$ct['js'] = 'application/javascript';
	$ct['swf'] = 'application/x-shockwave-flash';
	$ct['tar'] = 'application/x-tar';
    $ct['7z'] = 'application/x7zcompressed';
    $ct['rar'] = 'application/x-rar-compressed';
	$ct['xhtml'] = 'application/xhtml xml';
	$ct['au'] = 'audio/basic';
	$ct['snd'] = 'audio/basic';
	$ct['midi'] = 'audio/midi';
	$ct['mid'] = 'audio/midi';
	$ct['m3u'] = 'audio/x-mpegurl';
	$ct['tiff'] = 'image/tiff';
	$ct['tif'] = 'image/tiff';
	$ct['rtf'] = 'text/rtf';
	$ct['wml'] = 'text/vnd.wap.wml';
	$ct['wmls'] = 'text/vnd.wap.wmlscript';
	$ct['xsl'] = 'text/xml';
	$ct['xml'] = 'text/xml';
	
	return isset($ct[strtolower($extension)]) ? $ct[strtolower($extension)] : 'text/html';
}

伪静态规则

  • 如果你的服务器是Apache,可以在jsd.php文件所在的文件夹新建一个.htaccess文件,内容如下
代码语言:javascript复制
# Apache
<IfModule mod_rewrite.c>
	RewriteEngine on
	# 这里的/记得修改为jsd.php所在的实际绝对路径
	RewriteBase /
 
	RewriteCond %{REQUEST_FILENAME} !-f 
	RewriteCond %{REQUEST_FILENAME} !-d
	RewriteRule .* jsd.php [QSA,L]
	
</IfModule>
  • 如果服务器是Nginx,因为我不用,不知道怎么写,自己看着编吧

用法介绍

  • 如果放在根目录,直接把cdn.jsdelivr.net替换成你的域名就好了
  • 如果放在某个子目录下,比如cdn目录下,把cdn.jsdelivr.net替换成你的域名/cdn(如https://example.com/cdn)即可

笔者为TDP成员,点击了解TDP详情

原文链接:https://nongxue.top/p/daima/7.html

0 人点赞