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). /// /// /// 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. A different scheme is a different transport and is /// left alone; an unparseable advertised URL falls back to the configured one. /// /// 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; same fix as the client's EndpointSelector). var builder = new UriBuilder(advertised) { Host = configured.DnsSafeHost, Port = configured.Port > 0 ? configured.Port : advertised.Port, }; return builder.Uri.ToString(); } }