browsed 0.2.5 → 0.2.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/Gemfile.lock +2 -2
- data/README.md +12 -17
- data/bin/browse +13 -10
- data/lib/browsed.rb +9 -2
- data/lib/browsed/browser.rb +82 -0
- data/lib/browsed/chrome.rb +21 -12
- data/lib/browsed/client.rb +48 -130
- data/lib/browsed/constants.rb +7 -1
- data/lib/browsed/errors.rb +1 -0
- data/lib/browsed/extensions/hash.rb +27 -0
- data/lib/browsed/firefox.rb +18 -17
- data/lib/browsed/manager.rb +5 -5
- data/lib/browsed/{poltergeist.rb → phantomjs.rb} +2 -2
- data/lib/browsed/proxies.rb +26 -0
- data/lib/browsed/{extensions → proxies}/chrome/proxy_authentication.rb +3 -3
- data/lib/browsed/proxies/proxy-chain-server/node_modules/.bin/portastic +1 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/LICENSE +21 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/README.md +57 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/changelog.md +1 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/browser/bluebird.core.js +3824 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/browser/bluebird.core.min.js +31 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/browser/bluebird.js +5666 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/browser/bluebird.min.js +31 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/any.js +21 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/assert.js +55 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/async.js +165 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/bind.js +67 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/bluebird.js +11 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/call_get.js +123 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/cancel.js +129 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/catch_filter.js +42 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/context.js +69 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/debuggability.js +934 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/direct_resolve.js +46 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/each.js +30 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/errors.js +116 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/es5.js +80 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/filter.js +12 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/finally.js +146 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/generators.js +223 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/join.js +168 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/map.js +168 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/method.js +55 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/nodeback.js +51 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/nodeify.js +58 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/promise.js +789 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/promise_array.js +185 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/promisify.js +314 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/props.js +118 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/queue.js +73 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/race.js +49 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/reduce.js +172 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/schedule.js +62 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/settle.js +43 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/some.js +148 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/synchronous_inspection.js +103 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/thenables.js +86 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/timers.js +93 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/using.js +226 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/util.js +389 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/package.json +102 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/commander/CHANGELOG.md +408 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/commander/LICENSE +22 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/commander/Readme.md +428 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/commander/index.js +1224 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/commander/package.json +70 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/commander/typings/index.d.ts +310 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/.coveralls.yml +1 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/.eslintrc +11 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/.npmignore +9 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/.travis.yml +14 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/CHANGELOG.md +362 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/LICENSE +19 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/Makefile +50 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/README.md +312 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/component.json +19 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/karma.conf.js +70 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/node.js +1 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/package.json +88 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/src/browser.js +185 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/src/debug.js +202 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/src/index.js +10 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/src/inspector-log.js +15 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/src/node.js +248 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/ms/index.js +152 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/ms/license.md +21 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/ms/package.json +69 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/ms/readme.md +51 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/LICENSE +13 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/README.md +148 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/bin/portastic +65 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/history.md +7 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/index.js +4 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/lib/monitor.js +59 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/lib/portastic.js +167 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/LICENSE +21 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/README.md +677 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/changelog.md +1730 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/browser/bluebird.js +4892 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/browser/bluebird.min.js +31 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/any.js +21 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/assert.js +55 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/async.js +150 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/bind.js +72 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/bluebird.js +11 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/call_get.js +123 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/cancel.js +48 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/captured_trace.js +493 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/catch_filter.js +66 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/context.js +38 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/debuggability.js +162 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/direct_resolve.js +63 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/each.js +12 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/errors.js +111 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/es5.js +80 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/filter.js +12 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/finally.js +98 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/generators.js +136 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/join.js +107 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/map.js +133 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/method.js +44 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/nodeify.js +59 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/progress.js +76 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/promise.js +759 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/promise_array.js +142 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/promise_resolver.js +123 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/promisify.js +307 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/props.js +79 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/queue.js +90 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/race.js +47 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/reduce.js +148 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/schedule.js +35 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/settle.js +40 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/some.js +125 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/synchronous_inspection.js +94 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/thenables.js +84 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/timers.js +64 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/using.js +213 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/util.js +321 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/package.json +103 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/package.json +69 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/CHANGELOG.md +109 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/LICENSE +201 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/README.md +278 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/build/anonymize_proxy.js +100 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/build/handler_base.js +319 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/build/handler_custom_response.js +96 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/build/handler_forward.js +173 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/build/handler_tunnel_chain.js +132 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/build/handler_tunnel_direct.js +89 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/build/handler_tunnel_tcp_chain.js +271 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/build/index.js +26 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/build/run_locally.js +66 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/build/server.js +595 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/build/tcp_tunnel.js +138 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/build/testing_tcp_service.js +37 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/build/tools.js +220 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/package.json +104 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/underscore/LICENSE +23 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/underscore/README.md +28 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/underscore/package.json +81 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/underscore/underscore-min.js +5 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/underscore/underscore-min.js.map +1 -0
- data/lib/browsed/proxies/proxy-chain-server/node_modules/underscore/underscore.js +1692 -0
- data/lib/browsed/proxies/proxy-chain-server/package-lock.json +63 -0
- data/lib/browsed/proxies/proxy-chain-server/package.json +15 -0
- data/lib/browsed/proxies/proxy-chain-server/server.js +25 -0
- data/lib/browsed/proxies/proxy_chain.rb +36 -0
- data/lib/browsed/version.rb +1 -1
- metadata +160 -4
@@ -0,0 +1,100 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.closeAnonymizedProxy = exports.anonymizeProxy = undefined;
|
7
|
+
|
8
|
+
var _bluebird = require('bluebird');
|
9
|
+
|
10
|
+
var _bluebird2 = _interopRequireDefault(_bluebird);
|
11
|
+
|
12
|
+
var _server = require('./server');
|
13
|
+
|
14
|
+
var _tools = require('./tools');
|
15
|
+
|
16
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
17
|
+
|
18
|
+
// Dictionary, key is value returned from anonymizeProxy(), value is Server instance.
|
19
|
+
var anonymizedProxyUrlToServer = {};
|
20
|
+
|
21
|
+
/**
|
22
|
+
* Parses and validates a HTTP proxy URL. If the proxy requires authentication, then the function
|
23
|
+
* starts an open local proxy server that forwards to the upstream proxy.
|
24
|
+
* @param proxyUrl
|
25
|
+
* @param callback Optional callback that receives the anonymous proxy URL
|
26
|
+
* @return If no callback was supplied, returns a promise that resolves to a String with
|
27
|
+
* anonymous proxy URL or the original URL if it was already anonymous.
|
28
|
+
*/
|
29
|
+
var anonymizeProxy = exports.anonymizeProxy = function anonymizeProxy(proxyUrl, callback) {
|
30
|
+
var parsedProxyUrl = (0, _tools.parseUrl)(proxyUrl);
|
31
|
+
if (!parsedProxyUrl.host || !parsedProxyUrl.port) {
|
32
|
+
throw new Error('Invalid "proxyUrl" option: the URL must contain both hostname and port.');
|
33
|
+
}
|
34
|
+
if (parsedProxyUrl.scheme !== 'http') {
|
35
|
+
throw new Error('Invalid "proxyUrl" option: only HTTP proxies are currently supported.');
|
36
|
+
}
|
37
|
+
|
38
|
+
// If upstream proxy requires no password, return it directly
|
39
|
+
if (!parsedProxyUrl.username && !parsedProxyUrl.password) {
|
40
|
+
return _bluebird2.default.resolve(proxyUrl).nodeify(callback);
|
41
|
+
}
|
42
|
+
|
43
|
+
var port = void 0;
|
44
|
+
var server = void 0;
|
45
|
+
|
46
|
+
var startServer = function startServer(maxRecursion) {
|
47
|
+
return _bluebird2.default.resolve().then(function () {
|
48
|
+
return (0, _tools.findFreePort)();
|
49
|
+
}).then(function (result) {
|
50
|
+
port = result;
|
51
|
+
server = new _server.Server({
|
52
|
+
// verbose: true,
|
53
|
+
port: port,
|
54
|
+
prepareRequestFunction: function prepareRequestFunction() {
|
55
|
+
return {
|
56
|
+
requestAuthentication: false,
|
57
|
+
upstreamProxyUrl: proxyUrl
|
58
|
+
};
|
59
|
+
}
|
60
|
+
});
|
61
|
+
|
62
|
+
return server.listen();
|
63
|
+
}).catch(function (err) {
|
64
|
+
// It might happen that the port was taken in the meantime,
|
65
|
+
// in which case retry the search
|
66
|
+
if (err.code === 'EADDRINUSE' && maxRecursion > 0) {
|
67
|
+
return startServer(maxRecursion - 1);
|
68
|
+
}
|
69
|
+
throw err;
|
70
|
+
});
|
71
|
+
};
|
72
|
+
|
73
|
+
return startServer(_tools.PORT_SELECTION_CONFIG.RETRY_COUNT).then(function () {
|
74
|
+
var url = 'http://127.0.0.1:' + port;
|
75
|
+
anonymizedProxyUrlToServer[url] = server;
|
76
|
+
return url;
|
77
|
+
}).nodeify(callback);
|
78
|
+
};
|
79
|
+
|
80
|
+
/**
|
81
|
+
* Closes anonymous proxy previously started by `anonymizeProxy()`.
|
82
|
+
* If proxy was not found or was already closed, the function has no effect
|
83
|
+
* and its result if `false`. Otherwise the result is `true`.
|
84
|
+
* @param anonymizedProxyUrl
|
85
|
+
* @param closeConnections If true, pending proxy connections are forcibly closed.
|
86
|
+
* @param callback Optional callback
|
87
|
+
* @returns Returns a promise if no callback was supplied
|
88
|
+
*/
|
89
|
+
var closeAnonymizedProxy = exports.closeAnonymizedProxy = function closeAnonymizedProxy(anonymizedProxyUrl, closeConnections, callback) {
|
90
|
+
var server = anonymizedProxyUrlToServer[anonymizedProxyUrl];
|
91
|
+
if (!server) {
|
92
|
+
return _bluebird2.default.resolve(false).nodeify(callback);
|
93
|
+
}
|
94
|
+
|
95
|
+
delete anonymizedProxyUrlToServer[anonymizedProxyUrl];
|
96
|
+
|
97
|
+
return server.close(closeConnections).then(function () {
|
98
|
+
return true;
|
99
|
+
}).nodeify(callback);
|
100
|
+
};
|
@@ -0,0 +1,319 @@
|
|
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 _events = require('events');
|
14
|
+
|
15
|
+
var _events2 = _interopRequireDefault(_events);
|
16
|
+
|
17
|
+
var _server = require('./server');
|
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
|
+
/* globals Buffer */
|
28
|
+
|
29
|
+
/**
|
30
|
+
* Base class for proxy connection handlers. It emits the `destroyed` event
|
31
|
+
* when the handler is no longer used.
|
32
|
+
*/
|
33
|
+
var HandlerBase = function (_EventEmitter) {
|
34
|
+
_inherits(HandlerBase, _EventEmitter);
|
35
|
+
|
36
|
+
function HandlerBase(_ref) {
|
37
|
+
var server = _ref.server,
|
38
|
+
id = _ref.id,
|
39
|
+
srcRequest = _ref.srcRequest,
|
40
|
+
srcHead = _ref.srcHead,
|
41
|
+
srcResponse = _ref.srcResponse,
|
42
|
+
trgParsed = _ref.trgParsed,
|
43
|
+
upstreamProxyUrlParsed = _ref.upstreamProxyUrlParsed;
|
44
|
+
|
45
|
+
_classCallCheck(this, HandlerBase);
|
46
|
+
|
47
|
+
var _this = _possibleConstructorReturn(this, (HandlerBase.__proto__ || Object.getPrototypeOf(HandlerBase)).call(this));
|
48
|
+
|
49
|
+
if (!server) throw new Error('The "server" option is required');
|
50
|
+
if (!id) throw new Error('The "id" option is required');
|
51
|
+
if (!srcRequest) throw new Error('The "srcRequest" option is required');
|
52
|
+
if (!trgParsed.hostname) throw new Error('The "trgParsed.hostname" option is required');
|
53
|
+
|
54
|
+
_this.server = server;
|
55
|
+
_this.id = id;
|
56
|
+
|
57
|
+
_this.srcRequest = srcRequest;
|
58
|
+
_this.srcHead = srcHead;
|
59
|
+
_this.srcResponse = srcResponse;
|
60
|
+
_this.srcSocket = srcRequest.socket;
|
61
|
+
|
62
|
+
_this.trgRequest = null;
|
63
|
+
_this.trgSocket = null;
|
64
|
+
_this.trgParsed = trgParsed;
|
65
|
+
_this.trgParsed.port = _this.trgParsed.port || DEFAULT_TARGET_PORT;
|
66
|
+
|
67
|
+
// Indicates that source socket might have received some data already
|
68
|
+
_this.srcGotResponse = false;
|
69
|
+
|
70
|
+
_this.isClosed = false;
|
71
|
+
|
72
|
+
_this.upstreamProxyUrlParsed = upstreamProxyUrlParsed;
|
73
|
+
|
74
|
+
// Create ServerResponse for the client HTTP request if it doesn't exist
|
75
|
+
// NOTE: This is undocumented API, it might break in the future
|
76
|
+
if (!_this.srcResponse) {
|
77
|
+
_this.srcResponse = new _http2.default.ServerResponse(srcRequest);
|
78
|
+
_this.srcResponse.shouldKeepAlive = false;
|
79
|
+
_this.srcResponse.chunkedEncoding = false;
|
80
|
+
_this.srcResponse.useChunkedEncodingByDefault = false;
|
81
|
+
_this.srcResponse.assignSocket(_this.srcSocket);
|
82
|
+
}
|
83
|
+
|
84
|
+
// Bind all event handlers to this instance
|
85
|
+
_this.bindHandlersToThis(['onSrcResponseFinish', 'onSrcSocketClose', 'onSrcSocketEnd', 'onSrcSocketError', 'onTrgSocket', 'onTrgSocketClose', 'onTrgSocketEnd', 'onTrgSocketError']);
|
86
|
+
|
87
|
+
// Called for the ServerResponse's "finish" event
|
88
|
+
// Normally, Node's "http" module has a "finish" event listener that would
|
89
|
+
// take care of closing the socket once the HTTP response has completed, but
|
90
|
+
// since we're making this ServerResponse instance manually, that event handler
|
91
|
+
// never gets hooked up, so we must manually close the socket...
|
92
|
+
_this.srcResponse.once('finish', _this.onSrcResponseFinish);
|
93
|
+
|
94
|
+
_this.srcSocket.once('close', _this.onSrcSocketClose);
|
95
|
+
_this.srcSocket.once('end', _this.onSrcSocketEnd);
|
96
|
+
_this.srcSocket.once('error', _this.onSrcSocketError);
|
97
|
+
return _this;
|
98
|
+
}
|
99
|
+
|
100
|
+
_createClass(HandlerBase, [{
|
101
|
+
key: 'bindHandlersToThis',
|
102
|
+
value: function bindHandlersToThis(handlerNames) {
|
103
|
+
var _this2 = this;
|
104
|
+
|
105
|
+
handlerNames.forEach(function (evt) {
|
106
|
+
_this2[evt] = _this2[evt].bind(_this2);
|
107
|
+
});
|
108
|
+
}
|
109
|
+
}, {
|
110
|
+
key: 'log',
|
111
|
+
value: function log(str) {
|
112
|
+
this.server.log(this.id, str);
|
113
|
+
}
|
114
|
+
|
115
|
+
// Abstract method, needs to be overridden
|
116
|
+
|
117
|
+
}, {
|
118
|
+
key: 'run',
|
119
|
+
value: function run() {} // eslint-disable-line
|
120
|
+
|
121
|
+
// If the client closes the connection prematurely,
|
122
|
+
// then immediately destroy the upstream socket, there's nothing we can do with it
|
123
|
+
|
124
|
+
}, {
|
125
|
+
key: 'onSrcSocketClose',
|
126
|
+
value: function onSrcSocketClose() {
|
127
|
+
if (this.isClosed) return;
|
128
|
+
this.log('Source socket closed');
|
129
|
+
this.close();
|
130
|
+
}
|
131
|
+
}, {
|
132
|
+
key: 'onSrcSocketEnd',
|
133
|
+
value: function onSrcSocketEnd() {
|
134
|
+
if (this.isClosed) return;
|
135
|
+
this.log('Source socket ended');
|
136
|
+
this.close();
|
137
|
+
}
|
138
|
+
}, {
|
139
|
+
key: 'onSrcSocketError',
|
140
|
+
value: function onSrcSocketError(err) {
|
141
|
+
if (this.isClosed) return;
|
142
|
+
this.log('Source socket failed: ' + (err.stack || err));
|
143
|
+
this.close();
|
144
|
+
}
|
145
|
+
}, {
|
146
|
+
key: 'onSrcResponseFinish',
|
147
|
+
value: function onSrcResponseFinish() {
|
148
|
+
if (this.isClosed) return;
|
149
|
+
this.log('Source response finished, ending source socket');
|
150
|
+
// NOTE: We cannot destroy the socket, since there might be pending data that wouldn't be delivered!
|
151
|
+
// This code is inspired by resOnFinish() in _http_server.js in Node.js code base.
|
152
|
+
if (typeof this.srcSocket.destroySoon === 'function') {
|
153
|
+
this.srcSocket.destroySoon();
|
154
|
+
} else {
|
155
|
+
this.srcSocket.end();
|
156
|
+
}
|
157
|
+
}
|
158
|
+
}, {
|
159
|
+
key: 'onTrgSocket',
|
160
|
+
value: function onTrgSocket(socket) {
|
161
|
+
if (this.isClosed) return;
|
162
|
+
this.log('Target socket assigned');
|
163
|
+
|
164
|
+
this.trgSocket = socket;
|
165
|
+
|
166
|
+
socket.once('close', this.onTrgSocketClose);
|
167
|
+
socket.once('end', this.onTrgSocketEnd);
|
168
|
+
socket.once('error', this.onTrgSocketError);
|
169
|
+
}
|
170
|
+
|
171
|
+
// Once target socket closes, we need to give time
|
172
|
+
// to source socket to receive pending data, so we only call end()
|
173
|
+
|
174
|
+
}, {
|
175
|
+
key: 'onTrgSocketClose',
|
176
|
+
value: function onTrgSocketClose() {
|
177
|
+
if (this.isClosed) return;
|
178
|
+
this.log('Target socket closed');
|
179
|
+
// If socket is closed here instead of response, phantomjs does not properly parse the response as http response.
|
180
|
+
if (this.srcResponse) {
|
181
|
+
this.srcResponse.end();
|
182
|
+
} else if (this.srcSocket) {
|
183
|
+
// Handler tunnel chain does not use srcResponse, but needs to close srcSocket
|
184
|
+
this.srcSocket.end();
|
185
|
+
}
|
186
|
+
}
|
187
|
+
}, {
|
188
|
+
key: 'onTrgSocketEnd',
|
189
|
+
value: function onTrgSocketEnd() {
|
190
|
+
if (this.isClosed) return;
|
191
|
+
this.log('Target socket ended');
|
192
|
+
// If socket is closed here instead of response, phantomjs does not properly parse the response as http response.
|
193
|
+
if (this.srcResponse) {
|
194
|
+
this.srcResponse.end();
|
195
|
+
} else if (this.srcSocket) {
|
196
|
+
// Handler tunnel chain does not use srcResponse, but needs to close srcSocket
|
197
|
+
this.srcSocket.end();
|
198
|
+
}
|
199
|
+
}
|
200
|
+
}, {
|
201
|
+
key: 'onTrgSocketError',
|
202
|
+
value: function onTrgSocketError(err) {
|
203
|
+
if (this.isClosed) return;
|
204
|
+
this.log('Target socket failed: ' + (err.stack || err));
|
205
|
+
this.fail(err);
|
206
|
+
}
|
207
|
+
|
208
|
+
/**
|
209
|
+
* Checks whether response from upstream proxy is 407 Proxy Authentication Required
|
210
|
+
* and if so, responds 502 Bad Gateway to client.
|
211
|
+
* @param response
|
212
|
+
* @return {boolean}
|
213
|
+
*/
|
214
|
+
|
215
|
+
}, {
|
216
|
+
key: 'checkUpstreamProxy407',
|
217
|
+
value: function checkUpstreamProxy407(response) {
|
218
|
+
if (this.upstreamProxyUrlParsed && response.statusCode === 407) {
|
219
|
+
this.fail(new _server.RequestError('Invalid credentials provided for the upstream proxy.', 502));
|
220
|
+
return true;
|
221
|
+
}
|
222
|
+
return false;
|
223
|
+
}
|
224
|
+
}, {
|
225
|
+
key: 'fail',
|
226
|
+
value: function fail(err) {
|
227
|
+
if (this.srcGotResponse) {
|
228
|
+
this.log('Source already received a response, just destroying the socket...');
|
229
|
+
this.close();
|
230
|
+
} else if (err.statusCode) {
|
231
|
+
// Error is RequestError with HTTP status code
|
232
|
+
this.log(err + ', responding with custom status code ' + err.statusCode + ' to client');
|
233
|
+
this.srcResponse.writeHead(err.statusCode);
|
234
|
+
this.srcResponse.end('' + err.message);
|
235
|
+
} else if (err.code === 'ENOTFOUND' && !this.upstreamProxyUrlParsed) {
|
236
|
+
this.log('Target server not found, sending 404 to client');
|
237
|
+
this.srcResponse.writeHead(404);
|
238
|
+
this.srcResponse.end('Target server not found');
|
239
|
+
} else if (err.code === 'ENOTFOUND' && this.upstreamProxyUrlParsed) {
|
240
|
+
this.log('Upstream proxy not found, sending 502 to client');
|
241
|
+
this.srcResponse.writeHead(502);
|
242
|
+
this.srcResponse.end('Upstream proxy was not found');
|
243
|
+
} else if (err.code === 'ECONNREFUSED') {
|
244
|
+
this.log('Upstream proxy refused connection, sending 502 to client');
|
245
|
+
this.srcResponse.writeHead(502);
|
246
|
+
this.srcResponse.end('Upstream proxy refused connection');
|
247
|
+
} else if (err.code === 'ETIMEDOUT') {
|
248
|
+
this.log('Connection timed out, sending 502 to client');
|
249
|
+
this.srcResponse.writeHead(502);
|
250
|
+
this.srcResponse.end('Connection to upstream proxy timed out');
|
251
|
+
} else if (err.code === 'ECONNRESET') {
|
252
|
+
this.log('Connection lost, sending 502 to client');
|
253
|
+
this.srcResponse.writeHead(502);
|
254
|
+
this.srcResponse.end('Connection lost');
|
255
|
+
} else if (err.code === 'EPIPE') {
|
256
|
+
this.log('Socket closed before write, sending 502 to client');
|
257
|
+
this.srcResponse.writeHead(502);
|
258
|
+
this.srcResponse.end('Connection interrupted');
|
259
|
+
} else {
|
260
|
+
this.log('Unknown error, sending 500 to client');
|
261
|
+
this.srcResponse.writeHead(500);
|
262
|
+
this.srcResponse.end('Internal error in proxy server');
|
263
|
+
}
|
264
|
+
}
|
265
|
+
}, {
|
266
|
+
key: 'getStats',
|
267
|
+
value: function getStats() {
|
268
|
+
return {
|
269
|
+
srcTxBytes: this.srcSocket ? this.srcSocket.bytesWritten : null,
|
270
|
+
srcRxBytes: this.srcSocket ? this.srcSocket.bytesRead : null,
|
271
|
+
trgTxBytes: this.trgSocket ? this.trgSocket.bytesWritten : null,
|
272
|
+
trgRxBytes: this.trgSocket ? this.trgSocket.bytesRead : null
|
273
|
+
};
|
274
|
+
}
|
275
|
+
|
276
|
+
/**
|
277
|
+
* Detaches all listeners and destroys all sockets.
|
278
|
+
*/
|
279
|
+
|
280
|
+
}, {
|
281
|
+
key: 'close',
|
282
|
+
value: function close() {
|
283
|
+
if (!this.isClosed) {
|
284
|
+
this.log('Closing handler');
|
285
|
+
|
286
|
+
// Save stats before sockets are destroyed
|
287
|
+
var stats = this.getStats();
|
288
|
+
|
289
|
+
if (this.srcRequest) {
|
290
|
+
this.srcRequest.destroy();
|
291
|
+
this.srcRequest = null;
|
292
|
+
}
|
293
|
+
|
294
|
+
if (this.srcSocket) {
|
295
|
+
this.srcSocket.destroy();
|
296
|
+
this.srcSocket = null;
|
297
|
+
}
|
298
|
+
|
299
|
+
if (this.trgRequest) {
|
300
|
+
this.trgRequest.abort();
|
301
|
+
this.trgRequest = null;
|
302
|
+
}
|
303
|
+
|
304
|
+
if (this.trgSocket) {
|
305
|
+
this.trgSocket.destroy();
|
306
|
+
this.trgSocket = null;
|
307
|
+
}
|
308
|
+
|
309
|
+
this.isClosed = true;
|
310
|
+
|
311
|
+
this.emit('close', { stats: stats });
|
312
|
+
}
|
313
|
+
}
|
314
|
+
}]);
|
315
|
+
|
316
|
+
return HandlerBase;
|
317
|
+
}(_events2.default);
|
318
|
+
|
319
|
+
exports.default = HandlerBase;
|
@@ -0,0 +1,96 @@
|
|
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 _underscore = require('underscore');
|
10
|
+
|
11
|
+
var _underscore2 = _interopRequireDefault(_underscore);
|
12
|
+
|
13
|
+
var _bluebird = require('bluebird');
|
14
|
+
|
15
|
+
var _bluebird2 = _interopRequireDefault(_bluebird);
|
16
|
+
|
17
|
+
var _handler_base = require('./handler_base');
|
18
|
+
|
19
|
+
var _handler_base2 = _interopRequireDefault(_handler_base);
|
20
|
+
|
21
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
22
|
+
|
23
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
24
|
+
|
25
|
+
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; }
|
26
|
+
|
27
|
+
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; }
|
28
|
+
|
29
|
+
/**
|
30
|
+
* Represents a proxied HTTP request for which the response is generated by custom user function.
|
31
|
+
* This is useful e.g. when providing access to external APIs via HTTP proxy interface.
|
32
|
+
*/
|
33
|
+
var HandlerCustomResponse = function (_HandlerBase) {
|
34
|
+
_inherits(HandlerCustomResponse, _HandlerBase);
|
35
|
+
|
36
|
+
function HandlerCustomResponse(options) {
|
37
|
+
_classCallCheck(this, HandlerCustomResponse);
|
38
|
+
|
39
|
+
var _this = _possibleConstructorReturn(this, (HandlerCustomResponse.__proto__ || Object.getPrototypeOf(HandlerCustomResponse)).call(this, options));
|
40
|
+
|
41
|
+
_this.customResponseFunction = options.customResponseFunction;
|
42
|
+
if (!_this.customResponseFunction) throw new Error('The "customResponseFunction" option is required');
|
43
|
+
return _this;
|
44
|
+
}
|
45
|
+
|
46
|
+
_createClass(HandlerCustomResponse, [{
|
47
|
+
key: 'run',
|
48
|
+
value: function run() {
|
49
|
+
var _this2 = this;
|
50
|
+
|
51
|
+
var reqOpts = this.trgParsed;
|
52
|
+
reqOpts.method = this.srcRequest.method;
|
53
|
+
reqOpts.headers = {};
|
54
|
+
|
55
|
+
_bluebird2.default.resolve().then(function () {
|
56
|
+
return _this2.customResponseFunction();
|
57
|
+
}).then(function (customResponse) {
|
58
|
+
if (_this2.isClosed) return;
|
59
|
+
|
60
|
+
if (!customResponse) {
|
61
|
+
throw new Error('The user-provided "customResponseFunction" must return an object.');
|
62
|
+
}
|
63
|
+
|
64
|
+
var statusCode = customResponse.statusCode || 200;
|
65
|
+
var length = customResponse.body ? customResponse.body.length : null;
|
66
|
+
|
67
|
+
_this2.log('Received custom user response (' + statusCode + ', length: ' + length + ', encoding: ' + customResponse.encoding + ')');
|
68
|
+
|
69
|
+
// Forward custom response to source
|
70
|
+
_this2.srcResponse.statusCode = statusCode;
|
71
|
+
|
72
|
+
_underscore2.default.each(customResponse.headers, function (value, key) {
|
73
|
+
_this2.srcResponse.setHeader(key, value);
|
74
|
+
});
|
75
|
+
|
76
|
+
return new _bluebird2.default(function (resolve, reject) {
|
77
|
+
_this2.srcGotResponse = true;
|
78
|
+
_this2.srcResponse.end(customResponse.body, customResponse.encoding, function (err) {
|
79
|
+
if (err) return reject(err);
|
80
|
+
resolve();
|
81
|
+
});
|
82
|
+
});
|
83
|
+
}).then(function () {
|
84
|
+
_this2.log('Custom response sent to source');
|
85
|
+
}).catch(function (err) {
|
86
|
+
if (_this2.isClosed) return;
|
87
|
+
_this2.log('Custom response function failed: ' + (err.stack || err));
|
88
|
+
_this2.fail(err);
|
89
|
+
});
|
90
|
+
}
|
91
|
+
}]);
|
92
|
+
|
93
|
+
return HandlerCustomResponse;
|
94
|
+
}(_handler_base2.default);
|
95
|
+
|
96
|
+
exports.default = HandlerCustomResponse;
|