whistle.script 1.2.6 → 1.2.9

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.
Files changed (2) hide show
  1. package/lib/util.js +31 -6
  2. 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;
@@ -154,7 +156,7 @@ const request = (url, headers, data) => {
154
156
  clearTimeout(timer); // eslint-disable-line
155
157
  if (body) {
156
158
  try {
157
- resolve(JSON.parse(body.toString()) || '');
159
+ return resolve(JSON.parse(body.toString()) || '');
158
160
  } catch (e) {}
159
161
  }
160
162
  resolve('');
@@ -181,11 +183,14 @@ 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
+ if (typeof ruleValue === 'string') {
192
+ return ruleValue.indexOf(`=${name}`) !== -1 || ruleValue.indexOf(`&${name}`) !== -1;
193
+ }
189
194
  };
190
195
 
191
196
  const isRemote = (req) => {
@@ -198,12 +203,32 @@ exports.isSni = (req) => {
198
203
  return hasPolicy(req, 'sni');
199
204
  };
200
205
 
206
+ const getValue = ({ originalReq: req }, name) => {
207
+ const { pluginVars, globalPluginVars } = req;
208
+ const vars = globalPluginVars ? pluginVars.concat(globalPluginVars) : pluginVars;
209
+ const len = vars && vars.length;
210
+ if (!len) {
211
+ return;
212
+ }
213
+ for (let i = 0; i < len; i++) {
214
+ const item = vars[i];
215
+ const index = item.indexOf('=');
216
+ if (index !== -1 && item.substring(0, index) === name) {
217
+ return item.substring(index + 1);
218
+ }
219
+ }
220
+ };
221
+
222
+ const getVarName = (name) => name.substring(PREFIX_LEN).replace(/-(.)/g, (_, ch) => ch.toUpperCase());
223
+
201
224
  exports.getRemoteUrl = (req, name) => {
202
225
  let url = req.headers[name];
203
- if (typeof url === 'string') {
226
+ if (url && typeof url === 'string') {
204
227
  url = decodeURIComponent(url);
205
- if (URL_RE.test(url)) {
206
- return url;
207
- }
228
+ } else {
229
+ url = getValue(req, getVarName(name));
230
+ }
231
+ if (URL_RE.test(url)) {
232
+ return url;
208
233
  }
209
234
  };
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.6",
4
+ "version": "1.2.9",
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": {