夸克增加直链优先/转码优先切换
This commit is contained in:
@@ -3936,6 +3936,7 @@ const NetDiskConfigComponent = ({
|
||||
const [enabled, setEnabled] = useState(false);
|
||||
const [cookie, setCookie] = useState('');
|
||||
const [savePath, setSavePath] = useState('/');
|
||||
const [quarkPlayMode, setQuarkPlayMode] = useState<'direct_first' | 'transcode_first'>('direct_first');
|
||||
const [mobileEnabled, setMobileEnabled] = useState(false);
|
||||
const [mobileAuthorization, setMobileAuthorization] = useState('');
|
||||
const [baiduEnabled, setBaiduEnabled] = useState(false);
|
||||
@@ -3959,6 +3960,7 @@ const NetDiskConfigComponent = ({
|
||||
setEnabled(quark?.Enabled || false);
|
||||
setCookie(quark?.Cookie || '');
|
||||
setSavePath(quark?.SavePath || '/');
|
||||
setQuarkPlayMode(quark?.PlayMode === 'transcode_first' ? 'transcode_first' : 'direct_first');
|
||||
setMobileEnabled(mobile?.Enabled || false);
|
||||
setMobileAuthorization(mobile?.Authorization || '');
|
||||
setBaiduEnabled(config?.NetDiskConfig?.Baidu?.Enabled || false);
|
||||
@@ -3988,6 +3990,7 @@ const NetDiskConfigComponent = ({
|
||||
Enabled: enabled,
|
||||
Cookie: cookie,
|
||||
SavePath: savePath,
|
||||
PlayMode: quarkPlayMode,
|
||||
},
|
||||
Mobile: {
|
||||
Enabled: mobileEnabled,
|
||||
@@ -4306,6 +4309,24 @@ const NetDiskConfigComponent = ({
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className='block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2'>
|
||||
播放方式
|
||||
</label>
|
||||
<select
|
||||
value={quarkPlayMode}
|
||||
onChange={(e) => setQuarkPlayMode(e.target.value === 'transcode_first' ? 'transcode_first' : 'direct_first')}
|
||||
disabled={!enabled}
|
||||
className='w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100 focus:ring-2 focus:ring-blue-500 focus:border-transparent disabled:opacity-50 disabled:cursor-not-allowed'
|
||||
>
|
||||
<option value='direct_first'>直链优先</option>
|
||||
<option value='transcode_first'>转码优先</option>
|
||||
</select>
|
||||
<p className='text-xs text-gray-500 dark:text-gray-400 mt-1'>
|
||||
直链优先会优先使用原画下载地址;转码优先会优先使用夸克转码播放地址。
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div className='flex gap-3'>
|
||||
<button
|
||||
onClick={handleValidate}
|
||||
|
||||
@@ -79,6 +79,7 @@ export async function POST(request: NextRequest) {
|
||||
Enabled: Boolean(Quark?.Enabled),
|
||||
Cookie: normalizedCookie,
|
||||
SavePath: Quark?.SavePath || '/',
|
||||
PlayMode: Quark?.PlayMode === 'transcode_first' ? 'transcode_first' : 'direct_first',
|
||||
};
|
||||
adminConfig.NetDiskConfig.Mobile = {
|
||||
Enabled: Boolean(Mobile?.Enabled),
|
||||
|
||||
@@ -27,7 +27,7 @@ export async function GET(request: NextRequest) {
|
||||
return NextResponse.json({ error: '无效的 episodeIndex' }, { status: 400 });
|
||||
}
|
||||
|
||||
const { session, cookie, savePath } = await resolveQuarkSession(id);
|
||||
const { session, cookie, savePath, playMode } = await resolveQuarkSession(id);
|
||||
const file = session.files[episodeIndex];
|
||||
if (!file) {
|
||||
return NextResponse.json({ error: '播放文件不存在' }, { status: 404 });
|
||||
@@ -52,45 +52,142 @@ export async function GET(request: NextRequest) {
|
||||
}
|
||||
refreshQuarkNetdiskSession(id);
|
||||
|
||||
const playUrls = await getQuarkPlayUrls(cookie, savedFileId);
|
||||
const playUrls = await getQuarkPlayUrls(cookie, savedFileId, playMode);
|
||||
const selected = playUrls.find((item) => item.name === quality) || playUrls[0];
|
||||
if (!selected) {
|
||||
const candidates = selected
|
||||
? [
|
||||
selected,
|
||||
...playUrls.filter((item) => item.url !== selected.url),
|
||||
]
|
||||
: [];
|
||||
if (candidates.length === 0) {
|
||||
return NextResponse.json({ error: '未获取到夸克播放地址' }, { status: 500 });
|
||||
}
|
||||
|
||||
const range = request.headers.get('range');
|
||||
const passthroughHeaderNames = [
|
||||
'accept',
|
||||
'accept-language',
|
||||
'accept-encoding',
|
||||
'connection',
|
||||
'sec-fetch-dest',
|
||||
'sec-fetch-mode',
|
||||
'sec-fetch-site',
|
||||
];
|
||||
const passthroughHeaders: Record<string, string> = {};
|
||||
for (const name of passthroughHeaderNames) {
|
||||
const value = request.headers.get(name);
|
||||
if (value) passthroughHeaders[name] = value;
|
||||
}
|
||||
|
||||
try {
|
||||
let upstream: Response | null = null;
|
||||
let lastStatus = 500;
|
||||
const headerProfiles = [
|
||||
{
|
||||
name: 'quark-empty-ua',
|
||||
headers: {
|
||||
...passthroughHeaders,
|
||||
...getQuarkPlayHeaders(cookie),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'quark-api-ua',
|
||||
headers: {
|
||||
...passthroughHeaders,
|
||||
cookie,
|
||||
referer: 'https://pan.quark.cn/',
|
||||
'user-agent':
|
||||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) quark-cloud-drive/2.5.20 Chrome/100.0.4896.160 Electron/18.3.5.4-b478491100 Safari/537.36 Channel/pckk_other_ch',
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'quark-no-ua',
|
||||
headers: {
|
||||
...passthroughHeaders,
|
||||
cookie,
|
||||
referer: 'https://pan.quark.cn/',
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'browser-origin',
|
||||
headers: {
|
||||
...passthroughHeaders,
|
||||
cookie,
|
||||
origin: 'https://pan.quark.cn',
|
||||
referer: 'https://pan.quark.cn/',
|
||||
'user-agent':
|
||||
request.headers.get('user-agent') ||
|
||||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36',
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
for (const candidate of candidates) {
|
||||
for (const profile of headerProfiles) {
|
||||
const abortController = new AbortController();
|
||||
const timeoutId = setTimeout(() => abortController.abort(), 300000);
|
||||
|
||||
try {
|
||||
const upstream = await fetch(selected.url, {
|
||||
headers: {
|
||||
...getQuarkPlayHeaders(cookie),
|
||||
const requestHeaders = {
|
||||
...profile.headers,
|
||||
...(range ? { Range: range } : {}),
|
||||
},
|
||||
};
|
||||
const response = await fetch(candidate.url, {
|
||||
headers: requestHeaders,
|
||||
cache: 'no-store',
|
||||
signal: abortController.signal,
|
||||
});
|
||||
|
||||
clearTimeout(timeoutId);
|
||||
if (response.ok && response.body) {
|
||||
upstream = response;
|
||||
if (candidate.url !== selected.url) {
|
||||
console.warn(`[quark] fallback play url used: ${selected.name} -> ${candidate.name}`);
|
||||
}
|
||||
if (profile.name !== 'quark-empty-ua') {
|
||||
console.warn(`[quark] fallback header profile used: ${profile.name}`);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (!upstream.ok || !upstream.body) {
|
||||
lastStatus = response.status || 500;
|
||||
const errorText = await response.text().catch(() => '');
|
||||
console.warn(
|
||||
`[quark] play url failed: ${candidate.name} / ${profile.name} (${lastStatus}) ${errorText.slice(0, 200)}`
|
||||
);
|
||||
} catch (error) {
|
||||
clearTimeout(timeoutId);
|
||||
if (error instanceof Error && error.name === 'AbortError') {
|
||||
throw error;
|
||||
}
|
||||
console.warn(`[quark] play url request failed: ${candidate.name} / ${profile.name}`, error);
|
||||
}
|
||||
}
|
||||
|
||||
if (upstream) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!upstream) {
|
||||
return NextResponse.json(
|
||||
{ error: `夸克视频代理失败 (${upstream.status})` },
|
||||
{ status: upstream.status || 500 }
|
||||
{ error: `夸克视频代理失败 (${lastStatus})` },
|
||||
{ status: lastStatus }
|
||||
);
|
||||
}
|
||||
|
||||
const response = upstream as Response & { body: ReadableStream<Uint8Array> };
|
||||
const responseHeaders = new Headers();
|
||||
const copyHeaders = ['content-type', 'content-length', 'content-range', 'accept-ranges', 'etag', 'last-modified'];
|
||||
copyHeaders.forEach((name) => {
|
||||
const value = upstream.headers.get(name);
|
||||
const value = response.headers.get(name);
|
||||
if (value) responseHeaders.set(name, value);
|
||||
});
|
||||
responseHeaders.set('Cache-Control', 'private, no-store');
|
||||
|
||||
const { readable, writable } = new TransformStream();
|
||||
const reader = upstream.body.getReader();
|
||||
const reader = response.body.getReader();
|
||||
|
||||
void (async () => {
|
||||
const writer = writable.getWriter();
|
||||
@@ -125,11 +222,10 @@ export async function GET(request: NextRequest) {
|
||||
})();
|
||||
|
||||
return new Response(readable, {
|
||||
status: range && upstream.headers.get('content-range') ? 206 : upstream.status,
|
||||
status: range && response.headers.get('content-range') ? 206 : response.status,
|
||||
headers: responseHeaders,
|
||||
});
|
||||
} catch (error) {
|
||||
clearTimeout(timeoutId);
|
||||
if (error instanceof Error && error.name === 'AbortError') {
|
||||
return NextResponse.json({ error: '夸克网盘代理超时' }, { status: 504 });
|
||||
}
|
||||
|
||||
@@ -152,6 +152,7 @@ export interface AdminConfig {
|
||||
Enabled: boolean;
|
||||
Cookie: string;
|
||||
SavePath: string;
|
||||
PlayMode?: 'direct_first' | 'transcode_first';
|
||||
};
|
||||
Mobile?: {
|
||||
Enabled: boolean;
|
||||
|
||||
@@ -724,6 +724,7 @@ export function configSelfCheck(adminConfig: AdminConfig): AdminConfig {
|
||||
Enabled: false,
|
||||
Cookie: '',
|
||||
SavePath: '/',
|
||||
PlayMode: 'direct_first',
|
||||
},
|
||||
Mobile: {
|
||||
Enabled: false,
|
||||
@@ -761,8 +762,12 @@ export function configSelfCheck(adminConfig: AdminConfig): AdminConfig {
|
||||
Enabled: false,
|
||||
Cookie: '',
|
||||
SavePath: '/',
|
||||
PlayMode: 'direct_first',
|
||||
};
|
||||
}
|
||||
if (!adminConfig.NetDiskConfig.Quark.PlayMode) {
|
||||
adminConfig.NetDiskConfig.Quark.PlayMode = 'direct_first';
|
||||
}
|
||||
|
||||
if (!adminConfig.NetDiskConfig.Mobile) {
|
||||
adminConfig.NetDiskConfig.Mobile = {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { getConfig } from '@/lib/config';
|
||||
|
||||
import { listQuarkShareVideos } from './quark.client';
|
||||
import type { QuarkPlayMode } from './quark.client';
|
||||
import {
|
||||
createQuarkNetdiskSession,
|
||||
getQuarkNetdiskSession,
|
||||
@@ -33,5 +34,12 @@ export async function resolveQuarkSession(id: string) {
|
||||
throw new Error('夸克网盘播放信息恢复失败');
|
||||
}
|
||||
|
||||
return { session, cookie: quarkConfig.Cookie, savePath: quarkConfig.SavePath || '/' };
|
||||
const playMode: QuarkPlayMode = quarkConfig.PlayMode === 'transcode_first' ? 'transcode_first' : 'direct_first';
|
||||
|
||||
return {
|
||||
session,
|
||||
cookie: quarkConfig.Cookie,
|
||||
savePath: quarkConfig.SavePath || '/',
|
||||
playMode,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
const QUARK_SHARE_API_BASE = 'https://drive-h.quark.cn/1/clouddrive';
|
||||
const QUARK_DRIVE_API_BASE = 'https://drive-pc.quark.cn/1/clouddrive';
|
||||
const QUARK_QUERY = 'pr=ucpro&fr=pc';
|
||||
const QUARK_API_USER_AGENT =
|
||||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) quark-cloud-drive/2.5.20 Chrome/100.0.4896.160 Electron/18.3.5.4-b478491100 Safari/537.36 Channel/pckk_other_ch';
|
||||
|
||||
export interface QuarkShareLinkInfo {
|
||||
pwdId: string;
|
||||
@@ -40,6 +42,8 @@ export interface QuarkShareVideoListResult {
|
||||
}>;
|
||||
}
|
||||
|
||||
export type QuarkPlayMode = 'direct_first' | 'transcode_first';
|
||||
|
||||
const VIDEO_EXTENSIONS = [
|
||||
'.mp4',
|
||||
'.mkv',
|
||||
@@ -69,20 +73,16 @@ function getHeaders(cookie: string): HeadersInit {
|
||||
return {
|
||||
'content-type': 'application/json',
|
||||
cookie,
|
||||
origin: 'https://pan.quark.cn',
|
||||
referer: 'https://pan.quark.cn/',
|
||||
'user-agent':
|
||||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36',
|
||||
'user-agent': QUARK_API_USER_AGENT,
|
||||
};
|
||||
}
|
||||
|
||||
export function getQuarkPlayHeaders(cookie: string): Record<string, string> {
|
||||
return {
|
||||
cookie,
|
||||
origin: 'https://pan.quark.cn',
|
||||
referer: 'https://pan.quark.cn/',
|
||||
'user-agent':
|
||||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36',
|
||||
'user-agent': QUARK_API_USER_AGENT,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -685,7 +685,8 @@ export async function saveQuarkShareFile(
|
||||
|
||||
export async function getQuarkPlayUrls(
|
||||
cookie: string,
|
||||
savedFileId: string
|
||||
savedFileId: string,
|
||||
playMode: QuarkPlayMode = 'direct_first'
|
||||
): Promise<Array<{ name: string; url: string; priority: number }>> {
|
||||
const safeCookie = assertQuarkCookieHeaderSafe(cookie);
|
||||
const headers = getHeaders(safeCookie);
|
||||
@@ -710,8 +711,8 @@ export async function getQuarkPlayUrls(
|
||||
priority: 9999,
|
||||
});
|
||||
}
|
||||
} catch {
|
||||
// ignore download failure, continue transcoding fallback
|
||||
} catch (error) {
|
||||
console.warn('[quark] get original download url failed:', error);
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -748,12 +749,18 @@ export async function getQuarkPlayUrls(
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// ignore transcoding failure
|
||||
} catch (error) {
|
||||
console.warn('[quark] get transcoding play url failed:', error);
|
||||
}
|
||||
|
||||
const deduped = urls.filter((item, index, array) => array.findIndex((v) => v.url === item.url) === index);
|
||||
deduped.sort((a, b) => b.priority - a.priority);
|
||||
deduped.sort((a, b) => {
|
||||
if (playMode === 'transcode_first') {
|
||||
if (a.name === '原画' && b.name !== '原画') return 1;
|
||||
if (a.name !== '原画' && b.name === '原画') return -1;
|
||||
}
|
||||
return b.priority - a.priority;
|
||||
});
|
||||
|
||||
if (deduped.length === 0) {
|
||||
throw new Error('未获取到夸克播放地址');
|
||||
|
||||
Reference in New Issue
Block a user