ssh-config.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.ensureHostAndKeyPath = exports.findHostSection = exports.getConfigPath = exports.isHostDirective = exports.isDirective = exports.loadFromPath = exports.SSHConfig = void 0;
  4. const tslib_1 = require("tslib");
  5. const utils_fs_1 = require("@ionic/utils-fs");
  6. const os_1 = tslib_1.__importDefault(require("os"));
  7. const path = tslib_1.__importStar(require("path"));
  8. const ssh_config_1 = tslib_1.__importDefault(require("ssh-config"));
  9. exports.SSHConfig = ssh_config_1.default;
  10. async function loadFromPath(p) {
  11. const s = await (0, utils_fs_1.fileToString)(p);
  12. return ssh_config_1.default.parse(s);
  13. }
  14. exports.loadFromPath = loadFromPath;
  15. function isDirective(entry) {
  16. return entry && entry.type === ssh_config_1.default.DIRECTIVE;
  17. }
  18. exports.isDirective = isDirective;
  19. function isHostDirective(entry) {
  20. return isDirective(entry) && entry.param === 'Host';
  21. }
  22. exports.isHostDirective = isHostDirective;
  23. function getConfigPath() {
  24. return path.resolve(os_1.default.homedir(), '.ssh', 'config');
  25. }
  26. exports.getConfigPath = getConfigPath;
  27. function findHostSection(conf, host) {
  28. return conf.find({ Host: host });
  29. }
  30. exports.findHostSection = findHostSection;
  31. function ensureHostAndKeyPath(conf, conn, keyPath) {
  32. const section = ensureHostSection(conf, conn.host);
  33. const index = conf.indexOf(section);
  34. ensureSectionLine(section, 'IdentityFile', keyPath);
  35. if (typeof conn.port === 'number' && conn.port !== 22) {
  36. ensureSectionLine(section, 'Port', String(conn.port));
  37. }
  38. // massage the section for proper whitespace
  39. if (index === 0) {
  40. section.before = '';
  41. }
  42. else {
  43. const previousSection = conf[index - 1];
  44. if (isHostDirective(previousSection)) {
  45. const previousSectionLastEntry = previousSection.config[previousSection.config.length - 1];
  46. if (previousSectionLastEntry) {
  47. previousSectionLastEntry.after = '\n';
  48. }
  49. }
  50. else {
  51. previousSection.after = '\n';
  52. }
  53. section.before = '\n';
  54. }
  55. section.after = '\n';
  56. if (!section.config) {
  57. section.config = [];
  58. }
  59. for (const entry of section.config) {
  60. entry.before = ' ';
  61. entry.after = '\n';
  62. }
  63. if (index !== conf.length - 1) {
  64. const lastEntry = section.config[section.config.length - 1];
  65. lastEntry.after = '\n\n';
  66. }
  67. }
  68. exports.ensureHostAndKeyPath = ensureHostAndKeyPath;
  69. function ensureHostSection(conf, host) {
  70. let section = findHostSection(conf, host);
  71. if (!section) {
  72. conf.push(ssh_config_1.default.parse(`\nHost ${host}\n`)[0]);
  73. section = findHostSection(conf, host);
  74. }
  75. if (!section) {
  76. throw new Error(`Could not find/insert section for host: ${host}`);
  77. }
  78. return section;
  79. }
  80. function ensureSectionLine(section, key, value) {
  81. const found = section.config.some(line => {
  82. if (isDirective(line)) {
  83. if (line.param === key) {
  84. line.value = value;
  85. return true;
  86. }
  87. }
  88. return false;
  89. });
  90. if (!found) {
  91. section.config = section.config.concat(ssh_config_1.default.parse(`${key} ${value}\n`));
  92. }
  93. }