Files
Study_Script/PHP/jsd加速/index.php

161 lines
5.4 KiB
PHP
Raw Permalink Normal View History

2025-02-24 22:48:23 +08:00
<?php
header('Access-Control-Allow-Origin: *');
2025-05-04 18:03:28 +08:00
// 获取请求路径和文件信息
2025-02-24 22:48:23 +08:00
$file = $_SERVER['REQUEST_URI'];
$file_info = pathinfo($file);
$file_ext = isset($file_info['extension']) ? $file_info['extension'] : '';
// 允许的文件后缀
2025-07-04 15:18:21 +08:00
$allow_ext = ['js', 'css', 'png', 'jpg', 'jpeg', 'gif', 'ico', 'json', 'txt', 'moc', 'moc3', 'svg', 'webp', 'hosts', 'ttf', 'woff', 'woff2', 'eot', 'sgmodule', 'exe'];
// 首页和帮助页
if ($_SERVER['REQUEST_URI'] == '/') {
2025-02-24 22:48:23 +08:00
header('content-type: text/html;charset=utf-8');
echo file_get_contents('jsd.html');
exit();
}
if ($_SERVER['REQUEST_URI'] == '/help') {
2025-02-24 22:48:23 +08:00
header('content-type: application/json;charset=utf-8');
echo json_encode([
2025-02-24 22:48:23 +08:00
'code' => 200,
'title' => 'Welcome to use OvOfish Studio API',
'message' => '这是一个反代接口,如有需要请联系管理员。',
'How_to_use' => '您只需将原有的域名更改为本站域名如需要使用jsdelivr的反代请添加/jsd如需要raw.githubusercontent.com则添加/gh',
'support_list' => '支持的接口:' . implode(',', ['/gh', '/jsd', '/unpkg']),
'file_ext_list' => '允许的后缀列表:' . implode(',', $allow_ext)
]);
2025-02-24 22:48:23 +08:00
exit();
}
if ($_SERVER['REQUEST_URI'] == '/contact') {
2025-02-24 22:48:23 +08:00
header('content-type: application/json;charset=utf-8');
echo json_encode([
2025-02-24 22:48:23 +08:00
'code' => 200,
'title' => 'Welcome to use OvOfish Studio API',
'message' => '这是一个反代接口,如有需要请联系管理员。',
2025-02-24 22:48:23 +08:00
'QQnumber' => '1709964150',
'email' => 'i@ovofish.com',
'blog' => 'https://blog.ovofish.com'
]);
2025-02-24 22:48:23 +08:00
exit();
}
// 检查文件类型是否允许
if (empty($file_ext)) {
2025-02-24 22:48:23 +08:00
header('content-type: application/json;charset=utf-8');
echo json_encode([
2025-02-24 22:48:23 +08:00
'code' => 403,
'title' => 'Welcome to use OvOfish Studio API',
2025-02-24 22:48:23 +08:00
'message' => '未能识别到文件类型',
'file_ext' => '当前文件类型为NULL',
'file_ext_list' => '允许的后缀列表:' . implode(',', $allow_ext)
]);
2025-02-24 22:48:23 +08:00
exit();
}
if (!in_array($file_ext, $allow_ext)) {
2025-02-24 22:48:23 +08:00
header('content-type: application/json;charset=utf-8');
echo json_encode([
2025-02-24 22:48:23 +08:00
'code' => 403,
'title' => 'Welcome to OvOfish Studio API',
'message' => '该文件类型不被允许,请不要滥用本接口。',
'file_ext' => '当前文件类型为:' . $file_ext,
'file_ext_list' => '允许的后缀列表:' . implode(',', $allow_ext)
]);
2025-02-24 22:48:23 +08:00
exit();
}
// 获取 MIME 类型
function get_mimetype($extension) {
$mime_types = [
'js' => 'application/javascript',
'css' => 'text/css',
'png' => 'image/png',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'gif' => 'image/gif',
'ico' => 'image/vnd.microsoft.icon',
'json' => 'application/json',
'txt' => 'text/plain',
'moc' => 'text/plain',
'moc3' => 'text/plain',
'svg' => 'image/svg+xml',
'webp' => 'image/webp',
'ttf' => 'font/ttf',
'woff' => 'font/woff',
'woff2' => 'font/woff2',
2025-07-04 15:18:21 +08:00
'eot' => 'application/vnd.ms-fontobject',
'sgmodule' => 'application/octet-stream',
'exe' => 'application/octet-stream'
];
return $mime_types[strtolower($extension)] ?? 'text/plain';
}
$mimetype = get_mimetype($file_ext);
header('content-type:' . $mimetype . ';charset=utf-8');
// 代理目标域名
2025-02-24 22:48:23 +08:00
$target_host = "";
$new_request_uri = "";
2025-02-24 22:48:23 +08:00
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'], 4);
} elseif (strpos($_SERVER['REQUEST_URI'], '/unpkg') === 0) {
$target_host = "https://unpkg.com";
$new_request_uri = substr($_SERVER['REQUEST_URI'], 6);
2025-07-04 15:18:21 +08:00
}elseif (strpos($_SERVER['REQUEST_URI'], '/github') === 0) {
$target_host = "https://github.com";
$new_request_uri = substr($_SERVER['REQUEST_URI'], 7);
2025-02-24 22:48:23 +08:00
} else {
header('content-type: application/json;charset=utf-8');
http_response_code(404);
echo json_encode([
2025-02-24 22:48:23 +08:00
'code' => 404,
'title' => 'Welcome to use OvOfish Studio API',
2025-02-24 22:48:23 +08:00
'message' => '无效的请求路径'
]);
2025-02-24 22:48:23 +08:00
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;
2025-02-24 22:48:23 +08:00
}
// 发起请求
$homepage = fetch_url($target_host . $new_request_uri);
if ($homepage === false) {
2025-02-24 22:48:23 +08:00
header('content-type: application/json;charset=utf-8');
http_response_code(404);
echo json_encode([
2025-02-24 22:48:23 +08:00
'code' => 404,
'title' => 'Welcome to use OvOfish Studio API',
'message' => '文件获取失败',
'debug' => [
'target_url' => $target_host . $new_request_uri,
]
]);
2025-02-24 22:48:23 +08:00
exit;
}
2025-05-04 18:03:28 +08:00
// 输出文件内容
echo $homepage;
2025-02-24 22:48:23 +08:00
?>