whistle.script 1.2.1 → 1.2.5

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/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  const handlePipe = require('./pipe');
2
2
 
3
3
  exports.auth = require('./auth');
4
+ exports.sniCallback = require('./sniCallback');
4
5
  exports.uiServer = require('./uiServer');
5
6
  exports.rulesServer = require('./rulesServer');
6
7
  exports.resRulesServer = require('./resRulesServer');
package/lib/pipe.js CHANGED
@@ -16,7 +16,7 @@ module.exports = (name) => {
16
16
  }, options);
17
17
  server.on(eventName, async (req, res) => {
18
18
  const oReq = req.originalReq;
19
- oReq.ruleValue = oReq.ruleValue || oReq.pipeValue;
19
+ oReq.ruleValue = oReq.pipeValue || oReq.ruleValue;
20
20
  const handleRequest = scripts.getHandler({ req })[getHandlerName(name)];
21
21
  if (!util.isFunction(handleRequest)) {
22
22
  return req.pipe(res);
@@ -0,0 +1,24 @@
1
+ const scripts = require('./scripts');
2
+ const { getRemoteUrl, getContext, getFn, request, SNI_URL, isSni } = require('./util');
3
+
4
+ module.exports = async (req, options) => {
5
+ if (!isSni(req)) {
6
+ const oReq = req.originalReq;
7
+ oReq.ruleValue = oReq.sniValue || oReq.ruleValue;
8
+ const ctx = getContext(req);
9
+ const { sniCallback, SNICallback } = scripts.getHandler(ctx);
10
+ const sniCb = getFn(sniCallback, SNICallback);
11
+ return sniCb && sniCb(req, options);
12
+ }
13
+ const sniUrl = getRemoteUrl(req, SNI_URL);
14
+ if (sniUrl) {
15
+ const result = await request(sniUrl, req.headers);
16
+ if (result === false || result.cert === false) {
17
+ return false;
18
+ }
19
+ if (result === true || result.cert === true) {
20
+ return true;
21
+ }
22
+ return result;
23
+ }
24
+ };
package/lib/util.js CHANGED
@@ -2,9 +2,11 @@ const zlib = require('zlib');
2
2
  const { EventEmitter } = require('events');
3
3
  const { parse: parseUrl } = require('url');
4
4
  const http = require('http');
5
+ const https = require('https');
5
6
  const dataSource = require('./dataSource');
6
7
 
7
8
  exports.AUTH_URL = 'x-whistle-.script-auth-url';
9
+ exports.SNI_URL = 'x-whistle-.script-sni-url';
8
10
  exports.REQ_RULES_URL = 'x-whistle-.script-req-rules-url';
9
11
  exports.RES_RULES_URL = 'x-whistle-.script-res-rules-url';
10
12
  exports.STATS_URL = 'x-whistle-.script-stats-url';
@@ -13,7 +15,7 @@ exports.noop = () => {};
13
15
 
14
16
  const POLICY = 'x-whistle-.script-policy';
15
17
  const isFunction = fn => typeof fn === 'function';
16
- const URL_RE = /^http:(?:\/\/|%3A%2F%2F)[\w.-]/;
18
+ const URL_RE = /^https?:(?:\/\/|%3A%2F%2F)[\w.-]/;
17
19
 
18
20
  exports.isFunction = isFunction;
19
21
  exports.noop = () => {};
@@ -140,7 +142,9 @@ const request = (url, headers, data) => {
140
142
  options.method = 'POST';
141
143
  }
142
144
  return new Promise((resolve, reject) => {
143
- const client = http.request(options, (res) => {
145
+ const httpModule = options.protocol === 'https:' ? https : http;
146
+ options.rejectUnauthorized = false;
147
+ const client = httpModule.request(options, (res) => {
144
148
  res.on('error', handleError); // eslint-disable-line
145
149
  let body;
146
150
  res.on('data', (chunk) => {
@@ -177,10 +181,23 @@ exports.request = async (url, headers, data) => {
177
181
  }
178
182
  };
179
183
 
180
- const isRemote = ({ headers }) => (headers[POLICY] === 'remote');
184
+ const hasPolicy = ({ headers }, name) => {
185
+ const policy = headers[POLICY];
186
+ if (typeof policy === 'string') {
187
+ return policy.toLowerCase().indexOf(name) !== -1;
188
+ }
189
+ };
190
+
191
+ const isRemote = (req) => {
192
+ return hasPolicy(req, 'remote');
193
+ };
181
194
 
182
195
  exports.isRemote = isRemote;
183
196
 
197
+ exports.isSni = (req) => {
198
+ return hasPolicy(req, 'sni');
199
+ };
200
+
184
201
  exports.getRemoteUrl = (req, name) => {
185
202
  let url = req.headers[name];
186
203
  if (typeof url === 'string') {
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.1",
4
+ "version": "1.2.5",
5
5
  "author": "avenwu <avenwu@vip.qq.com>",
6
6
  "contributors": [],
7
7
  "license": "MIT",
@@ -15,6 +15,9 @@
15
15
  "ssi"
16
16
  ],
17
17
  "registry": "https://github.com/whistle-plugins/whistle.script",
18
+ "whistleConfig": {
19
+ "pluginVars": true
20
+ },
18
21
  "repository": {
19
22
  "type": "git",
20
23
  "url": "https://github.com/whistle-plugins/whistle.script.git"
package/rules.txt CHANGED
@@ -1 +1,2 @@
1
- * whistle.script:// includeFilter://reqH.x-whistle-.script-policy=remote
1
+ * whistle.script:// delete://reqH.x-whistle-.script-policy|reqH.x-whistle-.script-auth-url|reqH.x-whistle-.script-req-rules-url|reqH.x-whistle-.script-res-rules-url|reqH.x-whistle-.script-stats-url|reqH.x-whistle-.script-data-url includeFilter://reqH.x-whistle-.script-policy=remote
2
+ * sniCallback://script delete://reqH.x-whistle-.script-policy|reqH.x-whistle-.script-sni-url includeFilter://reqH.x-whistle-.script-policy=sni