proxy_chain_rb 0.1.0
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.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +41 -0
- data/LICENSE.txt +21 -0
- data/README.md +43 -0
- data/Rakefile +6 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/proxy_chain_rb.rb +7 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/.bin/portastic +1 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/LICENSE +21 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/README.md +57 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/changelog.md +1 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/js/browser/bluebird.core.js +3824 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/js/browser/bluebird.core.min.js +31 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/js/browser/bluebird.js +5666 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/js/browser/bluebird.min.js +31 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/js/release/any.js +21 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/js/release/assert.js +55 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/js/release/async.js +165 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/js/release/bind.js +67 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/js/release/bluebird.js +11 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/js/release/call_get.js +123 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/js/release/cancel.js +129 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/js/release/catch_filter.js +42 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/js/release/context.js +69 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/js/release/debuggability.js +934 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/js/release/direct_resolve.js +46 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/js/release/each.js +30 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/js/release/errors.js +116 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/js/release/es5.js +80 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/js/release/filter.js +12 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/js/release/finally.js +146 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/js/release/generators.js +223 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/js/release/join.js +168 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/js/release/map.js +168 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/js/release/method.js +55 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/js/release/nodeback.js +51 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/js/release/nodeify.js +58 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/js/release/promise.js +789 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/js/release/promise_array.js +185 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/js/release/promisify.js +314 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/js/release/props.js +118 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/js/release/queue.js +73 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/js/release/race.js +49 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/js/release/reduce.js +172 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/js/release/schedule.js +62 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/js/release/settle.js +43 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/js/release/some.js +148 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/js/release/synchronous_inspection.js +103 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/js/release/thenables.js +86 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/js/release/timers.js +93 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/js/release/using.js +226 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/js/release/util.js +389 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/bluebird/package.json +102 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/commander/CHANGELOG.md +408 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/commander/LICENSE +22 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/commander/Readme.md +428 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/commander/index.js +1224 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/commander/package.json +70 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/commander/typings/index.d.ts +310 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/debug/.coveralls.yml +1 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/debug/.eslintrc +11 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/debug/.npmignore +9 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/debug/.travis.yml +14 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/debug/CHANGELOG.md +362 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/debug/LICENSE +19 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/debug/Makefile +50 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/debug/README.md +312 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/debug/component.json +19 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/debug/karma.conf.js +70 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/debug/node.js +1 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/debug/package.json +88 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/debug/src/browser.js +185 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/debug/src/debug.js +202 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/debug/src/index.js +10 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/debug/src/inspector-log.js +15 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/debug/src/node.js +248 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/ms/index.js +152 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/ms/license.md +21 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/ms/package.json +69 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/ms/readme.md +51 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/LICENSE +13 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/README.md +148 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/bin/portastic +65 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/history.md +7 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/index.js +4 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/lib/monitor.js +59 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/lib/portastic.js +167 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/LICENSE +21 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/README.md +677 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/changelog.md +1730 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/browser/bluebird.js +4892 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/browser/bluebird.min.js +31 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/any.js +21 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/assert.js +55 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/async.js +150 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/bind.js +72 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/bluebird.js +11 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/call_get.js +123 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/cancel.js +48 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/captured_trace.js +493 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/catch_filter.js +66 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/context.js +38 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/debuggability.js +162 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/direct_resolve.js +63 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/each.js +12 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/errors.js +111 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/es5.js +80 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/filter.js +12 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/finally.js +98 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/generators.js +136 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/join.js +107 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/map.js +133 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/method.js +44 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/nodeify.js +59 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/progress.js +76 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/promise.js +759 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/promise_array.js +142 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/promise_resolver.js +123 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/promisify.js +307 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/props.js +79 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/queue.js +90 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/race.js +47 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/reduce.js +148 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/schedule.js +35 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/settle.js +40 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/some.js +125 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/synchronous_inspection.js +94 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/thenables.js +84 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/timers.js +64 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/using.js +213 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/util.js +321 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/node_modules/bluebird/package.json +103 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/portastic/package.json +69 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/proxy-chain/CHANGELOG.md +109 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/proxy-chain/LICENSE +201 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/proxy-chain/README.md +278 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/proxy-chain/build/anonymize_proxy.js +100 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/proxy-chain/build/handler_base.js +319 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/proxy-chain/build/handler_custom_response.js +96 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/proxy-chain/build/handler_forward.js +173 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/proxy-chain/build/handler_tunnel_chain.js +132 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/proxy-chain/build/handler_tunnel_direct.js +89 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/proxy-chain/build/handler_tunnel_tcp_chain.js +271 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/proxy-chain/build/index.js +26 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/proxy-chain/build/run_locally.js +66 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/proxy-chain/build/server.js +595 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/proxy-chain/build/tcp_tunnel.js +138 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/proxy-chain/build/testing_tcp_service.js +37 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/proxy-chain/build/tools.js +220 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/proxy-chain/package.json +104 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/underscore/LICENSE +23 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/underscore/README.md +28 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/underscore/package.json +81 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/underscore/underscore-min.js +5 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/underscore/underscore-min.js.map +1 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/underscore/underscore.js +1692 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/package-lock.json +63 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/package.json +15 -0
- data/lib/proxy_chain_rb/node_js/proxy-chain-server/server.js +25 -0
- data/lib/proxy_chain_rb/server.rb +49 -0
- data/lib/proxy_chain_rb/version.rb +3 -0
- data/proxy_chain_rb.gemspec +31 -0
- metadata +266 -0
data/lib/proxy_chain_rb/node_js/proxy-chain-server/node_modules/proxy-chain/build/handler_forward.js
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
|
|
7
|
+
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
|
8
|
+
|
|
9
|
+
var _http = require('http');
|
|
10
|
+
|
|
11
|
+
var _http2 = _interopRequireDefault(_http);
|
|
12
|
+
|
|
13
|
+
var _tools = require('./tools');
|
|
14
|
+
|
|
15
|
+
var _handler_base = require('./handler_base');
|
|
16
|
+
|
|
17
|
+
var _handler_base2 = _interopRequireDefault(_handler_base);
|
|
18
|
+
|
|
19
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
20
|
+
|
|
21
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
22
|
+
|
|
23
|
+
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
|
|
24
|
+
|
|
25
|
+
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Represents a proxied request to a HTTP server, either direct or via another upstream proxy.
|
|
29
|
+
*/
|
|
30
|
+
var HandlerForward = function (_HandlerBase) {
|
|
31
|
+
_inherits(HandlerForward, _HandlerBase);
|
|
32
|
+
|
|
33
|
+
function HandlerForward(options) {
|
|
34
|
+
_classCallCheck(this, HandlerForward);
|
|
35
|
+
|
|
36
|
+
var _this = _possibleConstructorReturn(this, (HandlerForward.__proto__ || Object.getPrototypeOf(HandlerForward)).call(this, options));
|
|
37
|
+
|
|
38
|
+
_this.bindHandlersToThis(['onTrgResponse', 'onTrgError']);
|
|
39
|
+
return _this;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
_createClass(HandlerForward, [{
|
|
43
|
+
key: 'run',
|
|
44
|
+
value: function run() {
|
|
45
|
+
var reqOpts = this.trgParsed;
|
|
46
|
+
reqOpts.method = this.srcRequest.method;
|
|
47
|
+
reqOpts.headers = {};
|
|
48
|
+
|
|
49
|
+
// setup outbound proxy request HTTP headers
|
|
50
|
+
// TODO: var hasXForwardedFor = false;
|
|
51
|
+
// var hasVia = false;
|
|
52
|
+
// var via = '1.1 ' + hostname + ' (proxy/' + version + ')';
|
|
53
|
+
|
|
54
|
+
for (var i = 0; i < this.srcRequest.rawHeaders.length; i += 2) {
|
|
55
|
+
var headerName = this.srcRequest.rawHeaders[i];
|
|
56
|
+
var headerValue = this.srcRequest.rawHeaders[i + 1];
|
|
57
|
+
|
|
58
|
+
if (headerName === 'Connection' && headerValue === 'keep-alive') {
|
|
59
|
+
// Keep the "Connection: keep-alive" header, to reduce the chance that the server
|
|
60
|
+
// will detect we're not a browser and also to improve performance
|
|
61
|
+
} else if ((0, _tools.isHopByHopHeader)(headerName)) {
|
|
62
|
+
continue;
|
|
63
|
+
} else if ((0, _tools.isInvalidHeader)(headerName, headerValue)) {
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/*
|
|
68
|
+
if (!hasXForwardedFor && 'x-forwarded-for' === keyLower) {
|
|
69
|
+
// append to existing "X-Forwarded-For" header
|
|
70
|
+
// http://en.wikipedia.org/wiki/X-Forwarded-For
|
|
71
|
+
hasXForwardedFor = true;
|
|
72
|
+
value += ', ' + socket.remoteAddress;
|
|
73
|
+
debug.proxyRequest('appending to existing "%s" header: "%s"', key, value);
|
|
74
|
+
}
|
|
75
|
+
if (!hasVia && 'via' === keyLower) {
|
|
76
|
+
// append to existing "Via" header
|
|
77
|
+
hasVia = true;
|
|
78
|
+
value += ', ' + via;
|
|
79
|
+
debug.proxyRequest('appending to existing "%s" header: "%s"', key, value);
|
|
80
|
+
}
|
|
81
|
+
*/
|
|
82
|
+
|
|
83
|
+
(0, _tools.addHeader)(reqOpts.headers, headerName, headerValue);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/*
|
|
87
|
+
// add "X-Forwarded-For" header if it's still not here by now
|
|
88
|
+
// http://en.wikipedia.org/wiki/X-Forwarded-For
|
|
89
|
+
if (!hasXForwardedFor) {
|
|
90
|
+
headers['X-Forwarded-For'] = socket.remoteAddress;
|
|
91
|
+
debug.proxyRequest('adding new "X-Forwarded-For" header: "%s"', headers['X-Forwarded-For']);
|
|
92
|
+
}
|
|
93
|
+
// add "Via" header if still not set by now
|
|
94
|
+
if (!hasVia) {
|
|
95
|
+
headers.Via = via;
|
|
96
|
+
debug.proxyRequest('adding new "Via" header: "%s"', headers.Via);
|
|
97
|
+
}
|
|
98
|
+
// custom `http.Agent` support, set `server.agent`
|
|
99
|
+
var agent = server.agent;
|
|
100
|
+
if (null != agent) {
|
|
101
|
+
debug.proxyRequest('setting custom `http.Agent` option for proxy request: %s', agent);
|
|
102
|
+
parsed.agent = agent;
|
|
103
|
+
agent = null;
|
|
104
|
+
}
|
|
105
|
+
*/
|
|
106
|
+
|
|
107
|
+
// If desired, send the request via proxy
|
|
108
|
+
if (this.upstreamProxyUrlParsed) {
|
|
109
|
+
reqOpts.host = this.upstreamProxyUrlParsed.hostname;
|
|
110
|
+
reqOpts.hostname = reqOpts.host;
|
|
111
|
+
reqOpts.port = this.upstreamProxyUrlParsed.port;
|
|
112
|
+
|
|
113
|
+
// HTTP requests to proxy contain the full URL in path, for example:
|
|
114
|
+
// "GET http://www.example.com HTTP/1.1\r\n"
|
|
115
|
+
// So we need to replicate it here
|
|
116
|
+
reqOpts.path = this.srcRequest.url;
|
|
117
|
+
|
|
118
|
+
(0, _tools.maybeAddProxyAuthorizationHeader)(this.upstreamProxyUrlParsed, reqOpts.headers);
|
|
119
|
+
|
|
120
|
+
this.log('Connecting to upstream proxy ' + reqOpts.host + ':' + reqOpts.port);
|
|
121
|
+
} else {
|
|
122
|
+
this.log('Connecting to target ' + reqOpts.host);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// console.dir(requestOptions);
|
|
126
|
+
|
|
127
|
+
this.trgRequest = _http2.default.request(reqOpts);
|
|
128
|
+
this.trgRequest.on('response', this.onTrgResponse);
|
|
129
|
+
this.trgRequest.on('error', this.onTrgError);
|
|
130
|
+
this.trgRequest.on('socket', this.onTrgSocket);
|
|
131
|
+
|
|
132
|
+
// this.srcRequest.pipe(tee('to trg')).pipe(this.trgRequest);
|
|
133
|
+
this.srcRequest.pipe(this.trgRequest);
|
|
134
|
+
}
|
|
135
|
+
}, {
|
|
136
|
+
key: 'onTrgResponse',
|
|
137
|
+
value: function onTrgResponse(response) {
|
|
138
|
+
if (this.isClosed) return;
|
|
139
|
+
this.log('Received response from target (' + response.statusCode + ')');
|
|
140
|
+
// console.dir(response);
|
|
141
|
+
|
|
142
|
+
if (this.checkUpstreamProxy407(response)) return;
|
|
143
|
+
|
|
144
|
+
this.srcGotResponse = true;
|
|
145
|
+
|
|
146
|
+
// Prepare response headers
|
|
147
|
+
var headers = {};
|
|
148
|
+
for (var i = 0; i < response.rawHeaders.length; i += 2) {
|
|
149
|
+
var name = response.rawHeaders[i];
|
|
150
|
+
var value = response.rawHeaders[i + 1];
|
|
151
|
+
|
|
152
|
+
if ((0, _tools.isHopByHopHeader)(name)) continue;
|
|
153
|
+
if ((0, _tools.isInvalidHeader)(name, value)) continue;
|
|
154
|
+
|
|
155
|
+
(0, _tools.addHeader)(headers, name, value);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
this.srcResponse.writeHead(response.statusCode, headers);
|
|
159
|
+
response.pipe(this.srcResponse);
|
|
160
|
+
}
|
|
161
|
+
}, {
|
|
162
|
+
key: 'onTrgError',
|
|
163
|
+
value: function onTrgError(err) {
|
|
164
|
+
if (this.isClosed) return;
|
|
165
|
+
this.log('Target socket failed: ' + (err.stack || err));
|
|
166
|
+
this.fail(err);
|
|
167
|
+
}
|
|
168
|
+
}]);
|
|
169
|
+
|
|
170
|
+
return HandlerForward;
|
|
171
|
+
}(_handler_base2.default);
|
|
172
|
+
|
|
173
|
+
exports.default = HandlerForward;
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
|
|
7
|
+
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
|
8
|
+
|
|
9
|
+
var _http = require('http');
|
|
10
|
+
|
|
11
|
+
var _http2 = _interopRequireDefault(_http);
|
|
12
|
+
|
|
13
|
+
var _handler_base = require('./handler_base');
|
|
14
|
+
|
|
15
|
+
var _handler_base2 = _interopRequireDefault(_handler_base);
|
|
16
|
+
|
|
17
|
+
var _tools = require('./tools');
|
|
18
|
+
|
|
19
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
20
|
+
|
|
21
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
22
|
+
|
|
23
|
+
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
|
|
24
|
+
|
|
25
|
+
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
|
|
26
|
+
|
|
27
|
+
// import { tee } from './tools';
|
|
28
|
+
|
|
29
|
+
/* globals Buffer */
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Represents a connection from source client to an external proxy using HTTP CONNECT tunnel.
|
|
33
|
+
*/
|
|
34
|
+
var HandlerTunnelChain = function (_HandlerBase) {
|
|
35
|
+
_inherits(HandlerTunnelChain, _HandlerBase);
|
|
36
|
+
|
|
37
|
+
function HandlerTunnelChain(options) {
|
|
38
|
+
_classCallCheck(this, HandlerTunnelChain);
|
|
39
|
+
|
|
40
|
+
var _this = _possibleConstructorReturn(this, (HandlerTunnelChain.__proto__ || Object.getPrototypeOf(HandlerTunnelChain)).call(this, options));
|
|
41
|
+
|
|
42
|
+
if (!_this.upstreamProxyUrlParsed) throw new Error('The "upstreamProxyUrlParsed" option is required');
|
|
43
|
+
|
|
44
|
+
_this.bindHandlersToThis(['onTrgRequestConnect', 'onTrgRequestAbort', 'onTrgRequestError']);
|
|
45
|
+
return _this;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
_createClass(HandlerTunnelChain, [{
|
|
49
|
+
key: 'run',
|
|
50
|
+
value: function run() {
|
|
51
|
+
this.log('Connecting to upstream proxy...');
|
|
52
|
+
|
|
53
|
+
var targetHost = this.trgParsed.hostname + ':' + this.trgParsed.port;
|
|
54
|
+
|
|
55
|
+
var options = {
|
|
56
|
+
method: 'CONNECT',
|
|
57
|
+
hostname: this.upstreamProxyUrlParsed.hostname,
|
|
58
|
+
port: this.upstreamProxyUrlParsed.port,
|
|
59
|
+
path: targetHost,
|
|
60
|
+
headers: {
|
|
61
|
+
Host: targetHost
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
(0, _tools.maybeAddProxyAuthorizationHeader)(this.upstreamProxyUrlParsed, options.headers);
|
|
66
|
+
|
|
67
|
+
this.trgRequest = _http2.default.request(options);
|
|
68
|
+
|
|
69
|
+
this.trgRequest.once('connect', this.onTrgRequestConnect);
|
|
70
|
+
this.trgRequest.once('abort', this.onTrgRequestAbort);
|
|
71
|
+
this.trgRequest.once('error', this.onTrgRequestError);
|
|
72
|
+
this.trgRequest.on('socket', this.onTrgSocket);
|
|
73
|
+
|
|
74
|
+
// Send the data
|
|
75
|
+
this.trgRequest.end();
|
|
76
|
+
}
|
|
77
|
+
}, {
|
|
78
|
+
key: 'onTrgRequestConnect',
|
|
79
|
+
value: function onTrgRequestConnect(response, socket, head) {
|
|
80
|
+
if (this.isClosed) return;
|
|
81
|
+
this.log('Connected to upstream proxy');
|
|
82
|
+
|
|
83
|
+
if (this.checkUpstreamProxy407(response)) return;
|
|
84
|
+
|
|
85
|
+
this.srcGotResponse = true;
|
|
86
|
+
this.srcResponse.removeListener('finish', this.onSrcResponseFinish);
|
|
87
|
+
this.srcResponse.writeHead(200, 'Connection established');
|
|
88
|
+
|
|
89
|
+
// HACK: force a flush of the HTTP header. This is to ensure 'head' is empty to avoid
|
|
90
|
+
// assert at https://github.com/request/tunnel-agent/blob/master/index.js#L160
|
|
91
|
+
// See also https://github.com/nodejs/node/blob/master/lib/_http_outgoing.js#L217
|
|
92
|
+
this.srcResponse._send('');
|
|
93
|
+
|
|
94
|
+
// relinquish control of the `socket` from the ServerResponse instance
|
|
95
|
+
this.srcResponse.detachSocket(this.srcSocket);
|
|
96
|
+
|
|
97
|
+
// nullify the ServerResponse object, so that it can be cleaned
|
|
98
|
+
// up before this socket proxying is completed
|
|
99
|
+
this.srcResponse = null;
|
|
100
|
+
|
|
101
|
+
// Forward pre-parsed parts of the first packets (if any)
|
|
102
|
+
if (head && head.length > 0) {
|
|
103
|
+
this.srcSocket.write(head);
|
|
104
|
+
}
|
|
105
|
+
if (this.srcHead && this.srcHead.length > 0) {
|
|
106
|
+
this.trgSocket.write(this.srcHead);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Setup bi-directional tunnel
|
|
110
|
+
this.trgSocket.pipe(this.srcSocket);
|
|
111
|
+
this.srcSocket.pipe(this.trgSocket);
|
|
112
|
+
}
|
|
113
|
+
}, {
|
|
114
|
+
key: 'onTrgRequestAbort',
|
|
115
|
+
value: function onTrgRequestAbort() {
|
|
116
|
+
if (this.isClosed) return;
|
|
117
|
+
this.log('Target aborted');
|
|
118
|
+
this.close();
|
|
119
|
+
}
|
|
120
|
+
}, {
|
|
121
|
+
key: 'onTrgRequestError',
|
|
122
|
+
value: function onTrgRequestError(err) {
|
|
123
|
+
if (this.isClosed) return;
|
|
124
|
+
this.log('Target request failed: ' + (err.stack || err));
|
|
125
|
+
this.fail(err);
|
|
126
|
+
}
|
|
127
|
+
}]);
|
|
128
|
+
|
|
129
|
+
return HandlerTunnelChain;
|
|
130
|
+
}(_handler_base2.default);
|
|
131
|
+
|
|
132
|
+
exports.default = HandlerTunnelChain;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
|
|
7
|
+
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
|
8
|
+
|
|
9
|
+
var _net = require('net');
|
|
10
|
+
|
|
11
|
+
var _net2 = _interopRequireDefault(_net);
|
|
12
|
+
|
|
13
|
+
var _handler_base = require('./handler_base');
|
|
14
|
+
|
|
15
|
+
var _handler_base2 = _interopRequireDefault(_handler_base);
|
|
16
|
+
|
|
17
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
18
|
+
|
|
19
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
20
|
+
|
|
21
|
+
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
|
|
22
|
+
|
|
23
|
+
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Represents a proxied connection from source to the target HTTPS server.
|
|
27
|
+
*/
|
|
28
|
+
var HandlerTunnelDirect = function (_HandlerBase) {
|
|
29
|
+
_inherits(HandlerTunnelDirect, _HandlerBase);
|
|
30
|
+
|
|
31
|
+
function HandlerTunnelDirect(options) {
|
|
32
|
+
_classCallCheck(this, HandlerTunnelDirect);
|
|
33
|
+
|
|
34
|
+
var _this = _possibleConstructorReturn(this, (HandlerTunnelDirect.__proto__ || Object.getPrototypeOf(HandlerTunnelDirect)).call(this, options));
|
|
35
|
+
|
|
36
|
+
_this.bindHandlersToThis(['onTrgSocketConnect']);
|
|
37
|
+
return _this;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
_createClass(HandlerTunnelDirect, [{
|
|
41
|
+
key: 'run',
|
|
42
|
+
value: function run() {
|
|
43
|
+
this.log('Connecting to target ' + this.trgParsed.hostname + ':' + this.trgParsed.port);
|
|
44
|
+
|
|
45
|
+
var socket = _net2.default.createConnection(this.trgParsed.port, this.trgParsed.hostname);
|
|
46
|
+
socket.once('connect', this.onTrgSocketConnect);
|
|
47
|
+
|
|
48
|
+
this.onTrgSocket(socket);
|
|
49
|
+
}
|
|
50
|
+
}, {
|
|
51
|
+
key: 'onTrgSocketConnect',
|
|
52
|
+
value: function onTrgSocketConnect(response, socket, head) {
|
|
53
|
+
if (this.isClosed) return;
|
|
54
|
+
this.log('Connected');
|
|
55
|
+
|
|
56
|
+
this.srcGotResponse = true;
|
|
57
|
+
|
|
58
|
+
this.srcResponse.removeListener('finish', this.onSrcResponseFinish);
|
|
59
|
+
this.srcResponse.writeHead(200, 'Connection established');
|
|
60
|
+
|
|
61
|
+
// HACK: force a flush of the HTTP header. This is to ensure 'head' is empty to avoid
|
|
62
|
+
// assert at https://github.com/request/tunnel-agent/blob/master/index.js#L160
|
|
63
|
+
// See also https://github.com/nodejs/node/blob/master/lib/_http_outgoing.js#L217
|
|
64
|
+
this.srcResponse._send('');
|
|
65
|
+
|
|
66
|
+
// Relinquish control of the socket from the ServerResponse instance
|
|
67
|
+
this.srcResponse.detachSocket(this.srcSocket);
|
|
68
|
+
|
|
69
|
+
// ServerResponse is no longer needed
|
|
70
|
+
this.srcResponse = null;
|
|
71
|
+
|
|
72
|
+
// Forward pre-parsed parts of the first packets (if any)
|
|
73
|
+
if (head && head.length > 0) {
|
|
74
|
+
this.srcSocket.write(head);
|
|
75
|
+
}
|
|
76
|
+
if (this.srcHead && this.srcHead.length > 0) {
|
|
77
|
+
this.trgSocket.write(this.srcHead);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Setup bi-directional tunnel
|
|
81
|
+
this.trgSocket.pipe(this.srcSocket);
|
|
82
|
+
this.srcSocket.pipe(this.trgSocket);
|
|
83
|
+
}
|
|
84
|
+
}]);
|
|
85
|
+
|
|
86
|
+
return HandlerTunnelDirect;
|
|
87
|
+
}(_handler_base2.default);
|
|
88
|
+
|
|
89
|
+
exports.default = HandlerTunnelDirect;
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
|
|
7
|
+
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
|
8
|
+
|
|
9
|
+
var _http = require('http');
|
|
10
|
+
|
|
11
|
+
var _http2 = _interopRequireDefault(_http);
|
|
12
|
+
|
|
13
|
+
var _tools = require('./tools');
|
|
14
|
+
|
|
15
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
16
|
+
|
|
17
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
18
|
+
|
|
19
|
+
/* globals Buffer */
|
|
20
|
+
|
|
21
|
+
// TODO: please rename this class to something else than "Handler", it makes it look like the class inherits from HandlerBase, which it doesn't
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Represents a connection from source client to an external proxy using HTTP CONNECT tunnel, allows TCP connection.
|
|
25
|
+
*/
|
|
26
|
+
var HandlerTunnelTcpChain = function () {
|
|
27
|
+
function HandlerTunnelTcpChain(_ref) {
|
|
28
|
+
var srcSocket = _ref.srcSocket,
|
|
29
|
+
trgParsed = _ref.trgParsed,
|
|
30
|
+
upstreamProxyUrlParsed = _ref.upstreamProxyUrlParsed,
|
|
31
|
+
log = _ref.log;
|
|
32
|
+
|
|
33
|
+
_classCallCheck(this, HandlerTunnelTcpChain);
|
|
34
|
+
|
|
35
|
+
this.log = log;
|
|
36
|
+
|
|
37
|
+
// Bind all event handlers to this instance
|
|
38
|
+
this.bindHandlersToThis(['onSrcSocketClose', 'onSrcSocketEnd', 'onSrcSocketError', 'onTrgSocket', 'onTrgSocketClose', 'onTrgSocketEnd', 'onTrgSocketError', 'onTrgRequestConnect', 'onTrgRequestAbort', 'onTrgRequestError']);
|
|
39
|
+
|
|
40
|
+
if (!trgParsed.hostname) throw new Error('The "trgParsed.hostname" option is required');
|
|
41
|
+
if (!trgParsed.port) throw new Error('The "trgParsed.port" option is required');
|
|
42
|
+
|
|
43
|
+
this.trgRequest = null;
|
|
44
|
+
this.trgSocket = null;
|
|
45
|
+
this.trgParsed = trgParsed;
|
|
46
|
+
this.trgParsed.port = this.trgParsed.port || DEFAULT_TARGET_PORT;
|
|
47
|
+
|
|
48
|
+
this.srcSocket = srcSocket;
|
|
49
|
+
this.srcSocket.once('close', this.onSrcSocketClose);
|
|
50
|
+
this.srcSocket.once('end', this.onSrcSocketEnd);
|
|
51
|
+
this.srcSocket.once('error', this.onSrcSocketError);
|
|
52
|
+
|
|
53
|
+
this.upstreamProxyUrlParsed = upstreamProxyUrlParsed;
|
|
54
|
+
|
|
55
|
+
this.isClosed = false;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
_createClass(HandlerTunnelTcpChain, [{
|
|
59
|
+
key: 'bindHandlersToThis',
|
|
60
|
+
value: function bindHandlersToThis(handlerNames) {
|
|
61
|
+
var _this = this;
|
|
62
|
+
|
|
63
|
+
handlerNames.forEach(function (evt) {
|
|
64
|
+
_this[evt] = _this[evt].bind(_this);
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
}, {
|
|
68
|
+
key: 'run',
|
|
69
|
+
value: function run() {
|
|
70
|
+
this.log('Connecting to upstream proxy...');
|
|
71
|
+
|
|
72
|
+
var options = {
|
|
73
|
+
method: 'CONNECT',
|
|
74
|
+
hostname: this.upstreamProxyUrlParsed.hostname,
|
|
75
|
+
port: this.upstreamProxyUrlParsed.port,
|
|
76
|
+
path: this.trgParsed.hostname + ':' + this.trgParsed.port,
|
|
77
|
+
headers: {}
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
(0, _tools.maybeAddProxyAuthorizationHeader)(this.upstreamProxyUrlParsed, options.headers);
|
|
81
|
+
|
|
82
|
+
this.trgRequest = _http2.default.request(options);
|
|
83
|
+
|
|
84
|
+
this.trgRequest.once('connect', this.onTrgRequestConnect);
|
|
85
|
+
this.trgRequest.once('abort', this.onTrgRequestAbort);
|
|
86
|
+
this.trgRequest.once('error', this.onTrgRequestError);
|
|
87
|
+
this.trgRequest.on('socket', this.onTrgSocket);
|
|
88
|
+
|
|
89
|
+
// Send the data
|
|
90
|
+
this.trgRequest.end();
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// If the client closes the connection prematurely,
|
|
94
|
+
// then immediately destroy the upstream socket, there's nothing we can do with it
|
|
95
|
+
|
|
96
|
+
}, {
|
|
97
|
+
key: 'onSrcSocketClose',
|
|
98
|
+
value: function onSrcSocketClose() {
|
|
99
|
+
if (this.isClosed) return;
|
|
100
|
+
this.log('Source socket closed');
|
|
101
|
+
this.close();
|
|
102
|
+
}
|
|
103
|
+
}, {
|
|
104
|
+
key: 'onSrcSocketEnd',
|
|
105
|
+
value: function onSrcSocketEnd() {
|
|
106
|
+
if (this.isClosed) return;
|
|
107
|
+
this.log('Source socket ended');
|
|
108
|
+
this.close();
|
|
109
|
+
}
|
|
110
|
+
}, {
|
|
111
|
+
key: 'onSrcSocketError',
|
|
112
|
+
value: function onSrcSocketError(err) {
|
|
113
|
+
if (this.isClosed) return;
|
|
114
|
+
this.log('Source socket failed: ' + (err.stack || err));
|
|
115
|
+
this.close();
|
|
116
|
+
}
|
|
117
|
+
}, {
|
|
118
|
+
key: 'onTrgSocket',
|
|
119
|
+
value: function onTrgSocket(socket) {
|
|
120
|
+
if (this.isClosed) return;
|
|
121
|
+
|
|
122
|
+
this.log('Target socket assigned');
|
|
123
|
+
|
|
124
|
+
this.trgSocket = socket;
|
|
125
|
+
|
|
126
|
+
socket.once('close', this.onTrgSocketClose);
|
|
127
|
+
socket.once('end', this.onTrgSocketEnd);
|
|
128
|
+
socket.once('error', this.onTrgSocketError);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Once target socket closes, we need to give time
|
|
132
|
+
// to source socket to receive pending data, so we only call end()
|
|
133
|
+
|
|
134
|
+
}, {
|
|
135
|
+
key: 'onTrgSocketClose',
|
|
136
|
+
value: function onTrgSocketClose() {
|
|
137
|
+
var _this2 = this;
|
|
138
|
+
|
|
139
|
+
if (this.isClosed) return;
|
|
140
|
+
this.log('Target socket closed');
|
|
141
|
+
setTimeout(function () {
|
|
142
|
+
if (_this2.srcSocket) _this2.srcSocket.end();
|
|
143
|
+
}, 100);
|
|
144
|
+
}
|
|
145
|
+
}, {
|
|
146
|
+
key: 'onTrgSocketEnd',
|
|
147
|
+
value: function onTrgSocketEnd() {
|
|
148
|
+
var _this3 = this;
|
|
149
|
+
|
|
150
|
+
if (this.isClosed) return;
|
|
151
|
+
this.log('Target socket ended');
|
|
152
|
+
setTimeout(function () {
|
|
153
|
+
if (_this3.srcSocket) _this3.srcSocket.end();
|
|
154
|
+
}, 100);
|
|
155
|
+
}
|
|
156
|
+
}, {
|
|
157
|
+
key: 'onTrgSocketError',
|
|
158
|
+
value: function onTrgSocketError(err) {
|
|
159
|
+
if (this.isClosed) return;
|
|
160
|
+
this.log('Target socket failed: ' + (err.stack || err));
|
|
161
|
+
this.fail(err);
|
|
162
|
+
}
|
|
163
|
+
}, {
|
|
164
|
+
key: 'onTrgRequestConnect',
|
|
165
|
+
value: function onTrgRequestConnect(response) {
|
|
166
|
+
if (this.isClosed) return;
|
|
167
|
+
this.log('Connected to upstream proxy');
|
|
168
|
+
|
|
169
|
+
if (this.checkUpstreamProxy407(response)) return;
|
|
170
|
+
|
|
171
|
+
// Setup bi-directional tunnel
|
|
172
|
+
this.trgSocket.pipe(this.srcSocket);
|
|
173
|
+
this.srcSocket.pipe(this.trgSocket);
|
|
174
|
+
|
|
175
|
+
this.srcSocket.resume();
|
|
176
|
+
}
|
|
177
|
+
}, {
|
|
178
|
+
key: 'onTrgRequestAbort',
|
|
179
|
+
value: function onTrgRequestAbort() {
|
|
180
|
+
if (this.isClosed) return;
|
|
181
|
+
this.log('Target aborted');
|
|
182
|
+
this.close();
|
|
183
|
+
}
|
|
184
|
+
}, {
|
|
185
|
+
key: 'onTrgRequestError',
|
|
186
|
+
value: function onTrgRequestError(err) {
|
|
187
|
+
if (this.isClosed) return;
|
|
188
|
+
this.log('Target request failed: ' + (err.stack || err));
|
|
189
|
+
this.fail(err);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Checks whether response from upstream proxy is 407 Proxy Authentication Required
|
|
194
|
+
* and if so, responds 502 Bad Gateway to client.
|
|
195
|
+
* @param response
|
|
196
|
+
* @return {boolean}
|
|
197
|
+
*/
|
|
198
|
+
|
|
199
|
+
}, {
|
|
200
|
+
key: 'checkUpstreamProxy407',
|
|
201
|
+
value: function checkUpstreamProxy407(response) {
|
|
202
|
+
if (this.upstreamProxyUrlParsed && response.statusCode === 407) {
|
|
203
|
+
this.fail('Invalid credentials provided for the upstream proxy.', 502);
|
|
204
|
+
return true;
|
|
205
|
+
}
|
|
206
|
+
return false;
|
|
207
|
+
}
|
|
208
|
+
}, {
|
|
209
|
+
key: 'fail',
|
|
210
|
+
value: function fail(err, statusCode) {
|
|
211
|
+
if (this.srcGotResponse) {
|
|
212
|
+
this.log('Source already received a response, just destroying the socket...');
|
|
213
|
+
this.close();
|
|
214
|
+
} else if (statusCode) {
|
|
215
|
+
// Manual error
|
|
216
|
+
this.log(err + ', responding with custom status code ' + statusCode + ' to client');
|
|
217
|
+
} else if (err.code === 'ENOTFOUND' && !this.upstreamProxyUrlParsed) {
|
|
218
|
+
this.log('Target server not found, sending 404 to client');
|
|
219
|
+
} else if (err.code === 'ENOTFOUND' && this.upstreamProxyUrlParsed) {
|
|
220
|
+
this.log('Upstream proxy not found, sending 502 to client');
|
|
221
|
+
} else if (err.code === 'ECONNREFUSED') {
|
|
222
|
+
this.log('Upstream proxy refused connection, sending 502 to client');
|
|
223
|
+
} else if (err.code === 'ETIMEDOUT') {
|
|
224
|
+
this.log('Connection timed out, sending 502 to client');
|
|
225
|
+
} else if (err.code === 'ECONNRESET') {
|
|
226
|
+
this.log('Connection lost, sending 502 to client');
|
|
227
|
+
} else if (err.code === 'EPIPE') {
|
|
228
|
+
this.log('Socket closed before write, sending 502 to client');
|
|
229
|
+
} else {
|
|
230
|
+
this.log('Unknown error, sending 500 to client');
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Detaches all listeners and destroys all sockets.
|
|
235
|
+
*/
|
|
236
|
+
|
|
237
|
+
}, {
|
|
238
|
+
key: 'close',
|
|
239
|
+
value: function close() {
|
|
240
|
+
if (!this.isClosed) {
|
|
241
|
+
this.log('Closing handler');
|
|
242
|
+
|
|
243
|
+
if (this.srcRequest) {
|
|
244
|
+
this.srcRequest.destroy();
|
|
245
|
+
this.srcRequest = null;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
if (this.srcSocket) {
|
|
249
|
+
this.srcSocket.destroy();
|
|
250
|
+
this.srcSocket = null;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
if (this.trgRequest) {
|
|
254
|
+
this.trgRequest.abort();
|
|
255
|
+
this.trgRequest = null;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
if (this.trgSocket) {
|
|
259
|
+
this.trgSocket.destroy();
|
|
260
|
+
this.trgSocket = null;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
this.isClosed = true;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}]);
|
|
267
|
+
|
|
268
|
+
return HandlerTunnelTcpChain;
|
|
269
|
+
}();
|
|
270
|
+
|
|
271
|
+
exports.default = HandlerTunnelTcpChain;
|