service.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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;
  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. let ifaces = Object.values(os_1.default.networkInterfaces());
  38. for (let iface of ifaces) {
  39. let addrs = iface;
  40. for (let addr of addrs) {
  41. if (addr.internal || addr.mac === '00:00:00:00:00:00')
  42. continue;
  43. switch (addr.family) {
  44. case 'IPv4':
  45. records.push(this.RecordA(this, addr.address));
  46. break;
  47. case 'IPv6':
  48. records.push(this.RecordAAAA(this, addr.address));
  49. break;
  50. }
  51. }
  52. }
  53. return records;
  54. }
  55. RecordPTR(service) {
  56. return {
  57. name: `${service.type}${TLD}`,
  58. type: 'PTR',
  59. ttl: 28800,
  60. data: service.fqdn
  61. };
  62. }
  63. RecordSRV(service) {
  64. return {
  65. name: service.fqdn,
  66. type: 'SRV',
  67. ttl: 120,
  68. data: {
  69. port: service.port,
  70. target: service.host
  71. }
  72. };
  73. }
  74. RecordTXT(service) {
  75. return {
  76. name: service.fqdn,
  77. type: 'TXT',
  78. ttl: 4500,
  79. data: this.txtService.encode(service.txt)
  80. };
  81. }
  82. RecordA(service, ip) {
  83. return {
  84. name: service.host,
  85. type: 'A',
  86. ttl: 120,
  87. data: ip
  88. };
  89. }
  90. RecordAAAA(service, ip) {
  91. return {
  92. name: service.host,
  93. type: 'AAAA',
  94. ttl: 120,
  95. data: ip
  96. };
  97. }
  98. }
  99. exports.Service = Service;
  100. exports.default = Service;
  101. //# sourceMappingURL=service.js.map