Skip to content

Commit 2136df9

Browse files
metowolfclaude
andcommitted
feat: 添加 Cookie 支持和演示端点
- 新增 cookie 工具模块,支持读取和管理各平台 Cookie 文件 - 添加基于 referrer 的 Cookie 自动配置功能 - 新增 demo 服务端点用于测试和演示 - 更新配置文件支持 Cookie 主机白名单 - 改进 URL 处理逻辑,移除 vuutv 参数 - 添加 cookie/ 目录到 .gitignore 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 1d15eda commit 2136df9

File tree

7 files changed

+365
-312
lines changed

7 files changed

+365
-312
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules/
22
.env
3+
cookie/

src/config.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ export default {
2525
},
2626
meting: {
2727
url: process.env.METING_URL || '',
28-
token: process.env.METING_TOKEN || 'token'
28+
token: process.env.METING_TOKEN || 'token',
29+
cookie: {
30+
allowHosts: process.env.METING_COOKIE_ALLOW_HOSTS ?
31+
(process.env.METING_COOKIE_ALLOW_HOSTS).split(',').map(h => h.trim().toLowerCase()) :
32+
[]
33+
}
2934
}
3035
}

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { readFileSync } from 'node:fs'
66
import { requestLogger, logger } from './middleware/logger.js'
77
import errors from './middleware/errors.js'
88
import apiService from './service/api.js'
9+
import demoService from './service/demo.js'
910
import config from './config.js'
1011

1112
const app = new Hono()
@@ -14,6 +15,7 @@ const app = new Hono()
1415
.use(errors)
1516

1617
app.get(`${config.http.prefix}/api`, apiService)
18+
app.get(`${config.http.prefix}/demo`, demoService)
1719

1820
serve({
1921
fetch: app.fetch,

src/service/api.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import hashjs from 'hash.js'
33
import { HTTPException } from 'hono/http-exception'
44
import config from '../config.js'
55
import { format as lyricFormat } from '../utils/lyric.js'
6+
import { readCookieFile, isAllowedHost } from '../utils/cookie.js'
67
import { LRUCache } from 'lru-cache'
78

89
const cache = new LRUCache({
@@ -50,6 +51,16 @@ export default async (c) => {
5051
c.header('x-cache', 'miss')
5152
const meting = new Meting(server)
5253
meting.format(true)
54+
55+
// 检查 referrer 并配置 cookie
56+
const referrer = c.req.header('referer')
57+
if (isAllowedHost(referrer)) {
58+
const cookie = await readCookieFile(server)
59+
if (cookie) {
60+
meting.cookie(cookie)
61+
}
62+
}
63+
5364
const method = METING_METHODS[type]
5465
let response
5566
try {
@@ -80,6 +91,11 @@ export default async (c) => {
8091
.replace('://m7c.', '://m7.')
8192
.replace('://m8c.', '://m8.')
8293
.replace('http://', 'https://')
94+
if (url.includes('vuutv=')) {
95+
const tempUrl = new URL(url)
96+
tempUrl.search = ''
97+
url = tempUrl.toString()
98+
}
8399
}
84100
if (server === 'tencent') {
85101
url = url

src/service/demo.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import config from '../config.js'
2+
import { html } from 'hono/html'
3+
4+
export default async (c) => {
5+
// 1. 初始化参数
6+
const query = c.req.query()
7+
const server = query.server || 'netease'
8+
const type = query.type || 'search'
9+
const id = query.id || 'hello'
10+
11+
// 2. 生成 HTML
12+
return c.html(html`
13+
<html>
14+
<head>
15+
<meta charset="UTF-8">
16+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/APlayer.min.css">
17+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/APlayer.min.js"></script>
18+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/Meting.min.js"></script>
19+
</head>
20+
<body>
21+
<meting-js
22+
server="${server}"
23+
type="${type}"
24+
id="${id}"
25+
api="${config.meting.url}/api?server=:server&type=:type&id=:id&r=:r"
26+
/>
27+
</body>
28+
</html>
29+
`)
30+
}

src/utils/cookie.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { readFile } from 'node:fs/promises'
2+
import { resolve } from 'node:path'
3+
import { URL } from 'node:url'
4+
import config from '../config.js'
5+
6+
/**
7+
* 读取指定平台的 cookie 文件
8+
* @param {string} server - 平台名称 (netease, tencent 等)
9+
* @returns {Promise<string>} cookie 字符串,失败时返回空字符串
10+
*/
11+
export async function readCookieFile (server) {
12+
try {
13+
const cookiePath = resolve(process.cwd(), 'cookie', server)
14+
const cookie = await readFile(cookiePath, 'utf-8')
15+
return cookie.trim()
16+
} catch (error) {
17+
return ''
18+
}
19+
}
20+
21+
/**
22+
* 验证 referrer 是否在允许的主机列表中
23+
* @param {string} referrer - 请求的 referrer
24+
* @returns {boolean} 是否允许
25+
*/
26+
export function isAllowedHost (referrer) {
27+
if (config.meting.cookie.allowHosts.length === 0) return true
28+
if (!referrer) return false
29+
30+
try {
31+
const url = new URL(referrer)
32+
const hostname = url.hostname.toLowerCase()
33+
return config.meting.cookie.allowHosts.includes(hostname)
34+
} catch (error) {
35+
return false
36+
}
37+
}

0 commit comments

Comments
 (0)