Skip to content

Commit

Permalink
feat(Service): Add modifyRequestHeaders for services (馃檶 @kamyweb)
Browse files Browse the repository at this point in the history
  • Loading branch information
adlk committed Dec 29, 2021
2 parents 9d6a59f + 10ca850 commit 5ecc730
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion appveyor.yml
Expand Up @@ -30,4 +30,4 @@ artifacts:

branches:
except:
- i18n
- i18n
24 changes: 24 additions & 0 deletions src/index.js
Expand Up @@ -5,6 +5,7 @@ import {
BrowserWindow,
shell,
ipcMain,
session,
} from 'electron';

// import isDevMode from 'electron-is-dev';
Expand Down Expand Up @@ -394,6 +395,29 @@ ipcMain.on('feature-basic-auth-credentials', (e, { user, password }) => {
authCallback = noop;
});

ipcMain.on('modifyRequestHeaders', (e, { modifiedRequestHeaders, serviceId }) => {
debug('Received modifyRequestHeaders', modifiedRequestHeaders, serviceId);
modifiedRequestHeaders.forEach((headerFilterSet) => {
const { headers, requestFilters } = headerFilterSet;
session.fromPartition(`persist:service-${serviceId}`).webRequest.onBeforeSendHeaders(requestFilters, (details, callback) => {
for (const key in headers) {
if (Object.prototype.hasOwnProperty.call(headers, key)) {
const value = headers[key];
if (value === 'RefererHost') {
if (Object.prototype.hasOwnProperty.call(details.requestHeaders, 'Referer')) {
const { hostname } = new URL(details.requestHeaders.Referer);
details.requestHeaders[key] = `https://${hostname}`;
}
} else {
details.requestHeaders[key] = value;
}
}
}
callback({ requestHeaders: details.requestHeaders });
});
});
});

ipcMain.on('feature-basic-auth-cancel', () => {
debug('Cancel basic auth');

Expand Down
15 changes: 15 additions & 0 deletions src/models/Service.js
Expand Up @@ -2,6 +2,7 @@ import { webContents } from '@electron/remote';
import {
computed, observable, autorun,
} from 'mobx';
import { ipcRenderer } from 'electron';
import path from 'path';
import normalizeUrl from 'normalize-url';

Expand Down Expand Up @@ -231,6 +232,20 @@ export default class Service {
initializeWebViewEvents({ handleIPCMessage, openWindow, stores }) {
const webviewWebContents = webContents.fromId(this.webview.getWebContentsId());
// If the recipe has implemented modifyRequestHeaders,
// Send those headers to ipcMain so that it can be set in session
if (typeof this.recipe.modifyRequestHeaders === 'function') {
const modifiedRequestHeaders = this.recipe.modifyRequestHeaders();
console.warn(modifiedRequestHeaders);
debug(this.name, 'modifiedRequestHeaders', modifiedRequestHeaders);
ipcRenderer.send('modifyRequestHeaders', {
modifiedRequestHeaders,
serviceId: this.id,
});
} else {
debug(this.name, 'modifyRequestHeaders is not defined in the recipe');
}
const handleUserAgent = (url, forwardingHack = false) => {
if (url.startsWith('https://accounts.google.com')) {
if (!this.chromelessUserAgent) {
Expand Down

0 comments on commit 5ecc730

Please sign in to comment.