service.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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, start, stop) {
  14. super();
  15. this.start = start;
  16. this.stop = stop;
  17. this.probe = true;
  18. this.published = false;
  19. this.activated = false;
  20. this.destroyed = false;
  21. this.txtService = new dns_txt_1.default();
  22. if (!config.name)
  23. throw new Error('ServiceConfig requires `name` property to be set');
  24. if (!config.type)
  25. throw new Error('ServiceConfig requires `type` property to be set');
  26. if (!config.port)
  27. throw new Error('ServiceConfig requires `port` property to be set');
  28. this.name = config.name.split('.').join('-');
  29. this.protocol = config.protocol || 'tcp';
  30. this.type = (0, service_types_1.toString)({ name: config.type, protocol: this.protocol });
  31. this.port = config.port;
  32. this.host = config.host || os_1.default.hostname();
  33. this.fqdn = `${this.name}.${this.type}${TLD}`;
  34. this.txt = config.txt;
  35. this.subtypes = config.subtypes;
  36. this.disableIPv6 = !!config.disableIPv6;
  37. }
  38. records() {
  39. var records = [
  40. this.RecordPTR(this),
  41. this.RecordSRV(this),
  42. this.RecordTXT(this),
  43. this.RecordServicePTR(this),
  44. ];
  45. for (let subtype of this.subtypes || []) {
  46. records.push(this.RecordSubtypePTR(this, subtype));
  47. }
  48. let ifaces = Object.values(os_1.default.networkInterfaces());
  49. for (let iface of ifaces) {
  50. let addrs = iface;
  51. for (let addr of addrs) {
  52. if (addr.internal || addr.mac === '00:00:00:00:00:00')
  53. continue;
  54. switch (addr.family) {
  55. case 'IPv4':
  56. records.push(this.RecordA(this, addr.address));
  57. break;
  58. case 'IPv6':
  59. if (this.disableIPv6)
  60. break;
  61. records.push(this.RecordAAAA(this, addr.address));
  62. break;
  63. }
  64. }
  65. }
  66. return records;
  67. }
  68. RecordPTR(service) {
  69. return {
  70. name: `${service.type}${TLD}`,
  71. type: 'PTR',
  72. ttl: 28800,
  73. data: service.fqdn
  74. };
  75. }
  76. RecordServicePTR(service) {
  77. return {
  78. name: `_services._dns-sd._udp${TLD}`,
  79. type: 'PTR',
  80. ttl: 120,
  81. data: `${service.type}${TLD}`
  82. };
  83. }
  84. RecordSubtypePTR(service, subtype) {
  85. return {
  86. name: `_${subtype}._sub.${service.type}${TLD}`,
  87. type: 'PTR',
  88. ttl: 28800,
  89. data: `${service.name}.${service.type}${TLD}`
  90. };
  91. }
  92. RecordSRV(service) {
  93. return {
  94. name: service.fqdn,
  95. type: 'SRV',
  96. ttl: 120,
  97. data: {
  98. port: service.port,
  99. target: service.host
  100. }
  101. };
  102. }
  103. RecordTXT(service) {
  104. return {
  105. name: service.fqdn,
  106. type: 'TXT',
  107. ttl: 4500,
  108. data: this.txtService.encode(service.txt)
  109. };
  110. }
  111. RecordA(service, ip) {
  112. return {
  113. name: service.host,
  114. type: 'A',
  115. ttl: 120,
  116. data: ip
  117. };
  118. }
  119. RecordAAAA(service, ip) {
  120. return {
  121. name: service.host,
  122. type: 'AAAA',
  123. ttl: 120,
  124. data: ip
  125. };
  126. }
  127. }
  128. exports.Service = Service;
  129. exports.default = Service;
  130. //# sourceMappingURL=service.js.map