browser.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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.Browser = void 0;
  7. const dns_txt_1 = __importDefault(require("./dns-txt"));
  8. const dns_equal_1 = __importDefault(require("dns-equal"));
  9. const events_1 = require("events");
  10. const service_types_1 = require("./service-types");
  11. const filter_service_1 = __importDefault(require("./utils/filter-service"));
  12. const filter_txt_1 = __importDefault(require("./utils/filter-txt"));
  13. const TLD = '.local';
  14. const WILDCARD = '_services._dns-sd._udp' + TLD;
  15. class Browser extends events_1.EventEmitter {
  16. constructor(mdns, opts, onup) {
  17. super();
  18. this.onresponse = null;
  19. this.serviceMap = {};
  20. this.wildcard = false;
  21. this._services = [];
  22. if (typeof opts === 'function')
  23. return new Browser(mdns, null, opts);
  24. this.mdns = mdns;
  25. if (opts != null && opts.txt != null) {
  26. this.txt = new dns_txt_1.default(opts.txt);
  27. }
  28. else {
  29. this.txt = new dns_txt_1.default();
  30. }
  31. if (!opts || !opts.type) {
  32. this.name = WILDCARD;
  33. this.wildcard = true;
  34. }
  35. else {
  36. this.name = (0, service_types_1.toString)({ name: opts.type, protocol: opts.protocol || 'tcp' }) + TLD;
  37. if (opts.name)
  38. this.name = opts.name + '.' + this.name;
  39. this.wildcard = false;
  40. }
  41. if (opts != null && opts.txt !== undefined)
  42. this.txtQuery = (0, filter_txt_1.default)(opts.txt);
  43. if (onup)
  44. this.on('up', onup);
  45. this.start();
  46. }
  47. start() {
  48. if (this.onresponse || this.name === undefined)
  49. return;
  50. var self = this;
  51. var nameMap = {};
  52. if (!this.wildcard)
  53. nameMap[this.name] = true;
  54. this.onresponse = (packet, rinfo) => {
  55. if (self.wildcard) {
  56. packet.answers.forEach((answer) => {
  57. if (answer.type !== 'PTR' || answer.name !== self.name || answer.name in nameMap)
  58. return;
  59. nameMap[answer.data] = true;
  60. self.mdns.query(answer.data, 'PTR');
  61. });
  62. }
  63. Object.keys(nameMap).forEach(function (name) {
  64. self.goodbyes(name, packet).forEach(self.removeService.bind(self));
  65. var matches = self.buildServicesFor(name, packet, self.txt, rinfo);
  66. if (matches.length === 0)
  67. return;
  68. matches.forEach((service) => {
  69. if (self.serviceMap[service.fqdn])
  70. return;
  71. self.addService(service);
  72. });
  73. });
  74. };
  75. this.mdns.on('response', this.onresponse);
  76. this.update();
  77. }
  78. stop() {
  79. if (!this.onresponse)
  80. return;
  81. this.mdns.removeListener('response', this.onresponse);
  82. this.onresponse = null;
  83. }
  84. update() {
  85. this.mdns.query(this.name, 'PTR');
  86. }
  87. get services() {
  88. return this._services;
  89. }
  90. addService(service) {
  91. if ((0, filter_service_1.default)(service, this.txtQuery) === false)
  92. return;
  93. this._services.push(service);
  94. this.serviceMap[service.fqdn] = true;
  95. this.emit('up', service);
  96. }
  97. removeService(fqdn) {
  98. var service, index;
  99. this._services.some(function (s, i) {
  100. if ((0, dns_equal_1.default)(s.fqdn, fqdn)) {
  101. service = s;
  102. index = i;
  103. return true;
  104. }
  105. });
  106. if (!service || index === undefined)
  107. return;
  108. this._services.splice(index, 1);
  109. delete this.serviceMap[fqdn];
  110. this.emit('down', service);
  111. }
  112. goodbyes(name, packet) {
  113. return packet.answers.concat(packet.additionals)
  114. .filter((rr) => rr.type === 'PTR' && rr.ttl === 0 && (0, dns_equal_1.default)(rr.name, name))
  115. .map((rr) => rr.data);
  116. }
  117. buildServicesFor(name, packet, txt, referer) {
  118. var records = packet.answers.concat(packet.additionals).filter((rr) => rr.ttl > 0);
  119. return records
  120. .filter((rr) => rr.type === 'PTR' && (0, dns_equal_1.default)(rr.name, name))
  121. .map((ptr) => {
  122. const service = {
  123. addresses: []
  124. };
  125. records
  126. .filter((rr) => {
  127. return (rr.type === 'SRV' || rr.type === 'TXT') && (0, dns_equal_1.default)(rr.name, ptr.data);
  128. })
  129. .forEach((rr) => {
  130. if (rr.type === 'SRV') {
  131. var parts = rr.name.split('.');
  132. var name = parts[0];
  133. var types = (0, service_types_1.toType)(parts.slice(1, -1).join('.'));
  134. service.name = name;
  135. service.fqdn = rr.name;
  136. service.host = rr.data.target;
  137. service.referer = referer;
  138. service.port = rr.data.port;
  139. service.type = types.name;
  140. service.protocol = types.protocol;
  141. service.subtypes = types.subtypes;
  142. }
  143. else if (rr.type === 'TXT') {
  144. service.rawTxt = rr.data;
  145. service.txt = this.txt.decodeAll(rr.data);
  146. }
  147. });
  148. if (!service.name)
  149. return;
  150. records
  151. .filter((rr) => (rr.type === 'A' || rr.type === 'AAAA') && (0, dns_equal_1.default)(rr.name, service.host))
  152. .forEach((rr) => service.addresses.push(rr.data));
  153. return service;
  154. })
  155. .filter((rr) => !!rr);
  156. }
  157. }
  158. exports.Browser = Browser;
  159. exports.default = Browser;
  160. //# sourceMappingURL=browser.js.map