This repository was archived by the owner on Jul 13, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-proxy.mjs
More file actions
70 lines (62 loc) · 2.51 KB
/
Copy pathtest-proxy.mjs
File metadata and controls
70 lines (62 loc) · 2.51 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
// 本地测试代理 — 模拟 Cloudflare Workers 行为
// 用法: node test-proxy.mjs
// 测试: curl -I http://localhost:3456/v1.4.2/Finch-1.4.2-arm64.dmg
import http from 'node:http'
import https from 'node:https'
const PORT = 3456
const GITHUB_OWNER = 'puterjam'
const GITHUB_REPO = 'finch'
const server = http.createServer((req, res) => {
const match = req.url.match(/^\/(v[^/]+)\/(.+)$/)
if (!match) {
res.writeHead(404)
res.end('Not Found')
return
}
const [_, version, filename] = match
const targetUrl = `https://github.com/${GITHUB_OWNER}/${GITHUB_REPO}/releases/download/${version}/${filename}`
console.log(`[PROXY] ${req.url} → ${targetUrl}`)
https.get(targetUrl, {
headers: {
'User-Agent': 'Finch-Test-Proxy',
'Accept': '*/*',
'Range': req.headers['range'] || '',
}
}, (proxyRes) => {
// 302 重定向处理(GitHub 会 302 到 CDN)
if (proxyRes.statusCode >= 300 && proxyRes.statusCode < 400 && proxyRes.headers.location) {
console.log(`[REDIRECT] → ${proxyRes.headers.location}`)
https.get(proxyRes.headers.location, { headers: { 'User-Agent': 'Finch-Test-Proxy' } }, (finalRes) => {
res.writeHead(finalRes.statusCode, {
'Content-Type': finalRes.headers['content-type'] || 'application/octet-stream',
'Content-Length': finalRes.headers['content-length'] || '',
'Cache-Control': 'public, max-age=86400',
'Access-Control-Allow-Origin': '*',
})
finalRes.pipe(res)
})
return
}
res.writeHead(proxyRes.statusCode, {
'Content-Type': proxyRes.headers['content-type'] || 'application/octet-stream',
'Content-Length': proxyRes.headers['content-length'] || '',
'Cache-Control': 'public, max-age=86400',
'Access-Control-Allow-Origin': '*',
})
proxyRes.pipe(res)
}).on('error', (err) => {
console.error(`[ERROR] ${err.message}`)
res.writeHead(502)
res.end('Proxy Error: ' + err.message)
})
})
server.listen(PORT, () => {
console.log(`\n Finch Release Proxy 本地服务已启动\n`)
console.log(` 测试链接:\n`)
console.log(` http://localhost:${PORT}/v1.4.2/Finch-1.4.2-arm64.dmg`)
console.log(` http://localhost:${PORT}/v1.4.2/Finch-1.4.2-x64.dmg`)
console.log(` http://localhost:${PORT}/v1.4.2/Finch-1.4.2-setup-x64.exe`)
console.log(` http://localhost:${PORT}/v1.4.2/Finch-1.4.2-setup-ia32.exe`)
console.log(`\n 打开浏览器访问以上链接验证下载是否正常\n`)
console.log(` Ctrl+C 停止服务\n`)
})