Skip to content

Commit

Permalink
fix douban headers
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiram committed May 15, 2024
1 parent ed81ddb commit 6bb673f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
44 changes: 35 additions & 9 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,33 +270,59 @@ app.whenReady().then(async () => {
const isLocalhostRef = (headerValue) =>
`${headerValue}`.includes('//localhost') || `${headerValue}`.includes('//127.0.0.1');

// 设置或清除请求头
const setOrRemoveHeader = (headerName: string, value: string) => {
if (value) {
requestHeaders[headerName] = value;
} else {
delete requestHeaders[headerName];
}
};

// 处理请求头
const processHeaders = (headerName: string, customHeaderName: string) => {
const customHeaderValue = requestHeaders[customHeaderName] || headers[headerName];
setOrRemoveHeader(headerName, customHeaderValue);
delete requestHeaders[customHeaderName];
};

// 不处理本地地址
if (isLocalhostRef(url)) {
callback({ requestHeaders });
return;
}

const origin = new URL(url).origin;
const headers = reqIdMethod[details.id] || {};

// 设置或清除可能的本地Origin
if (requestHeaders['Origin'] === origin) delete requestHeaders['Origin'];
if (isLocalhostRef(requestHeaders['Origin'])) {
const origin = requestHeaders['custom-origin'] || headers?.['Origin'];
if (origin && !isLocalhostRef(origin)) {
if (requestHeaders['Origin'] === new URL(url).origin) {
delete requestHeaders['Origin'];
} else requestHeaders['Origin'] = origin;
} else {
delete requestHeaders['Origin'];
}
delete requestHeaders['custom-origin'];

// 设置或清除 User-Agent
requestHeaders['User-Agent'] = requestHeaders['custom-ua'] || headers['User-Agent'] || uaState;
setOrRemoveHeader('User-Agent', requestHeaders['custom-ua'] || headers['User-Agent'] || uaState);
delete requestHeaders['custom-ua'];

// 处理 Host
processHeaders('Host', 'custom-host');

// 处理 Connection
processHeaders('Connection', 'custom-connection');

// 处理 Cookie
requestHeaders['Cookie'] = requestHeaders['custom-cookie'] || headers['Cookie'];
delete requestHeaders['custom-cookie'];
processHeaders('Cookie', 'custom-cookie');

// 设置或清除可能的本地 Referer
if (requestHeaders['custom-referer'] || headers?.['Referer'])
requestHeaders['Referer'] = requestHeaders['custom-referer'] || headers?.['Referer'];
if (isLocalhostRef(requestHeaders['Referer'])) {
const referer = requestHeaders['custom-referer'] || headers?.['Referer'];
if (referer && !isLocalhostRef(referer)) {
requestHeaders['Referer'] = referer;
} else {
delete requestHeaders['Referer'];
};
delete requestHeaders['custom-referer'];
Expand Down
3 changes: 3 additions & 0 deletions src/renderer/src/utils/drpy/drpy3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1495,6 +1495,9 @@ const request = (url: string, obj: any = undefined, ocr_flag: boolean = false) =
if (obj?.headers) {
const customHeaders = {
'custom-cookie': 'Cookie',
'custom-origin': 'Origin',
'custom-host': 'Host',
'custom-connection': 'Connection',
'custom-ua': 'User-Agent',
'custom-referer': 'Referer',
};
Expand Down
3 changes: 3 additions & 0 deletions src/renderer/src/utils/drpy/drpyInject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ const baseRequest = (_url: string, _object: RequestOptions, _js_type: number = 0

const customHeaders = {
Cookie: 'custom-cookie',
Origin: 'custom-origin',
Host: 'custom-host',
Connection: 'custom-connection',
'User-Agent': 'custom-ua',
Referer: 'custom-referer',
Redirect: 'custom-redirect',
Expand Down

0 comments on commit 6bb673f

Please sign in to comment.