- 客户端通常会查询 OPCUA 服务器的端点,以找出用于连接服务器的最佳安全和加密模式。
- getEndpoints 是不需要客户端在服务器上打开会话的服务之一。
// with node-opcua@0.4.1
const opcua = require("node-opcua");
async function getEndpoints(endpointUrl) {
let client = new opcua.OPCUAClient();
await client.connect(endpointUrl);
const endpoints = await client.getEndpoints();
const reducedEndpoints = endpoints.map(endpoint => ({
endpointUrl: endpoint.endpointUrl,
securityMode: endpoint.securityMode.toString(),
securityPolicy: endpoint.securityPolicyUri.toString(),
}));
await client.disconnect();
return reducedEndpoints;
}
async function main() {
const endpoints = await getEndpoints("opc.tcp://opcuademo.sterfive.com:26543");
console.log(endpoints);
}
main().then();
此代码将输出:
[ { endpointUrl: 'opc.tcp://opcuademo.sterfive.com:26543',
securityMode: 'NONE',
securityPolicy: 'http://opcfoundation.org/UA/SecurityPolicy#None' },
{ endpointUrl: 'opc.tcp://opcuademo.sterfive.com:26543',
securityMode: 'SIGN',
securityPolicy: 'http://opcfoundation.org/UA/SecurityPolicy#Basic128Rsa15' },
{ endpointUrl: 'opc.tcp://opcuademo.sterfive.com:26543',
securityMode: 'SIGN',
securityPolicy: 'http://opcfoundation.org/UA/SecurityPolicy#Basic256' },
{ endpointUrl: 'opc.tcp://opcuademo.sterfive.com:26543',
securityMode: 'SIGN',
securityPolicy: 'http://opcfoundation.org/UA/SecurityPolicy#Basic256Sha256' },
{ endpointUrl: 'opc.tcp://opcuademo.sterfive.com:26543',
securityMode: 'SIGNANDENCRYPT',
securityPolicy: 'http://opcfoundation.org/UA/SecurityPolicy#Basic128Rsa15' },
{ endpointUrl: 'opc.tcp://opcuademo.sterfive.com:26543',
securityMode: 'SIGNANDENCRYPT',
securityPolicy: 'http://opcfoundation.org/UA/SecurityPolicy#Basic256' },
{ endpointUrl: 'opc.tcp://opcuademo.sterfive.com:26543',
securityMode: 'SIGNANDENCRYPT',
securityPolicy: 'http://opcfoundation.org/UA/SecurityPolicy#Basic256Sha256' } ]
话虽如此,node-opcua 客户端将在连接期间自动查询服务器端点,并验证用户请求的 securityMode 和 securityPolicy 是否可用。
// with node-opcua@0.4.1
const opcua = require("node-opcua");
async function verifyEndpointAndConnect(endpointUrl) {
let client = new opcua.OPCUAClient();
await client.connect(endpointUrl);
// note that client has already requested the server endpoints
// during the connection. We can now simply query the Application
// description matching our security settings
const applicationDescription = client.findEndpointForSecurity(
opcua.MessageSecurityMode.SIGN,
opcua.SecurityPolicy.Basic256Sha256
);
await client.disconnect();
if (applicationDescription) {
console.log("Yes! the server support this endpoints:");
console.log(applicationDescription.toString());
}else {
console.log("Sorry! this server do not support the requested security mode");
return;
}
// let recreate our client with the requested security mode
client = new opcua.OPCUAClient({
securityMode: opcua.MessageSecurityMode.SIGN,
securityPolicy: opcua.SecurityPolicy.Basic256Sha256,
});
await client.connect(endpointUrl);
// [...] do something with this connected client.
await client.disconnect();
}
async function main() {
await verifyEndpointAndConnect("opc.tcp://opcuademo.sterfive.com:26543");
console.log("done");
}
main();