namespace ZB.MOM.WW.OtOpcUa.Driver.OpcUaClient;
///
/// The ONE implementation of "connect where the operator pointed us, not where the server says it lives" for the
/// OpcUaClient driver family. A server reports its GetEndpoints results with ITS view of its own address; behind
/// NAT, a port map, a container port publish or a proxy that address is unreachable from here (or bypasses the
/// proxy). Shared by the runtime driver (Gitea #676) and the AdminUI live-browse picker (Gitea #806, which used
/// the advertised URL unchanged and failed DNS on a published-port fixture the driver handled), and — since
/// Gitea #812 — by the Client CLI/UI's EndpointSelector (Client.Shared), whose own copy of this rule it
/// replaced.
///
///
/// Pure string/ logic, deliberately in .Contracts: it is not session-typed, so it must not
/// pull the OPC UA SDK in (pinned by ContractsIsDependencyLightTests). It only chooses the network
/// address; the security policy, mode and server certificate of the selected endpoint are untouched, so the
/// secure-channel certificate check still validates the server actually reached.
///
public static class OpcUaEndpointUrlRewriter
{
///
/// The address to open the session on. When the advertised URL's scheme matches the configured one, the
/// configured host AND port (when it names one) replace the advertised ones and the advertised path is kept —
/// the SDK's own CoreClientUtils.SelectEndpoint rule. When host (case-insensitively) and port already
/// match, the advertised string is returned verbatim. A different scheme is a different transport and is left
/// alone; an unparseable advertised URL falls back to the configured one (callers that must fail loudly
/// instead, like the Client CLI/UI, validate before calling).
///
/// The EndpointUrl the server returned from GetEndpoints.
/// The URL the operator configured (the one GetEndpoints was sent to).
/// The URL to connect the session to.
public static string Rewrite(string? advertisedUrl, string configuredUrl)
{
if (!Uri.TryCreate(configuredUrl, UriKind.Absolute, out var configured)) return advertisedUrl ?? configuredUrl;
if (string.IsNullOrWhiteSpace(advertisedUrl)
|| !Uri.TryCreate(advertisedUrl, UriKind.Absolute, out var advertised))
return configuredUrl;
if (!string.Equals(advertised.Scheme, configured.Scheme, StringComparison.OrdinalIgnoreCase)) return advertisedUrl;
// A port-less configured URL (Uri.Port == -1 for opc.tcp) keeps the advertised port — copying -1 would drop it
// and the SDK would fall back to 4840 (review of 3b88056f).
var port = configured.Port > 0 ? configured.Port : advertised.Port;
// Nothing to rewrite: return the advertised string verbatim rather than a re-serialised copy (no added
// trailing slash, no case-folding of a host that only differs in case — DNS is case-insensitive). This is the
// Client CLI/UI's long-standing rule, kept when its own copy was folded in here (#812).
if (string.Equals(advertised.DnsSafeHost, configured.DnsSafeHost, StringComparison.OrdinalIgnoreCase)
&& advertised.Port == port)
return advertisedUrl;
var builder = new UriBuilder(advertised) { Host = configured.DnsSafeHost, Port = port };
// AbsoluteUri, not ToString(): ToString() un-escapes, turning an advertised "/x%20y" into "/x y" (#812).
return builder.Uri.AbsoluteUri;
}
}