-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathwebdav-proxy.js
More file actions
364 lines (309 loc) · 11.8 KB
/
webdav-proxy.js
File metadata and controls
364 lines (309 loc) · 11.8 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/**
* WebDAV 代理控制器模块
* 提供 WebDAV 文件的 HTTP 直链访问功能
* @module webdav-proxy-controller
*/
import {ENV} from '../utils/env.js';
import {WebDAVClient} from '../utils/webdav.js';
import {readFileSync} from 'fs';
import path from 'path';
/**
* WebDAV 代理控制器插件
* @param {Object} fastify - Fastify实例
* @param {Object} options - 插件选项
* @param {Function} done - 完成回调
*/
export default (fastify, options, done) => {
// WebDAV 客户端缓存
const webdavClients = new Map();
// 文件信息缓存
const fileCache = new Map();
// 缓存超时时间(5分钟)
const cacheTimeout = 5 * 60 * 1000;
/**
* 获取或创建 WebDAV 客户端
*/
function getWebDAVClient(config) {
const key = `${config.baseURL}:${config.username}`;
if (!webdavClients.has(key)) {
const client = new WebDAVClient(config);
webdavClients.set(key, client);
}
return webdavClients.get(key);
}
/**
* 加载默认配置
*/
function loadDefaultConfig() {
try {
const configPath = path.join(process.cwd(), 'json', 'webdav.json');
const configData = readFileSync(configPath, 'utf8');
const parsed = JSON.parse(configData);
// 如果是数组,取第一个配置
if (Array.isArray(parsed)) {
return parsed.length > 0 ? parsed[0] : null;
}
return parsed;
} catch (error) {
console.warn('Could not load default WebDAV config:', error.message);
return null;
}
}
/**
* WebDAV 健康检查接口
* GET /webdav/health - 检查 WebDAV 代理服务状态
*/
fastify.get('/webdav/health', async (request, reply) => {
console.log(`[webdavController] Health check request`);
return reply.send({
status: 'ok',
service: 'WebDAV Proxy',
timestamp: new Date().toISOString(),
cache: {
clients: webdavClients.size,
files: fileCache.size
}
});
});
/**
* WebDAV 文件直链访问接口
* GET /webdav/file - 获取文件直链
*/
fastify.get('/webdav/file', async (request, reply) => {
const {path: filePath, config: configParam} = request.query;
console.log(`[webdavController] File request: ${filePath}`);
// 验证必需参数
if (!filePath) {
return reply.status(400).send({error: 'Missing required parameter: path'});
}
try {
// 获取 WebDAV 配置
let config;
if (configParam) {
try {
config = JSON.parse(decodeURIComponent(configParam));
} catch (e) {
return reply.status(400).send({error: 'Invalid config parameter'});
}
} else {
config = loadDefaultConfig();
}
if (!config) {
return reply.status(400).send({error: 'WebDAV config not provided'});
}
const client = getWebDAVClient(config);
// 检查缓存
const cacheKey = `${config.baseURL}:${filePath}`;
const cached = fileCache.get(cacheKey);
const now = Date.now();
let fileInfo;
if (cached && (now - cached.timestamp) < cacheTimeout) {
fileInfo = cached.info;
} else {
// 获取文件信息并缓存
fileInfo = await client.getInfo(filePath);
fileCache.set(cacheKey, {
info: fileInfo,
timestamp: now
});
}
if (!fileInfo || fileInfo.isDirectory) {
return reply.status(404).send({error: 'File not found or is a directory'});
}
// 处理 Range 请求
const range = request.headers.range;
let streamOptions = {};
if (range) {
const parts = range.replace(/bytes=/, "").split("-");
const start = parseInt(parts[0], 10);
const end = parts[1] ? parseInt(parts[1], 10) : fileInfo.size - 1;
if (start >= fileInfo.size || end >= fileInfo.size) {
reply.status(416);
reply.header('Content-Range', `bytes */${fileInfo.size}`);
return;
}
streamOptions.headers = {
'Range': `bytes=${start}-${end}`
};
reply.status(206);
reply.header('Content-Range', `bytes ${start}-${end}/${fileInfo.size}`);
reply.header('Accept-Ranges', 'bytes');
reply.header('Content-Length', end - start + 1);
} else {
reply.header('Content-Length', fileInfo.size);
reply.header('Accept-Ranges', 'bytes');
}
// 设置响应头
reply.header('Content-Type', fileInfo.contentType || 'application/octet-stream');
if (fileInfo.lastModified) {
reply.header('Last-Modified', new Date(fileInfo.lastModified).toUTCString());
}
if (fileInfo.etag) {
reply.header('ETag', fileInfo.etag);
}
reply.header('Cache-Control', 'public, max-age=3600');
reply.header('Access-Control-Allow-Origin', '*');
// 获取文件流
const {stream} = await client.getFileStream(filePath, streamOptions);
// 返回流
return reply.send(stream);
} catch (error) {
console.error('[webdavController] File request error:', error);
return reply.status(500).send({error: error.message});
}
});
/**
* WebDAV 文件信息获取接口
* GET /webdav/info - 获取文件或目录信息
*/
fastify.get('/webdav/info', async (request, reply) => {
const {path: filePath, config: configParam} = request.query;
console.log(`[webdavController] Info request: ${filePath}`);
// 验证必需参数
if (!filePath) {
return reply.status(400).send({error: 'Missing required parameter: path'});
}
try {
let config;
if (configParam) {
config = JSON.parse(decodeURIComponent(configParam));
} else {
config = loadDefaultConfig();
}
if (!config) {
return reply.status(400).send({error: 'WebDAV config not provided'});
}
const client = getWebDAVClient(config);
const fileInfo = await client.getInfo(filePath);
return reply.send(fileInfo);
} catch (error) {
console.error('[webdavController] Info request error:', error);
return reply.status(500).send({error: error.message});
}
});
/**
* WebDAV 目录列表接口
* GET /webdav/list - 获取目录内容列表
*/
fastify.get('/webdav/list', async (request, reply) => {
const {path: dirPath = '/', config: configParam} = request.query;
console.log(`[webdavController] List request: ${dirPath}`);
try {
let config;
if (configParam) {
config = JSON.parse(decodeURIComponent(configParam));
} else {
config = loadDefaultConfig();
}
if (!config) {
return reply.status(400).send({error: 'WebDAV config not provided'});
}
const client = getWebDAVClient(config);
const items = await client.listDirectory(dirPath);
return reply.send(items);
} catch (error) {
console.error('[webdavController] List request error:', error);
return reply.status(500).send({error: error.message});
}
});
/**
* WebDAV 配置测试接口
* POST /webdav/config - 测试和配置 WebDAV 连接
*/
fastify.post('/webdav/config', async (request, reply) => {
console.log(`[webdavController] Config test request`);
try {
const config = request.body;
// 验证配置
if (!config.baseURL || !config.username || !config.password) {
return reply.status(400).send({
error: 'Missing required config fields: baseURL, username, password'
});
}
// 测试连接
const client = getWebDAVClient(config);
await client.testConnection();
return reply.send({
success: true,
message: 'WebDAV connection configured and tested successfully',
config: {
baseURL: config.baseURL,
username: config.username,
// 不返回密码
}
});
} catch (error) {
console.error('[webdavController] Config test error:', error);
return reply.status(500).send({error: error.message});
}
});
/**
* WebDAV 缓存管理接口
* DELETE /webdav/cache - 清理缓存
*/
fastify.delete('/webdav/cache', async (request, reply) => {
console.log(`[webdavController] Cache clear request`);
try {
// 非VERCEL环境可在设置中心控制此功能是否开启
if (!process.env.VERCEL) {
if (ENV.get('allow_webdav_cache_clear') !== '1') {
return reply.status(403).send({error: 'Cache clear is not allowed by owner'});
}
}
const clientCount = webdavClients.size;
const fileCount = fileCache.size;
// 清理缓存
fileCache.clear();
webdavClients.clear();
return reply.send({
success: true,
message: 'Cache cleared successfully',
cleared: {
clients: clientCount,
files: fileCount
}
});
} catch (error) {
console.error('[webdavController] Cache clear error:', error);
return reply.status(500).send({error: error.message});
}
});
/**
* WebDAV 代理状态接口
* GET /webdav/status - 获取代理服务状态
*/
fastify.get('/webdav/status', async (request, reply) => {
console.log(`[webdavController] Status request`);
try {
const config = loadDefaultConfig();
return reply.send({
service: 'WebDAV Proxy Controller',
version: '1.0.0',
status: 'running',
cache: {
clients: webdavClients.size,
files: fileCache.size,
timeout: cacheTimeout
},
config: config ? {
baseURL: config.baseURL,
username: config.username,
hasPassword: !!config.password
} : null,
endpoints: [
'GET /webdav/health - Health check',
'GET /webdav/file?path=<file_path> - Get file direct link',
'GET /webdav/info?path=<file_path> - Get file information',
'GET /webdav/list?path=<dir_path> - List directory contents',
'POST /webdav/config - Test WebDAV configuration',
'DELETE /webdav/cache - Clear cache',
'GET /webdav/status - Get service status'
]
});
} catch (error) {
console.error('[webdavController] Status request error:', error);
return reply.status(500).send({error: error.message});
}
});
done();
};