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