Как подключить клиентов и сертификат клиента на сервере node-opcua

У меня есть установка сервера node-opcua с двумя подключенными к нему клиентами с режимом безопасности, установленным на SignAndEncrypt. Теперь у меня 2 вопроса:

  1. Есть ли у сервера способ узнать, сколько клиентов к нему подключено?
  2. Серверное приложение захочет узнать личность подключенного клиента, есть ли API для получения сертификата клиента, полученного во время OpenSecureChannel?

person Konsy    schedule 16.08.2019    source источник


Ответы (1)


Сервер OPCUA может предоставлять такую ​​информацию под узлом RootFolder.Server.ServerDiagnostics, и необходимая вам информация должна быть доступна через OPCUA.

Эта небольшая клиентская программа node-opcua покажет вам, как это сделать.

Заметка:

  • что для определенных данных, таких как диагностика безопасности, требуется безопасное соединение и неанонимный пользователь

client_extract_server_diagnostic.ts

// this script is typescript and can be run this way
// $ npx ts-node client_extract_server_diagnostic.ts

import { 
    AttributeIds,
    OPCUAClient,
    ClientSession, 
    StatusCodes,
    MessageSecurityMode,
    SecurityPolicy,
    UserIdentityInfoUserName,
    UserTokenType   
} from "node-opcua";

// the opcua server to connect to
const endpointUrl = "opc.tcp://localhost:48010";

// the credential 
const userIdentityToken: UserIdentityInfoUserName = {
    password: "secret",
    userName: "root",
    type: UserTokenType.UserName
};

async function extractServerStatistics(session: ClientSession) {

    const nodesToRead = [
        { 
            attributeIds: AttributeIds.Value, nodeId:"Server_ServerDiagnostics_EnabledFlag" 
        },
        { 
            attributeIds: AttributeIds.Value, nodeId:"Server_ServerDiagnostics_ServerDiagnosticsSummary_CurrentSessionCount" //i=2277 
        },
        {
            attributeIds: AttributeIds.Value, nodeId:"Server_ServerDiagnostics_ServerDiagnosticsSummary_CurrentSubscriptionCount" // i=2285 
        },
        {
            attributeIds: AttributeIds.Value, nodeId: "Server_ServerDiagnostics_ServerDiagnosticsSummary_CumulatedSessionCount" // i=2278 
        },
        {
            attributeIds: AttributeIds.Value, nodeId: "Server_ServerDiagnostics_ServerDiagnosticsSummary_CumulatedSubscriptionCount" // i=2278 
        },
        {
            attributeIds: AttributeIds.Value, nodeId: "Server_ServerDiagnostics_SessionsDiagnosticsSummary_SessionSecurityDiagnosticsArray" // i=3708 
        }

    ];
    const dataValues = await session.read(nodesToRead);

    console.log("Diagnostic enabled ?         = ", dataValues[0].value.value);
    console.log("Current Session Count        = ", dataValues[1].value.value);
    console.log("Current Subscription Count   = ", dataValues[2].value.value);
    console.log("Cumulated Session Count      = ", dataValues[3].value.value);
    console.log("Cumulated Subscription Count = ", dataValues[4].value.value);

    // note reading SessionSecurityDiagnotiscArray may requires authenticated session to succeed
    console.log("SessionSecurityDiagnotiscArray       = ");

    if (dataValues[5].statusCode === StatusCodes.Good) {
        const sessionSecurityDiagnosticArray = dataValues[5].value.value;
        // console.log(dataValues[5].value.value.toString());
        for (const sessionSecurityDiagnostic of sessionSecurityDiagnosticArray)  {
            console.log(" session client certificate ", sessionSecurityDiagnostic.clientCertificate.toString("base64"));
            console.log();
        }
    } else {
        console.log(dataValues[5].toString());
    }
}
( async () => {

    try {
        const client = OPCUAClient.create({
            endpoint_must_exist: false,
            securityMode: MessageSecurityMode.SignAndEncrypt,
            securityPolicy: SecurityPolicy.Basic256Sha256,

        });
        client.on("backoff",() => console.log("still trying to connec to ", endpointUrl));

        await client.connect(endpointUrl);

        const session = await client.createSession(userIdentityToken);

        await extractServerStatistics(session);

        await session.close();
        await client.disconnect();
        console.log("done");
    } catch(err) {
        console.log("Err" , err.message);
        process.exit(1);
    }
})();
person Etienne    schedule 22.08.2019