-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
89 lines (73 loc) · 2.49 KB
/
worker.js
File metadata and controls
89 lines (73 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
* 文件下载代理 - Cloudflare Worker版本
*/
// 简单日志函数
function log(message) {
console.log(`[${new Date().toISOString()}] ${message}`);
}
// 从URL中提取真实域名
function extractDomainFromUrl(urlString) {
try {
// 确保URL有协议前缀
if (!urlString.startsWith('http://') && !urlString.startsWith('https://')) {
urlString = 'https://' + urlString;
}
const parsedUrl = new URL(urlString);
return parsedUrl.hostname;
} catch (error) {
// 如果解析失败,返回原始字符串的一部分作为标识符
return urlString.replace(/[^a-zA-Z0-9]/g, '_').substring(0, 30);
}
}
// 处理请求
async function handleRequest(request) {
const url = new URL(request.url);
const path = url.pathname;
// 首页 - 只显示运行状况
if (path === '/' || path === '') {
return new Response('文件下载代理服务器正在运行', {
headers: { 'Content-Type': 'text/plain; charset=utf-8' }
});
}
// 提取域名和文件路径
const pathParts = path.substring(1).split('/');
if (pathParts.length < 2) {
return new Response('无效的URL格式。正确格式: /https://example.com/path/to/file', {
status: 400,
headers: { 'Content-Type': 'text/plain; charset=utf-8' }
});
}
const domain = pathParts[0];
const filePath = pathParts.slice(1).join('/');
// 构建完整URL
const fileUrl = `${domain}/${filePath}`;
try {
log(`下载: ${fileUrl}`);
// 获取文件
const response = await fetch(fileUrl);
if (!response.ok) {
throw new Error(`源站返回错误: ${response.status} ${response.statusText}`);
}
// 获取文件名
const fileName = filePath.split('/').pop() || 'download';
// 创建新的响应对象,添加下载头
const headers = new Headers(response.headers);
headers.set('Content-Disposition', `attachment; filename=${fileName}`);
// 返回响应
return new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers
});
} catch (error) {
log(`错误: ${error.message}`);
return new Response(`下载错误: ${error.message}`, {
status: 500,
headers: { 'Content-Type': 'text/plain; charset=utf-8' }
});
}
}
// 注册事件监听器
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});