Skip to content

Commit

Permalink
Merge pull request #250 from viktor44/hotfix/user_agent
Browse files Browse the repository at this point in the history
Fixing User-Agent header
  • Loading branch information
magne4000 authored Nov 15, 2023
2 parents 30254b6 + 9cade57 commit 34b5e8a
Show file tree
Hide file tree
Showing 18 changed files with 18 additions and 61 deletions.
7 changes: 4 additions & 3 deletions app/app-main.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { EventEmitter } from 'events';
import { parse } from 'url';
import { app, webContents } from 'electron';
import { app, session, webContents } from 'electron';
import log from 'electron-log';
import services from './services/servicesManager';
import { observer } from './services/lib/helpers';
import { handleError } from './services/api/helpers';
import { start } from './webui/webUIHandler';

require('./session');
import { enhanceSession } from './session';

export default class BrowserXAppMain extends EventEmitter {
init() {
Expand All @@ -18,10 +17,12 @@ export default class BrowserXAppMain extends EventEmitter {

initAppLifeCycle() {
app.on('ready', async () => {
enhanceSession(session.defaultSession);
// can register a onOpen function that should return a promise
if (typeof this.onOpen === 'function') {
await this.onOpen();
}

});
}

Expand Down
2 changes: 0 additions & 2 deletions app/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@ const loadCliWindow = async (command: string) => {
const initWorker = () => {
app.on('ready', () => {

enhanceSession(session.defaultSession);

loadWorker();

if (module.hot) {
Expand Down
6 changes: 5 additions & 1 deletion app/services/services/auto-updater/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ import { isPackaged } from '../../../utils/env';
import AutoUpdaterMock from './AutoUpdaterMock';
import { autoUpdater as autoUpdaterProd } from 'electron-updater';

export const autoUpdater = !isPackaged ? new AutoUpdaterMock() : autoUpdaterProd;
if (isPackaged) {
autoUpdaterProd.allowPrerelease = false;
}

export const autoUpdater = isPackaged ? autoUpdaterProd : new AutoUpdaterMock();
40 changes: 4 additions & 36 deletions app/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,7 @@ import { Session, OnBeforeSendHeadersListenerDetails, OnHeadersReceivedListenerD
HeadersReceivedResponse, BeforeSendResponse } from 'electron';
import enhanceWebRequest from 'electron-better-web-request';

const defaultUserAgent = [
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)',
'AppleWebKit/537.36 (KHTML, like Gecko)',
'Chrome/112.0.0.0',
'Safari/537.36',
].join(' ');

export const getUserAgentForApp = (url: string, currentUserAgent: string): string => {

if (url.startsWith('file://') || url.startsWith('http://localhost')) {
return currentUserAgent;
}
else if (url.startsWith('https://accounts.google.com')) {
return 'Chrome/87.0.4280.141';
}

return defaultUserAgent;
};
const defaultUserAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.5735.289 Safari/537.36';

const getHeaderName = (headerName: string, headers?: Record<string, string>): string | undefined => {
if (headers) {
Expand Down Expand Up @@ -56,32 +39,17 @@ export const getRefererForApp = (referer: string): string => {
export const enhanceSession = (session: Session) => {
enhanceWebRequest(session);

session.setUserAgent(defaultUserAgent);

session.webRequest.onBeforeSendHeaders(
(details: OnBeforeSendHeadersListenerDetails, callback: (beforeSendResponse: BeforeSendResponse) => void) => {
details.requestHeaders['User-Agent'] = getUserAgentForApp(details.url, session.getUserAgent());
details.referrer = getRefererForApp(details.referrer);
details.requestHeaders.Referer = details.referrer;
details.requestHeaders['Referer'] = details.referrer;

callback({
cancel: false,
requestHeaders: details.requestHeaders
});
}
);

// session.webRequest.onHeadersReceived(
// (details: OnHeadersReceivedListenerDetails, callback: (headersReceivedResponse: HeadersReceivedResponse) => void) => {

// let headers = details.responseHeaders;
// if (getHeader('X-Frame-Options', headers)) {
// headers = setHeader('X-Frame-Options', 'SAMEORIGIN', headers);
// }

// callback({
// cancel: false,
// responseHeaders: headers,
// statusLine: details.statusLine
// });
// }
// );
}
3 changes: 1 addition & 2 deletions manifests/definitions/114.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,5 @@
"instance_wording": "account",
"instance_label_tpl": "{{email}}",
"start_url_tpl": "https://inbox.google.com/{{#if moreThanOneIdentity}}u/{{userIdentity.profileData.email}}{{/if}}"
},
"bx_override_user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36 Edge/80.0.361.62"
}
}
1 change: 0 additions & 1 deletion manifests/definitions/122.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
],
"theme_color": "#25D366",
"scope": "https://web.whatsapp.com",
"bx_override_user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36",
"bx_keep_always_loaded": true,
"bx_legacy_service_id": "whatsapp",
"recommendedPosition": "4"
Expand Down
1 change: 0 additions & 1 deletion manifests/definitions/131.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@
"theme_color": "#5559AE",
"scope": "https://teams.live.com",
"bx_legacy_service_id": "microsoft-teams",
"bx_override_user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36",
"recommendedPosition": "25"
}
1 change: 0 additions & 1 deletion manifests/definitions/14.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
],
"theme_color": "#e75a4d",
"scope": "https://mail.google.com",
"bx_override_user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36 Edge/80.0.361.62",
"bx_keep_always_loaded": true,
"bx_single_page": true,
"bx_legacy_service_id": "gmail",
Expand Down
1 change: 0 additions & 1 deletion manifests/definitions/156.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@
"instance_label_tpl": "{{email}}",
"start_url_tpl": "https://keep.google.com/{{#if moreThanOneIdentity}}u/{{userIdentity.profileData.email}}{{/if}}"
},
"bx_override_user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36 Edge/80.0.361.62",
"recommendedPosition": "14"
}
1 change: 0 additions & 1 deletion manifests/definitions/16.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,5 @@
"start_url_tpl": "https://accounts.google.com/AddSession?passive=true&Email={{userIdentity.profileData.email}}&continue=https://drive.google.com/drive/{{#if moreThanOneIdentity}}u/{{userIdentity.profileData.email}}{{/if}}",
"new_page_url_tpl": "https://drive.google.com/drive/{{#if moreThanOneIdentity}}u/{{userIdentity.profileData.email}}{{/if}}"
},
"bx_override_user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36 Edge/80.0.361.62",
"recommendedPosition": "3"
}
1 change: 0 additions & 1 deletion manifests/definitions/18.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,5 @@
"instance_label_tpl": "{{email}}",
"start_url_tpl": "https://accounts.google.com/AddSession?passive=true&Email={{userIdentity.profileData.email}}&continue=https://calendar.google.com/calendar/{{#if moreThanOneIdentity}}b/{{userIdentity.profileData.email}}{{/if}}"
},
"bx_override_user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36 Edge/80.0.361.62",
"recommendedPosition": "2"
}
1 change: 0 additions & 1 deletion manifests/definitions/2165.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@
}
],
"theme_color": "#fe362a",
"bx_override_user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36 Edge/80.0.361.62",
"recommendedPosition": "16"
}
1 change: 0 additions & 1 deletion manifests/definitions/254.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"theme_color": "#C75B12",
"scope": "https://www.office.com",
"bx_legacy_service_id": "office-365",
"bx_override_user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36",
"extended_scopes": [
"https://office.live.com",
"https://www.onenote.com",
Expand Down
3 changes: 1 addition & 2 deletions manifests/definitions/272.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@
],
"theme_color": "#DC4B3E",
"scope": "https://maps.google.com",
"bx_legacy_service_id": "googlemaps",
"bx_override_user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36 Edge/80.0.361.62"
"bx_legacy_service_id": "googlemaps"
}
3 changes: 1 addition & 2 deletions manifests/definitions/284.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@
],
"theme_color": "#2494CA",
"scope": "https://workflowy.com",
"bx_legacy_service_id": "workflowy",
"bx_override_user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36"
"bx_legacy_service_id": "workflowy"
}
1 change: 0 additions & 1 deletion manifests/definitions/403.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@
"theme_color": "#4c8cf4",
"scope": "https://translate.google.com",
"bx_legacy_service_id": "google-translate",
"bx_override_user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36 Edge/80.0.361.62",
"recommendedPosition": "26"
}
3 changes: 1 addition & 2 deletions manifests/definitions/517.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,5 @@
"instance_wording": "account",
"instance_label_tpl": "{{email}}",
"start_url_tpl": "https://accounts.google.com/AddSession?passive=true&Email={{userIdentity.profileData.email}}&continue=https://chat.google.com/chat/u/{{userIdentity.profileData.email}}"
},
"bx_override_user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36"
}
}
3 changes: 1 addition & 2 deletions manifests/definitions/8563.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@
}
],
"theme_color": "#bcf",
"scope": "https://cs.chromium.org",
"bx_override_user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36 Edge/80.0.361.62"
"scope": "https://cs.chromium.org"
}

0 comments on commit 34b5e8a

Please sign in to comment.