service.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.Service = void 0;
  7. const os_1 = __importDefault(require("os"));
  8. const dns_txt_1 = __importDefault(require("./dns-txt"));
  9. const events_1 = require("events");
  10. const service_types_1 = require("./service-types");
  11. const TLD = '.local';
  12. class Service extends events_1.EventEmitter {
  13. constructor(config) {
  14. super();
  15. this.probe = true;
  16. this.published = false;
  17. this.activated = false;
  18. this.destroyed = false;
  19. this.txtService = new dns_txt_1.default();
  20. if (!config.name)
  21. throw new Error('ServiceConfig requires `name` property to be set');
  22. if (!config.type)
  23. throw new Error('ServiceConfig requires `type` property to be set');
  24. if (!config.port)
  25. throw new Error('ServiceConfig requires `port` property to be set');
  26. this.name = config.name.split('.').join('-');
  27. this.protocol = config.protocol || 'tcp';
  28. this.type = (0, service_types_1.toString)({ name: config.type, protocol: this.protocol });
  29. this.port = config.port;
  30. this.host = config.host || os_1.default.hostname();
  31. this.fqdn = `${this.name}.${this.type}${TLD}`;
  32. this.txt = config.txt;
  33. this.subtypes = config.subtypes;
  34. }
  35. records() {
  36. var records = [this.RecordPTR(this), this.RecordSRV(this), this.RecordTXT(this)];
  37. for (let subtype of this.subtypes || []) {
  38. records.push(this.RecordSubtypePTR(this, subtype));
  39. }
  40. let ifaces = Object.values(os_1.default.networkInterfaces());
  41. for (let iface of ifaces) {
  42. let addrs = iface;
  43. for (let addr of addrs) {
  44. if (addr.internal || addr.mac === '00:00:00:00:00:00')
  45. continue;
  46. switch (addr.family) {
  47. case 'IPv4':
  48. records.push(this.RecordA(this, addr.address));
  49. break;
  50. case 'IPv6':
  51. records.push(this.RecordAAAA(this, addr.address));
  52. break;
  53. }
  54. }
  55. }
  56. return records;
  57. }
  58. RecordPTR(service) {
  59. return {
  60. name: `${service.type}${TLD}`,
  61. type: 'PTR',
  62. ttl: 28800,
  63. data: service.fqdn
  64. };
  65. }
  66. RecordSubtypePTR(service, subtype) {
  67. return {
  68. name: `_${subtype}._sub.${service.type}${TLD}`,
  69. type: 'PTR',
  70. ttl: 28800,
  71. data: `${service.name}.${service.type}${TLD}`
  72. };
  73. }
  74. RecordSRV(service) {
  75. return {
  76. name: service.fqdn,
  77. type: 'SRV',
  78. ttl: 120,
  79. data: {
  80. port: service.port,
  81. target: service.host
  82. }
  83. };
  84. }
  85. RecordTXT(service) {
  86. return {
  87. name: service.fqdn,
  88. type: 'TXT',
  89. ttl: 4500,
  90. data: this.txtService.encode(service.txt)
  91. };
  92. }
  93. RecordA(service, ip) {
  94. return {
  95. name: service.host,
  96. type: 'A',
  97. ttl: 120,
  98. data: ip
  99. };
  100. }
  101. RecordAAAA(service, ip) {
  102. return {
  103. name: service.host,
  104. type: 'AAAA',
  105. ttl: 120,
  106. data: ip
  107. };
  108. }
  109. }
  110. exports.Service = Service;
  111. exports.default = Service;
  112. //# sourceMappingURL=service.js.map