socksInterceptor.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.SocksInterceptor = void 0;
  6. var socks = _interopRequireWildcard(require("../common/socksProxy"));
  7. var _events = _interopRequireDefault(require("events"));
  8. var _validator = require("../protocol/validator");
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
  11. function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  12. /**
  13. * Copyright (c) Microsoft Corporation.
  14. *
  15. * Licensed under the Apache License, Version 2.0 (the 'License");
  16. * you may not use this file except in compliance with the License.
  17. * You may obtain a copy of the License at
  18. *
  19. * http://www.apache.org/licenses/LICENSE-2.0
  20. *
  21. * Unless required by applicable law or agreed to in writing, software
  22. * distributed under the License is distributed on an "AS IS" BASIS,
  23. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  24. * See the License for the specific language governing permissions and
  25. * limitations under the License.
  26. */
  27. class SocksInterceptor {
  28. constructor(transport, pattern, redirectPortForTest) {
  29. this._handler = void 0;
  30. this._channel = void 0;
  31. this._socksSupportObjectGuid = void 0;
  32. this._ids = new Set();
  33. this._handler = new socks.SocksProxyHandler(pattern, redirectPortForTest);
  34. let lastId = -1;
  35. this._channel = new Proxy(new _events.default(), {
  36. get: (obj, prop) => {
  37. if (prop in obj || obj[prop] !== undefined || typeof prop !== 'string') return obj[prop];
  38. return params => {
  39. try {
  40. const id = --lastId;
  41. this._ids.add(id);
  42. const validator = (0, _validator.findValidator)('SocksSupport', prop, 'Params');
  43. params = validator(params, '', {
  44. tChannelImpl: tChannelForSocks,
  45. binary: 'toBase64'
  46. });
  47. transport.send({
  48. id,
  49. guid: this._socksSupportObjectGuid,
  50. method: prop,
  51. params,
  52. metadata: {
  53. stack: [],
  54. apiName: '',
  55. internal: true
  56. }
  57. });
  58. } catch (e) {}
  59. };
  60. }
  61. });
  62. this._handler.on(socks.SocksProxyHandler.Events.SocksConnected, payload => this._channel.socksConnected(payload));
  63. this._handler.on(socks.SocksProxyHandler.Events.SocksData, payload => this._channel.socksData(payload));
  64. this._handler.on(socks.SocksProxyHandler.Events.SocksError, payload => this._channel.socksError(payload));
  65. this._handler.on(socks.SocksProxyHandler.Events.SocksFailed, payload => this._channel.socksFailed(payload));
  66. this._handler.on(socks.SocksProxyHandler.Events.SocksEnd, payload => this._channel.socksEnd(payload));
  67. this._channel.on('socksRequested', payload => this._handler.socketRequested(payload));
  68. this._channel.on('socksClosed', payload => this._handler.socketClosed(payload));
  69. this._channel.on('socksData', payload => this._handler.sendSocketData(payload));
  70. }
  71. cleanup() {
  72. this._handler.cleanup();
  73. }
  74. interceptMessage(message) {
  75. if (this._ids.has(message.id)) {
  76. this._ids.delete(message.id);
  77. return true;
  78. }
  79. if (message.method === '__create__' && message.params.type === 'SocksSupport') {
  80. this._socksSupportObjectGuid = message.params.guid;
  81. return false;
  82. }
  83. if (this._socksSupportObjectGuid && message.guid === this._socksSupportObjectGuid) {
  84. const validator = (0, _validator.findValidator)('SocksSupport', message.method, 'Event');
  85. const params = validator(message.params, '', {
  86. tChannelImpl: tChannelForSocks,
  87. binary: 'fromBase64'
  88. });
  89. this._channel.emit(message.method, params);
  90. return true;
  91. }
  92. return false;
  93. }
  94. }
  95. exports.SocksInterceptor = SocksInterceptor;
  96. function tChannelForSocks(names, arg, path, context) {
  97. throw new _validator.ValidationError(`${path}: channels are not expected in SocksSupport`);
  98. }