config.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. var util = require('util');
  2. var defaults = require('lodash.defaults');
  3. var debug = require('debug')('tunnel-ssh-config');
  4. var ConfigError = function (message, extra) {
  5. Error.captureStackTrace(this, this.constructor);
  6. this.name = this.constructor.name;
  7. this.message = message;
  8. this.extra = extra;
  9. };
  10. util.inherits(ConfigError, Error);
  11. function createConfig(config) {
  12. var env = process.env;
  13. defaults(config || {}, {
  14. username: env.TUNNELSSH_USER || env.USER || env.USERNAME || 'root',
  15. port: 22,
  16. host: null,
  17. srcPort: 0,
  18. srcHost: '127.0.0.1',
  19. dstPort: null,
  20. dstHost: '127.0.0.1',
  21. localHost: '127.0.0.1',
  22. localPort: config.dstPort,
  23. agent: process.env.SSH_AUTH_SOCK
  24. });
  25. if (!config.host) {
  26. throw new ConfigError('host not set');
  27. }
  28. if (!config.dstPort) {
  29. throw new ConfigError('dstPort not set');
  30. }
  31. debug('ssh-config', (function () {
  32. var hiddenValues = ['password', 'privateKey'];
  33. return Object.keys(config).reduce(function (obj, key) {
  34. if (hiddenValues.indexOf(key) === -1) {
  35. obj[key] = config[key];
  36. } else {
  37. obj[key] = '***HIDDEN***';
  38. }
  39. return obj;
  40. }, {});
  41. })());
  42. return config;
  43. }
  44. module.exports = createConfig;