browsed 0.2.5 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (172) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -0
  3. data/Gemfile.lock +2 -2
  4. data/README.md +12 -17
  5. data/bin/browse +13 -10
  6. data/lib/browsed.rb +9 -2
  7. data/lib/browsed/browser.rb +82 -0
  8. data/lib/browsed/chrome.rb +21 -12
  9. data/lib/browsed/client.rb +48 -130
  10. data/lib/browsed/constants.rb +7 -1
  11. data/lib/browsed/errors.rb +1 -0
  12. data/lib/browsed/extensions/hash.rb +27 -0
  13. data/lib/browsed/firefox.rb +18 -17
  14. data/lib/browsed/manager.rb +5 -5
  15. data/lib/browsed/{poltergeist.rb → phantomjs.rb} +2 -2
  16. data/lib/browsed/proxies.rb +26 -0
  17. data/lib/browsed/{extensions → proxies}/chrome/proxy_authentication.rb +3 -3
  18. data/lib/browsed/proxies/proxy-chain-server/node_modules/.bin/portastic +1 -0
  19. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/LICENSE +21 -0
  20. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/README.md +57 -0
  21. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/changelog.md +1 -0
  22. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/browser/bluebird.core.js +3824 -0
  23. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/browser/bluebird.core.min.js +31 -0
  24. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/browser/bluebird.js +5666 -0
  25. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/browser/bluebird.min.js +31 -0
  26. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/any.js +21 -0
  27. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/assert.js +55 -0
  28. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/async.js +165 -0
  29. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/bind.js +67 -0
  30. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/bluebird.js +11 -0
  31. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/call_get.js +123 -0
  32. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/cancel.js +129 -0
  33. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/catch_filter.js +42 -0
  34. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/context.js +69 -0
  35. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/debuggability.js +934 -0
  36. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/direct_resolve.js +46 -0
  37. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/each.js +30 -0
  38. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/errors.js +116 -0
  39. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/es5.js +80 -0
  40. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/filter.js +12 -0
  41. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/finally.js +146 -0
  42. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/generators.js +223 -0
  43. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/join.js +168 -0
  44. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/map.js +168 -0
  45. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/method.js +55 -0
  46. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/nodeback.js +51 -0
  47. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/nodeify.js +58 -0
  48. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/promise.js +789 -0
  49. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/promise_array.js +185 -0
  50. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/promisify.js +314 -0
  51. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/props.js +118 -0
  52. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/queue.js +73 -0
  53. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/race.js +49 -0
  54. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/reduce.js +172 -0
  55. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/schedule.js +62 -0
  56. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/settle.js +43 -0
  57. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/some.js +148 -0
  58. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/synchronous_inspection.js +103 -0
  59. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/thenables.js +86 -0
  60. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/timers.js +93 -0
  61. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/using.js +226 -0
  62. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/util.js +389 -0
  63. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/package.json +102 -0
  64. data/lib/browsed/proxies/proxy-chain-server/node_modules/commander/CHANGELOG.md +408 -0
  65. data/lib/browsed/proxies/proxy-chain-server/node_modules/commander/LICENSE +22 -0
  66. data/lib/browsed/proxies/proxy-chain-server/node_modules/commander/Readme.md +428 -0
  67. data/lib/browsed/proxies/proxy-chain-server/node_modules/commander/index.js +1224 -0
  68. data/lib/browsed/proxies/proxy-chain-server/node_modules/commander/package.json +70 -0
  69. data/lib/browsed/proxies/proxy-chain-server/node_modules/commander/typings/index.d.ts +310 -0
  70. data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/.coveralls.yml +1 -0
  71. data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/.eslintrc +11 -0
  72. data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/.npmignore +9 -0
  73. data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/.travis.yml +14 -0
  74. data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/CHANGELOG.md +362 -0
  75. data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/LICENSE +19 -0
  76. data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/Makefile +50 -0
  77. data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/README.md +312 -0
  78. data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/component.json +19 -0
  79. data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/karma.conf.js +70 -0
  80. data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/node.js +1 -0
  81. data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/package.json +88 -0
  82. data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/src/browser.js +185 -0
  83. data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/src/debug.js +202 -0
  84. data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/src/index.js +10 -0
  85. data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/src/inspector-log.js +15 -0
  86. data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/src/node.js +248 -0
  87. data/lib/browsed/proxies/proxy-chain-server/node_modules/ms/index.js +152 -0
  88. data/lib/browsed/proxies/proxy-chain-server/node_modules/ms/license.md +21 -0
  89. data/lib/browsed/proxies/proxy-chain-server/node_modules/ms/package.json +69 -0
  90. data/lib/browsed/proxies/proxy-chain-server/node_modules/ms/readme.md +51 -0
  91. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/LICENSE +13 -0
  92. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/README.md +148 -0
  93. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/bin/portastic +65 -0
  94. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/history.md +7 -0
  95. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/index.js +4 -0
  96. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/lib/monitor.js +59 -0
  97. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/lib/portastic.js +167 -0
  98. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/LICENSE +21 -0
  99. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/README.md +677 -0
  100. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/changelog.md +1730 -0
  101. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/browser/bluebird.js +4892 -0
  102. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/browser/bluebird.min.js +31 -0
  103. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/any.js +21 -0
  104. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/assert.js +55 -0
  105. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/async.js +150 -0
  106. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/bind.js +72 -0
  107. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/bluebird.js +11 -0
  108. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/call_get.js +123 -0
  109. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/cancel.js +48 -0
  110. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/captured_trace.js +493 -0
  111. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/catch_filter.js +66 -0
  112. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/context.js +38 -0
  113. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/debuggability.js +162 -0
  114. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/direct_resolve.js +63 -0
  115. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/each.js +12 -0
  116. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/errors.js +111 -0
  117. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/es5.js +80 -0
  118. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/filter.js +12 -0
  119. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/finally.js +98 -0
  120. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/generators.js +136 -0
  121. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/join.js +107 -0
  122. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/map.js +133 -0
  123. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/method.js +44 -0
  124. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/nodeify.js +59 -0
  125. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/progress.js +76 -0
  126. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/promise.js +759 -0
  127. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/promise_array.js +142 -0
  128. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/promise_resolver.js +123 -0
  129. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/promisify.js +307 -0
  130. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/props.js +79 -0
  131. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/queue.js +90 -0
  132. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/race.js +47 -0
  133. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/reduce.js +148 -0
  134. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/schedule.js +35 -0
  135. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/settle.js +40 -0
  136. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/some.js +125 -0
  137. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/synchronous_inspection.js +94 -0
  138. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/thenables.js +84 -0
  139. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/timers.js +64 -0
  140. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/using.js +213 -0
  141. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/util.js +321 -0
  142. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/package.json +103 -0
  143. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/package.json +69 -0
  144. data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/CHANGELOG.md +109 -0
  145. data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/LICENSE +201 -0
  146. data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/README.md +278 -0
  147. data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/build/anonymize_proxy.js +100 -0
  148. data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/build/handler_base.js +319 -0
  149. data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/build/handler_custom_response.js +96 -0
  150. data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/build/handler_forward.js +173 -0
  151. data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/build/handler_tunnel_chain.js +132 -0
  152. data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/build/handler_tunnel_direct.js +89 -0
  153. data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/build/handler_tunnel_tcp_chain.js +271 -0
  154. data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/build/index.js +26 -0
  155. data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/build/run_locally.js +66 -0
  156. data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/build/server.js +595 -0
  157. data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/build/tcp_tunnel.js +138 -0
  158. data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/build/testing_tcp_service.js +37 -0
  159. data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/build/tools.js +220 -0
  160. data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/package.json +104 -0
  161. data/lib/browsed/proxies/proxy-chain-server/node_modules/underscore/LICENSE +23 -0
  162. data/lib/browsed/proxies/proxy-chain-server/node_modules/underscore/README.md +28 -0
  163. data/lib/browsed/proxies/proxy-chain-server/node_modules/underscore/package.json +81 -0
  164. data/lib/browsed/proxies/proxy-chain-server/node_modules/underscore/underscore-min.js +5 -0
  165. data/lib/browsed/proxies/proxy-chain-server/node_modules/underscore/underscore-min.js.map +1 -0
  166. data/lib/browsed/proxies/proxy-chain-server/node_modules/underscore/underscore.js +1692 -0
  167. data/lib/browsed/proxies/proxy-chain-server/package-lock.json +63 -0
  168. data/lib/browsed/proxies/proxy-chain-server/package.json +15 -0
  169. data/lib/browsed/proxies/proxy-chain-server/server.js +25 -0
  170. data/lib/browsed/proxies/proxy_chain.rb +36 -0
  171. data/lib/browsed/version.rb +1 -1
  172. 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;