This commit is contained in:
xiaofeng.mxf 2019-02-26 23:26:50 +08:00
parent ce1327205c
commit 9682926e67
6 changed files with 57 additions and 18 deletions

View File

@ -550,14 +550,14 @@ function getConnectReqHandler(userRule, recorder, httpsServerMgr) {
shouldIntercept = reqHandlerCtx.forceProxyHttps; shouldIntercept = reqHandlerCtx.forceProxyHttps;
} }
}) })
.then(() => .then(() => {
new Promise((resolve) => { return new Promise((resolve) => {
// mark socket connection as established, to detect the request protocol // mark socket connection as established, to detect the request protocol
cltSocket.write('HTTP/' + req.httpVersion + ' 200 OK\r\n\r\n', 'UTF-8', resolve); cltSocket.write('HTTP/' + req.httpVersion + ' 200 OK\r\n\r\n', 'UTF-8', resolve);
}) });
) })
.then(() => .then(() => {
new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let resolved = false; let resolved = false;
cltSocket.on('data', (chunk) => { cltSocket.on('data', (chunk) => {
requestStream.push(chunk); requestStream.push(chunk);
@ -580,11 +580,19 @@ function getConnectReqHandler(userRule, recorder, httpsServerMgr) {
resolve(); 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', () => { cltSocket.on('end', () => {
requestStream.push(null); requestStream.push(null);
}); });
}) });
) })
.then((result) => { .then((result) => {
// log and recorder // log and recorder
if (shouldIntercept) { if (shouldIntercept) {

View File

@ -66,4 +66,16 @@ module.exports = {
*onConnectError(requestDetail, error) { *onConnectError(requestDetail, error) {
return null; return null;
}, },
/**
*
*
* @param {any} requestDetail
* @param {any} error
* @returns
*/
*onClientSocketError(requestDetail, error) {
return null;
},
}; };

View File

@ -1,6 +1,6 @@
{ {
"name": "anyproxy", "name": "anyproxy",
"version": "4.0.12", "version": "4.0.13",
"description": "A fully configurable HTTP/HTTPS proxy in Node.js", "description": "A fully configurable HTTP/HTTPS proxy in Node.js",
"main": "proxy.js", "main": "proxy.js",
"bin": { "bin": {
@ -61,7 +61,6 @@
"eslint-plugin-react": "^7.4.0", "eslint-plugin-react": "^7.4.0",
"extract-text-webpack-plugin": "^3.0.2", "extract-text-webpack-plugin": "^3.0.2",
"file-loader": "^0.9.0", "file-loader": "^0.9.0",
"https-proxy-agent": "^1.0.0",
"jasmine": "^2.5.3", "jasmine": "^2.5.3",
"koa": "^1.2.1", "koa": "^1.2.1",
"koa-body": "^1.4.0", "koa-body": "^1.4.0",
@ -85,6 +84,7 @@
"stream-equal": "0.1.8", "stream-equal": "0.1.8",
"style-loader": "^0.13.1", "style-loader": "^0.13.1",
"svg-inline-loader": "^0.7.1", "svg-inline-loader": "^0.7.1",
"tunnel": "^0.0.6",
"url-loader": "^0.5.7", "url-loader": "^0.5.7",
"webpack": "^3.10.0", "webpack": "^3.10.0",
"worker-loader": "^0.7.1" "worker-loader": "^0.7.1"

View File

@ -271,7 +271,7 @@ class ProxyCore extends events.EventEmitter {
for (const cltSocketItem of this.requestHandler.cltSockets) { for (const cltSocketItem of this.requestHandler.cltSockets) {
const key = cltSocketItem[0]; const key = cltSocketItem[0];
const cltSocket = cltSocketItem[1]; const cltSocket = cltSocketItem[1];
logUtil.printLog(`endding https cltSocket : ${key}`); logUtil.printLog(`closing https cltSocket : ${key}`);
cltSocket.end(); cltSocket.end();
} }

View File

@ -4,9 +4,10 @@
*/ */
const ProxyServerUtil = require('../util/ProxyServerUtil.js'); const ProxyServerUtil = require('../util/ProxyServerUtil.js');
const { proxyGet } = require('../util/HttpUtil.js'); const { proxyGet } = require('../util/HttpUtil.js');
const { printLog } = require('../util/CommonUtil.js'); const { printLog } = require('../util/CommonUtil.js');
const domain_not_exists = 'not_exist.not_exist_anyproxy_io_domain.com';
let errorInRule = null; let errorInRule = null;
const ruleNotDealError = { const ruleNotDealError = {
*onError(requestDetail, error) { *onError(requestDetail, error) {
@ -62,7 +63,7 @@ function testWrapper(protocol) {
}); });
it('Should get a request error', done => { it('Should get a request error', done => {
const url = protocol + '://not_exist_url.anyproxy.io'; const url = protocol + `://${domain_not_exists}`;
proxyGet(url) proxyGet(url)
.then(proxyRes => { .then(proxyRes => {
expect(proxyRes.statusCode).toEqual(500); expect(proxyRes.statusCode).toEqual(500);
@ -99,7 +100,7 @@ function testWrapper(protocol) {
}); });
it('Should get a request error', done => { it('Should get a request error', done => {
const url = protocol + '://not_exist_url.anyproxy.io'; const url = protocol + `://${domain_not_exists}`;
proxyGet(url) proxyGet(url)
.then(proxyRes => { .then(proxyRes => {
expect(proxyRes.statusCode).toEqual(200); expect(proxyRes.statusCode).toEqual(200);
@ -140,7 +141,7 @@ function testHttpsConnect() {
}); });
it('Should get a request error', done => { it('Should get a request error', done => {
const url = 'https://not_exist_url.anyproxy.io'; const url = `https://${domain_not_exists}`;
proxyGet(url) proxyGet(url)
.then(proxyRes => { .then(proxyRes => {
done.fail('should throw an error when requesting'); done.fail('should throw an error when requesting');

View File

@ -6,19 +6,21 @@
const request = require('request'); const request = require('request');
const fs = require('fs'); const fs = require('fs');
const WebSocket = require('ws'); const WebSocket = require('ws');
const HttpsProxyAgent = require('https-proxy-agent'); const tunnel = require('tunnel');
const stream = require('stream'); const stream = require('stream');
const nodeUrl = require('url');
const PROXY_HOST = 'http://localhost:8001'; const PROXY_HOST = 'http://localhost:8001';
const SOCKET_PROXY_HOST = 'http://localhost:8001'; const SOCKET_PROXY_HOST = 'http://localhost:8001';
const HTTP_SERVER_BASE = 'http://localhost:3000'; const HTTP_SERVER_BASE = 'http://localhost:3000';
const HTTPS_SERVER_BASE = 'https://localhost:3001'; const HTTPS_SERVER_BASE = 'https://localhost:3001';
const WS_SERVER_BASE = 'ws://localhost:3000'; const WS_SERVER_BASE = 'ws://localhost:3000';
const WSS_SERVER_BASE = 'wss://localhost:3001'; const WSS_SERVER_BASE = 'wss://localhost:3001';
const DEFAULT_CHUNK_COLLECT_THRESHOLD = 20 * 1024 * 1024; // about 20 mb const DEFAULT_CHUNK_COLLECT_THRESHOLD = 20 * 1024 * 1024; // about 20 mb
const SOCKE_PROXY_URL_OBJ = nodeUrl.parse(SOCKET_PROXY_HOST);
class commonStream extends stream.Readable { class commonStream extends stream.Readable {
constructor(config) { constructor(config) {
super({ super({
@ -191,7 +193,23 @@ function doWebSocket(url, headers = {}, isProxy) {
let ws; let ws;
if (isProxy) { if (isProxy) {
headers['via-proxy'] = 'true'; headers['via-proxy'] = 'true';
const agent = new HttpsProxyAgent(SOCKET_PROXY_HOST); let agent = new tunnel.httpOverHttp({
proxy: {
hostname: SOCKE_PROXY_URL_OBJ.hostname,
port: SOCKE_PROXY_URL_OBJ.port
}
})
if (url.indexOf('wss') === 0) {
agent = new tunnel.httpsOverHttp({
rejectUnauthorized: false,
proxy: {
hostname: SOCKE_PROXY_URL_OBJ.hostname,
port: SOCKE_PROXY_URL_OBJ.port
}
})
}
ws = new WebSocket(url, { ws = new WebSocket(url, {
agent, agent,
rejectUnauthorized: false, rejectUnauthorized: false,