From 8951fb68ef752b2934bca741559b903c7e297d08 Mon Sep 17 00:00:00 2001 From: "xiaofeng.mxf" Date: Wed, 1 Jan 2020 16:45:58 +0800 Subject: [PATCH] update eslint config --- lib/recorder.js | 2 +- lib/requestHandler.js | 247 +++++++++++++------------- lib/systemProxyMgr.js | 26 +-- lib/webInterface.js | 1 - module_sample/https_config.js | 6 +- package.json | 2 +- proxy.js | 3 +- test/spec_outweb/test_realweb_spec.js | 12 +- test/util/HttpUtil.js | 20 +-- test/util/ProxyServerUtil.js | 4 +- 10 files changed, 160 insertions(+), 163 deletions(-) diff --git a/lib/recorder.js b/lib/recorder.js index d4decf7..728afc5 100644 --- a/lib/recorder.js +++ b/lib/recorder.js @@ -87,7 +87,7 @@ class Recorder extends events.EventEmitter { this.cachePath = getCacheDir(); this.db = new Datastore(); - this.recordBodyMap = []; // id - body + this.recordBodyMap = []; // id - body } setDbAutoCompact() { diff --git a/lib/requestHandler.js b/lib/requestHandler.js index 012a3ed..0f8c3f7 100644 --- a/lib/requestHandler.js +++ b/lib/requestHandler.js @@ -365,7 +365,7 @@ function getUserReqHandler(userRule, recorder) { if (!global._throttle && transferEncoding !== 'chunked' && !(responseBody instanceof CommonReadableStream) - ) { + ) { resHeader['Content-Length'] = util.getByteSize(responseBody); } @@ -550,142 +550,142 @@ function getConnectReqHandler(userRule, recorder, httpsServerMgr) { shouldIntercept = reqHandlerCtx.forceProxyHttps; } }) - .then(() => { - return new Promise((resolve) => { + .then(() => { + return new Promise((resolve) => { // mark socket connection as established, to detect the request protocol - cltSocket.write('HTTP/' + req.httpVersion + ' 200 OK\r\n\r\n', 'UTF-8', resolve); - }); - }) - .then(() => { - return new Promise((resolve, reject) => { - let resolved = false; - cltSocket.on('data', (chunk) => { - requestStream.push(chunk); - if (!resolved) { - resolved = true; - try { - const chunkString = chunk.toString(); - if (chunkString.indexOf('GET ') === 0) { - shouldIntercept = false; // websocket, do not intercept - - // if there is '/do-not-proxy' in the request, do not intercept the websocket - // to avoid AnyProxy itself be proxied - if (reqHandlerCtx.wsIntercept && chunkString.indexOf('GET /do-not-proxy') !== 0) { - interceptWsRequest = true; - } - } - } catch (e) { - console.error(e); - } - resolve(); - } + cltSocket.write('HTTP/' + req.httpVersion + ' 200 OK\r\n\r\n', 'UTF-8', resolve); }); - cltSocket.on('error', (error) => { - logUtil.printLog(util.collectErrorLog(error), logUtil.T_ERR); - co.wrap(function *() { - try { - yield userRule.onClientSocketError(requestDetail, error); - } catch (e) { } + }) + .then(() => { + return new Promise((resolve, reject) => { + let resolved = false; + cltSocket.on('data', (chunk) => { + requestStream.push(chunk); + if (!resolved) { + resolved = true; + try { + const chunkString = chunk.toString(); + if (chunkString.indexOf('GET ') === 0) { + shouldIntercept = false; // websocket, do not intercept + + // if there is '/do-not-proxy' in the request, do not intercept the websocket + // to avoid AnyProxy itself be proxied + if (reqHandlerCtx.wsIntercept && chunkString.indexOf('GET /do-not-proxy') !== 0) { + interceptWsRequest = true; + } + } + } catch (e) { + console.error(e); + } + resolve(); + } + }); + cltSocket.on('error', (error) => { + logUtil.printLog(util.collectErrorLog(error), logUtil.T_ERR); + co.wrap(function *() { + try { + yield userRule.onClientSocketError(requestDetail, error); + } catch (e) { } + }); + }); + cltSocket.on('end', () => { + requestStream.push(null); }); }); - cltSocket.on('end', () => { - requestStream.push(null); - }); - }); - }) - .then((result) => { + }) + .then((result) => { // log and recorder - if (shouldIntercept) { - logUtil.printLog('will forward to local https server'); - } else { - logUtil.printLog('will bypass the man-in-the-middle proxy'); - } + if (shouldIntercept) { + logUtil.printLog('will forward to local https server'); + } else { + logUtil.printLog('will bypass the man-in-the-middle proxy'); + } - //record - if (recorder) { - resourceInfo = { - host, - method: req.method, - path: '', - url: 'https://' + host, - req, - startTime: new Date().getTime() - }; - resourceInfoId = recorder.appendRecord(resourceInfo); - } - }) - .then(() => { + //record + if (recorder) { + resourceInfo = { + host, + method: req.method, + path: '', + url: 'https://' + host, + req, + startTime: new Date().getTime() + }; + resourceInfoId = recorder.appendRecord(resourceInfo); + } + }) + .then(() => { // determine the request target - if (!shouldIntercept) { + if (!shouldIntercept) { // server info from the original request - const originServer = { - host, - port: (targetPort === 80) ? 443 : targetPort - } - - const localHttpServer = { - host: 'localhost', - port: reqHandlerCtx.httpServerPort - } - - // for ws request, redirect them to local ws server - return interceptWsRequest ? localHttpServer : originServer; - } else { - return httpsServerMgr.getSharedHttpsServer(host).then(serverInfo => ({ host: serverInfo.host, port: serverInfo.port })); - } - }) - .then((serverInfo) => { - if (!serverInfo.port || !serverInfo.host) { - throw new Error('failed to get https server info'); - } - - return new Promise((resolve, reject) => { - const conn = net.connect(serverInfo.port, serverInfo.host, () => { - //throttle for direct-foward https - if (global._throttle && !shouldIntercept) { - requestStream.pipe(conn); - conn.pipe(global._throttle.throttle()).pipe(cltSocket); - } else { - requestStream.pipe(conn); - conn.pipe(cltSocket); + const originServer = { + host, + port: (targetPort === 80) ? 443 : targetPort } - resolve(); + const localHttpServer = { + host: 'localhost', + port: reqHandlerCtx.httpServerPort + } + + // for ws request, redirect them to local ws server + return interceptWsRequest ? localHttpServer : originServer; + } else { + return httpsServerMgr.getSharedHttpsServer(host).then(serverInfo => ({ host: serverInfo.host, port: serverInfo.port })); + } + }) + .then((serverInfo) => { + if (!serverInfo.port || !serverInfo.host) { + throw new Error('failed to get https server info'); + } + + return new Promise((resolve, reject) => { + const conn = net.connect(serverInfo.port, serverInfo.host, () => { + //throttle for direct-foward https + if (global._throttle && !shouldIntercept) { + requestStream.pipe(conn); + conn.pipe(global._throttle.throttle()).pipe(cltSocket); + } else { + requestStream.pipe(conn); + conn.pipe(cltSocket); + } + + resolve(); + }); + + conn.on('error', (e) => { + reject(e); + }); + + reqHandlerCtx.conns.set(serverInfo.host + ':' + serverInfo.port, conn) + reqHandlerCtx.cltSockets.set(serverInfo.host + ':' + serverInfo.port, cltSocket) }); + }) + .then(() => { + if (recorder) { + resourceInfo.endTime = new Date().getTime(); + resourceInfo.statusCode = '200'; + resourceInfo.resHeader = {}; + resourceInfo.resBody = ''; + resourceInfo.length = 0; - conn.on('error', (e) => { - reject(e); - }); + recorder && recorder.updateRecord(resourceInfoId, resourceInfo); + } + }) + .catch(co.wrap(function *(error) { + logUtil.printLog(util.collectErrorLog(error), logUtil.T_ERR); - reqHandlerCtx.conns.set(serverInfo.host + ':' + serverInfo.port, conn) - reqHandlerCtx.cltSockets.set(serverInfo.host + ':' + serverInfo.port, cltSocket) - }); - }) - .then(() => { - if (recorder) { - resourceInfo.endTime = new Date().getTime(); - resourceInfo.statusCode = '200'; - resourceInfo.resHeader = {}; - resourceInfo.resBody = ''; - resourceInfo.length = 0; + try { + yield userRule.onConnectError(requestDetail, error); + } catch (e) { } - recorder && recorder.updateRecord(resourceInfoId, resourceInfo); - } - }) - .catch(co.wrap(function *(error) { - logUtil.printLog(util.collectErrorLog(error), logUtil.T_ERR); - - try { - yield userRule.onConnectError(requestDetail, error); - } catch (e) { } - - try { - let errorHeader = 'Proxy-Error: true\r\n'; - errorHeader += 'Proxy-Error-Message: ' + (error || 'null') + '\r\n'; - errorHeader += 'Content-Type: text/html\r\n'; - cltSocket.write('HTTP/1.1 502\r\n' + errorHeader + '\r\n\r\n'); - } catch (e) { } - })); + try { + let errorHeader = 'Proxy-Error: true\r\n'; + errorHeader += 'Proxy-Error-Message: ' + (error || 'null') + '\r\n'; + errorHeader += 'Content-Type: text/html\r\n'; + cltSocket.write('HTTP/1.1 502\r\n' + errorHeader + '\r\n\r\n'); + } catch (e) { } + })); } } @@ -844,7 +844,6 @@ function getWsHandler(userRule, recorder, wsClient, wsReq) { } class RequestHandler { - /** * Creates an instance of RequestHandler. * diff --git a/lib/systemProxyMgr.js b/lib/systemProxyMgr.js index 353bd6b..3a1bbf3 100644 --- a/lib/systemProxyMgr.js +++ b/lib/systemProxyMgr.js @@ -82,16 +82,16 @@ macProxyManager.enableGlobalProxy = (ip, port, proxyType) => { // set http proxy execSync( - 'networksetup -setwebproxy ${networkType} ${ip} ${port} && networksetup -setproxybypassdomains ${networkType} 127.0.0.1 localhost' - .replace(/\${networkType}/g, networkType) - .replace('${ip}', ip) - .replace('${port}', port)) : + 'networksetup -setwebproxy ${networkType} ${ip} ${port} && networksetup -setproxybypassdomains ${networkType} 127.0.0.1 localhost' + .replace(/\${networkType}/g, networkType) + .replace('${ip}', ip) + .replace('${port}', port)) : // set https proxy execSync('networksetup -setsecurewebproxy ${networkType} ${ip} ${port} && networksetup -setproxybypassdomains ${networkType} 127.0.0.1 localhost' - .replace(/\${networkType}/g, networkType) - .replace('${ip}', ip) - .replace('${port}', port)); + .replace(/\${networkType}/g, networkType) + .replace('${ip}', ip) + .replace('${port}', port)); }; macProxyManager.disableGlobalProxy = (proxyType) => { @@ -101,13 +101,13 @@ macProxyManager.disableGlobalProxy = (proxyType) => { // set http proxy execSync( - 'networksetup -setwebproxystate ${networkType} off' - .replace('${networkType}', networkType)) : + 'networksetup -setwebproxystate ${networkType} off' + .replace('${networkType}', networkType)) : // set https proxy execSync( - 'networksetup -setsecurewebproxystate ${networkType} off' - .replace('${networkType}', networkType)); + 'networksetup -setsecurewebproxystate ${networkType} off' + .replace('${networkType}', networkType)); }; macProxyManager.getProxyState = () => { @@ -136,8 +136,8 @@ winProxyManager.enableGlobalProxy = (ip, port) => { return execSync( // set proxy 'reg add "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings" /v ProxyServer /t REG_SZ /d ${ip}:${port} /f & ' - .replace('${ip}', ip) - .replace('${port}', port) + + .replace('${ip}', ip) + .replace('${port}', port) + // enable proxy 'reg add "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings" /v ProxyEnable /t REG_DWORD /d 1 /f'); diff --git a/lib/webInterface.js b/lib/webInterface.js index 8d1925d..c87cbae 100644 --- a/lib/webInterface.js +++ b/lib/webInterface.js @@ -29,7 +29,6 @@ const certFileTypes = ['crt', 'cer', 'pem', 'der']; * @extends {events.EventEmitter} */ class webInterface extends events.EventEmitter { - /** * Creates an instance of webInterface. * diff --git a/module_sample/https_config.js b/module_sample/https_config.js index 92ffc82..c36f298 100644 --- a/module_sample/https_config.js +++ b/module_sample/https_config.js @@ -3,7 +3,7 @@ const exec = require('child_process').exec; if (!AnyProxy.utils.certMgr.ifRootCAFileExists()) { AnyProxy.utils.certMgr.generateRootCA((error, keyPath) => { - // let users to trust this CA before using proxy + // let users to trust this CA before using proxy if (!error) { const certDir = require('path').dirname(keyPath); console.log('The cert is generated at', certDir); @@ -18,6 +18,6 @@ if (!AnyProxy.utils.certMgr.ifRootCAFileExists()) { } }); } else { - // clear all the certificates - // AnyProxy.utils.certMgr.clearCerts() + // clear all the certificates + // AnyProxy.utils.certMgr.clearCerts() } diff --git a/package.json b/package.json index 99e537a..b674142 100644 --- a/package.json +++ b/package.json @@ -54,7 +54,7 @@ "babel-register": "^6.11.6", "babel-runtime": "^6.11.6", "css-loader": "^0.23.1", - "eslint": "^3.5.0", + "eslint": ">=4.18.2", "eslint-config-airbnb": "^15.1.0", "eslint-plugin-import": "^2.7.0", "eslint-plugin-jsx-a11y": "^5.1.1", diff --git a/proxy.js b/proxy.js index 841faa5..1230a07 100644 --- a/proxy.js +++ b/proxy.js @@ -47,7 +47,6 @@ const PROXY_STATUS_CLOSED = 'CLOSED'; * @extends {events.EventEmitter} */ class ProxyCore extends events.EventEmitter { - /** * Creates an instance of ProxyCore. * @@ -135,7 +134,7 @@ class ProxyCore extends events.EventEmitter { */ handleExistConnections(socket) { const self = this; - self.socketIndex ++; + self.socketIndex++; const key = `socketIndex_${self.socketIndex}`; self.socketPool[key] = socket; diff --git a/test/spec_outweb/test_realweb_spec.js b/test/spec_outweb/test_realweb_spec.js index 6970865..50cc76f 100644 --- a/test/spec_outweb/test_realweb_spec.js +++ b/test/spec_outweb/test_realweb_spec.js @@ -63,20 +63,20 @@ function test(url, requestHeaders = {}) { done(); }); }) + .catch((err) => { + printError(err); + done(); + }); + }) .catch((err) => { printError(err); done(); }); - }) + }) .catch((err) => { printError(err); done(); }); - }) - .catch((err) => { - printError(err); - done(); - }); }); }) }) diff --git a/test/util/HttpUtil.js b/test/util/HttpUtil.js index 5b79360..76281d1 100644 --- a/test/util/HttpUtil.js +++ b/test/util/HttpUtil.js @@ -328,16 +328,16 @@ function getRequestListFromPage(pageUrl, cb) { }, _outObj); return _page.open(pageUrl); }) - .then(status => _outObj.property('urls')) - .then(urls => { - _page.close(); - _ph.exit(); - return urls; - }) - .catch((err) => { - console.log(`failed to collecting requests from ${pageUrl}`); - console.log(err); - }); + .then(status => _outObj.property('urls')) + .then(urls => { + _page.close(); + _ph.exit(); + return urls; + }) + .catch((err) => { + console.log(`failed to collecting requests from ${pageUrl}`); + console.log(err); + }); } diff --git a/test/util/ProxyServerUtil.js b/test/util/ProxyServerUtil.js index 68a8a86..abd9449 100644 --- a/test/util/ProxyServerUtil.js +++ b/test/util/ProxyServerUtil.js @@ -10,10 +10,10 @@ const DEFAULT_OPTIONS = { port: 8001, webInterface: { enable: true, - webPort: 8002, // optional, port for web interface + webPort: 8002, // optional, port for web interface }, wsIntercept: true, - throttle: 10000, // optional, speed limit in kb/s + 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.