update:修复反代unpkg失败的问题
This commit is contained in:
parent
e93fe95524
commit
2f9be25025
@ -1,215 +1,75 @@
|
|||||||
<?php
|
<?php
|
||||||
//允许跨域
|
|
||||||
header('Access-Control-Allow-Origin: *');
|
header('Access-Control-Allow-Origin: *');
|
||||||
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
|
|
||||||
header('Access-Control-Allow-Headers: Content-Type, Authorization');
|
|
||||||
|
|
||||||
// 获取用户 IP 地址
|
// 获取请求路径和文件信息
|
||||||
$ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
|
|
||||||
// 可能会获取到多个ip,仅此使用最前面的IP
|
|
||||||
$ip_address = explode(',', $ip_address)[0];
|
|
||||||
|
|
||||||
$info_ip = file_get_contents("https://v2.xxapi.cn/api/ip?ip=".$ip_address);
|
|
||||||
// 获取网络json的data中的address
|
|
||||||
$info_ip = json_decode($info_ip, true);
|
|
||||||
$info_ip_address = $info_ip['data']['address'];
|
|
||||||
$info_ip_isp = $info_ip['data']['isp'];
|
|
||||||
$format = $_GET['format'];
|
|
||||||
|
|
||||||
if($format === 'json'){
|
|
||||||
if($ip_address !== ''){
|
|
||||||
header('Content-Type: application/json');
|
|
||||||
http_response_code(200);
|
|
||||||
$data = array(
|
|
||||||
'ip' => $ip_address,
|
|
||||||
'address' => $info_ip_address,
|
|
||||||
'isp' => $info_ip_isp
|
|
||||||
);
|
|
||||||
echo json_encode($data);
|
|
||||||
exit();
|
|
||||||
}else{
|
|
||||||
header('Content-Type: application/json');
|
|
||||||
http_response_code(403);
|
|
||||||
$data = array(
|
|
||||||
'info' => '获取失败'
|
|
||||||
);
|
|
||||||
echo json_encode($data);
|
|
||||||
exit();
|
|
||||||
}
|
|
||||||
}else if($format === 'text'){
|
|
||||||
echo $ip_address;
|
|
||||||
exit();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// 获取文件名
|
|
||||||
$file = $_SERVER['REQUEST_URI'];
|
$file = $_SERVER['REQUEST_URI'];
|
||||||
// 获取文件类型
|
|
||||||
$file_info = pathinfo($file);
|
$file_info = pathinfo($file);
|
||||||
// 获取文件后缀名
|
$file_ext = isset($file_info['extension']) ? $file_info['extension'] : '';
|
||||||
$file_ext = $file_info['extension'];
|
|
||||||
// 定义允许的后缀
|
// 允许的文件后缀
|
||||||
$allow_ext = array('js', 'css', 'png', 'jpg', 'jpeg', 'gif', 'ico', 'json', 'txt', 'moc', 'moc3', 'svg', 'webp' , 'hosts', 'ttf', 'woff', 'woff2', 'eot', 'sgmodule', 'apk', 'zip', 'exe');
|
$allow_ext = ['js', 'css', 'png', 'jpg', 'jpeg', 'gif', 'ico', 'json', 'txt', 'moc', 'moc3', 'svg', 'webp', 'hosts', 'ttf', 'woff', 'woff2', 'eot', 'sgmodule'];
|
||||||
if($_SERVER['REQUEST_URI'] == '/'){
|
|
||||||
// 输出首页
|
// 首页和帮助页
|
||||||
|
if ($_SERVER['REQUEST_URI'] == '/') {
|
||||||
header('content-type: text/html;charset=utf-8');
|
header('content-type: text/html;charset=utf-8');
|
||||||
echo file_get_contents('jsd.html');
|
echo file_get_contents('jsd.html');
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
if($_SERVER['REQUEST_URI'] == '/help'){
|
|
||||||
|
if ($_SERVER['REQUEST_URI'] == '/help') {
|
||||||
header('content-type: application/json;charset=utf-8');
|
header('content-type: application/json;charset=utf-8');
|
||||||
echo json_encode(array(
|
echo json_encode([
|
||||||
'code' => 200,
|
'code' => 200,
|
||||||
'title'=> 'Welcome to use OvOfish Studio API',
|
'title' => 'Welcome to use OvOfish Studio API',
|
||||||
'message' => '这是一个反代接口,如有需要请联系管理员。',
|
'message' => '这是一个反代接口,如有需要请联系管理员。',
|
||||||
'How_to_use' => '您只需将原有的域名更改为本站域名如需要使用jsdelivr的反代请添加/jsd,如需要raw.githubusercontent.com则添加/gh',
|
'How_to_use' => '您只需将原有的域名更改为本站域名,如需要使用jsdelivr的反代请添加/jsd,如需要raw.githubusercontent.com则添加/gh',
|
||||||
'support_list' => '支持的接口:'.implode(',', array('/gh', '/jsd', '/unpkg' ,'/releases')),
|
'support_list' => '支持的接口:' . implode(',', ['/gh', '/jsd', '/unpkg']),
|
||||||
'file_ext_list' => '允许的后缀列表:'.implode(',', $allow_ext)
|
'file_ext_list' => '允许的后缀列表:' . implode(',', $allow_ext)
|
||||||
));
|
]);
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
if($_SERVER['REQUEST_URI'] == '/contact'){
|
|
||||||
|
if ($_SERVER['REQUEST_URI'] == '/contact') {
|
||||||
header('content-type: application/json;charset=utf-8');
|
header('content-type: application/json;charset=utf-8');
|
||||||
echo json_encode(array(
|
echo json_encode([
|
||||||
'code' => 200,
|
'code' => 200,
|
||||||
'title'=> 'Welcome to use OvOfish Studio API',
|
'title' => 'Welcome to use OvOfish Studio API',
|
||||||
'message' => '这是一个反代接口,如有需要请联系管理员。',
|
'message' => '这是一个反代接口,如有需要请联系管理员。',
|
||||||
'QQnumber' => '1709964150',
|
'QQnumber' => '1709964150',
|
||||||
'email' => 'mirai@lolicon.team',
|
'email' => 'i@ovofish.com',
|
||||||
'blog' => 'https://lolicon.team'
|
'blog' => 'https://blog.ovofish.com'
|
||||||
));
|
]);
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
if(empty($file_ext)){
|
|
||||||
|
// 检查文件类型是否允许
|
||||||
|
if (empty($file_ext)) {
|
||||||
header('content-type: application/json;charset=utf-8');
|
header('content-type: application/json;charset=utf-8');
|
||||||
echo json_encode(array(
|
echo json_encode([
|
||||||
'code' => 403,
|
'code' => 403,
|
||||||
'title'=> 'Welcome to use OvOfish Studio API',
|
'title' => 'Welcome to use OvOfish Studio API',
|
||||||
'message' => '未能识别到文件类型',
|
'message' => '未能识别到文件类型',
|
||||||
'file_ext' => '当前文件类型为:NULL',
|
'file_ext' => '当前文件类型为:NULL',
|
||||||
'file_ext_list' => '允许的后缀列表:'.implode(',', $allow_ext)
|
'file_ext_list' => '允许的后缀列表:' . implode(',', $allow_ext)
|
||||||
));
|
]);
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
// 判断后缀是否在允许的后缀列表中 如果不在,则返回403
|
|
||||||
if(!in_array($file_ext, $allow_ext)){
|
if (!in_array($file_ext, $allow_ext)) {
|
||||||
header('content-type: application/json;charset=utf-8');
|
header('content-type: application/json;charset=utf-8');
|
||||||
echo json_encode(array(
|
echo json_encode([
|
||||||
'code' => 403,
|
'code' => 403,
|
||||||
'title'=> 'Welcome to OvOfish Studio API',
|
'title' => 'Welcome to OvOfish Studio API',
|
||||||
'message' => '该文件类型不被允许, 请不要滥用本接口。',
|
'message' => '该文件类型不被允许,请不要滥用本接口。',
|
||||||
'file_ext' => '当前文件类型为:'.$file_ext,
|
'file_ext' => '当前文件类型为:' . $file_ext,
|
||||||
'file_ext_list' => '允许的后缀列表:'.implode(',', $allow_ext)
|
'file_ext_list' => '允许的后缀列表:' . implode(',', $allow_ext)
|
||||||
));
|
]);
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
$mimetype = get_mimetype($file_info['extension']);
|
|
||||||
header('content-type:'. $mimetype .';charset=utf-8');
|
// 获取 MIME 类型
|
||||||
// 代理的域名及使用的协议最后不用加/
|
|
||||||
$target_host = "";
|
|
||||||
$new_request_uri = "";
|
|
||||||
if (strpos($_SERVER['REQUEST_URI'], '/gh') === 0) {
|
|
||||||
$target_host = "https://raw.githubusercontent.com";
|
|
||||||
$new_request_uri = substr($_SERVER['REQUEST_URI'], 3);
|
|
||||||
} elseif (strpos($_SERVER['REQUEST_URI'], '/jsd') === 0) {
|
|
||||||
$target_host = "https://cdn.jsdelivr.net";
|
|
||||||
$new_request_uri = substr($_SERVER['REQUEST_URI'], 5);
|
|
||||||
}elseif (strpos($_SERVER['REQUEST_URI'], '/unpkg') === 0) {
|
|
||||||
$target_host = "https://unpkg.com";
|
|
||||||
$new_request_uri = substr($_SERVER['REQUEST_URI'], 6);
|
|
||||||
}elseif (strpos($_SERVER['REQUEST_URI'], '/releases') === 0) {
|
|
||||||
$target_host = "https://github.com";
|
|
||||||
$new_request_uri = substr($_SERVER['REQUEST_URI'], 9);
|
|
||||||
} else {
|
|
||||||
header('content-type: application/json;charset=utf-8');
|
|
||||||
// 返回404状态
|
|
||||||
http_response_code(404);
|
|
||||||
echo json_encode(array(
|
|
||||||
'code' => 404,
|
|
||||||
'title'=> 'Welcome to use OvOfish Studio API',
|
|
||||||
'message' => '无效的请求路径'
|
|
||||||
));
|
|
||||||
exit;
|
|
||||||
}
|
|
||||||
// 解析url参数
|
|
||||||
function get_request_params()
|
|
||||||
{
|
|
||||||
$url = $_SERVER["REQUEST_URI"];
|
|
||||||
$refer_url = parse_url($url);
|
|
||||||
$params = $refer_url['query'];
|
|
||||||
$arr = array();
|
|
||||||
if(!empty($params))
|
|
||||||
{
|
|
||||||
$paramsArr = explode('&',$params);
|
|
||||||
foreach($paramsArr as $k=>$v)
|
|
||||||
{
|
|
||||||
$a = explode('=',$v);
|
|
||||||
$arr[$a[0]] = $a[1];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return $arr;
|
|
||||||
}
|
|
||||||
// 解析HTTP响应头
|
|
||||||
function parse_headers($headers)
|
|
||||||
{
|
|
||||||
global $root, $top;
|
|
||||||
foreach( $headers as $k=>$v )
|
|
||||||
{
|
|
||||||
$t = explode( ':', $v, 2 );
|
|
||||||
if( isset( $t[1] ) )
|
|
||||||
{
|
|
||||||
if(strcasecmp('Set-Cookie',trim($t[0]))==0)
|
|
||||||
{
|
|
||||||
$targetcookie=trim( $t[1] ).";";
|
|
||||||
$res_cookie=preg_replace("/domain=.*?;/","domain=".$_SERVER["SERVER_NAME"].";",$targetcookie);
|
|
||||||
$res_cookie=substr($res_cookie,0,strlen($res_cookie)-1);
|
|
||||||
header("Set-Cookie: ".$res_cookie);
|
|
||||||
}
|
|
||||||
elseif(strcasecmp('Content-Type',trim($t[0]))==0)
|
|
||||||
{
|
|
||||||
header("Content-Type: ".trim( $t[1] ));
|
|
||||||
}
|
|
||||||
elseif(strcasecmp('Location',trim( $t[0] ))==0)
|
|
||||||
{
|
|
||||||
$relocation=str_replace($protocal_host['host'],$_SERVER["SERVER_NAME"],trim( $t[1] ));
|
|
||||||
header("Location: ".$relocation);
|
|
||||||
}
|
|
||||||
elseif(strcasecmp('cache-control',trim( $t[0] ))==0)
|
|
||||||
{
|
|
||||||
header("cache-control: ".trim( $t[1] ));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// 组装HTTP请求头
|
|
||||||
$opts = array(
|
|
||||||
'http'=>array(
|
|
||||||
'method'=>$_SERVER['REQUEST_METHOD'],
|
|
||||||
'header'=>"Accept-language: zh-CN\r\n" .
|
|
||||||
"user-agent: {$_SERVER['HTTP_USER_AGENT']}\r\n".
|
|
||||||
"Cookie: ".array_to_str($_COOKIE)."\r\n"
|
|
||||||
)
|
|
||||||
);
|
|
||||||
$context = stream_context_create($opts);
|
|
||||||
// 发送请求
|
|
||||||
$homepage = file_get_contents($target_host.'/'.$new_request_uri,false,$context);
|
|
||||||
if ($homepage === FALSE) {
|
|
||||||
header('content-type: application/json;charset=utf-8');
|
|
||||||
echo json_encode(array(
|
|
||||||
'code' => 404,
|
|
||||||
'title'=> 'Welcome to use OvOfish Studio API',
|
|
||||||
'message' => '未能找到文件',
|
|
||||||
'file_ext' => '当前文件类型为:'.$file_ext
|
|
||||||
));
|
|
||||||
exit;
|
|
||||||
}
|
|
||||||
function get_mimetype($extension) {
|
function get_mimetype($extension) {
|
||||||
// MIME类型数组
|
$mime_types = [
|
||||||
$ct = array(
|
|
||||||
'js' => 'application/javascript',
|
'js' => 'application/javascript',
|
||||||
'css' => 'text/css',
|
'css' => 'text/css',
|
||||||
'png' => 'image/png',
|
'png' => 'image/png',
|
||||||
@ -223,33 +83,74 @@ function get_mimetype($extension) {
|
|||||||
'moc3' => 'text/plain',
|
'moc3' => 'text/plain',
|
||||||
'svg' => 'image/svg+xml',
|
'svg' => 'image/svg+xml',
|
||||||
'webp' => 'image/webp',
|
'webp' => 'image/webp',
|
||||||
'zip' => 'application/zip',
|
'ttf' => 'font/ttf',
|
||||||
'exe' => 'application/octet-stream',
|
'woff' => 'font/woff',
|
||||||
'apk' => 'application/vnd.android.package-archive'
|
'woff2' => 'font/woff2',
|
||||||
);
|
'eot' => 'application/vnd.ms-fontobject'
|
||||||
return isset($ct[strtolower($extension)]) ? $ct[strtolower($extension)] : 'text/plain';
|
];
|
||||||
|
return $mime_types[strtolower($extension)] ?? 'text/plain';
|
||||||
}
|
}
|
||||||
function array_to_str($array)
|
|
||||||
{
|
$mimetype = get_mimetype($file_ext);
|
||||||
$string="";
|
header('content-type:' . $mimetype . ';charset=utf-8');
|
||||||
if (is_array($array))
|
|
||||||
{
|
// 代理目标域名
|
||||||
foreach ($array as $key => $value)
|
$target_host = "";
|
||||||
{
|
$new_request_uri = "";
|
||||||
if(!empty($string))
|
|
||||||
$string.="; ".$key."=".$value;
|
if (strpos($_SERVER['REQUEST_URI'], '/gh') === 0) {
|
||||||
else
|
$target_host = "https://raw.githubusercontent.com";
|
||||||
$string.=$key."=".$value;
|
$new_request_uri = substr($_SERVER['REQUEST_URI'], 3);
|
||||||
}
|
} elseif (strpos($_SERVER['REQUEST_URI'], '/jsd') === 0) {
|
||||||
} else
|
$target_host = "https://cdn.jsdelivr.net";
|
||||||
{
|
$new_request_uri = substr($_SERVER['REQUEST_URI'], 4);
|
||||||
$string = $array;
|
} elseif (strpos($_SERVER['REQUEST_URI'], '/unpkg') === 0) {
|
||||||
}
|
$target_host = "https://unpkg.com";
|
||||||
return $string;
|
$new_request_uri = substr($_SERVER['REQUEST_URI'], 6);
|
||||||
}
|
} else {
|
||||||
// 输出网页内容
|
header('content-type: application/json;charset=utf-8');
|
||||||
|
http_response_code(404);
|
||||||
|
echo json_encode([
|
||||||
|
'code' => 404,
|
||||||
|
'title' => 'Welcome to use OvOfish Studio API',
|
||||||
|
'message' => '无效的请求路径'
|
||||||
|
]);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 确保路径以 / 开头
|
||||||
|
$new_request_uri = '/' . ltrim($new_request_uri, '/');
|
||||||
|
|
||||||
|
// 使用 curl 获取远程文件(支持自动重定向)
|
||||||
|
function fetch_url($url) {
|
||||||
|
$ch = curl_init();
|
||||||
|
curl_setopt($ch, CURLOPT_URL, $url);
|
||||||
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // 跟随 301/302 重定向
|
||||||
|
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT'] ?? 'Mozilla/5.0');
|
||||||
|
$result = curl_exec($ch);
|
||||||
|
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||||
|
curl_close($ch);
|
||||||
|
return $http_code == 200 ? $result : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 发起请求
|
||||||
|
$homepage = fetch_url($target_host . $new_request_uri);
|
||||||
|
|
||||||
|
if ($homepage === false) {
|
||||||
|
header('content-type: application/json;charset=utf-8');
|
||||||
|
http_response_code(404);
|
||||||
|
echo json_encode([
|
||||||
|
'code' => 404,
|
||||||
|
'title' => 'Welcome to use OvOfish Studio API',
|
||||||
|
'message' => '文件获取失败',
|
||||||
|
'debug' => [
|
||||||
|
'target_url' => $target_host . $new_request_uri,
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 输出文件内容
|
||||||
echo $homepage;
|
echo $homepage;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
?>
|
?>
|
@ -217,7 +217,7 @@
|
|||||||
<div class="offcanvas-body ms-lg-auto d-flex flex-column h-100">
|
<div class="offcanvas-body ms-lg-auto d-flex flex-column h-100">
|
||||||
<ul class="navbar-nav"><a href="/" class="nav-link">首页</a><a href="/help"
|
<ul class="navbar-nav"><a href="/" class="nav-link">首页</a><a href="/help"
|
||||||
class="nav-link">帮助</a>
|
class="nav-link">帮助</a>
|
||||||
<li class="nav-item"><a href="https://lolicon.team" target="_blank"
|
<li class="nav-item"><a href="https://blog.ovofish.com" target="_blank"
|
||||||
class="nav-link">博客<br /></a></li>
|
class="nav-link">博客<br /></a></li>
|
||||||
<li class="nav-item dropdown"></li>
|
<li class="nav-item dropdown"></li>
|
||||||
</ul><!-- /.navbar-nav -->
|
</ul><!-- /.navbar-nav -->
|
||||||
@ -248,7 +248,7 @@
|
|||||||
<li class="nav-item"><a href="/" class="nav-link">首页</a></li>
|
<li class="nav-item"><a href="/" class="nav-link">首页</a></li>
|
||||||
<li class="nav-item dropdown"><a href="/help" class="nav-link">帮助</a></li>
|
<li class="nav-item dropdown"><a href="/help" class="nav-link">帮助</a></li>
|
||||||
<li class="nav-item dropdown">
|
<li class="nav-item dropdown">
|
||||||
</li><a href="https://lolicon.team" class="nav-link">博客</a>
|
</li><a href="https://blog.ovofish.com" class="nav-link">博客</a>
|
||||||
</ul><!-- /.navbar-nav -->
|
</ul><!-- /.navbar-nav -->
|
||||||
</div><!-- /.offcanvas-body -->
|
</div><!-- /.offcanvas-body -->
|
||||||
</div><!-- /.navbar-collapse -->
|
</div><!-- /.navbar-collapse -->
|
||||||
|
Loading…
x
Reference in New Issue
Block a user