Skip to content

Instantly share code, notes, and snippets.

@erossignon
Last active November 30, 2019 07:32
Show Gist options
  • Save erossignon/05065b2ad331a6cf751eb7d6a4cf5055 to your computer and use it in GitHub Desktop.
Save erossignon/05065b2ad331a6cf751eb7d6a4cf5055 to your computer and use it in GitHub Desktop.
// mkdir myproj
// cd myproj
// npm init
// npm install node-opcua
// => copy this file
// node --inspect-brk test_server.js
const path = require("path");
const fs = require("fs");
const child_process = require("child_process");
const {
OPCUAServer,
OPCUAClient,
OPCUACertificateManager,
MessageSecurityMode,
SecurityPolicy,
} = require("node-opcua");
const certificateFolder = path.join(__dirname, "certificates");
const PKIFolder = path.join(certificateFolder, "PKI");
const serverCertificate = path.join(certificateFolder, "server_certificate.pem");
const port = 4840;
const os = require('os');
function findMyIpAddresses() {
const ipAddresses = [];
const ifaces = os.networkInterfaces();
for (let ifname of Object.keys(ifaces)) {
const networkInterface = ifaces[ifname];
for (let iface of networkInterface) {
if ('IPv4' !== iface.family || iface.internal !== false) {
// skip over internal (i.e. 127.0.0.1) and non-ipv4 addresses
continue;
}
ipAddresses.push(iface.address);
}
}
return ipAddresses;
}
const ipAddresses = findMyIpAddresses();
console.log(" My ipaddresses are ", ipAddresses.join(" "));
async function clientGetEndpoint(ipAddress) {
const client = OPCUAClient.create({
endpoint_must_exist: false,
});
client.on("backoff", () => {
console.log("Cannot connect to ", ipAddress, "please check your settings");
});
await client.connect("opc.tcp://" + ipAddress + ":" + port);
const e = await client.getEndpoints();
console.log(e.toString());
await client.disconnect();
}
async function fileExists(path) {
return await new Promise((resolve, reject) =>
fs.access(path, fs.constants.F_OK, (err) => resolve(!err))
);
}
async function folderExists(path) {
return await new Promise((resolve, reject) =>
fs.access(path, fs.constants.F_OK, (err) => resolve(!err))
);
}
async function exec(cmd) {
console.log("Executing ", cmd);
const promise = new Promise((resolve, reject) => {
const child = child_process.exec(cmd, function(err) {
});
child.stdout.pipe(process.stdout);
child.on("close", function(code) {
console.log("done ... (" + code + ")");
if (code == 0) {
resolve();
} else {
reject(new Error("command ended with code " + code));
}
});
});
await promise;
}
const applicationUri = "urn:HelloWorldThisIsMyServer";
async function createSelfSignedCertificate(ipAddresses, serverCertificate) {
const args = [
"node", "node_modules/node-opcua-pki/bin/crypto_create_CA.js",
// command
"certificate",
// arguments
"-s", "-o", serverCertificate,
"--ip", ipAddresses.join(","),
"-a", applicationUri
];
await exec(args.join(" "));
}
async function createServerCertificate() {
const certificateFolderExists = await fileExists(certificateFolder);
const serverCertificateExist = await fileExists(serverCertificate);
if (!certificateFolderExists || !serverCertificateExist) {
await createSelfSignedCertificate(ipAddresses, serverCertificate);
console.log(" serverCertificate ", serverCertificate, "created");
}
}
(async () => {
try {
await createServerCertificate();
const serverCertificateManager = new OPCUACertificateManager({
automaticallyAcceptUnknownCertificate: true,
rootFolder: PKIFolder,
name: "pki"
});
const server = new OPCUAServer({
port,
allowAnonymous: true,
alternateHostname: ipAddresses,
serverInfo: {
applicationName: { text: "Mini NodeOPCUA Server", locale: "en" },
applicationUri,
productUri: "Mini NodeOPCUA-Server"
},
serverCertificateManager,
certificateFile: serverCertificate,
privateKeyFile: serverCertificateManager.privateKey,
securityModes: [MessageSecurityMode.None],
securityPolicies: [SecurityPolicy.None]
});
await server.initialize();
await server.start();
await clientGetEndpoint(ipAddresses[0]);
console.log("server certificate = ", server.certificateFile);
console.log("server key file = ", server.privateKeyFile);
const endpointUrl = server.endpoints[0].endpointDescriptions()[0].endpointUrl;
console.log(" server is ready on ", endpointUrl);
console.log("CTRL+C to stop");
await exec(["openssl", "x509", "-in", serverCertificate, "-text"].join(" "));
let stopped = false;
process.on("SIGINT", async () => {
if (stopped) return;
console.log("shutting down ...")
stopped = true;
await server.shutdown();
console.log("... done..");
});
}
catch (err) {
console.log("err = ", err.message);
console.log(err);
process.exit(-1);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment