whistle.script 1.2.5 → 1.2.8
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/lib/util.js +28 -5
- package/lib/wsServer.js +18 -1
- package/package.json +2 -1
package/lib/util.js
CHANGED
|
@@ -13,6 +13,7 @@ exports.STATS_URL = 'x-whistle-.script-stats-url';
|
|
|
13
13
|
exports.DATA_URL = 'x-whistle-.script-data-url';
|
|
14
14
|
exports.noop = () => {};
|
|
15
15
|
|
|
16
|
+
const PREFIX_LEN = 'x-whistle-.script-'.length;
|
|
16
17
|
const POLICY = 'x-whistle-.script-policy';
|
|
17
18
|
const isFunction = fn => typeof fn === 'function';
|
|
18
19
|
const URL_RE = /^https?:(?:\/\/|%3A%2F%2F)[\w.-]/;
|
|
@@ -140,6 +141,7 @@ const request = (url, headers, data) => {
|
|
|
140
141
|
if (data) {
|
|
141
142
|
data = Buffer.from(JSON.stringify(data));
|
|
142
143
|
options.method = 'POST';
|
|
144
|
+
options.headers['content-type'] = 'application/json';
|
|
143
145
|
}
|
|
144
146
|
return new Promise((resolve, reject) => {
|
|
145
147
|
const httpModule = options.protocol === 'https:' ? https : http;
|
|
@@ -181,11 +183,12 @@ exports.request = async (url, headers, data) => {
|
|
|
181
183
|
}
|
|
182
184
|
};
|
|
183
185
|
|
|
184
|
-
const hasPolicy = ({ headers }, name) => {
|
|
186
|
+
const hasPolicy = ({ headers, originalReq: { ruleValue } }, name) => {
|
|
185
187
|
const policy = headers[POLICY];
|
|
186
188
|
if (typeof policy === 'string') {
|
|
187
189
|
return policy.toLowerCase().indexOf(name) !== -1;
|
|
188
190
|
}
|
|
191
|
+
return ruleValue === `policy=${name}`;
|
|
189
192
|
};
|
|
190
193
|
|
|
191
194
|
const isRemote = (req) => {
|
|
@@ -198,12 +201,32 @@ exports.isSni = (req) => {
|
|
|
198
201
|
return hasPolicy(req, 'sni');
|
|
199
202
|
};
|
|
200
203
|
|
|
204
|
+
const getValue = ({ originalReq: req }, name) => {
|
|
205
|
+
const { pluginVars, globalPluginVars } = req;
|
|
206
|
+
const vars = globalPluginVars ? pluginVars.concat(globalPluginVars) : pluginVars;
|
|
207
|
+
const len = vars && vars.length;
|
|
208
|
+
if (!len) {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
for (let i = 0; i < len; i++) {
|
|
212
|
+
const item = vars[i];
|
|
213
|
+
const index = item.indexOf('=');
|
|
214
|
+
if (index !== -1 && item.substring(0, index) === name) {
|
|
215
|
+
return item.substring(index + 1);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
const getVarName = (name) => name.substring(PREFIX_LEN).replace(/-(.)/g, (_, ch) => ch.toUpperCase());
|
|
221
|
+
|
|
201
222
|
exports.getRemoteUrl = (req, name) => {
|
|
202
223
|
let url = req.headers[name];
|
|
203
|
-
if (typeof url === 'string') {
|
|
224
|
+
if (url && typeof url === 'string') {
|
|
204
225
|
url = decodeURIComponent(url);
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
226
|
+
} else {
|
|
227
|
+
url = getValue(req, getVarName(name));
|
|
228
|
+
}
|
|
229
|
+
if (URL_RE.test(url)) {
|
|
230
|
+
return url;
|
|
208
231
|
}
|
|
209
232
|
};
|
package/lib/wsServer.js
CHANGED
|
@@ -3,6 +3,17 @@ const iconv = require('iconv-lite');
|
|
|
3
3
|
const { getDataSource, getFn } = require('./util');
|
|
4
4
|
const scripts = require('./scripts');
|
|
5
5
|
|
|
6
|
+
const toBuffer = (data) => {
|
|
7
|
+
if (Buffer.isBuffer(data)) {
|
|
8
|
+
return data;
|
|
9
|
+
}
|
|
10
|
+
if (data == null) {
|
|
11
|
+
data = '';
|
|
12
|
+
} else if (typeof data !== 'string') {
|
|
13
|
+
data = JSON.stringify(data);
|
|
14
|
+
}
|
|
15
|
+
return data && Buffer.from(data);
|
|
16
|
+
};
|
|
6
17
|
|
|
7
18
|
module.exports = (server, options) => {
|
|
8
19
|
const { getReceiver, getSender } = options.wsParser;
|
|
@@ -18,7 +29,13 @@ module.exports = (server, options) => {
|
|
|
18
29
|
err.code = code;
|
|
19
30
|
socket.emit('error', err);
|
|
20
31
|
};
|
|
21
|
-
socket.send =
|
|
32
|
+
socket.send = (data, opts, cb) => {
|
|
33
|
+
try {
|
|
34
|
+
return sender.send(toBuffer(data), opts, cb);
|
|
35
|
+
} catch (e) {
|
|
36
|
+
socket.emit('error', e);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
22
39
|
socket.ping = sender.ping.bind(sender);
|
|
23
40
|
socket.pong = sender.pong.bind(sender);
|
|
24
41
|
socket.disconnect = sender.close.bind(sender);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "whistle.script",
|
|
3
3
|
"description": "The plugin for the extension script for whistle",
|
|
4
|
-
"version": "1.2.
|
|
4
|
+
"version": "1.2.8",
|
|
5
5
|
"author": "avenwu <avenwu@vip.qq.com>",
|
|
6
6
|
"contributors": [],
|
|
7
7
|
"license": "MIT",
|
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
],
|
|
17
17
|
"registry": "https://github.com/whistle-plugins/whistle.script",
|
|
18
18
|
"whistleConfig": {
|
|
19
|
+
"priority": 999999999,
|
|
19
20
|
"pluginVars": true
|
|
20
21
|
},
|
|
21
22
|
"repository": {
|