Skip to content

Commit aa200a7

Browse files
committed
Restructure proxy check a little for error catching & clarity
1 parent f96ee7f commit aa200a7

File tree

1 file changed

+45
-47
lines changed

1 file changed

+45
-47
lines changed

src/index.ts

Lines changed: 45 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -455,59 +455,57 @@ if (!amMainInstance) {
455455
process.exit(2);
456456
});
457457

458-
// Check we're happy using the default proxy settings: true if so, false if not.
459-
const proxyCheck = getSystemProxy()
460-
.catch((e) => {
461-
reportError(e);
462-
return undefined;
463-
})
464-
.then((proxyConfig) => {
465-
// If there's no proxy then using the default settings is totally fine:
466-
if (!proxyConfig) return true;
467-
468-
// If the proxy is local, don't use it (this probably means HTTP Toolkit itself is the
469-
// system proxy, which causes lots of problems - we avoid in that case).
470-
const proxyHostname = new URL(proxyConfig.proxyUrl).hostname;
471-
if (proxyHostname === 'localhost' || proxyHostname.startsWith('127.0.0')) return false;
472-
473-
// Otherwise: we have a valid remote proxy server. We should use it - it might be
474-
// required for us to get any connectivity at all.
475-
return true;
476-
});
477-
478-
proxyCheck.then((shouldUseProxy) => {
479-
if (!shouldUseProxy) {
480-
console.warn("Ignoring localhost system proxy setting");
458+
// Check we're happy using the default proxy settings
459+
getSystemProxy().then((proxyConfig) => {
460+
let shouldDisableProxy = false;
461+
462+
if (proxyConfig) {
463+
// If the proxy is local, we don't use it (this probably means HTTP Toolkit itself is the
464+
// system proxy, which causes lots of problems - we avoid in that case).
465+
const proxyHostname = new URL(proxyConfig.proxyUrl).hostname;
466+
if (proxyHostname === 'localhost' || proxyHostname.startsWith('127.0.0')) {
467+
shouldDisableProxy = true;
468+
}
481469

482-
// If the proxy is unsuitable (there is none, or its localhost and so might be a loop) then
483-
// we drop all proxy config and try to connect to everything directly instead.
470+
// If there's no proxy config, if we can't easily parse it, or if it's not localhost
471+
// then we use it as normal - it might be required for connectivity.
472+
}
484473

485-
// This tries to avoid passing bad config through to the server. Nice to do but not critical,
486-
// since upstream (i.e. everything except updates & error reports) is configured by the UI.
487-
['http_proxy', 'HTTP_PROXY', 'https_proxy', 'HTTPS_PROXY'].forEach((v) => delete process.env[v]);
474+
if (shouldDisableProxy) {
475+
console.warn("Ignoring localhost system proxy setting");
488476

489-
if (app.isReady()) {
490-
// If the app has already started at this point, things get more messy.
477+
// If the proxy is unsuitable (there is none, or its localhost and so might be a loop) then
478+
// we drop all proxy config and try to connect to everything directly instead.
491479

492-
// First, we change the default session to avoid the proxy:
493-
session.defaultSession.setProxy({ mode: 'direct' });
480+
// This tries to avoid passing bad config through to the server. Nice to do but not critical,
481+
// since upstream (i.e. everything except updates & error reports) is checked & configured by the UI.
482+
['http_proxy', 'HTTP_PROXY', 'https_proxy', 'HTTPS_PROXY'].forEach((v) => delete process.env[v]);
494483

495-
// Then we have to reset any existing windows, so that they avoid the proxy. They're
496-
// probably broken anyway at this stage.
497-
windows.forEach(window => {
498-
const { session } = window.webContents;
499-
session.closeAllConnections()
500-
session.setProxy({ mode: 'direct' });
501-
window.reload();
502-
});
503-
} else {
504-
// If the app hasn't started yet it's easy: we disable Chromium's proxy detection entirely
505-
app.commandLine.appendSwitch('no-proxy-server');
484+
if (!app.isReady()) {
485+
// If the app hasn't started yet it's easy: we disable Chromium's proxy detection entirely
486+
app.commandLine.appendSwitch('no-proxy-server');
487+
} else {
488+
// If the app has already started at this point, things get more messy.
489+
490+
// First, we change the default session to avoid the proxy:
491+
session.defaultSession.setProxy({ mode: 'direct' });
492+
493+
// Then we have to reset any existing windows, so that they avoid the proxy. They're
494+
// probably broken anyway at this stage.
495+
windows.forEach(window => {
496+
const { session } = window.webContents;
497+
session.closeAllConnections()
498+
session.setProxy({ mode: 'direct' });
499+
window.reload();
500+
});
501+
}
506502
}
507-
508-
}
509-
// Otherwise we just let Electron use the defaults - no problem at all.
510-
});
503+
// Otherwise we just let Electron use the defaults - no problem at all.
504+
})
505+
.catch((e) => {
506+
reportError(e);
507+
return undefined;
508+
});
511509

512510
Promise.all([
513511
cleanupOldServers().catch(console.log),

0 commit comments

Comments
 (0)