You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.6 KiB

4 years ago
  1. 'use strict';
  2. var Buffer = require('safe-buffer').Buffer,
  3. Stream = require('stream').Stream,
  4. url = require('url'),
  5. util = require('util'),
  6. Base = require('./base'),
  7. Headers = require('./headers'),
  8. HttpParser = require('../http_parser');
  9. var PORTS = { 'ws:': 80, 'wss:': 443 };
  10. var Proxy = function(client, origin, options) {
  11. this._client = client;
  12. this._http = new HttpParser('response');
  13. this._origin = (typeof client.url === 'object') ? client.url : url.parse(client.url);
  14. this._url = (typeof origin === 'object') ? origin : url.parse(origin);
  15. this._options = options || {};
  16. this._state = 0;
  17. this.readable = this.writable = true;
  18. this._paused = false;
  19. this._headers = new Headers();
  20. this._headers.set('Host', this._origin.host);
  21. this._headers.set('Connection', 'keep-alive');
  22. this._headers.set('Proxy-Connection', 'keep-alive');
  23. var auth = this._url.auth && Buffer.from(this._url.auth, 'utf8').toString('base64');
  24. if (auth) this._headers.set('Proxy-Authorization', 'Basic ' + auth);
  25. };
  26. util.inherits(Proxy, Stream);
  27. var instance = {
  28. setHeader: function(name, value) {
  29. if (this._state !== 0) return false;
  30. this._headers.set(name, value);
  31. return true;
  32. },
  33. start: function() {
  34. if (this._state !== 0) return false;
  35. this._state = 1;
  36. var origin = this._origin,
  37. port = origin.port || PORTS[origin.protocol],
  38. start = 'CONNECT ' + origin.hostname + ':' + port + ' HTTP/1.1';
  39. var headers = [start, this._headers.toString(), ''];
  40. this.emit('data', Buffer.from(headers.join('\r\n'), 'utf8'));
  41. return true;
  42. },
  43. pause: function() {
  44. this._paused = true;
  45. },
  46. resume: function() {
  47. this._paused = false;
  48. this.emit('drain');
  49. },
  50. write: function(chunk) {
  51. if (!this.writable) return false;
  52. this._http.parse(chunk);
  53. if (!this._http.isComplete()) return !this._paused;
  54. this.statusCode = this._http.statusCode;
  55. this.headers = this._http.headers;
  56. if (this.statusCode === 200) {
  57. this.emit('connect', new Base.ConnectEvent());
  58. } else {
  59. var message = "Can't establish a connection to the server at " + this._origin.href;
  60. this.emit('error', new Error(message));
  61. }
  62. this.end();
  63. return !this._paused;
  64. },
  65. end: function(chunk) {
  66. if (!this.writable) return;
  67. if (chunk !== undefined) this.write(chunk);
  68. this.readable = this.writable = false;
  69. this.emit('close');
  70. this.emit('end');
  71. },
  72. destroy: function() {
  73. this.end();
  74. }
  75. };
  76. for (var key in instance)
  77. Proxy.prototype[key] = instance[key];
  78. module.exports = Proxy;