Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure endpoints are returned when alternate url port does not match #2511

Merged
merged 3 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Libraries/Opc.Ua.Client/CoreClientUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public static EndpointDescription SelectEndpoint(
EndpointDescription endpoint = endpoints[ii];

// check for a match on the URL scheme.
if (endpoint.EndpointUrl.StartsWith(url.Scheme))
if (endpoint.EndpointUrl.StartsWith(url.Scheme, StringComparison.Ordinal))
{
// check if security was requested.
if (useSecurity)
Expand Down Expand Up @@ -297,7 +297,7 @@ public static EndpointDescription SelectEndpoint(
// pick the first available endpoint by default.
if (selectedEndpoint == null && endpoints.Count > 0)
{
selectedEndpoint = endpoints.FirstOrDefault(e => e.EndpointUrl?.StartsWith(url.Scheme) == true);
selectedEndpoint = endpoints.FirstOrDefault(e => e.EndpointUrl?.StartsWith(url.Scheme, StringComparison.Ordinal) == true);
}

// return the selected endpoint.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/

using System;
using System.Collections.Generic;

namespace Opc.Ua
{
Expand Down
101 changes: 59 additions & 42 deletions Stack/Opc.Ua.Core/Stack/Server/ServerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
using System.Collections.Generic;
using System.Globalization;
using System.Net;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
Expand Down Expand Up @@ -1023,7 +1024,10 @@ protected IList<BaseAddress> FilterByEndpointUrl(Uri endpointUrl, IList<BaseAddr
{
if (alternateUrl.DnsSafeHost == endpointUrl.DnsSafeHost)
{
accessibleAddresses.Add(new BaseAddress() { Url = alternateUrl, ProfileUri = baseAddress.ProfileUri, DiscoveryUrl = alternateUrl });
if (!accessibleAddresses.Any(item => item.Url == alternateUrl))
{
accessibleAddresses.Add(new BaseAddress() { Url = alternateUrl, ProfileUri = baseAddress.ProfileUri, DiscoveryUrl = alternateUrl });
}
break;
}
}
Expand Down Expand Up @@ -1133,59 +1137,72 @@ protected EndpointDescriptionCollection TranslateEndpointDescriptions(
{
EndpointDescriptionCollection translations = new EndpointDescriptionCollection();

// process endpoints
foreach (EndpointDescription endpoint in endpoints)
bool matchPort = false;
do
{
UriBuilder endpointUrl = new UriBuilder(endpoint.EndpointUrl);
// first round with port match
matchPort = !matchPort;

// find matching base address.
foreach (BaseAddress baseAddress in baseAddresses)
// process endpoints
foreach (EndpointDescription endpoint in endpoints)
{
bool translateHttpsEndpoint = false;
if (endpoint.TransportProfileUri == Profiles.HttpsBinaryTransport && baseAddress.ProfileUri == Profiles.HttpsBinaryTransport)
{
translateHttpsEndpoint = true;
}
UriBuilder endpointUrl = new UriBuilder(endpoint.EndpointUrl);

if (endpoint.TransportProfileUri != baseAddress.ProfileUri && !translateHttpsEndpoint)
// find matching base address.
foreach (BaseAddress baseAddress in baseAddresses)
{
continue;
}
bool translateHttpsEndpoint = false;
if (endpoint.TransportProfileUri == Profiles.HttpsBinaryTransport && baseAddress.ProfileUri == Profiles.HttpsBinaryTransport)
{
translateHttpsEndpoint = true;
}

if ((endpointUrl.Scheme != baseAddress.Url.Scheme) || (endpointUrl.Port != baseAddress.Url.Port))
{
continue;
}
if (endpoint.TransportProfileUri != baseAddress.ProfileUri && !translateHttpsEndpoint)
{
continue;
}

EndpointDescription translation = new EndpointDescription();
if (endpointUrl.Scheme != baseAddress.Url.Scheme)
{
continue;
}

translation.EndpointUrl = baseAddress.Url.ToString();
// try to match port in the first round, skip in the second round
if (matchPort && endpointUrl.Port != baseAddress.Url.Port)
{
continue;
}

if (endpointUrl.Path.StartsWith(baseAddress.Url.PathAndQuery, StringComparison.Ordinal) &&
endpointUrl.Path.Length > baseAddress.Url.PathAndQuery.Length)
{
string suffix = endpointUrl.Path.Substring(baseAddress.Url.PathAndQuery.Length);
translation.EndpointUrl += suffix;
}
EndpointDescription translation = new EndpointDescription();

translation.ProxyUrl = endpoint.ProxyUrl;
translation.SecurityLevel = endpoint.SecurityLevel;
translation.SecurityMode = endpoint.SecurityMode;
translation.SecurityPolicyUri = endpoint.SecurityPolicyUri;
translation.ServerCertificate = endpoint.ServerCertificate;
translation.TransportProfileUri = endpoint.TransportProfileUri;
translation.UserIdentityTokens = endpoint.UserIdentityTokens;
translation.Server = application;

if (!translations.Exists(match =>
match.EndpointUrl.Equals(translation.EndpointUrl, StringComparison.Ordinal) &&
match.SecurityMode == translation.SecurityMode &&
match.SecurityPolicyUri.Equals(translation.SecurityPolicyUri, StringComparison.Ordinal)))
{
translations.Add(translation);
translation.EndpointUrl = baseAddress.Url.ToString();

if (endpointUrl.Path.StartsWith(baseAddress.Url.PathAndQuery, StringComparison.Ordinal) &&
endpointUrl.Path.Length > baseAddress.Url.PathAndQuery.Length)
{
string suffix = endpointUrl.Path.Substring(baseAddress.Url.PathAndQuery.Length);
translation.EndpointUrl += suffix;
}

translation.ProxyUrl = endpoint.ProxyUrl;
translation.SecurityLevel = endpoint.SecurityLevel;
translation.SecurityMode = endpoint.SecurityMode;
translation.SecurityPolicyUri = endpoint.SecurityPolicyUri;
translation.ServerCertificate = endpoint.ServerCertificate;
translation.TransportProfileUri = endpoint.TransportProfileUri;
translation.UserIdentityTokens = endpoint.UserIdentityTokens;
translation.Server = application;

if (!translations.Exists(match =>
match.EndpointUrl.Equals(translation.EndpointUrl, StringComparison.Ordinal) &&
match.SecurityMode == translation.SecurityMode &&
match.SecurityPolicyUri.Equals(translation.SecurityPolicyUri, StringComparison.Ordinal)))
{
translations.Add(translation);
}
}
}
}
} while (matchPort && translations.Count == 0);

translations.Sort((ep1, ep2) => string.Compare(ep1.EndpointUrl, ep2.EndpointUrl, StringComparison.Ordinal));

Expand Down
Loading
Loading