Skip to content

Commit

Permalink
support drpy req redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiram committed May 9, 2024
1 parent 09806c1 commit a84584f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
47 changes: 35 additions & 12 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ let windowState: any = setting.find({ key: "windowPosition" }).value || {
}
};
let reqIdMethod = {}; // 请求id与header列表
let reqIdRedirect = {};

const windowManage = (win) => {
const bounds = win.getBounds();
Expand Down Expand Up @@ -263,7 +264,7 @@ app.whenReady().then(async() => {
});

defaultSession.webRequest.onBeforeSendHeaders((details, callback) => {
const { requestHeaders, url } = details;
const { requestHeaders, url, id } = details;
const isLocalhostRef = (headerValue) => `${headerValue}`.includes('//localhost') || `${headerValue}`.includes('//127.0.0.1');

// 不处理本地地址
Expand Down Expand Up @@ -293,10 +294,18 @@ app.whenReady().then(async() => {
if (requestHeaders['custom-referer'] || headers?.['Referer']) requestHeaders['Referer'] = requestHeaders['custom-referer'] || headers?.['Referer'];
if (isLocalhostRef(requestHeaders['Referer'])) {
delete requestHeaders['Referer']
}
};

if (requestHeaders['custom-redirect'] === 'manual') {
delete requestHeaders['custom-redirect']
logger.info(requestHeaders)
logger.info(url)

reqIdRedirect[`${id}`] = headers;
};

// 清理不再需要的记录
delete reqIdMethod[details.id];
delete reqIdMethod[`${id}`];

callback({ requestHeaders });
});
Expand Down Expand Up @@ -353,21 +362,35 @@ app.whenReady().then(async() => {
}

defaultSession.webRequest.onHeadersReceived((details, callback) => {
const { id, responseHeaders, statusCode } = details;

const headersToRemove = ['X-Frame-Options', 'x-frame-options'];
const cookieHeader = details.responseHeaders?.['Set-Cookie'] || details.responseHeaders?.['set-cookie'];
const cookieHeader = responseHeaders?.['Set-Cookie'] || responseHeaders?.['set-cookie'];

for (const header of headersToRemove) {
if (details.responseHeaders?.[header]) {
delete details.responseHeaders[header];
}
}
if (responseHeaders?.[header]) {
delete responseHeaders[header];
};
};

if (cookieHeader) {
const updatedCookieHeader = cookieHeader.map((cookie) => `${cookie}; SameSite=None; Secure`);
delete details.responseHeaders!['Set-Cookie'];
details.responseHeaders!['custom-set-cookie'] = cookieHeader;
details.responseHeaders!['set-cookie'] = updatedCookieHeader;
}
delete responseHeaders!['Set-Cookie'];
responseHeaders!['custom-set-cookie'] = cookieHeader;
responseHeaders!['set-cookie'] = updatedCookieHeader;
};

if (reqIdRedirect[`${id}`] && statusCode === 302) {
callback({
cancel: false,
responseHeaders: {
...details.responseHeaders
},
statusLine: 'HTTP/1.1 200 OK' // 篡改响应头第一行
});
delete reqIdRedirect[`${id}`];
return;
};

callback({ cancel: false, responseHeaders: details.responseHeaders });
});
Expand Down
4 changes: 4 additions & 0 deletions src/renderer/src/utils/drpy/drpyInject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ interface RequestOptions {
headers?: { [key: string]: string };
withHeaders?: boolean;
buffer?: number;
redirect?: 0 | 1 | true | false;
}

interface Response {
Expand All @@ -38,8 +39,10 @@ const baseRequest = (_url: string, _object: RequestOptions, _js_type: number = 0
const withHeaders: boolean = _object.withHeaders || false;
const body: string = _object.body || '';
const bufferType: number = _object.buffer || 0;
const redirect = _object?.redirect === 1 || _object?.redirect === true ? 'follow' : 'manual';
let data: any = _object.data || {};
const headers = _object.headers || {};
if (redirect) headers['Redirect'] = redirect;
const emptyResult: Response = { content: '', body: '', headers: {} };

if (body && Object.keys(data).length == 0) {
Expand All @@ -57,6 +60,7 @@ const baseRequest = (_url: string, _object: RequestOptions, _js_type: number = 0
'Cookie': 'custom-cookie',
'User-Agent': 'custom-ua',
'Referer': 'custom-referer',
'Redirect': 'custom-redirect'
};

for (const [originalHeader, customHeader] of Object.entries(customHeaders)) {
Expand Down

0 comments on commit a84584f

Please sign in to comment.