node-opcua

registering OPC-UA Node server to LDS


hello i'm trying to register my OPC-UA server to a local run (for now) local discovery server. So when i run my server whit registerServerMethod set to lds i get this error :

RegisterServer to the LDS  has failed during secure connection  => please check that you server certificate is trusted by the LDS. err: The connection has been rejected by server,
Please check that client certificate is trusted by server.
Err = (connect EINVAL 0.0.14.186:4840 - Local (0.0.0.0:0)) 

so when i check the rejected folder of the LDS at (opt/opcfoundation/ualds/pki/rejected/certs) no certificates have been placed there.

i've tried several different certs, i've tied placing a copy of the cert in the trusted folder but nothing has worked so far. am i doing something wrong om the LDS ore is there something wrong whit my server code:

/*global require,setInterval,console */
const opcua = require("node-opcua");
const { SecurityPolicy, OPCUACertificateManager } = require("node-opcua");

// Let's create an instance of OPCUAServer
const server = new opcua.OPCUAServer({
    port: 4354, // the port of the listening socket of the server
    resourcePath: "/UA/testserver1", // this path will be added to the endpoint resource name
    buildInfo: {
        productName: "testserver",
        buildNumber: "0001",
        buildDate: new Date(2020, 7, 9)
    },
    //certificate and key
    certificateFile: "testcert2.pem",
    privateKeyFile: "testkey2.pem",
    //certificate manager
    serverCertificateManager: new OPCUACertificateManager({
        automaticallyAcceptUnknownCertificate: true,
        rootFolder: "./certs",
    }),
    //security policies
    securityPolicies: [SecurityPolicy.Basic256, SecurityPolicy.None],
    securityModes: [opcua.MessageSecurityMode.SignAndEncrypt, opcua.MessageSecurityMode.None],
    // setup LDS conncetion
    registerServerMethod: 3, // regsiterservermethod 3 = LDS
    discoveryServerEndpointUrl: "opc.tcp://localhost:4840",
});

function post_initialize() {
    console.log("initialized");
    function construct_my_address_space(server) {

        const addressSpace = server.engine.addressSpace;
        const namespace = addressSpace.getOwnNamespace();

        // declare a new object
        const device = namespace.addObject({
            organizedBy: addressSpace.rootFolder.objects,
            browseName: "MyDevice"
        });

        // add some variables
        // add a variable named MyVariable1 to the newly created folder "MyDevice"
        let variable1 = 1;

        // emulate variable1 changing every 500 ms
        setInterval(function () { variable1 += 1; }, 500);

        namespace.addVariable({
            componentOf: device,
            browseName: "MyVariable1",
            dataType: "Double",
            value: {
                get: function () {
                    return new opcua.Variant({ dataType: opcua.DataType.Double, value: variable1 });
                }
            }
        });

        // add a variable named MyVariable2 to the newly created folder "MyDevice"
        let variable2 = 10.0;

        namespace.addVariable({
            componentOf: device,
            nodeId: "ns=1;b=1020FFAA", // some opaque NodeId in namespace 4
            browseName: "MyVariable2",
            dataType: "Double",
            value: {
                get: function () {
                    return new opcua.Variant({ dataType: opcua.DataType.Double, value: variable2 });
                },
                set: function (variant) {
                    variable2 = parseFloat(variant.value);
                    return opcua.StatusCodes.Good;
                }
            }
        });

        const os = require("os");
        /**
         * returns the percentage of free memory on the running machine
         * @return {double}
         */

        function available_memory() {
            // var value = process.memoryUsage().heapUsed / 1000000;
            const percentageMemUsed = os.freemem() / os.totalmem() * 100.0;
            return percentageMemUsed;
        }

        namespace.addVariable({
            componentOf: device,
            nodeId: "s=free_memory", // a string nodeID
            browseName: "FreeMemory",
            dataType: "Double",
            value: {
                get: function () { return new opcua.Variant({ dataType: opcua.DataType.Double, value: available_memory() }); }
            }
        });
    }

    construct_my_address_space(server);

    server.start(function () {
        console.log("Server is now listening ... ( press CTRL+C to stop)");
        console.log("port ", server.endpoints[0].port);
        const endpointUrl = server.endpoints[0].endpointDescriptions()[0].endpointUrl;
        console.log(" the primary server endpoint url is ", endpointUrl);
    });
}


server.initialize(post_initialize);

Solution

  • the problem i had was due to the hostname of my PC being a number, this caused a timeout and so the certificates weren't being send.

    the solution was simple, change the hostname of my pc to something that starts whit a letter.