@modern-js/plugin-proxy 1.2.1 → 1.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +35 -0
- package/dist/js/modern/index.js +33 -32
- package/dist/js/modern/types.js +1 -0
- package/dist/js/node/index.js +41 -41
- package/dist/js/node/types.js +3 -0
- package/dist/types/index.d.ts +5 -8
- package/dist/types/types.d.ts +7 -0
- package/dist/types/utils/createProxyRule.d.ts +1 -1
- package/modern.config.js +5 -1
- package/package.json +23 -13
- package/src/index.ts +0 -44
- package/src/utils/createProxyRule.ts +0 -50
- package/src/utils/execSync.ts +0 -21
- package/src/utils/macCAManager.ts +0 -58
- package/src/utils/macProxyManager.ts +0 -31
- package/src/utils/whistleProxy.ts +0 -67
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,40 @@
|
|
|
1
1
|
# @modern-js/plugin-proxy
|
|
2
2
|
|
|
3
|
+
## 1.2.4
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 681a1ff9: feat: remove unnecessary peerDependencies
|
|
8
|
+
- Updated dependencies [c2046f37]
|
|
9
|
+
- @modern-js/utils@1.3.6
|
|
10
|
+
|
|
11
|
+
## 1.2.3
|
|
12
|
+
|
|
13
|
+
### Patch Changes
|
|
14
|
+
|
|
15
|
+
- 9b2640fe: fix: dev.proxy type not work
|
|
16
|
+
- Updated dependencies [5bf5868d]
|
|
17
|
+
- Updated dependencies [80d3cfb7]
|
|
18
|
+
- Updated dependencies [42c6b136]
|
|
19
|
+
- Updated dependencies [4e7dcbd5]
|
|
20
|
+
- Updated dependencies [9e8bc4ab]
|
|
21
|
+
- Updated dependencies [0c556e59]
|
|
22
|
+
- Updated dependencies [80d8ddfe]
|
|
23
|
+
- Updated dependencies [2008fdbd]
|
|
24
|
+
- @modern-js/utils@1.3.5
|
|
25
|
+
- @modern-js/core@1.5.0
|
|
26
|
+
|
|
27
|
+
## 1.2.2
|
|
28
|
+
|
|
29
|
+
### Patch Changes
|
|
30
|
+
|
|
31
|
+
- 83166714: change .npmignore
|
|
32
|
+
- Updated dependencies [83166714]
|
|
33
|
+
- Updated dependencies [c3de9882]
|
|
34
|
+
- Updated dependencies [33ff48af]
|
|
35
|
+
- @modern-js/core@1.3.2
|
|
36
|
+
- @modern-js/utils@1.2.2
|
|
37
|
+
|
|
3
38
|
## 1.2.1
|
|
4
39
|
|
|
5
40
|
### Patch Changes
|
package/dist/js/modern/index.js
CHANGED
|
@@ -1,42 +1,43 @@
|
|
|
1
|
-
import { createPlugin, useAppContext, useResolvedConfigContext } from '@modern-js/core';
|
|
2
1
|
import { PLUGIN_SCHEMAS } from '@modern-js/utils';
|
|
3
2
|
import { createProxyRule } from "./utils/createProxyRule";
|
|
4
3
|
import WhistleProxy from "./utils/whistleProxy";
|
|
5
|
-
|
|
6
|
-
export default
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
import "./types";
|
|
5
|
+
export default (() => {
|
|
6
|
+
let proxyServer;
|
|
7
|
+
return {
|
|
8
|
+
name: '@modern-js/plugin-proxy',
|
|
9
|
+
setup: api => ({
|
|
10
|
+
validateSchema() {
|
|
11
|
+
return PLUGIN_SCHEMAS['@modern-js/plugin-proxy'];
|
|
12
|
+
},
|
|
10
13
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
} = useAppContext();
|
|
19
|
-
/* eslint-enable react-hooks/rules-of-hooks */
|
|
14
|
+
async afterDev() {
|
|
15
|
+
const {
|
|
16
|
+
dev
|
|
17
|
+
} = api.useResolvedConfigContext();
|
|
18
|
+
const {
|
|
19
|
+
internalDirectory
|
|
20
|
+
} = api.useAppContext();
|
|
20
21
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
if (!(dev !== null && dev !== void 0 && dev.proxy)) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
24
25
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
26
|
+
const rule = createProxyRule(internalDirectory, dev.proxy);
|
|
27
|
+
proxyServer = new WhistleProxy({
|
|
28
|
+
port: 8899,
|
|
29
|
+
rule
|
|
30
|
+
});
|
|
31
|
+
await proxyServer.start();
|
|
32
|
+
},
|
|
32
33
|
|
|
33
|
-
|
|
34
|
-
|
|
34
|
+
beforeExit() {
|
|
35
|
+
var _proxyServer;
|
|
35
36
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
37
|
+
// terminate whistle proxy
|
|
38
|
+
(_proxyServer = proxyServer) === null || _proxyServer === void 0 ? void 0 : _proxyServer.close();
|
|
39
|
+
}
|
|
39
40
|
|
|
40
|
-
})
|
|
41
|
-
|
|
41
|
+
})
|
|
42
|
+
};
|
|
42
43
|
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import '@modern-js/core';
|
package/dist/js/node/index.js
CHANGED
|
@@ -5,54 +5,54 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
|
|
8
|
-
var _core = require("@modern-js/core");
|
|
9
|
-
|
|
10
8
|
var _utils = require("@modern-js/utils");
|
|
11
9
|
|
|
12
10
|
var _createProxyRule = require("./utils/createProxyRule");
|
|
13
11
|
|
|
14
12
|
var _whistleProxy = _interopRequireDefault(require("./utils/whistleProxy"));
|
|
15
13
|
|
|
14
|
+
require("./types");
|
|
15
|
+
|
|
16
16
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
})
|
|
55
|
-
|
|
56
|
-
}
|
|
18
|
+
var _default = () => {
|
|
19
|
+
let proxyServer;
|
|
20
|
+
return {
|
|
21
|
+
name: '@modern-js/plugin-proxy',
|
|
22
|
+
setup: api => ({
|
|
23
|
+
validateSchema() {
|
|
24
|
+
return _utils.PLUGIN_SCHEMAS['@modern-js/plugin-proxy'];
|
|
25
|
+
},
|
|
26
|
+
|
|
27
|
+
async afterDev() {
|
|
28
|
+
const {
|
|
29
|
+
dev
|
|
30
|
+
} = api.useResolvedConfigContext();
|
|
31
|
+
const {
|
|
32
|
+
internalDirectory
|
|
33
|
+
} = api.useAppContext();
|
|
34
|
+
|
|
35
|
+
if (!(dev !== null && dev !== void 0 && dev.proxy)) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const rule = (0, _createProxyRule.createProxyRule)(internalDirectory, dev.proxy);
|
|
40
|
+
proxyServer = new _whistleProxy.default({
|
|
41
|
+
port: 8899,
|
|
42
|
+
rule
|
|
43
|
+
});
|
|
44
|
+
await proxyServer.start();
|
|
45
|
+
},
|
|
46
|
+
|
|
47
|
+
beforeExit() {
|
|
48
|
+
var _proxyServer;
|
|
49
|
+
|
|
50
|
+
// terminate whistle proxy
|
|
51
|
+
(_proxyServer = proxyServer) === null || _proxyServer === void 0 ? void 0 : _proxyServer.close();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
})
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
57
|
|
|
58
58
|
exports.default = _default;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
import type { CliPlugin } from '@modern-js/core';
|
|
2
|
+
import './types';
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
interface DevConfig {
|
|
7
|
-
proxy?: ProxyOptions;
|
|
8
|
-
}
|
|
9
|
-
}
|
|
4
|
+
declare const _default: () => CliPlugin;
|
|
5
|
+
|
|
6
|
+
export default _default;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { ProxyOptions } from '
|
|
1
|
+
import type { ProxyOptions } from '../types';
|
|
2
2
|
export declare const createProxyRule: (appDirectory: string, proxyOptions: ProxyOptions) => string;
|
package/modern.config.js
CHANGED
package/package.json
CHANGED
|
@@ -11,12 +11,22 @@
|
|
|
11
11
|
"modern",
|
|
12
12
|
"modern.js"
|
|
13
13
|
],
|
|
14
|
-
"version": "1.2.
|
|
14
|
+
"version": "1.2.4",
|
|
15
15
|
"jsnext:source": "./src/index.ts",
|
|
16
16
|
"types": "./dist/types/index.d.ts",
|
|
17
17
|
"main": "./dist/js/node/index.js",
|
|
18
18
|
"module": "./dist/js/treeshaking/index.js",
|
|
19
19
|
"jsnext:modern": "./dist/js/modern/index.js",
|
|
20
|
+
"typesVersions": {
|
|
21
|
+
"*": {
|
|
22
|
+
".": [
|
|
23
|
+
"./dist/types/index.d.ts"
|
|
24
|
+
],
|
|
25
|
+
"types": [
|
|
26
|
+
"./dist/types/types.d.ts"
|
|
27
|
+
]
|
|
28
|
+
}
|
|
29
|
+
},
|
|
20
30
|
"exports": {
|
|
21
31
|
".": {
|
|
22
32
|
"node": {
|
|
@@ -29,38 +39,38 @@
|
|
|
29
39
|
"./cli": {
|
|
30
40
|
"jsnext:source": "./src/index.ts",
|
|
31
41
|
"default": "./dist/js/node/index.js"
|
|
42
|
+
},
|
|
43
|
+
"./types": {
|
|
44
|
+
"node": {
|
|
45
|
+
"import": "./dist/js/modern/types.js",
|
|
46
|
+
"require": "./dist/js/node/types.js",
|
|
47
|
+
"types": "./dist/types/types.d.ts"
|
|
48
|
+
},
|
|
49
|
+
"default": "./dist/js/treeshaking/types.js"
|
|
32
50
|
}
|
|
33
51
|
},
|
|
34
52
|
"dependencies": {
|
|
35
53
|
"@babel/runtime": "^7",
|
|
36
|
-
"@modern-js/utils": "^1.
|
|
54
|
+
"@modern-js/utils": "^1.3.6",
|
|
37
55
|
"whistle": "^2.7.18"
|
|
38
56
|
},
|
|
39
57
|
"devDependencies": {
|
|
40
58
|
"@types/jest": "^26",
|
|
41
59
|
"@types/node": "^14",
|
|
42
60
|
"typescript": "^4",
|
|
43
|
-
"@modern-js/core": "^1.
|
|
61
|
+
"@modern-js/core": "^1.6.0",
|
|
44
62
|
"@scripts/build": "0.0.0",
|
|
45
63
|
"jest": "^27",
|
|
46
64
|
"@scripts/jest-config": "0.0.0"
|
|
47
65
|
},
|
|
48
|
-
"peerDependencies": {
|
|
49
|
-
"@modern-js/core": "^1.3.1"
|
|
50
|
-
},
|
|
51
66
|
"sideEffects": false,
|
|
52
|
-
"modernConfig": {
|
|
53
|
-
"output": {
|
|
54
|
-
"packageMode": "node-js"
|
|
55
|
-
}
|
|
56
|
-
},
|
|
57
67
|
"publishConfig": {
|
|
58
68
|
"registry": "https://registry.npmjs.org/",
|
|
59
|
-
"access": "public"
|
|
60
|
-
"types": "./dist/types/index.d.ts"
|
|
69
|
+
"access": "public"
|
|
61
70
|
},
|
|
62
71
|
"scripts": {
|
|
63
72
|
"new": "modern new",
|
|
73
|
+
"dev": "modern build --watch",
|
|
64
74
|
"build": "modern build",
|
|
65
75
|
"test": "jest --passWithNoTests"
|
|
66
76
|
},
|
package/src/index.ts
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
createPlugin,
|
|
3
|
-
useAppContext,
|
|
4
|
-
useResolvedConfigContext,
|
|
5
|
-
} from '@modern-js/core';
|
|
6
|
-
import { PLUGIN_SCHEMAS } from '@modern-js/utils';
|
|
7
|
-
import { createProxyRule } from './utils/createProxyRule';
|
|
8
|
-
import WhistleProxy from './utils/whistleProxy';
|
|
9
|
-
|
|
10
|
-
let proxyServer: WhistleProxy;
|
|
11
|
-
export default createPlugin(
|
|
12
|
-
() => ({
|
|
13
|
-
validateSchema() {
|
|
14
|
-
return PLUGIN_SCHEMAS['@modern-js/plugin-proxy'];
|
|
15
|
-
},
|
|
16
|
-
async afterDev() {
|
|
17
|
-
/* eslint-disable react-hooks/rules-of-hooks */
|
|
18
|
-
const { dev } = useResolvedConfigContext();
|
|
19
|
-
const { internalDirectory } = useAppContext();
|
|
20
|
-
/* eslint-enable react-hooks/rules-of-hooks */
|
|
21
|
-
|
|
22
|
-
if (!dev?.proxy) {
|
|
23
|
-
return;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const rule = createProxyRule(internalDirectory, dev.proxy);
|
|
27
|
-
proxyServer = new WhistleProxy({ port: 8899, rule });
|
|
28
|
-
await proxyServer.start();
|
|
29
|
-
},
|
|
30
|
-
beforeExit() {
|
|
31
|
-
// terminate whistle proxy
|
|
32
|
-
proxyServer?.close();
|
|
33
|
-
},
|
|
34
|
-
}),
|
|
35
|
-
{ name: '@modern-js/plugin-proxy' },
|
|
36
|
-
) as any;
|
|
37
|
-
|
|
38
|
-
export type ProxyOptions = string | Record<string, string>;
|
|
39
|
-
|
|
40
|
-
declare module '@modern-js/core' {
|
|
41
|
-
export interface DevConfig {
|
|
42
|
-
proxy?: ProxyOptions;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
import { logger, fs } from '@modern-js/utils';
|
|
3
|
-
import type { ProxyOptions } from '..';
|
|
4
|
-
|
|
5
|
-
interface ProxyRule {
|
|
6
|
-
pattern: string;
|
|
7
|
-
target: string;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
const createWhistleProxyRule = (ruleDirectory: string, rules: ProxyRule[]) => {
|
|
11
|
-
const dest = path.resolve(ruleDirectory, 'proxy.rule.js');
|
|
12
|
-
|
|
13
|
-
let code = `/.*/ enable://intercept\n`;
|
|
14
|
-
|
|
15
|
-
for (const rule of rules) {
|
|
16
|
-
const { pattern, target } = rule;
|
|
17
|
-
code += `${pattern} ${target}\n`;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
fs.outputFileSync(
|
|
21
|
-
dest,
|
|
22
|
-
`exports.name = 'modernjs proxy rule';\nexports.rules = \`${code}\`;`,
|
|
23
|
-
);
|
|
24
|
-
return dest;
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
export const createProxyRule = (
|
|
28
|
-
appDirectory: string,
|
|
29
|
-
proxyOptions: ProxyOptions,
|
|
30
|
-
) => {
|
|
31
|
-
const rules = [];
|
|
32
|
-
|
|
33
|
-
if (proxyOptions && typeof proxyOptions === 'string') {
|
|
34
|
-
return proxyOptions;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
if (typeof proxyOptions === 'object') {
|
|
38
|
-
for (const pattern of Object.keys(proxyOptions)) {
|
|
39
|
-
const target = proxyOptions[pattern];
|
|
40
|
-
if (!target || typeof target !== 'string') {
|
|
41
|
-
logger.error(`dev.proxy.${pattern} value should be string type`);
|
|
42
|
-
// eslint-disable-next-line no-process-exit
|
|
43
|
-
process.exit(1);
|
|
44
|
-
}
|
|
45
|
-
rules.push({ pattern, target });
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return createWhistleProxyRule(appDirectory, rules);
|
|
50
|
-
};
|
package/src/utils/execSync.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { execSync as nodeExecSync } from 'child_process';
|
|
2
|
-
|
|
3
|
-
function execSync(cmd: string) {
|
|
4
|
-
let stdout;
|
|
5
|
-
let status = 0;
|
|
6
|
-
try {
|
|
7
|
-
stdout = nodeExecSync(cmd);
|
|
8
|
-
} catch (err: any) {
|
|
9
|
-
/* eslint-disable prefer-destructuring */
|
|
10
|
-
stdout = err.stdout;
|
|
11
|
-
status = err.status;
|
|
12
|
-
/* eslint-enable prefer-destructuring */
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
return {
|
|
16
|
-
stdout: stdout.toString(),
|
|
17
|
-
status,
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export default execSync;
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import os from 'os';
|
|
2
|
-
import http from 'http';
|
|
3
|
-
import path from 'path';
|
|
4
|
-
import { fs, logger } from '@modern-js/utils';
|
|
5
|
-
import execSync from './execSync';
|
|
6
|
-
|
|
7
|
-
const defaultCertDir = path.resolve(os.homedir(), './.whistle-proxy');
|
|
8
|
-
export const defaultRootCA = path.resolve(defaultCertDir, './rootCA.crt');
|
|
9
|
-
|
|
10
|
-
export const trustRootCA = () => {
|
|
11
|
-
logger.info(`please type the password to trust the https certificate`);
|
|
12
|
-
const { status } = execSync(
|
|
13
|
-
`sudo security add-trusted-cert -d -k /Library/Keychains/System.keychain ${defaultRootCA}`,
|
|
14
|
-
);
|
|
15
|
-
if (status === 0) {
|
|
16
|
-
logger.info('Root CA install, you are ready to intercept the https now');
|
|
17
|
-
} else {
|
|
18
|
-
logger.info('Failed to trust the root CA, please trust it manually');
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
23
|
-
const isRootCATrusted = () => {
|
|
24
|
-
// current empty
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
export const isRootCAExists = () => {
|
|
28
|
-
if (fs.existsSync(defaultRootCA)) {
|
|
29
|
-
return true;
|
|
30
|
-
}
|
|
31
|
-
return false;
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
export const generateRootCA = () =>
|
|
35
|
-
new Promise((resolve, reject) => {
|
|
36
|
-
if (fs.existsSync(defaultRootCA)) {
|
|
37
|
-
fs.removeSync(defaultRootCA);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
fs.ensureDirSync(defaultCertDir);
|
|
41
|
-
const stream = fs.createWriteStream(defaultRootCA);
|
|
42
|
-
|
|
43
|
-
http
|
|
44
|
-
.get('http://localhost:8899/cgi-bin/rootca', response => {
|
|
45
|
-
response.pipe(stream);
|
|
46
|
-
stream
|
|
47
|
-
.on('finish', () => {
|
|
48
|
-
resolve(defaultRootCA);
|
|
49
|
-
})
|
|
50
|
-
.on('error', err => {
|
|
51
|
-
reject(err);
|
|
52
|
-
});
|
|
53
|
-
})
|
|
54
|
-
.on('error', err => {
|
|
55
|
-
fs.unlink(defaultRootCA); // Delete the file
|
|
56
|
-
reject(err);
|
|
57
|
-
});
|
|
58
|
-
});
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import execSync from './execSync';
|
|
2
|
-
|
|
3
|
-
const networkTypes = ['Ethernet', 'Thunderbolt Ethernet', 'Wi-Fi'];
|
|
4
|
-
|
|
5
|
-
const getNetworkType = () => {
|
|
6
|
-
// eslint-disable-next-line @typescript-eslint/prefer-for-of
|
|
7
|
-
for (let i = 0; i < networkTypes.length; i++) {
|
|
8
|
-
const type = networkTypes[i];
|
|
9
|
-
const result = execSync(`networksetup -getwebproxy ${type}`);
|
|
10
|
-
|
|
11
|
-
if (result.status === 0) {
|
|
12
|
-
return type;
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
throw new Error('Unknown network type');
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
export const enableGlobalProxy = (ip: string, port: string) => {
|
|
20
|
-
const networkType = getNetworkType();
|
|
21
|
-
|
|
22
|
-
// && networksetup -setproxybypassdomains ${networkType} localhost localhost
|
|
23
|
-
execSync(`networksetup -setwebproxy ${networkType} ${ip} ${port}`);
|
|
24
|
-
execSync(`networksetup -setsecurewebproxy ${networkType} ${ip} ${port}`);
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
export const disableGlobalProxy = () => {
|
|
28
|
-
const networkType = getNetworkType();
|
|
29
|
-
execSync(`networksetup -setwebproxystate ${networkType} off`);
|
|
30
|
-
execSync(`networksetup -setsecurewebproxystate ${networkType} off`);
|
|
31
|
-
};
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
import { logger } from '@modern-js/utils';
|
|
3
|
-
import execSync from './execSync';
|
|
4
|
-
import {
|
|
5
|
-
isRootCAExists,
|
|
6
|
-
generateRootCA,
|
|
7
|
-
defaultRootCA,
|
|
8
|
-
trustRootCA,
|
|
9
|
-
} from './macCAManager';
|
|
10
|
-
|
|
11
|
-
const { disableGlobalProxy, enableGlobalProxy } = require('./macProxyManager');
|
|
12
|
-
|
|
13
|
-
interface ProxyConfig {
|
|
14
|
-
rule: string;
|
|
15
|
-
port: number;
|
|
16
|
-
// mode: 'pureProxy'|'debug'|'multiEnv'
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export default class WhistleProxy {
|
|
20
|
-
private readonly rule: string;
|
|
21
|
-
|
|
22
|
-
private readonly port: number;
|
|
23
|
-
|
|
24
|
-
private readonly bin: string;
|
|
25
|
-
|
|
26
|
-
private readonly certDir: string;
|
|
27
|
-
|
|
28
|
-
constructor(config: ProxyConfig) {
|
|
29
|
-
this.rule = config.rule;
|
|
30
|
-
this.port = config.port;
|
|
31
|
-
// unused
|
|
32
|
-
// this.mode = config.mode; // pureProxy|debug|multiEnv
|
|
33
|
-
this.bin = path.resolve(
|
|
34
|
-
path.dirname(require.resolve('whistle')),
|
|
35
|
-
'bin/whistle.js',
|
|
36
|
-
);
|
|
37
|
-
this.certDir = path.dirname(defaultRootCA);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
async installRootCA() {
|
|
41
|
-
try {
|
|
42
|
-
if (!isRootCAExists()) {
|
|
43
|
-
await generateRootCA();
|
|
44
|
-
trustRootCA();
|
|
45
|
-
}
|
|
46
|
-
} catch (err) {
|
|
47
|
-
this.close();
|
|
48
|
-
throw err;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
async start() {
|
|
53
|
-
logger.info(`Starting the proxy server.....`);
|
|
54
|
-
execSync(`${this.bin} start --certDir=${this.certDir} --port=${this.port}`);
|
|
55
|
-
execSync(`${this.bin} use ${this.rule} --force`);
|
|
56
|
-
await this.installRootCA();
|
|
57
|
-
|
|
58
|
-
enableGlobalProxy('localhost', this.port);
|
|
59
|
-
logger.info(`Proxy Server start on localhost:${this.port}\n`);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
close() {
|
|
63
|
-
execSync(`${this.bin} stop`);
|
|
64
|
-
disableGlobalProxy();
|
|
65
|
-
logger.info(`Proxy Server has been closed`);
|
|
66
|
-
}
|
|
67
|
-
}
|