-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathutil.js
More file actions
80 lines (72 loc) · 1.92 KB
/
Copy pathutil.js
File metadata and controls
80 lines (72 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const request = require('request');
const assert = require('assert');
// TODO
const { freshRequire, getFreePort } = require('../lib/util.js');
function basicProxyRequest(proxyHost, method, url, headers, qs, payload) {
assert(method && url, 'method and url are required');
assert(proxyHost, 'proxyHost is required');
headers = Object.assign({
'via-anyproxy': 'true',
}, headers || {});
const requestOpt = {
method,
url,
headers,
followRedirect: false,
rejectUnauthorized: false,
qs,
proxy: proxyHost,
};
return new Promise((resolve, reject) => {
const callback = (error, response, body) => {
if (error) {
reject(error);
} else {
resolve({
response,
body,
});
}
};
if (payload) {
payload.pipe(request(requestOpt, callback));
} else {
request(requestOpt, callback);
}
});
}
const DEFAULT_OPTIONS = {
type: 'http',
port: 8001,
webInterface: false,
wsIntercept: true,
// throttle: 10000, // optional, speed limit in kb/s
forceProxyHttps: true, // intercept https as well
dangerouslyIgnoreUnauthorized: true,
silent: false //optional, do not print anything into terminal. do not set it when you are still debugging.
};
async function proxyServerWithRule(rule, overrideConfig) {
const AnyProxy = freshRequire('../proxy.js');
const freeportA = await getFreePort();
const freeportB = await getFreePort();
const options = Object.assign(DEFAULT_OPTIONS, {
port: freeportA,
webInterface: {
enable: true,
webPort: freeportB,
}
}, overrideConfig || {});
options.rule = rule;
return new Promise((resolve, reject) => {
const instance = new AnyProxy.ProxyServer(options);
instance.on('error', reject);
instance.on('ready', () => {
resolve(instance);
});
instance.start();
});
}
module.exports = {
basicProxyRequest,
proxyServerWithRule,
};