Skip to content

Commit a9b11df

Browse files
committed
Detect and expose the system proxy settings
1 parent a149202 commit a9b11df

File tree

5 files changed

+113
-1
lines changed

5 files changed

+113
-1
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
declare module "@cypress/get-windows-proxy" {
2+
function getWindowsProxy(): {
3+
httpProxy: string | undefined;
4+
noProxy: string | undefined;
5+
} | undefined;
6+
7+
export = getWindowsProxy;
8+
}

package-lock.json

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"homepage": "https://github.com/httptoolkit/httptoolkit-server",
2727
"bugs": "https://github.com/httptoolkit/httptoolkit-server/issues",
2828
"dependencies": {
29+
"@cypress/get-windows-proxy": "^1.6.2",
2930
"@devicefarmer/adbkit": "^2.11.2",
3031
"@graphql-tools/schema": "^6.0.18",
3132
"@graphql-tools/utils": "^6.0.18",
@@ -53,6 +54,7 @@
5354
"iconv-lite": "^0.4.24",
5455
"lodash": "^4.17.21",
5556
"lookpath": "^1.2.1",
57+
"mac-system-proxy": "^1.0.0",
5658
"mime-types": "^2.1.27",
5759
"mockttp": "^2.0.0",
5860
"node-fetch": "^2.6.1",

src/api-server.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { reportError, addBreadcrumb } from './error-tracking';
1515
import { buildInterceptors, Interceptor, ActivationError } from './interceptors';
1616
import { ALLOWED_ORIGINS } from './constants';
1717
import { delay } from './util';
18+
import { getSystemProxyConfig } from './detect-proxy';
1819

1920
const ENABLE_PLAYGROUND = false;
2021

@@ -44,6 +45,7 @@ const typeDefs = `
4445
interceptors: [Interceptor!]!
4546
interceptor(id: ID!): Interceptor!
4647
networkInterfaces: Json
48+
systemProxy: Proxy
4749
}
4850
4951
type Mutation {
@@ -74,6 +76,11 @@ const typeDefs = `
7476
isActive(proxyPort: Int!): Boolean!
7577
}
7678
79+
type Proxy {
80+
proxyUrl: String!
81+
noProxy: [String!]
82+
}
83+
7784
enum MetadataType {
7885
SUMMARY,
7986
DETAILED
@@ -116,7 +123,8 @@ const buildResolvers = (
116123
// convenient to do it up front.
117124
certificateFingerprint: generateSPKIFingerprint(config.https.certContent)
118125
}),
119-
networkInterfaces: () => os.networkInterfaces()
126+
networkInterfaces: () => os.networkInterfaces(),
127+
systemProxy: () => getSystemProxyConfig()
120128
},
121129

122130
Mutation: {

src/detect-proxy.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { ProxyConfig } from 'mockttp';
2+
import getWindowsProxy = require('@cypress/get-windows-proxy');
3+
import { getMacSystemProxy, MacProxySettings } from 'mac-system-proxy';
4+
5+
import { reportError } from './error-tracking';
6+
7+
export async function getSystemProxyConfig(): Promise<ProxyConfig | undefined> {
8+
if (process.platform === 'win32') {
9+
const proxySettings = getWindowsProxy();
10+
if (!proxySettings || !proxySettings.httpProxy) return undefined;
11+
else return {
12+
proxyUrl: proxySettings.httpProxy,
13+
noProxy: proxySettings.noProxy
14+
? proxySettings.noProxy.split(',')
15+
: []
16+
};
17+
} else if (process.platform === 'darwin') {
18+
const proxySettings = await getMacSystemProxy().catch((e) => {
19+
reportError(e);
20+
return {} as MacProxySettings;
21+
});
22+
23+
const noProxy = proxySettings.ExceptionsList;
24+
25+
if (proxySettings.HTTPSEnable && proxySettings.HTTPSProxy && proxySettings.HTTPSPort) {
26+
return {
27+
proxyUrl: `https://${proxySettings.HTTPSProxy}:${proxySettings.HTTPSPort}`,
28+
noProxy
29+
};
30+
} else if (proxySettings.HTTPEnable && proxySettings.HTTPProxy && proxySettings.HTTPPort) {
31+
return {
32+
proxyUrl: `http://${proxySettings.HTTPProxy}:${proxySettings.HTTPPort}`,
33+
noProxy
34+
};
35+
} else if (proxySettings.SOCKSEnable && proxySettings.SOCKSProxy && proxySettings.SOCKSProxy) {
36+
return {
37+
proxyUrl: `socks://${proxySettings.SOCKSProxy}:${proxySettings.SOCKSProxy}`,
38+
noProxy
39+
};
40+
} else {
41+
return undefined;
42+
}
43+
} else {
44+
const proxyUrl = process.env.HTTPS_PROXY ||
45+
process.env.HTTP_PROXY ||
46+
process.env.https_proxy ||
47+
process.env.http_proxy;
48+
49+
if (!proxyUrl) return undefined;
50+
51+
const noProxy = process.env.NO_PROXY
52+
? process.env.NO_PROXY.split(',')
53+
: undefined;
54+
55+
return {
56+
proxyUrl,
57+
noProxy
58+
};
59+
}
60+
}

0 commit comments

Comments
 (0)