Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

http-agents.js 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. var HttpAgent = require('http').Agent;
  2. var HttpsAgent = require('https').Agent;
  3. var inherits = require('util').inherits;
  4. var Client;
  5. [HttpAgent, HttpsAgent].forEach((ctor) => {
  6. function SSHAgent(connectCfg, agentOptions) {
  7. if (!(this instanceof SSHAgent))
  8. return new SSHAgent(connectCfg, agentOptions);
  9. ctor.call(this, agentOptions);
  10. this._connectCfg = connectCfg;
  11. this._defaultSrcIP = (agentOptions && agentOptions.srcIP) || 'localhost';
  12. }
  13. inherits(SSHAgent, ctor);
  14. SSHAgent.prototype.createConnection = createConnection;
  15. exports[ctor === HttpAgent ? 'SSHTTPAgent' : 'SSHTTPSAgent'] = SSHAgent;
  16. });
  17. function createConnection(options, cb) {
  18. var srcIP = (options && options.localAddress) || this._defaultSrcIP;
  19. var srcPort = (options && options.localPort) || 0;
  20. var dstIP = options.host;
  21. var dstPort = options.port;
  22. if (Client === undefined)
  23. Client = require('./client').Client;
  24. var client = new Client();
  25. var triedForward = false;
  26. client.on('ready', () => {
  27. client.forwardOut(srcIP, srcPort, dstIP, dstPort, (err, stream) => {
  28. triedForward = true;
  29. if (err) {
  30. client.end();
  31. return cb(err);
  32. }
  33. stream.once('close', () => {
  34. client.end();
  35. });
  36. cb(null, decorateStream(stream));
  37. });
  38. }).on('error', cb).on('close', () => {
  39. if (!triedForward)
  40. cb(new Error('Unexpected connection loss'));
  41. }).connect(this._connectCfg);
  42. }
  43. function noop() {}
  44. function decorateStream(stream) {
  45. stream.setKeepAlive = noop;
  46. stream.setNoDelay = noop;
  47. stream.setTimeout = noop;
  48. stream.ref = noop;
  49. stream.unref = noop;
  50. stream.destroySoon = stream.destroy;
  51. return stream;
  52. }