node-opcua-samples 2.98.0 → 2.98.1

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.
Files changed (50) hide show
  1. package/dist/get_endpoints.d.ts +2 -0
  2. package/dist/get_endpoints.js +142 -0
  3. package/dist/get_endpoints.js.map +1 -0
  4. package/dist/mini_server.d.ts +1 -0
  5. package/dist/mini_server.js +89 -0
  6. package/dist/mini_server.js.map +1 -0
  7. package/dist/server_with_changing_password.d.ts +1 -0
  8. package/dist/server_with_changing_password.js +101 -0
  9. package/dist/server_with_changing_password.js.map +1 -0
  10. package/dist/server_with_push_certificate.d.ts +2 -0
  11. package/dist/server_with_push_certificate.js +112 -0
  12. package/dist/server_with_push_certificate.js.map +1 -0
  13. package/dist/simple_client_ts.d.ts +2 -0
  14. package/dist/simple_client_ts.js.map +1 -0
  15. package/dist/simple_findservers.d.ts +2 -0
  16. package/dist/simple_findservers.js +49 -0
  17. package/dist/simple_findservers.js.map +1 -0
  18. package/dist/simple_secure_server.d.ts +2 -0
  19. package/dist/simple_secure_server.js +126 -0
  20. package/dist/simple_secure_server.js.map +1 -0
  21. package/dist/simple_server_with_custom_extension_objects.d.ts +2 -0
  22. package/dist/simple_server_with_custom_extension_objects.js +82 -0
  23. package/dist/simple_server_with_custom_extension_objects.js.map +1 -0
  24. package/dist/stressing_client.d.ts +1 -0
  25. package/dist/stressing_client.js +37 -0
  26. package/dist/stressing_client.js.map +1 -0
  27. package/dist/tiny_client.d.ts +1 -0
  28. package/dist/tiny_client.js +33 -0
  29. package/dist/tiny_client.js.map +1 -0
  30. package/package.json +14 -11
  31. package/bin/createOPCUACertificate.cmd +0 -6
  32. package/bin/create_certificates.js +0 -2
  33. package/bin/crypto_create_CA.js +0 -2
  34. package/bin/demo_server_with_alarm.js +0 -51
  35. package/bin/findServersOnNetwork.js +0 -32
  36. package/bin/get_endpoints.ts +0 -166
  37. package/bin/machineryServer.js +0 -83
  38. package/bin/mini_server.ts +0 -106
  39. package/bin/more.js +0 -40
  40. package/bin/node-opcua.js +0 -2
  41. package/bin/opcua_interceptor.js +0 -122
  42. package/bin/server_with_changing_password.ts +0 -97
  43. package/bin/server_with_push_certificate.ts +0 -122
  44. package/bin/simple_client.js +0 -830
  45. package/bin/simple_client_ts.ts +0 -847
  46. package/bin/simple_findservers.ts +0 -45
  47. package/bin/simple_secure_server.ts +0 -152
  48. package/bin/simple_server_with_custom_extension_objects.ts +0 -89
  49. package/bin/stressing_client.ts +0 -28
  50. package/bin/tiny_client.ts +0 -24
@@ -1,83 +0,0 @@
1
- const { OPCUAServer, nodesets } = require("node-opcua");
2
- const chalk = require("chalk");
3
-
4
- (async () => {
5
-
6
- const server = new OPCUAServer({
7
- nodeset_filename: [
8
- nodesets.standard,
9
- nodesets.di,
10
- nodesets.machinery,
11
- nodesets.cnc,
12
- nodesets.machineTool,
13
- ],
14
- });
15
-
16
-
17
- await server.initialize();
18
-
19
- const addressSpace = server.engine.addressSpace;
20
-
21
- const nsDI = addressSpace.getNamespaceIndex("http://opcfoundation.org/UA/DI/");
22
- const deviceType = addressSpace.findObjectType("DeviceType", nsDI);
23
-
24
- const nsMachinery = addressSpace.getNamespaceIndex("http://opcfoundation.org/UA/Machinery/");
25
-
26
- const machineIdentificationType = addressSpace.findObjectType("MachineIdentificationType", nsMachinery);
27
-
28
- const namespace2 = addressSpace.registerNamespace("urn:MyNameSpaceWithType");
29
- const myDeviceType = namespace2.addObjectType({
30
- browseName: "MyDeviceType",
31
- subtypeOf: deviceType
32
- });
33
-
34
- const myDevice = myDeviceType.instantiate({
35
- browseName: "MyDevice",
36
- organizedBy: addressSpace.rootFolder.objects.deviceSet,
37
- });
38
-
39
- myDevice.addReference({
40
- nodeId: addressSpace.rootFolder.objects.machines,
41
- referenceType: "Organizes",
42
- isForward: false
43
- });
44
-
45
- const machineryIdentification = machineIdentificationType.instantiate({
46
- browseName: "Identification",
47
- // addInOf:
48
- optionals: [
49
- "Location",
50
- "ManufacturerUri",
51
- "MonthOfConstruction",
52
- "YearOfConstruction",
53
- ]
54
- });
55
-
56
- machineryIdentification.location.setValueFromSource({ dataType: "String", value: "Paris" });
57
- machineryIdentification.monthOfConstruction.setValueFromSource({ dataType: "UInt32", value: 10 });
58
- machineryIdentification.yearOfConstruction.setValueFromSource({ dataType: "UInt32", value: 2020 });
59
-
60
- machineryIdentification.addReference({
61
- nodeId: myDevice,
62
- referenceType: "HasAddIn",
63
- isForward: false
64
- });
65
-
66
-
67
- await server.start();
68
-
69
- const endpointUrl = server.getEndpointUrl();
70
-
71
- console.log(chalk.yellow(" server on port :"), chalk.cyan(server.endpoints[0].port.toString()));
72
- console.log(chalk.yellow(" endpointUrl :"), chalk.cyan(endpointUrl));
73
- console.log(chalk.yellow("\n server now waiting for connections. CTRL+C to stop"));
74
-
75
- console.log(chalk.cyan("\nvisit https://www.sterfive.com for more advanced examples and professional support."));
76
-
77
- process.on("SIGINT", async () => {
78
- // only work on li nux apparently
79
- await server.shutdown(1000);
80
- console.log(chalk.red.bold(" shutting down completed "));
81
- process.exit(-1);
82
- });
83
- })();
@@ -1,106 +0,0 @@
1
- // tslint:disable:no-console
2
- import * as path from "path";
3
- import * as os from "os";
4
-
5
- import * as chalk from "chalk";
6
-
7
- import {
8
- makeApplicationUrn,
9
- get_mini_nodeset_filename,
10
- nodesets,
11
- OPCUAServer,
12
- ServerSession,
13
- OPCUACertificateManager,
14
- OPCUAServerOptions
15
- } from "node-opcua";
16
- Error.stackTraceLimit = Infinity;
17
-
18
- const port = 26544;
19
-
20
- import envPaths from "env-paths";
21
- const config = envPaths("MiniNodeOPCUA-Server").config;
22
- const pkiFolder = path.join(config, "PKI");
23
-
24
- const serverOptions: OPCUAServerOptions = {
25
- serverCertificateManager: new OPCUACertificateManager({
26
- rootFolder: pkiFolder
27
- }),
28
-
29
- port,
30
-
31
- maxConnectionsPerEndpoint: 2,
32
-
33
- nodeset_filename: [get_mini_nodeset_filename()],
34
-
35
- serverInfo: {
36
- applicationUri: makeApplicationUrn(os.hostname(), "MiniNodeOPCUA-Server"),
37
- productUri: "Mini NodeOPCUA-Server",
38
-
39
- applicationName: { text: "Mini NodeOPCUA Server", locale: "en" },
40
- gatewayServerUri: null,
41
-
42
- discoveryProfileUri: null,
43
-
44
- discoveryUrls: []
45
- },
46
-
47
- buildInfo: {
48
- buildNumber: "1234"
49
- },
50
-
51
- serverCapabilities: {
52
- maxSessions: 10,
53
-
54
- maxBrowseContinuationPoints: 10,
55
- maxHistoryContinuationPoints: 10,
56
- // maxInactiveLockTime
57
- operationLimits: {
58
- maxNodesPerBrowse: 10,
59
- maxNodesPerHistoryReadData: 6,
60
- maxNodesPerHistoryReadEvents: 10,
61
- maxNodesPerHistoryUpdateData: 10,
62
- maxNodesPerRead: 10,
63
- maxNodesPerWrite: 10
64
- }
65
- },
66
-
67
- isAuditing: false
68
- };
69
-
70
- async function main() {
71
- process.title = "Node OPCUA Server on port : " + serverOptions.port;
72
-
73
- const server = new OPCUAServer(serverOptions);
74
- console.log(chalk.yellow(" server PID :"), process.pid);
75
-
76
- try {
77
- await server.start();
78
- } catch (err) {
79
- console.log(" Server failed to start ... exiting");
80
- process.exit(-3);
81
- }
82
-
83
- const endpointUrl = server.getEndpointUrl()!;
84
-
85
- console.log(chalk.yellow(" server on port :"), server.endpoints[0].port.toString());
86
- console.log(chalk.yellow(" endpointUrl :"), chalk.cyan(endpointUrl));
87
- console.log(chalk.yellow("\n server now waiting for connections. CTRL+C to stop"));
88
-
89
- server.on("create_session", (session: ServerSession) => {
90
- console.log(" SESSION CREATED");
91
- console.log(chalk.cyan(" client application URI: "), session.clientDescription!.applicationUri);
92
- console.log(chalk.cyan(" client product URI: "), session.clientDescription!.productUri);
93
- console.log(chalk.cyan(" client application name: "), session.clientDescription!.applicationName.toString());
94
- console.log(chalk.cyan(" client application type: "), session.clientDescription!.applicationType.toString());
95
- console.log(chalk.cyan(" session name: "), session.sessionName ? session.sessionName.toString() : "<null>");
96
- console.log(chalk.cyan(" session timeout: "), session.sessionTimeout);
97
- console.log(chalk.cyan(" session id: "), session.nodeId);
98
- });
99
-
100
- server.on("session_closed", (session: ServerSession, reason: string) => {
101
- console.log(" SESSION CLOSED :", reason);
102
- console.log(chalk.cyan(" session name: "), session.sessionName ? session.sessionName.toString() : "<null>");
103
- });
104
- }
105
-
106
- main();
package/bin/more.js DELETED
@@ -1,40 +0,0 @@
1
- #!/usr/bin/env node
2
- "use strict";
3
- /*
4
- * write a file to the console, preserving the Ansi color decoration
5
- */
6
- const argv = require("yargs")
7
- .usage("Usage: $0 <file>")
8
- .argv;
9
-
10
- const fs = require("fs");
11
-
12
-
13
- function readLines(input, func) {
14
- let remaining = "";
15
-
16
- input.on("data", function (data) {
17
- remaining += data;
18
- let index = remaining.indexOf("\n");
19
- while (index > -1) {
20
- const line = remaining.substring(0, index);
21
- remaining = remaining.substring(index + 1);
22
- func(line);
23
- index = remaining.indexOf("\n");
24
- }
25
- });
26
-
27
- input.on("end", function () {
28
- if (remaining.length > 0) {
29
- func(remaining);
30
- }
31
- });
32
- }
33
-
34
- function func(data) {
35
- console.log(data);
36
- }
37
-
38
- const input = fs.createReadStream(argv._[0]);
39
-
40
- readLines(input, func);
package/bin/node-opcua.js DELETED
@@ -1,2 +0,0 @@
1
- module.exports = require("..");
2
-
@@ -1,122 +0,0 @@
1
- /* eslint no-process-exit: 0 */
2
- "use strict";
3
- const net = require("net");
4
-
5
- const chalk = require("chalk");
6
- const yargs = require("yargs");
7
- const argv = yargs.usage("Usage: $0 --portServer [num] --port [num] --hostname <hostname> -block").argv;
8
-
9
- const opcua = require("node-opcua");
10
-
11
- const { hexDump } = require("node-opcua-utils");
12
- const { MessageBuilder } = require("../lib/misc/message_builder");
13
- const { BinaryStream } = require("../lib/misc/binaryStream");
14
-
15
- const { analyseExtensionObject } = require("../lib/misc/analyzePacket");
16
- const { messageHeaderToString } = require("../lib/misc/message_header");
17
-
18
- const s = require("../lib/datamodel/structures");
19
-
20
- const remote_port = parseInt(argv.port, 10) || 4841;
21
- const hostname = argv.hostname || "localhost";
22
-
23
- const my_port = parseInt(argv.portServer, 10) || remote_port + 1;
24
-
25
- const TrafficAnalyser = function (id) {
26
- this.id = id;
27
- };
28
-
29
- TrafficAnalyser.prototype.add = function (data) {
30
- const stream = new BinaryStream(data);
31
- if (argv.block) {
32
- console.log(hexDump(data));
33
- return;
34
- }
35
- const messageHeader = opcua.readMessageHeader(stream);
36
-
37
- if (messageHeader.msgType === "ERR") {
38
- const err = new s.TCPErrorMessage();
39
- err.decode(stream);
40
- console.log(" Error 0x" + err.statusCode.toString() + " reason:" + err.reason);
41
- console.log(hexDump(data));
42
- }
43
-
44
- const messageBuild = new MessageBuilder();
45
- messageBuild.on("full_message_body", function (full_message_body) {
46
- console.log(hexDump(full_message_body));
47
-
48
- try {
49
- analyseExtensionObject(full_message_body);
50
- } catch (err) {
51
- console.log(chalk.red("ERROR : "), err);
52
- }
53
- });
54
-
55
- switch (messageHeader.msgType) {
56
- case "HEL":
57
- case "ACK":
58
- if (this.id % 2) {
59
- console.log(chalk.red.bold(JSON.stringify(messageHeader, null, "")));
60
- } else {
61
- console.log(chalk.yellow.bold(JSON.stringify(messageHeader, null, "")));
62
- }
63
- break;
64
-
65
- case "OPN": // open secure channel
66
- case "CLO": // close secure channel
67
- case "MSG": // message
68
- // decode secure message
69
- if (this.id % 2) {
70
- console.log(chalk.red.bold(messageHeaderToString(data)));
71
- } else {
72
- console.log(chalk.yellow.bold(messageHeaderToString(data)));
73
- }
74
-
75
- messageBuild.feed(data);
76
- break;
77
- case "ERR":
78
- console.log(hexDump(data));
79
- break;
80
- default:
81
- break;
82
- }
83
- };
84
-
85
- require("net")
86
- .createServer(function (socket) {
87
- console.log("connected");
88
- const ta_client = new TrafficAnalyser(1);
89
-
90
- const ta_server = new TrafficAnalyser(2);
91
-
92
- const proxy_client = new net.Socket();
93
- proxy_client.connect(remote_port, hostname);
94
-
95
- proxy_client.on("data", function (data) {
96
- console.log(" server -> client : packet length " + data.length);
97
- ta_server.add(data);
98
- try {
99
- socket.write(data);
100
- } catch (err) {
101
- /** */
102
- }
103
- });
104
-
105
- socket.on("data", function (data) {
106
- console.log(" client -> server : packet length " + data.length);
107
- ta_client.add(data);
108
- proxy_client.write(data);
109
- });
110
- socket.on("close", function () {
111
- console.log("server disconnected (CLOSE)");
112
- proxy_client.end();
113
- });
114
-
115
- socket.on("end", function () {
116
- console.log("server disconnected (END)");
117
- });
118
- })
119
- .listen(my_port);
120
-
121
- console.log(" registering OPCUA server on port " + my_port);
122
- console.log(" +-> redirecting conversation to server port " + remote_port);
@@ -1,97 +0,0 @@
1
- import { OPCUAServer, makeRoles, WellKnownRoles, NodeId, nodesets } from "node-opcua";
2
-
3
- const port = 2510;
4
- let server: OPCUAServer;
5
- const users = [
6
- {
7
- username: "user1",
8
- password: "passwor1",
9
- roles: makeRoles([WellKnownRoles.AuthenticatedUser, WellKnownRoles.ConfigureAdmin])
10
- }
11
- ];
12
-
13
- const userManager = {
14
- isValidUser: (username: string, password: string): boolean => {
15
- const uIndex = users.findIndex(function (u) {
16
- return u.username === username;
17
- });
18
- if (uIndex < 0) {
19
- return false;
20
- }
21
- if (users[uIndex].password !== password) {
22
- return false;
23
- }
24
- return true;
25
- },
26
-
27
- getUserRoles: (username: string): NodeId[] => {
28
- const uIndex = users.findIndex(function (x) {
29
- return x.username === username;
30
- });
31
- if (uIndex < 0) {
32
- return makeRoles("Anonymous");
33
- }
34
- const userRole = users[uIndex].roles;
35
- return userRole;
36
- }
37
- };
38
- async function startServer() {
39
- server = new OPCUAServer({
40
- port: port,
41
- nodeset_filename: [nodesets.standard],
42
- userManager,
43
- maxConnectionsPerEndpoint: 1,
44
- serverCapabilities: {
45
- maxSessions: 2,
46
- maxSubscriptions: 10,
47
- maxSubscriptionsPerSession: 3
48
- }
49
- });
50
- await server.initialize();
51
- await server.start();
52
-
53
- server.on("newChannel", () => {
54
- console.log("server => new channel");
55
- });
56
- server.on("session_activated", () => {
57
- console.log("server session activated");
58
- });
59
- server.on("connectionRefused", () => {
60
- console.log("server connection refused");
61
- });
62
- server.on("session_closed", () => {
63
- console.log("server sesion closed");
64
- });
65
- server.on("create_session", () => {
66
- console.log("server create session");
67
- });
68
- return server;
69
- }
70
-
71
- (async () => {
72
- let counter = 0;
73
-
74
- process.on("SIGINT", () => {
75
- if (counter > 5) {
76
- process.exit();
77
- }
78
- });
79
-
80
- while (true) {
81
- users[0].password = `password${counter % 2}`;
82
- counter++;
83
- console.log("user: user1, password: ", users[0].password);
84
- const server = await startServer();
85
- console.log("server started at", server.getEndpointUrl());
86
-
87
- await new Promise((resolve) => {
88
- console.log("waiting for CTRL+C to cycle");
89
- process.once("SIGINT", resolve);
90
- });
91
-
92
- console.log("now shutting down");
93
-
94
- await server.shutdown(1000);
95
- console.log("server stopped for maintenance");
96
- }
97
- })();
@@ -1,122 +0,0 @@
1
- #!/usr/bin/env ts-node
2
- /* eslint no-process-exit: 0 */
3
- // tslint:disable:no-console
4
- import * as path from "path";
5
- import * as chalk from "chalk";
6
-
7
- import { makeRoles, nodesets, OPCUACertificateManager, OPCUAServer, OPCUAServerOptions, WellKnownRoles } from "node-opcua";
8
- import { CertificateManager } from "node-opcua-pki";
9
- import { installPushCertificateManagement } from "node-opcua-server-configuration";
10
- import * as yargs from "yargs";
11
-
12
- const rootFolder = path.join(__dirname, "../../..");
13
-
14
- import envPaths from "env-paths";
15
- const config = envPaths("node-opcua-default").config;
16
- const pkiFolder = path.join(config, "PKI");
17
-
18
- const certificateManager = new OPCUACertificateManager({
19
- automaticallyAcceptUnknownCertificate: true,
20
- name: "PKI",
21
- rootFolder: pkiFolder
22
- });
23
-
24
- const users = [
25
- {
26
- username: "user1",
27
- password: "password1",
28
- role: makeRoles([WellKnownRoles.AuthenticatedUser, WellKnownRoles.ConfigureAdmin, WellKnownRoles.SecurityAdmin])
29
- },
30
- { username: "user2", password: "password2", role: makeRoles([WellKnownRoles.AuthenticatedUser, WellKnownRoles.Operator]) }
31
- ];
32
-
33
- const userManager = {
34
- isValidUser(username: string, password: string) {
35
- const uIndex = users.findIndex((x) => x.username === username);
36
- if (uIndex < 0) {
37
- return false;
38
- }
39
- if (users[uIndex].password !== password) {
40
- return false;
41
- }
42
- return true;
43
- },
44
- getUserRoles(username: string) {
45
- const uIndex = users.findIndex((x) => x.username === username);
46
- if (uIndex < 0) {
47
- return [];
48
- }
49
- const userRole = users[uIndex].role;
50
- return userRole;
51
- }
52
- };
53
-
54
- async function main() {
55
- const argv = await yargs.wrap(132).option("port", {
56
- alias: "p",
57
- default: "26543",
58
- describe: "port to listen"
59
- }).argv;
60
-
61
- const port = parseInt(argv.port, 10) || 26555;
62
-
63
- const serverOptions: OPCUAServerOptions = {
64
- port,
65
-
66
- nodeset_filename: [nodesets.standard],
67
-
68
- serverCertificateManager: certificateManager,
69
- userCertificateManager: certificateManager,
70
-
71
- userManager
72
- };
73
-
74
- process.title = "Node OPCUA Server on port : " + serverOptions.port;
75
- const tmpFolder = path.join(__dirname, "../certificates/myApp");
76
-
77
- const applicationGroup = new CertificateManager({
78
- location: tmpFolder
79
- });
80
- await applicationGroup.initialize();
81
-
82
- const server = new OPCUAServer(serverOptions);
83
-
84
- console.log(" Configuration rootdir = ", server.serverCertificateManager.rootDir);
85
- console.log(chalk.yellow(" server PID :"), process.pid);
86
-
87
- await server.initialize();
88
-
89
- const addressSpace = server.engine.addressSpace!;
90
- // to do: expose new nodeid here
91
- const ns = addressSpace.getNamespaceIndex("http://yourorganisation.org/my_data_type/");
92
-
93
- await installPushCertificateManagement(addressSpace, {
94
- applicationGroup: server.serverCertificateManager,
95
- userTokenGroup: server.userCertificateManager,
96
- applicationUri: server.serverInfo.applicationUri!
97
- });
98
-
99
- console.log("Certificate rejected folder ", server.serverCertificateManager.rejectedFolder);
100
-
101
- try {
102
- await server.start();
103
- } catch (err) {
104
- console.log(" Server failed to start ... exiting");
105
- process.exit(-3);
106
- }
107
-
108
- const endpointUrl = server.getEndpointUrl()!;
109
-
110
- console.log(chalk.yellow(" server on port :"), chalk.cyan(server.endpoints[0].port.toString()));
111
- console.log(chalk.yellow(" endpointUrl :"), chalk.cyan(endpointUrl));
112
- console.log(chalk.yellow("\n server now waiting for connections. CTRL+C to stop"));
113
-
114
- process.once("SIGINT", async () => {
115
- // only work on linux apparently
116
- await server.shutdown(1000);
117
- console.log(chalk.red.bold(" shutting down completed "));
118
- process.exit(-1);
119
- });
120
- }
121
-
122
- main();