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