forked from sokarovski/bonjour
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathService.js
More file actions
197 lines (159 loc) · 4.38 KB
/
Service.js
File metadata and controls
197 lines (159 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
'use strict'
var os = require('os')
var util = require('util')
var EventEmitter = require('events').EventEmitter
var serviceName = require('multicast-dns-service-types')
var net = require('net')
var TLD = '.local'
var REANNOUNCE_MAX_MS = 60 * 60 * 1000
var REANNOUNCE_FACTOR = 3
var Service = function (opts) {
if (!opts.name) throw new Error('Required name not given')
if (!opts.type) throw new Error('Required type not given')
if (!opts.port) throw new Error('Required port not given')
this.name = opts.name
this.protocol = opts.protocol || 'tcp'
this.probe = opts.probe !== false
this.type = serviceName.stringify(opts.type, this.protocol)
this.host = opts.host || os.hostname()
this.port = opts.port
this.fqdn = this.name + '.' + this.type + TLD
this.subtypes = opts.subtypes || null
this.txt = opts.txt || null
this.published = false
this.ip = opts.ip || null
this._activated = false // indicates intent - true: starting/started, false: stopping/stopped
}
util.inherits(Service, EventEmitter)
var proto = {
start: function () {
if (this._activated) { return }
this._activated = true
this.emit('service-publish', this)
},
stop: function (cb) {
if (!this._activated) { return } // cb && cb('Not active'); // TODO: What about the callback?
this.emit('service-unpublish', this, cb)
},
updateTxt: function (txt) {
if (this.packet) { this.emit('service-packet-change', this.packet, this.onAnnounceComplete.bind(this)) }
this.packet = null
this.txt = txt
if (!this.published) { return }
this._unpublish()
this.announce()
},
announce: function () {
if (this._destroyed) { return }
if (!this.packet) { this.packet = this._records() }
if (this.timer) { clearTimeout(this.timer) }
this.delay = 1000
this.emit('service-announce-request', this.packet, this.onAnnounceComplete.bind(this))
},
onAnnounceComplete: function () {
if (!this.published) {
this._activated = true // not sure if this is needed here
this.published = true
this.emit('up')
}
this.delay = this.delay * REANNOUNCE_FACTOR
if (this.delay < REANNOUNCE_MAX_MS && !this._destroyed && this._activated) {
this.timer = setTimeout(this.announce.bind(this), this.delay).unref()
} else {
this.timer = undefined
this.delay = undefined
}
},
deactivate: function () {
this._unpublish()
this._activated = false
},
destroy: function () {
this._unpublish()
this.removeAllListeners()
this._destroyed = true
},
_unpublish: function () {
if (this.timer) { clearTimeout(this.timer) }
this.published = false
},
_records: function () {
var records = [this._rrPtr(), this._rrSrv(), this._rrTxt()]
if (this.ip) {
if (net.isIP(this.ip) === 4) {
records.push(rrA(self, this.ip))
return records
} else if (net.isIP(this.ip) === 6) {
records.push(rrAaaa(self, this.ip))
return records
}
}
var interfaces = os.networkInterfaces()
for (var ifaceID in interfaces) {
var iface = interfaces[ifaceID]
for (var i = 0; i < iface.length; i++) {
var address = iface[i]
if (address.internal) { continue }
records.push(
address.family === 'IPv4'
? this._rrA(address.address)
: this._rrAaaa(address.address))
}
}
return records
},
_rrPtr: function () {
return {
name: this.type + TLD,
type: 'PTR',
ttl: 28800,
data: this.fqdn
}
},
_rrSrv: function () {
return {
name: this.fqdn,
type: 'SRV',
ttl: 120,
data: {
port: this.port,
target: this.host
}
}
},
_rrTxt: function () {
var data = []
if (this.txt) {
var txtRecords = this.txt
var keys = Object.keys(txtRecords)
keys.forEach((key) => {
var val = txtRecords[key]
data.push(key + "=" + val);
});
}
return {
name: this.fqdn,
type: 'TXT',
ttl: 120,
data: data
}
},
_rrA: function (ip) {
return {
name: this.host,
type: 'A',
ttl: 120,
data: ip
}
},
_rrAaaa: function (ip) {
return {
name: this.host,
type: 'AAAA',
ttl: 120,
data: ip
}
}
}
for (var x in proto) { Service.prototype[x] = proto[x] }
module.exports = Service