node-opcua-server-discovery 2.64.1 → 2.66.0

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