node-opcua-server-discovery 2.76.1 → 2.76.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,450 +1,450 @@
1
- "use strict";
2
- /**
3
- * @module node-opcua-server-discovery
4
- */
5
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
6
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
7
- return new (P || (P = Promise))(function (resolve, reject) {
8
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
9
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
10
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
11
- step((generator = generator.apply(thisArg, _arguments || [])).next());
12
- });
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.OPCUADiscoveryServer = void 0;
16
- const os = require("os");
17
- const path = require("path");
18
- const url = require("url");
19
- const util_1 = require("util");
20
- const chalk = require("chalk");
21
- const envPaths = require("env-paths");
22
- const node_opcua_assert_1 = require("node-opcua-assert");
23
- const node_opcua_common_1 = require("node-opcua-common");
24
- const node_opcua_debug_1 = require("node-opcua-debug");
25
- const node_opcua_secure_channel_1 = require("node-opcua-secure-channel");
26
- const node_opcua_server_1 = require("node-opcua-server");
27
- const node_opcua_service_discovery_1 = require("node-opcua-service-discovery");
28
- const node_opcua_certificate_manager_1 = require("node-opcua-certificate-manager");
29
- const node_opcua_service_endpoints_1 = require("node-opcua-service-endpoints");
30
- const node_opcua_service_endpoints_2 = require("node-opcua-service-endpoints");
31
- const node_opcua_status_code_1 = require("node-opcua-status-code");
32
- const mdns_responder_1 = require("./mdns_responder");
33
- const debugLog = (0, node_opcua_debug_1.make_debugLog)(__filename);
34
- const doDebug = (0, node_opcua_debug_1.checkDebugFlag)(__filename);
35
- function hasCapabilities(serverCapabilities, serverCapabilityFilter) {
36
- if (serverCapabilities == null) {
37
- return true; // filter is empty => no filtering should take place
38
- }
39
- if (serverCapabilityFilter.length === 0) {
40
- return true; // filter is empty => no filtering should take place
41
- }
42
- return !!serverCapabilities.join(" ").match(serverCapabilityFilter);
43
- }
44
- const defaultProductUri = "NodeOPCUA-LocalDiscoveryServer";
45
- const defaultApplicationUri = (0, node_opcua_common_1.makeApplicationUrn)(os.hostname(), defaultProductUri);
46
- function getDefaultCertificateManager() {
47
- const config = envPaths(defaultProductUri).config;
48
- return new node_opcua_certificate_manager_1.OPCUACertificateManager({
49
- name: "PKI",
50
- rootFolder: path.join(config, "PKI"),
51
- automaticallyAcceptUnknownCertificate: true
52
- });
53
- }
54
- class OPCUADiscoveryServer extends node_opcua_server_1.OPCUABaseServer {
55
- constructor(options) {
56
- options.serverInfo = options.serverInfo || {};
57
- const serverInfo = options.serverInfo;
58
- serverInfo.applicationType = node_opcua_service_endpoints_2.ApplicationType.DiscoveryServer;
59
- serverInfo.applicationUri = serverInfo.applicationUri || defaultApplicationUri;
60
- serverInfo.productUri = serverInfo.productUri || defaultProductUri;
61
- serverInfo.applicationName = serverInfo.applicationName || {
62
- text: defaultProductUri,
63
- locale: null
64
- };
65
- serverInfo.gatewayServerUri = serverInfo.gatewayServerUri || "";
66
- serverInfo.discoveryProfileUri = serverInfo.discoveryProfileUri || "";
67
- serverInfo.discoveryUrls = serverInfo.discoveryUrls || [];
68
- options.serverCertificateManager = options.serverCertificateManager || getDefaultCertificateManager();
69
- super(options);
70
- this.bonjourHolder = new node_opcua_service_discovery_1.BonjourHolder();
71
- // see OPC UA Spec 1.2 part 6 : 7.4 Well Known Addresses
72
- // opc.tcp://localhost:4840/UADiscovery
73
- const port = options.port || 4840;
74
- this.capabilitiesForMDNS = ["LDS"];
75
- this.registeredServers = {};
76
- this.mDnsResponder = undefined;
77
- this._delayInit = () => __awaiter(this, void 0, void 0, function* () {
78
- const endPoint = new node_opcua_server_1.OPCUAServerEndPoint({
79
- port,
80
- certificateChain: this.getCertificateChain(),
81
- certificateManager: this.serverCertificateManager,
82
- privateKey: this.getPrivateKey(),
83
- serverInfo: this.serverInfo
84
- });
85
- endPoint.addStandardEndpointDescriptions();
86
- this.endpoints.push(endPoint);
87
- endPoint.on("message", (message, channel) => {
88
- this.on_request(message, channel);
89
- });
90
- });
91
- }
92
- start(callback) {
93
- (0, node_opcua_assert_1.assert)(!this.mDnsResponder);
94
- (0, node_opcua_assert_1.assert)(Array.isArray(this.capabilitiesForMDNS));
95
- this._preInitTask.push(() => __awaiter(this, void 0, void 0, function* () {
96
- yield this._delayInit();
97
- }));
98
- super.start((err) => {
99
- if (err) {
100
- return callback(err);
101
- }
102
- this.mDnsResponder = new mdns_responder_1.MDNSResponder();
103
- // declare discovery server in bonjour
104
- this.bonjourHolder.announcedOnMulticastSubnetWithCallback({
105
- capabilities: this.capabilitiesForMDNS,
106
- name: this.serverInfo.applicationUri,
107
- path: "/DiscoveryServer",
108
- port: this.endpoints[0].port
109
- }, (err2) => {
110
- callback(err2);
111
- });
112
- });
113
- }
114
- shutdown(callback) {
115
- debugLog("stopping announcement of LDS on mDNS");
116
- if (this.mDnsResponder) {
117
- this.mDnsResponder.dispose();
118
- this.mDnsResponder = undefined;
119
- }
120
- this.bonjourHolder.stopAnnouncedOnMulticastSubnetWithCallback((err) => {
121
- if (err) {
122
- console.log("Error ", err.message);
123
- }
124
- debugLog("stopping announcement of LDS on mDNS - DONE");
125
- debugLog("Shutting down Discovery Server");
126
- super.shutdown(() => {
127
- setTimeout(() => {
128
- callback();
129
- }, 100);
130
- });
131
- });
132
- }
133
- /**
134
- * returns the number of registered servers
135
- */
136
- get registeredServerCount() {
137
- return Object.keys(this.registeredServers).length;
138
- }
139
- getServers(channel) {
140
- this.serverInfo.discoveryUrls = this.getDiscoveryUrls();
141
- const servers = [this.serverInfo];
142
- for (const registered_server of Object.values(this.registeredServers)) {
143
- const serverInfo = new node_opcua_service_endpoints_1.ApplicationDescription(registered_server.serverInfo);
144
- servers.push(serverInfo);
145
- }
146
- return servers;
147
- }
148
- _on_RegisterServer2Request(message, channel) {
149
- (0, node_opcua_assert_1.assert)(message.request instanceof node_opcua_service_discovery_1.RegisterServer2Request);
150
- const request = message.request;
151
- (0, node_opcua_assert_1.assert)(request.schema.name === "RegisterServer2Request");
152
- request.discoveryConfiguration = request.discoveryConfiguration || [];
153
- this.__internalRegisterServerWithCallback(node_opcua_service_discovery_1.RegisterServer2Response, request.server, request.discoveryConfiguration, (err, response) => {
154
- // istanbul ignore next
155
- if (err) {
156
- // tslint:disable-next-line: no-console
157
- console.log("What shall I do ?", err.message);
158
- // tslint:disable-next-line: no-console
159
- console.log(err);
160
- let additional_messages = [];
161
- additional_messages.push("EXCEPTION CAUGHT WHILE PROCESSING REQUEST !!! " + request.schema.name);
162
- additional_messages.push(err.message);
163
- if (err.stack) {
164
- additional_messages = additional_messages.concat(err.stack.split("\n"));
165
- }
166
- response = OPCUADiscoveryServer.makeServiceFault(node_opcua_status_code_1.StatusCodes.BadInternalError, additional_messages);
167
- channel.send_response("MSG", response, message);
168
- }
169
- else {
170
- channel.send_response("MSG", response, message);
171
- }
172
- });
173
- }
174
- _on_RegisterServerRequest(message, channel) {
175
- (0, node_opcua_assert_1.assert)(message.request instanceof node_opcua_service_discovery_1.RegisterServerRequest);
176
- const request = message.request;
177
- (0, node_opcua_assert_1.assert)(request.schema.name === "RegisterServerRequest");
178
- this.__internalRegisterServerWithCallback(node_opcua_service_discovery_1.RegisterServerResponse, request.server, undefined, (err, response) => {
179
- channel.send_response("MSG", response, message);
180
- });
181
- }
182
- _on_FindServersOnNetworkRequest(message, channel) {
183
- // from OPCUA 1.04 part 4
184
- // This Service returns the Servers known to a Discovery Server. Unlike FindServer, this Service is
185
- // only implemented by Discovery Servers.
186
- // The Client may reduce the number of results returned by specifying filter criteria. An empty list is
187
- // returned if no Server matches the criteria specified by the Client.
188
- // This Service shall not require message security but it may require transport layer security.
189
- // Each time the Discovery Server creates or updates a record in its cache it shall assign a
190
- // monotonically increasing identifier to the record. This allows Clients to request records in batches
191
- // by specifying the identifier for the last record received in the last call to FindServersOnNetwork.
192
- // To support this the Discovery Server shall return records in numerical order starting from the
193
- // lowest record identifier. The Discovery Server shall also return the last time the counter was reset
194
- // for example due to a restart of the Discovery Server. If a Client detects that this time is more
195
- // recent than the last time the Client called the Service it shall call the Service again with a
196
- // startingRecordId of 0.
197
- // This Service can be used without security and it is therefore vulnerable to Denial Of Service
198
- // (DOS) attacks. A Server should minimize the amount of processing required to send the response
199
- // for this Service. This can be achieved by preparing the result in advance
200
- (0, node_opcua_assert_1.assert)(message.request instanceof node_opcua_service_discovery_1.FindServersOnNetworkRequest);
201
- const request = message.request;
202
- (0, node_opcua_assert_1.assert)(request.schema.name === "FindServersOnNetworkRequest");
203
- function sendError(statusCode) {
204
- const response1 = new node_opcua_service_discovery_1.FindServersOnNetworkResponse({ responseHeader: { serviceResult: statusCode } });
205
- return channel.send_response("MSG", response1, message);
206
- }
207
- // startingRecordId Counter Only records with an identifier greater than this number will be
208
- // returned.
209
- // Specify 0 to start with the first record in the cache.
210
- // maxRecordsToReturn UInt32 The maximum number of records to return in the response.
211
- // 0 indicates that there is no limit.
212
- // serverCapabilityFilter[] String List of Server capability filters. The set of allowed server capabilities
213
- // are defined in Part 12.
214
- // Only records with all of the specified server capabilities are
215
- // returned.
216
- // The comparison is case insensitive.
217
- // If this list is empty then no filtering is performed
218
- // ------------------------
219
- // The last time the counters were reset.
220
- const lastCounterResetTime = new Date();
221
- // servers[] ServerOnNetwork List of DNS service records that meet criteria specified in the
222
- // request. This list is empty if no Servers meet the criteria
223
- const servers = [];
224
- request.serverCapabilityFilter = request.serverCapabilityFilter || [];
225
- const serverCapabilityFilter = request.serverCapabilityFilter
226
- .map((x) => x.toUpperCase())
227
- .sort()
228
- .join(" ");
229
- debugLog(" startingRecordId = ", request.startingRecordId);
230
- if (this.mDnsResponder) {
231
- for (const server of this.mDnsResponder.registeredServers) {
232
- debugLog("Exploring server ", server.serverName);
233
- if (server.recordId <= request.startingRecordId) {
234
- continue;
235
- }
236
- if (!hasCapabilities(server.serverCapabilities, serverCapabilityFilter)) {
237
- // istanbul ignore next
238
- if (doDebug) {
239
- debugLog(" server ", server.serverName, server.serverCapabilities ? server.serverCapabilities.join(",") : [], " does not match serverCapabilities ", serverCapabilityFilter);
240
- }
241
- continue;
242
- }
243
- debugLog(" server ", server.serverName, " found");
244
- servers.push(server);
245
- if (servers.length === request.maxRecordsToReturn) {
246
- debugLog("max records to return reached", request.maxRecordsToReturn);
247
- break;
248
- }
249
- }
250
- }
251
- const response = new node_opcua_service_discovery_1.FindServersOnNetworkResponse({
252
- lastCounterResetTime,
253
- servers
254
- });
255
- channel.send_response("MSG", response, message);
256
- }
257
- __internalRegisterServerWithCallback(RegisterServerXResponse /* RegisterServer2Response | RegisterServerResponse */, rawServer, discoveryConfigurations, callback) {
258
- return __awaiter(this, void 0, void 0, function* () {
259
- // istanbul ignore next
260
- callback(new Error("internal Error"));
261
- });
262
- }
263
- // eslint-disable-next-line max-statements
264
- __internalRegisterServer(RegisterServerXResponse /* RegisterServer2Response | RegisterServerResponse */, rawServer, discoveryConfigurations) {
265
- return __awaiter(this, void 0, void 0, function* () {
266
- const server = rawServer;
267
- if (!discoveryConfigurations) {
268
- discoveryConfigurations = [
269
- new node_opcua_service_discovery_1.MdnsDiscoveryConfiguration({
270
- mdnsServerName: undefined,
271
- serverCapabilities: ["NA"]
272
- })
273
- ];
274
- }
275
- function sendError(statusCode) {
276
- debugLog(chalk.red("_on_RegisterServer(2)Request error"), statusCode.toString());
277
- const response1 = new node_opcua_secure_channel_1.ServiceFault({
278
- responseHeader: { serviceResult: statusCode }
279
- });
280
- return response1;
281
- }
282
- function _stop_announcedOnMulticastSubnet(conf) {
283
- return __awaiter(this, void 0, void 0, function* () {
284
- const b = conf.bonjourHolder;
285
- yield b.stopAnnnouncedOnMulticastSubnet();
286
- conf.bonjourHolder = undefined;
287
- });
288
- }
289
- function _announcedOnMulticastSubnet(conf, announcement) {
290
- return __awaiter(this, void 0, void 0, function* () {
291
- const serviceConfig = (0, node_opcua_service_discovery_1.announcementToServiceConfig)(announcement);
292
- let b = conf.bonjourHolder;
293
- if (b && b.serviceConfig) {
294
- if ((0, node_opcua_service_discovery_1.sameService)(b.serviceConfig, serviceConfig)) {
295
- debugLog("Configuration ", conf.mdnsServerName, " has not changed !");
296
- // nothing to do
297
- return;
298
- }
299
- else {
300
- // istanbul ignore next
301
- if (doDebug) {
302
- debugLog("Configuration ", conf.mdnsServerName, " HAS changed !");
303
- debugLog(" Was ", (0, node_opcua_service_discovery_1.serviceToString)(b.serviceConfig));
304
- debugLog(" is ", announcement);
305
- }
306
- }
307
- yield _stop_announcedOnMulticastSubnet(conf);
308
- }
309
- b = new node_opcua_service_discovery_1.BonjourHolder();
310
- conf.bonjourHolder = b;
311
- yield b.announcedOnMulticastSubnet(announcement);
312
- });
313
- }
314
- function dealWithDiscoveryConfiguration(previousConfMap, server1, serverInfo, discoveryConfiguration) {
315
- return __awaiter(this, void 0, void 0, function* () {
316
- // mdnsServerName String The name of the Server when it is announced via mDNS.
317
- // See Part 12 for the details about mDNS. This string shall be less than 64 bytes.
318
- // If not specified the first element of the serverNames array is used
319
- // (truncated to 63 bytes if necessary).
320
- // serverCapabilities [] String The set of Server capabilities supported by the Server.
321
- // A Server capability is a short identifier for a feature
322
- // The set of allowed Server capabilities are defined in Part 12.
323
- discoveryConfiguration.mdnsServerName = discoveryConfiguration.mdnsServerName || server1.serverNames[0].text;
324
- serverInfo.discoveryUrls = serverInfo.discoveryUrls || [];
325
- const endpointUrl = serverInfo.discoveryUrls[0];
326
- const parsedUrl = url.parse(endpointUrl);
327
- discoveryConfiguration.serverCapabilities = discoveryConfiguration.serverCapabilities || [];
328
- const announcement = {
329
- capabilities: discoveryConfiguration.serverCapabilities.map((x) => x) || ["DA"],
330
- name: discoveryConfiguration.mdnsServerName,
331
- path: parsedUrl.pathname || "/",
332
- port: parseInt(parsedUrl.port, 10)
333
- };
334
- if (previousConfMap[discoveryConfiguration.mdnsServerName]) {
335
- // configuration already exists
336
- debugLog("Configuration ", discoveryConfiguration.mdnsServerName, " already exists !");
337
- const prevConf = previousConfMap[discoveryConfiguration.mdnsServerName];
338
- delete previousConfMap[discoveryConfiguration.mdnsServerName];
339
- discoveryConfiguration.bonjourHolder = prevConf.bonjourHolder;
340
- }
341
- // let's announce the server on the multicast DNS
342
- yield _announcedOnMulticastSubnet(discoveryConfiguration, announcement);
343
- return node_opcua_status_code_1.StatusCodes.Good;
344
- });
345
- }
346
- // check serverType is valid
347
- if (!_isValidServerType(server.serverType)) {
348
- debugLog("Invalid server Type", node_opcua_service_endpoints_2.ApplicationType[server.serverType]);
349
- return sendError(node_opcua_status_code_1.StatusCodes.BadInvalidArgument);
350
- }
351
- if (!server.serverUri) {
352
- debugLog("Missing serverURI");
353
- return sendError(node_opcua_status_code_1.StatusCodes.BadInvalidArgument);
354
- }
355
- // BadServerUriInvalid
356
- // TODO
357
- server.serverNames = server.serverNames || [];
358
- // BadServerNameMissing
359
- if (server.serverNames.length === 0 || !server.serverNames[0].text) {
360
- return sendError(node_opcua_status_code_1.StatusCodes.BadServerNameMissing);
361
- }
362
- // BadDiscoveryUrlMissing
363
- server.discoveryUrls = server.discoveryUrls || [];
364
- if (server.discoveryUrls.length === 0 || !server.discoveryUrls[0]) {
365
- return sendError(node_opcua_status_code_1.StatusCodes.BadDiscoveryUrlMissing);
366
- }
367
- const key = server.serverUri;
368
- let configurationResults = null;
369
- if (server.isOnline) {
370
- debugLog(chalk.cyan(" registering server : "), chalk.yellow(server.serverUri));
371
- // prepare serverInfo which will be used by FindServers
372
- const serverInfo = {
373
- applicationName: server.serverNames[0],
374
- applicationType: server.serverType,
375
- applicationUri: server.serverUri,
376
- discoveryUrls: server.discoveryUrls,
377
- gatewayServerUri: server.gatewayServerUri,
378
- productUri: server.productUri
379
- // XXX ?????? serverInfo.discoveryProfileUri = serverInfo.discoveryProfileUri;
380
- };
381
- const previousConfMap = [];
382
- if (this.registeredServers[key]) {
383
- // server already exists and must only be updated
384
- const previousServer = this.registeredServers[key];
385
- for (const conf of previousServer.discoveryConfiguration) {
386
- previousConfMap[conf.mdnsServerName] = conf;
387
- }
388
- }
389
- this.registeredServers[key] = server;
390
- // xx server.semaphoreFilePath = server.semaphoreFilePath;
391
- // xx server.serverNames = server.serverNames;
392
- server.serverInfo = serverInfo;
393
- server.discoveryConfiguration = discoveryConfigurations;
394
- (0, node_opcua_assert_1.assert)(discoveryConfigurations);
395
- configurationResults = [];
396
- for (const conf of discoveryConfigurations) {
397
- const statusCode = yield dealWithDiscoveryConfiguration(previousConfMap, server, serverInfo, conf);
398
- configurationResults.push(statusCode);
399
- }
400
- // now also unregister unprocessed
401
- if (Object.keys(previousConfMap).length !== 0) {
402
- debugLog(" Warning some conf need to be removed !");
403
- }
404
- }
405
- else {
406
- // server is announced offline
407
- if (key in this.registeredServers) {
408
- const server1 = this.registeredServers[key];
409
- debugLog(chalk.cyan("unregistering server : "), chalk.yellow(server1.serverUri));
410
- configurationResults = [];
411
- discoveryConfigurations = server1.discoveryConfiguration || [];
412
- for (const conf of discoveryConfigurations) {
413
- yield _stop_announcedOnMulticastSubnet(conf);
414
- configurationResults.push(node_opcua_status_code_1.StatusCodes.Good);
415
- }
416
- delete this.registeredServers[key];
417
- }
418
- }
419
- const response = new RegisterServerXResponse({
420
- configurationResults
421
- });
422
- return response;
423
- });
424
- }
425
- }
426
- exports.OPCUADiscoveryServer = OPCUADiscoveryServer;
427
- /*== private
428
- * returns true if the serverType can be added to a discovery server.
429
- * @param serverType
430
- * @return {boolean}
431
- * @private
432
- */
433
- function _isValidServerType(serverType) {
434
- switch (serverType) {
435
- case node_opcua_service_endpoints_2.ApplicationType.Client:
436
- return false;
437
- case node_opcua_service_endpoints_2.ApplicationType.Server:
438
- case node_opcua_service_endpoints_2.ApplicationType.ClientAndServer:
439
- case node_opcua_service_endpoints_2.ApplicationType.DiscoveryServer:
440
- return true;
441
- }
442
- return false;
443
- }
444
- OPCUADiscoveryServer.prototype.__internalRegisterServerWithCallback = (0, util_1.callbackify)(OPCUADiscoveryServer.prototype.__internalRegisterServer);
445
- // tslint:disable-next-line: no-var-requires
446
- const thenify = require("thenify");
447
- const opts = { multiArgs: false };
448
- OPCUADiscoveryServer.prototype.start = thenify.withCallback(OPCUADiscoveryServer.prototype.start, opts);
449
- OPCUADiscoveryServer.prototype.shutdown = thenify.withCallback(OPCUADiscoveryServer.prototype.shutdown, opts);
1
+ "use strict";
2
+ /**
3
+ * @module node-opcua-server-discovery
4
+ */
5
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
6
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
7
+ return new (P || (P = Promise))(function (resolve, reject) {
8
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
9
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
10
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
11
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
12
+ });
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.OPCUADiscoveryServer = void 0;
16
+ const os = require("os");
17
+ const path = require("path");
18
+ const url = require("url");
19
+ const util_1 = require("util");
20
+ const chalk = require("chalk");
21
+ const env_paths_1 = require("env-paths");
22
+ const node_opcua_assert_1 = require("node-opcua-assert");
23
+ const node_opcua_common_1 = require("node-opcua-common");
24
+ const node_opcua_debug_1 = require("node-opcua-debug");
25
+ const node_opcua_secure_channel_1 = require("node-opcua-secure-channel");
26
+ const node_opcua_server_1 = require("node-opcua-server");
27
+ const node_opcua_service_discovery_1 = require("node-opcua-service-discovery");
28
+ const node_opcua_certificate_manager_1 = require("node-opcua-certificate-manager");
29
+ const node_opcua_service_endpoints_1 = require("node-opcua-service-endpoints");
30
+ const node_opcua_service_endpoints_2 = require("node-opcua-service-endpoints");
31
+ const node_opcua_status_code_1 = require("node-opcua-status-code");
32
+ const mdns_responder_1 = require("./mdns_responder");
33
+ const debugLog = (0, node_opcua_debug_1.make_debugLog)(__filename);
34
+ const doDebug = (0, node_opcua_debug_1.checkDebugFlag)(__filename);
35
+ function hasCapabilities(serverCapabilities, serverCapabilityFilter) {
36
+ if (serverCapabilities == null) {
37
+ return true; // filter is empty => no filtering should take place
38
+ }
39
+ if (serverCapabilityFilter.length === 0) {
40
+ return true; // filter is empty => no filtering should take place
41
+ }
42
+ return !!serverCapabilities.join(" ").match(serverCapabilityFilter);
43
+ }
44
+ const defaultProductUri = "NodeOPCUA-LocalDiscoveryServer";
45
+ const defaultApplicationUri = (0, node_opcua_common_1.makeApplicationUrn)(os.hostname(), defaultProductUri);
46
+ function getDefaultCertificateManager() {
47
+ const config = (0, env_paths_1.default)(defaultProductUri).config;
48
+ return new node_opcua_certificate_manager_1.OPCUACertificateManager({
49
+ name: "PKI",
50
+ rootFolder: path.join(config, "PKI"),
51
+ automaticallyAcceptUnknownCertificate: true
52
+ });
53
+ }
54
+ class OPCUADiscoveryServer extends node_opcua_server_1.OPCUABaseServer {
55
+ constructor(options) {
56
+ options.serverInfo = options.serverInfo || {};
57
+ const serverInfo = options.serverInfo;
58
+ serverInfo.applicationType = node_opcua_service_endpoints_2.ApplicationType.DiscoveryServer;
59
+ serverInfo.applicationUri = serverInfo.applicationUri || defaultApplicationUri;
60
+ serverInfo.productUri = serverInfo.productUri || defaultProductUri;
61
+ serverInfo.applicationName = serverInfo.applicationName || {
62
+ text: defaultProductUri,
63
+ locale: null
64
+ };
65
+ serverInfo.gatewayServerUri = serverInfo.gatewayServerUri || "";
66
+ serverInfo.discoveryProfileUri = serverInfo.discoveryProfileUri || "";
67
+ serverInfo.discoveryUrls = serverInfo.discoveryUrls || [];
68
+ options.serverCertificateManager = options.serverCertificateManager || getDefaultCertificateManager();
69
+ super(options);
70
+ this.bonjourHolder = new node_opcua_service_discovery_1.BonjourHolder();
71
+ // see OPC UA Spec 1.2 part 6 : 7.4 Well Known Addresses
72
+ // opc.tcp://localhost:4840/UADiscovery
73
+ const port = options.port || 4840;
74
+ this.capabilitiesForMDNS = ["LDS"];
75
+ this.registeredServers = {};
76
+ this.mDnsResponder = undefined;
77
+ this._delayInit = () => __awaiter(this, void 0, void 0, function* () {
78
+ const endPoint = new node_opcua_server_1.OPCUAServerEndPoint({
79
+ port,
80
+ certificateChain: this.getCertificateChain(),
81
+ certificateManager: this.serverCertificateManager,
82
+ privateKey: this.getPrivateKey(),
83
+ serverInfo: this.serverInfo
84
+ });
85
+ endPoint.addStandardEndpointDescriptions();
86
+ this.endpoints.push(endPoint);
87
+ endPoint.on("message", (message, channel) => {
88
+ this.on_request(message, channel);
89
+ });
90
+ });
91
+ }
92
+ start(callback) {
93
+ (0, node_opcua_assert_1.assert)(!this.mDnsResponder);
94
+ (0, node_opcua_assert_1.assert)(Array.isArray(this.capabilitiesForMDNS));
95
+ this._preInitTask.push(() => __awaiter(this, void 0, void 0, function* () {
96
+ yield this._delayInit();
97
+ }));
98
+ super.start((err) => {
99
+ if (err) {
100
+ return callback(err);
101
+ }
102
+ this.mDnsResponder = new mdns_responder_1.MDNSResponder();
103
+ // declare discovery server in bonjour
104
+ this.bonjourHolder.announcedOnMulticastSubnetWithCallback({
105
+ capabilities: this.capabilitiesForMDNS,
106
+ name: this.serverInfo.applicationUri,
107
+ path: "/DiscoveryServer",
108
+ port: this.endpoints[0].port
109
+ }, (err2) => {
110
+ callback(err2);
111
+ });
112
+ });
113
+ }
114
+ shutdown(callback) {
115
+ debugLog("stopping announcement of LDS on mDNS");
116
+ if (this.mDnsResponder) {
117
+ this.mDnsResponder.dispose();
118
+ this.mDnsResponder = undefined;
119
+ }
120
+ this.bonjourHolder.stopAnnouncedOnMulticastSubnetWithCallback((err) => {
121
+ if (err) {
122
+ console.log("Error ", err.message);
123
+ }
124
+ debugLog("stopping announcement of LDS on mDNS - DONE");
125
+ debugLog("Shutting down Discovery Server");
126
+ super.shutdown(() => {
127
+ setTimeout(() => {
128
+ callback();
129
+ }, 100);
130
+ });
131
+ });
132
+ }
133
+ /**
134
+ * returns the number of registered servers
135
+ */
136
+ get registeredServerCount() {
137
+ return Object.keys(this.registeredServers).length;
138
+ }
139
+ getServers(channel) {
140
+ this.serverInfo.discoveryUrls = this.getDiscoveryUrls();
141
+ const servers = [this.serverInfo];
142
+ for (const registered_server of Object.values(this.registeredServers)) {
143
+ const serverInfo = new node_opcua_service_endpoints_1.ApplicationDescription(registered_server.serverInfo);
144
+ servers.push(serverInfo);
145
+ }
146
+ return servers;
147
+ }
148
+ _on_RegisterServer2Request(message, channel) {
149
+ (0, node_opcua_assert_1.assert)(message.request instanceof node_opcua_service_discovery_1.RegisterServer2Request);
150
+ const request = message.request;
151
+ (0, node_opcua_assert_1.assert)(request.schema.name === "RegisterServer2Request");
152
+ request.discoveryConfiguration = request.discoveryConfiguration || [];
153
+ this.__internalRegisterServerWithCallback(node_opcua_service_discovery_1.RegisterServer2Response, request.server, request.discoveryConfiguration, (err, response) => {
154
+ // istanbul ignore next
155
+ if (err) {
156
+ // tslint:disable-next-line: no-console
157
+ console.log("What shall I do ?", err.message);
158
+ // tslint:disable-next-line: no-console
159
+ console.log(err);
160
+ let additional_messages = [];
161
+ additional_messages.push("EXCEPTION CAUGHT WHILE PROCESSING REQUEST !!! " + request.schema.name);
162
+ additional_messages.push(err.message);
163
+ if (err.stack) {
164
+ additional_messages = additional_messages.concat(err.stack.split("\n"));
165
+ }
166
+ response = OPCUADiscoveryServer.makeServiceFault(node_opcua_status_code_1.StatusCodes.BadInternalError, additional_messages);
167
+ channel.send_response("MSG", response, message);
168
+ }
169
+ else {
170
+ channel.send_response("MSG", response, message);
171
+ }
172
+ });
173
+ }
174
+ _on_RegisterServerRequest(message, channel) {
175
+ (0, node_opcua_assert_1.assert)(message.request instanceof node_opcua_service_discovery_1.RegisterServerRequest);
176
+ const request = message.request;
177
+ (0, node_opcua_assert_1.assert)(request.schema.name === "RegisterServerRequest");
178
+ this.__internalRegisterServerWithCallback(node_opcua_service_discovery_1.RegisterServerResponse, request.server, undefined, (err, response) => {
179
+ channel.send_response("MSG", response, message);
180
+ });
181
+ }
182
+ _on_FindServersOnNetworkRequest(message, channel) {
183
+ // from OPCUA 1.04 part 4
184
+ // This Service returns the Servers known to a Discovery Server. Unlike FindServer, this Service is
185
+ // only implemented by Discovery Servers.
186
+ // The Client may reduce the number of results returned by specifying filter criteria. An empty list is
187
+ // returned if no Server matches the criteria specified by the Client.
188
+ // This Service shall not require message security but it may require transport layer security.
189
+ // Each time the Discovery Server creates or updates a record in its cache it shall assign a
190
+ // monotonically increasing identifier to the record. This allows Clients to request records in batches
191
+ // by specifying the identifier for the last record received in the last call to FindServersOnNetwork.
192
+ // To support this the Discovery Server shall return records in numerical order starting from the
193
+ // lowest record identifier. The Discovery Server shall also return the last time the counter was reset
194
+ // for example due to a restart of the Discovery Server. If a Client detects that this time is more
195
+ // recent than the last time the Client called the Service it shall call the Service again with a
196
+ // startingRecordId of 0.
197
+ // This Service can be used without security and it is therefore vulnerable to Denial Of Service
198
+ // (DOS) attacks. A Server should minimize the amount of processing required to send the response
199
+ // for this Service. This can be achieved by preparing the result in advance
200
+ (0, node_opcua_assert_1.assert)(message.request instanceof node_opcua_service_discovery_1.FindServersOnNetworkRequest);
201
+ const request = message.request;
202
+ (0, node_opcua_assert_1.assert)(request.schema.name === "FindServersOnNetworkRequest");
203
+ function sendError(statusCode) {
204
+ const response1 = new node_opcua_service_discovery_1.FindServersOnNetworkResponse({ responseHeader: { serviceResult: statusCode } });
205
+ return channel.send_response("MSG", response1, message);
206
+ }
207
+ // startingRecordId Counter Only records with an identifier greater than this number will be
208
+ // returned.
209
+ // Specify 0 to start with the first record in the cache.
210
+ // maxRecordsToReturn UInt32 The maximum number of records to return in the response.
211
+ // 0 indicates that there is no limit.
212
+ // serverCapabilityFilter[] String List of Server capability filters. The set of allowed server capabilities
213
+ // are defined in Part 12.
214
+ // Only records with all of the specified server capabilities are
215
+ // returned.
216
+ // The comparison is case insensitive.
217
+ // If this list is empty then no filtering is performed
218
+ // ------------------------
219
+ // The last time the counters were reset.
220
+ const lastCounterResetTime = new Date();
221
+ // servers[] ServerOnNetwork List of DNS service records that meet criteria specified in the
222
+ // request. This list is empty if no Servers meet the criteria
223
+ const servers = [];
224
+ request.serverCapabilityFilter = request.serverCapabilityFilter || [];
225
+ const serverCapabilityFilter = request.serverCapabilityFilter
226
+ .map((x) => x.toUpperCase())
227
+ .sort()
228
+ .join(" ");
229
+ debugLog(" startingRecordId = ", request.startingRecordId);
230
+ if (this.mDnsResponder) {
231
+ for (const server of this.mDnsResponder.registeredServers) {
232
+ debugLog("Exploring server ", server.serverName);
233
+ if (server.recordId <= request.startingRecordId) {
234
+ continue;
235
+ }
236
+ if (!hasCapabilities(server.serverCapabilities, serverCapabilityFilter)) {
237
+ // istanbul ignore next
238
+ if (doDebug) {
239
+ debugLog(" server ", server.serverName, server.serverCapabilities ? server.serverCapabilities.join(",") : [], " does not match serverCapabilities ", serverCapabilityFilter);
240
+ }
241
+ continue;
242
+ }
243
+ debugLog(" server ", server.serverName, " found");
244
+ servers.push(server);
245
+ if (servers.length === request.maxRecordsToReturn) {
246
+ debugLog("max records to return reached", request.maxRecordsToReturn);
247
+ break;
248
+ }
249
+ }
250
+ }
251
+ const response = new node_opcua_service_discovery_1.FindServersOnNetworkResponse({
252
+ lastCounterResetTime,
253
+ servers
254
+ });
255
+ channel.send_response("MSG", response, message);
256
+ }
257
+ __internalRegisterServerWithCallback(RegisterServerXResponse /* RegisterServer2Response | RegisterServerResponse */, rawServer, discoveryConfigurations, callback) {
258
+ return __awaiter(this, void 0, void 0, function* () {
259
+ // istanbul ignore next
260
+ callback(new Error("internal Error"));
261
+ });
262
+ }
263
+ // eslint-disable-next-line max-statements
264
+ __internalRegisterServer(RegisterServerXResponse /* RegisterServer2Response | RegisterServerResponse */, rawServer, discoveryConfigurations) {
265
+ return __awaiter(this, void 0, void 0, function* () {
266
+ const server = rawServer;
267
+ if (!discoveryConfigurations) {
268
+ discoveryConfigurations = [
269
+ new node_opcua_service_discovery_1.MdnsDiscoveryConfiguration({
270
+ mdnsServerName: undefined,
271
+ serverCapabilities: ["NA"]
272
+ })
273
+ ];
274
+ }
275
+ function sendError(statusCode) {
276
+ debugLog(chalk.red("_on_RegisterServer(2)Request error"), statusCode.toString());
277
+ const response1 = new node_opcua_secure_channel_1.ServiceFault({
278
+ responseHeader: { serviceResult: statusCode }
279
+ });
280
+ return response1;
281
+ }
282
+ function _stop_announcedOnMulticastSubnet(conf) {
283
+ return __awaiter(this, void 0, void 0, function* () {
284
+ const b = conf.bonjourHolder;
285
+ yield b.stopAnnnouncedOnMulticastSubnet();
286
+ conf.bonjourHolder = undefined;
287
+ });
288
+ }
289
+ function _announcedOnMulticastSubnet(conf, announcement) {
290
+ return __awaiter(this, void 0, void 0, function* () {
291
+ const serviceConfig = (0, node_opcua_service_discovery_1.announcementToServiceConfig)(announcement);
292
+ let b = conf.bonjourHolder;
293
+ if (b && b.serviceConfig) {
294
+ if ((0, node_opcua_service_discovery_1.sameService)(b.serviceConfig, serviceConfig)) {
295
+ debugLog("Configuration ", conf.mdnsServerName, " has not changed !");
296
+ // nothing to do
297
+ return;
298
+ }
299
+ else {
300
+ // istanbul ignore next
301
+ if (doDebug) {
302
+ debugLog("Configuration ", conf.mdnsServerName, " HAS changed !");
303
+ debugLog(" Was ", (0, node_opcua_service_discovery_1.serviceToString)(b.serviceConfig));
304
+ debugLog(" is ", announcement);
305
+ }
306
+ }
307
+ yield _stop_announcedOnMulticastSubnet(conf);
308
+ }
309
+ b = new node_opcua_service_discovery_1.BonjourHolder();
310
+ conf.bonjourHolder = b;
311
+ yield b.announcedOnMulticastSubnet(announcement);
312
+ });
313
+ }
314
+ function dealWithDiscoveryConfiguration(previousConfMap, server1, serverInfo, discoveryConfiguration) {
315
+ return __awaiter(this, void 0, void 0, function* () {
316
+ // mdnsServerName String The name of the Server when it is announced via mDNS.
317
+ // See Part 12 for the details about mDNS. This string shall be less than 64 bytes.
318
+ // If not specified the first element of the serverNames array is used
319
+ // (truncated to 63 bytes if necessary).
320
+ // serverCapabilities [] String The set of Server capabilities supported by the Server.
321
+ // A Server capability is a short identifier for a feature
322
+ // The set of allowed Server capabilities are defined in Part 12.
323
+ discoveryConfiguration.mdnsServerName = discoveryConfiguration.mdnsServerName || server1.serverNames[0].text;
324
+ serverInfo.discoveryUrls = serverInfo.discoveryUrls || [];
325
+ const endpointUrl = serverInfo.discoveryUrls[0];
326
+ const parsedUrl = url.parse(endpointUrl);
327
+ discoveryConfiguration.serverCapabilities = discoveryConfiguration.serverCapabilities || [];
328
+ const announcement = {
329
+ capabilities: discoveryConfiguration.serverCapabilities.map((x) => x) || ["DA"],
330
+ name: discoveryConfiguration.mdnsServerName,
331
+ path: parsedUrl.pathname || "/",
332
+ port: parseInt(parsedUrl.port, 10)
333
+ };
334
+ if (previousConfMap[discoveryConfiguration.mdnsServerName]) {
335
+ // configuration already exists
336
+ debugLog("Configuration ", discoveryConfiguration.mdnsServerName, " already exists !");
337
+ const prevConf = previousConfMap[discoveryConfiguration.mdnsServerName];
338
+ delete previousConfMap[discoveryConfiguration.mdnsServerName];
339
+ discoveryConfiguration.bonjourHolder = prevConf.bonjourHolder;
340
+ }
341
+ // let's announce the server on the multicast DNS
342
+ yield _announcedOnMulticastSubnet(discoveryConfiguration, announcement);
343
+ return node_opcua_status_code_1.StatusCodes.Good;
344
+ });
345
+ }
346
+ // check serverType is valid
347
+ if (!_isValidServerType(server.serverType)) {
348
+ debugLog("Invalid server Type", node_opcua_service_endpoints_2.ApplicationType[server.serverType]);
349
+ return sendError(node_opcua_status_code_1.StatusCodes.BadInvalidArgument);
350
+ }
351
+ if (!server.serverUri) {
352
+ debugLog("Missing serverURI");
353
+ return sendError(node_opcua_status_code_1.StatusCodes.BadInvalidArgument);
354
+ }
355
+ // BadServerUriInvalid
356
+ // TODO
357
+ server.serverNames = server.serverNames || [];
358
+ // BadServerNameMissing
359
+ if (server.serverNames.length === 0 || !server.serverNames[0].text) {
360
+ return sendError(node_opcua_status_code_1.StatusCodes.BadServerNameMissing);
361
+ }
362
+ // BadDiscoveryUrlMissing
363
+ server.discoveryUrls = server.discoveryUrls || [];
364
+ if (server.discoveryUrls.length === 0 || !server.discoveryUrls[0]) {
365
+ return sendError(node_opcua_status_code_1.StatusCodes.BadDiscoveryUrlMissing);
366
+ }
367
+ const key = server.serverUri;
368
+ let configurationResults = null;
369
+ if (server.isOnline) {
370
+ debugLog(chalk.cyan(" registering server : "), chalk.yellow(server.serverUri));
371
+ // prepare serverInfo which will be used by FindServers
372
+ const serverInfo = {
373
+ applicationName: server.serverNames[0],
374
+ applicationType: server.serverType,
375
+ applicationUri: server.serverUri,
376
+ discoveryUrls: server.discoveryUrls,
377
+ gatewayServerUri: server.gatewayServerUri,
378
+ productUri: server.productUri
379
+ // XXX ?????? serverInfo.discoveryProfileUri = serverInfo.discoveryProfileUri;
380
+ };
381
+ const previousConfMap = [];
382
+ if (this.registeredServers[key]) {
383
+ // server already exists and must only be updated
384
+ const previousServer = this.registeredServers[key];
385
+ for (const conf of previousServer.discoveryConfiguration) {
386
+ previousConfMap[conf.mdnsServerName] = conf;
387
+ }
388
+ }
389
+ this.registeredServers[key] = server;
390
+ // xx server.semaphoreFilePath = server.semaphoreFilePath;
391
+ // xx server.serverNames = server.serverNames;
392
+ server.serverInfo = serverInfo;
393
+ server.discoveryConfiguration = discoveryConfigurations;
394
+ (0, node_opcua_assert_1.assert)(discoveryConfigurations);
395
+ configurationResults = [];
396
+ for (const conf of discoveryConfigurations) {
397
+ const statusCode = yield dealWithDiscoveryConfiguration(previousConfMap, server, serverInfo, conf);
398
+ configurationResults.push(statusCode);
399
+ }
400
+ // now also unregister unprocessed
401
+ if (Object.keys(previousConfMap).length !== 0) {
402
+ debugLog(" Warning some conf need to be removed !");
403
+ }
404
+ }
405
+ else {
406
+ // server is announced offline
407
+ if (key in this.registeredServers) {
408
+ const server1 = this.registeredServers[key];
409
+ debugLog(chalk.cyan("unregistering server : "), chalk.yellow(server1.serverUri));
410
+ configurationResults = [];
411
+ discoveryConfigurations = server1.discoveryConfiguration || [];
412
+ for (const conf of discoveryConfigurations) {
413
+ yield _stop_announcedOnMulticastSubnet(conf);
414
+ configurationResults.push(node_opcua_status_code_1.StatusCodes.Good);
415
+ }
416
+ delete this.registeredServers[key];
417
+ }
418
+ }
419
+ const response = new RegisterServerXResponse({
420
+ configurationResults
421
+ });
422
+ return response;
423
+ });
424
+ }
425
+ }
426
+ exports.OPCUADiscoveryServer = OPCUADiscoveryServer;
427
+ /*== private
428
+ * returns true if the serverType can be added to a discovery server.
429
+ * @param serverType
430
+ * @return {boolean}
431
+ * @private
432
+ */
433
+ function _isValidServerType(serverType) {
434
+ switch (serverType) {
435
+ case node_opcua_service_endpoints_2.ApplicationType.Client:
436
+ return false;
437
+ case node_opcua_service_endpoints_2.ApplicationType.Server:
438
+ case node_opcua_service_endpoints_2.ApplicationType.ClientAndServer:
439
+ case node_opcua_service_endpoints_2.ApplicationType.DiscoveryServer:
440
+ return true;
441
+ }
442
+ return false;
443
+ }
444
+ OPCUADiscoveryServer.prototype.__internalRegisterServerWithCallback = (0, util_1.callbackify)(OPCUADiscoveryServer.prototype.__internalRegisterServer);
445
+ // tslint:disable-next-line: no-var-requires
446
+ const thenify = require("thenify");
447
+ const opts = { multiArgs: false };
448
+ OPCUADiscoveryServer.prototype.start = thenify.withCallback(OPCUADiscoveryServer.prototype.start, opts);
449
+ OPCUADiscoveryServer.prototype.shutdown = thenify.withCallback(OPCUADiscoveryServer.prototype.shutdown, opts);
450
450
  //# sourceMappingURL=opcua_discovery_server.js.map