client.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. 'use strict';
  2. var util = require('util'),
  3. net = require('net'),
  4. tls = require('tls'),
  5. url = require('url'),
  6. driver = require('websocket-driver'),
  7. API = require('./api'),
  8. Event = require('./api/event');
  9. var DEFAULT_PORTS = { 'http:': 80, 'https:': 443, 'ws:':80, 'wss:': 443 },
  10. SECURE_PROTOCOLS = ['https:', 'wss:'];
  11. var Client = function(_url, protocols, options) {
  12. options = options || {};
  13. this.url = _url;
  14. this._driver = driver.client(this.url, { maxLength: options.maxLength, protocols: protocols });
  15. ['open', 'error'].forEach(function(event) {
  16. this._driver.on(event, function() {
  17. self.headers = self._driver.headers;
  18. self.statusCode = self._driver.statusCode;
  19. });
  20. }, this);
  21. var proxy = options.proxy || {},
  22. endpoint = url.parse(proxy.origin || this.url),
  23. port = endpoint.port || DEFAULT_PORTS[endpoint.protocol],
  24. secure = SECURE_PROTOCOLS.indexOf(endpoint.protocol) >= 0,
  25. onConnect = function() { self._onConnect() },
  26. netOptions = options.net || {},
  27. originTLS = options.tls || {},
  28. socketTLS = proxy.origin ? (proxy.tls || {}) : originTLS,
  29. self = this;
  30. netOptions.host = socketTLS.host = endpoint.hostname;
  31. netOptions.port = socketTLS.port = port;
  32. originTLS.ca = originTLS.ca || options.ca;
  33. socketTLS.servername = socketTLS.servername || endpoint.hostname;
  34. this._stream = secure
  35. ? tls.connect(socketTLS, onConnect)
  36. : net.connect(netOptions, onConnect);
  37. if (proxy.origin) this._configureProxy(proxy, originTLS);
  38. API.call(this, options);
  39. };
  40. util.inherits(Client, API);
  41. Client.prototype._onConnect = function() {
  42. var worker = this._proxy || this._driver;
  43. worker.start();
  44. };
  45. Client.prototype._configureProxy = function(proxy, originTLS) {
  46. var uri = url.parse(this.url),
  47. secure = SECURE_PROTOCOLS.indexOf(uri.protocol) >= 0,
  48. self = this,
  49. name;
  50. this._proxy = this._driver.proxy(proxy.origin);
  51. if (proxy.headers) {
  52. for (name in proxy.headers) this._proxy.setHeader(name, proxy.headers[name]);
  53. }
  54. this._proxy.pipe(this._stream, { end: false });
  55. this._stream.pipe(this._proxy);
  56. this._proxy.on('connect', function() {
  57. if (secure) {
  58. var options = { socket: self._stream, servername: uri.hostname };
  59. for (name in originTLS) options[name] = originTLS[name];
  60. self._stream = tls.connect(options);
  61. self._configureStream();
  62. }
  63. self._driver.io.pipe(self._stream);
  64. self._stream.pipe(self._driver.io);
  65. self._driver.start();
  66. });
  67. this._proxy.on('error', function(error) {
  68. self._driver.emit('error', error);
  69. });
  70. };
  71. module.exports = Client;