mirror of
https://github.com/OPCFoundation/UA-.NETStandard.git
synced 2026-10-18 14:43:08 +08:00
a81d981755
# Description
Pure code-style sweep across the repo — no behavioral changes. Drives
`dotnet format` info-level diagnostics down on a large slice of the
codebase (Stack, Libraries, Tests, Tools).
Six commits, applied bottom-up in safety order so each batch leaves the
build green:
1. **`style: fix whitespace and final-newline formatting (WHITESPACE,
FINALNEWLINE)`**
197 hits across ~30 files; pure mechanical cleanup.
2. **`style: apply dotnet format style fixes (IDE rules)`**
IDE-rule batch via `dotnet format style --severity info` —
`var`/explicit type, simplify names, collection expressions, expression
bodies, unused usings, etc.
3. **`style: apply dotnet format analyzers fixes (RCS blank-line
formatting)`**
Roslynator formatting (RCS0xxx) blank-line and punctuation rules.
4. **`style: convert comments to doc comments and inline local vars
(RCS1181, RCS1124)`**
24 files. RCS1181 promotes `// summary`-style comments above
public/internal members to `/// <summary>` doc comments; RCS1124 inlines
locals used exactly once. Generated `Tools/` files were reverted to keep
the source-generator outputs deterministic.
5. **`docs(agent): correct --diagnostics flag form and verify Roslynator
4.x code fixers`**
Updates `.github/agents/opc-ua-codestyle-enforcer.agent.md` with two
corrections discovered while running this sweep:
- `dotnet format --diagnostics "RULE1 RULE2"` (single quoted,
space-separated) silently fixes nothing on `dotnet format` 10.x — it
parses as one unknown ID and exits 0. The correct form is `--diagnostics
RULE1 --diagnostics RULE2 …`, repeated per rule.
- Roslynator 4.15.0 **does** ship code fixers for
RCS1078/1118/1124/1140/1142/1181/1221/1260 (the previous "no auto-fixer"
list was outdated). RCS1140/1142 default to `Hidden` so need `--severity
info` to surface and fire; RCS1078 requires
`roslynator_empty_string_style = field` (already set in
`.editorconfig`). Only NUnit4002/2046/2010 genuinely lack auto-fixers in
this stack.
6. **`style: format new code from master merge (dotnet format sweep)`**
After merging master into the branch (commit `e3ebc8742`), re-ran the
same three-phase format sweep on the 151 .cs files brought in by the
merge, scoped per project via repeated `--include`. 81 files changed;
same rule families as commits 1–4.
- Hand-fixed `Tests/Opc.Ua.MigrationAnalyzer.Tests/AnalyzerHarness.cs`
for `RCS1174` (redundant `async`/`await` removal) because the auto-fixer
didn't apply for that specific case.
- Reverted files that the formatter would break:
- `Libraries/Opc.Ua.Server/Server/ServerInternalData.cs` — multi-TFM
analysis disagreement injected `<<<<<<<` / `=======` / `>>>>>>>`
markers.
-
`Tools/Opc.Ua.MigrationAnalyzer.Core/{Core/Stack/Client/ChannelBase.cs,
Core/Stack/Types/IUserIdentityTokenHandler.cs,
Client/Session/Session.cs}` — IDE0005 removed `using
Opc.Ua.Security.Certificates` that IS used (the files are 1.5.378
reference fixtures for the analyzer).
- `Tests/Opc.Ua.Gds.Tests/PushTest.cs` — IDE0002 over-simplified
`OpcUa.ObjectTypeIds` to `ObjectTypeIds`, which then resolves to a
conflicting type.
### What this PR does **not** touch
- No behavioral or semantic logic changes.
- No `.editorconfig` rule promotions (rules stay at their current
severities; this is a backlog-burndown pass, not a rule tightening).
- Generated source under `Tools/` is intentionally excluded.
- Higher-risk rule families (`RCS1090`/`CA2007` `ConfigureAwait`,
`RCS1249` null-forgiving cleanup, `IDE0270`/`IDE0059`) are left for
follow-up sweeps because they need per-TFM build verification or
semantic review.
## Related Issues
_No tracking issue — this is a non-functional style sweep and the
agent-doc correction is a follow-up to the same work. Happy to open one
if the maintainers prefer._
## Checklist
- [ ] I have signed the
[CLA](https://opcfoundation.org/license/cla/ContributorLicenseAgreementv1.0.pdf)
and read the
[CONTRIBUTING](https://github.com/OPCFoundation/UA-.NETStandard/blob/master/CONTRIBUTING.md)
doc.
- [x] I have added tests that prove my fix is effective or that my
feature works and increased code coverage. _(N/A — style-only, no
behavioral change)_
- [x] I have added all necessary documentation. _(agent doc updated with
the two corrections)_
- [x] I have verified that my changes do not introduce (new) build or
analyzer warnings. _(verified clean on `net10.0` and `net48`; only
pre-existing `NETSDK1005`/`NETSDK1207` failures on Tools/AOT projects)_
- [ ] I ran **all** tests locally using the **UA.slnx** solution against
at least .net **framework** and .net **10**, and all passed. _(build
verified on net10 and net48 per batch; full test suite not yet run
locally — relying on CI)_
- [ ] I fixed **all** failing and flaky tests in the CI pipelines and
**all** CodeQL warnings.
- [ ] I have addressed **all** PR feedback received.
---------
Co-authored-by: agent <agent@local>
613 lines
24 KiB
C#
613 lines
24 KiB
C#
/* ========================================================================
|
|
* Copyright (c) 2005-2025 The OPC Foundation, Inc. All rights reserved.
|
|
*
|
|
* OPC Foundation MIT License 1.00
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person
|
|
* obtaining a copy of this software and associated documentation
|
|
* files (the "Software"), to deal in the Software without
|
|
* restriction, including without limitation the rights to use,
|
|
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the
|
|
* Software is furnished to do so, subject to the following
|
|
* conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be
|
|
* included in all copies or substantial portions of the Software.
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
* OTHER DEALINGS IN THE SOFTWARE.
|
|
*
|
|
* The complete license agreement can be found here:
|
|
* http://opcfoundation.org/License/MIT/1.00/
|
|
* ======================================================================*/
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Extensions.Logging;
|
|
using Opc.Ua.Bindings;
|
|
using Opc.Ua.Security.Certificates;
|
|
|
|
namespace Opc.Ua.Lds.Server
|
|
{
|
|
/// <summary>
|
|
/// OPC UA Local Discovery Server (LDS / LDS-ME) implementation per Part 12.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Subclasses <see cref="DiscoveryServerBase"/> so we get the discovery
|
|
/// service entry points (FindServers, FindServersOnNetwork, GetEndpoints,
|
|
/// RegisterServer, RegisterServer2) without inheriting Session,
|
|
/// Subscription, or NodeManager infrastructure from
|
|
/// <c>SessionServerBase</c>/<c>StandardServer</c>.
|
|
/// </remarks>
|
|
public class LdsServer : DiscoveryServerBase
|
|
{
|
|
private readonly ITelemetryContext m_telemetry;
|
|
private readonly TimeProvider m_timeProvider;
|
|
private ILogger m_log;
|
|
private readonly SemaphoreSlim m_lock;
|
|
private MulticastDiscovery m_multicast;
|
|
|
|
/// <summary>
|
|
/// Creates a new LDS server.
|
|
/// </summary>
|
|
/// <param name="telemetry">Telemetry context for logging.</param>
|
|
/// <param name="timeProvider">Optional <see cref="System.TimeProvider"/> used by the
|
|
/// registered-server store for prune scheduling and registration timestamps.
|
|
/// Defaults to <see cref="TimeProvider.System"/> when <c>null</c>.</param>
|
|
public LdsServer(ITelemetryContext telemetry = null, TimeProvider timeProvider = null)
|
|
: base(telemetry)
|
|
{
|
|
m_telemetry = telemetry;
|
|
m_timeProvider = timeProvider ?? TimeProvider.System;
|
|
m_lock = new SemaphoreSlim(1, 1);
|
|
Store = new RegisteredServerStore(timeProvider: m_timeProvider);
|
|
}
|
|
|
|
/// <summary>
|
|
/// In-memory database of registered servers and network records.
|
|
/// Exposed for tests so they can deterministically seed state.
|
|
/// </summary>
|
|
public RegisteredServerStore Store { get; }
|
|
|
|
/// <summary>
|
|
/// Optional multicast discovery layer (LDS-ME). Null when multicast
|
|
/// is disabled.
|
|
/// </summary>
|
|
public MulticastDiscovery Multicast => m_multicast;
|
|
|
|
/// <summary>
|
|
/// Optional hook tests use to plug in a multicast layer prior to
|
|
/// <see cref="ServerBase.StartAsync(ApplicationConfiguration, CancellationToken)"/>.
|
|
/// </summary>
|
|
public Func<LdsServer, MulticastDiscovery> MulticastFactory { get; set; }
|
|
|
|
/// <inheritdoc />
|
|
protected override void OnServerStarting(ApplicationConfiguration configuration)
|
|
{
|
|
base.OnServerStarting(configuration);
|
|
|
|
m_log = m_telemetry?.CreateLogger<LdsServer>()
|
|
?? Microsoft.Extensions.Logging.Abstractions.NullLogger<LdsServer>.Instance;
|
|
|
|
// wire up the optional multicast layer first so the capability
|
|
// computation below can reflect whether LDS-ME is available.
|
|
if (MulticastFactory != null)
|
|
{
|
|
m_multicast = MulticastFactory(this);
|
|
}
|
|
|
|
// Mark capabilities so this server self-advertises as an LDS or
|
|
// LDS-ME per OPC 10000-12 Annex D. The "LDS-ME" identifier is
|
|
// added when the MulticastExtension is configured so peers
|
|
// searching via FindServersOnNetwork can identify multicast-
|
|
// capable LDS instances.
|
|
ServerCapabilities = ComputeServerCapabilities(
|
|
ServerCapabilities,
|
|
m_multicast != null);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Computes the capability set self-advertised by this LDS per
|
|
/// OPC 10000-12 Annex D.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Always includes <c>LDS</c>; adds <c>LDS-ME</c> when the
|
|
/// MulticastExtension is available. Preserves any caller-provided
|
|
/// extra capabilities so applications can still surface custom
|
|
/// identifiers.
|
|
/// </remarks>
|
|
/// <param name="existing">Capabilities seeded prior to startup.</param>
|
|
/// <param name="hasMulticast">
|
|
/// <c>true</c> if the LDS-ME multicast layer is configured.
|
|
/// </param>
|
|
public static ArrayOf<string> ComputeServerCapabilities(
|
|
ArrayOf<string> existing,
|
|
bool hasMulticast)
|
|
{
|
|
if (existing.IsNull || existing.Count == 0)
|
|
{
|
|
return hasMulticast
|
|
? [s_ldsCapability, s_ldsMeCapability]
|
|
: [s_ldsCapability];
|
|
}
|
|
|
|
bool hasLds = false;
|
|
bool hasLdsMe = false;
|
|
foreach (string capability in existing)
|
|
{
|
|
if (string.Equals(capability, s_ldsCapability, StringComparison.Ordinal))
|
|
{
|
|
hasLds = true;
|
|
}
|
|
else if (string.Equals(capability, s_ldsMeCapability, StringComparison.Ordinal))
|
|
{
|
|
hasLdsMe = true;
|
|
}
|
|
}
|
|
|
|
var additions = new List<string>(2);
|
|
if (!hasLds)
|
|
{
|
|
additions.Add(s_ldsCapability);
|
|
}
|
|
if (hasMulticast && !hasLdsMe)
|
|
{
|
|
additions.Add(s_ldsMeCapability);
|
|
}
|
|
|
|
return additions.Count == 0
|
|
? existing
|
|
: ArrayOf.Combine(existing, [.. additions]);
|
|
}
|
|
|
|
/// <summary>
|
|
/// OPC 10000-12 Annex D capability identifiers exposed by an LDS.
|
|
/// </summary>
|
|
private const string s_ldsCapability = "LDS";
|
|
private const string s_ldsMeCapability = "LDS-ME";
|
|
|
|
/// <inheritdoc />
|
|
protected override async ValueTask StartApplicationAsync(
|
|
ApplicationConfiguration configuration,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
await base.StartApplicationAsync(configuration, cancellationToken).ConfigureAwait(false);
|
|
|
|
if (m_multicast != null)
|
|
{
|
|
IList<string> capabilities = ServerCapabilities.IsNull
|
|
? []
|
|
: ServerCapabilities.ToList();
|
|
IList<string> baseUris = [.. BaseAddresses
|
|
.Select(b => b.Url?.ToString())
|
|
.Where(u => !string.IsNullOrEmpty(u))];
|
|
await m_multicast
|
|
.StartAsync(configuration.ApplicationUri, baseUris, capabilities, cancellationToken)
|
|
.ConfigureAwait(false);
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
protected override async ValueTask OnServerStoppingAsync(CancellationToken cancellationToken = default)
|
|
{
|
|
try
|
|
{
|
|
if (m_multicast != null)
|
|
{
|
|
await m_multicast.StopAsync(cancellationToken).ConfigureAwait(false);
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
Store.Dispose();
|
|
await base.OnServerStoppingAsync(cancellationToken).ConfigureAwait(false);
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override async ValueTask<FindServersResponse> FindServersAsync(
|
|
SecureChannelContext secureChannelContext,
|
|
RequestHeader requestHeader,
|
|
string endpointUrl,
|
|
ArrayOf<string> localeIds,
|
|
ArrayOf<string> serverUris,
|
|
RequestLifetime requestLifetime)
|
|
{
|
|
ValidateRequest(requestHeader);
|
|
|
|
var servers = new List<ApplicationDescription>();
|
|
|
|
await m_lock.WaitAsync(requestLifetime.CancellationToken).ConfigureAwait(false);
|
|
try
|
|
{
|
|
IList<BaseAddress> baseAddresses = BaseAddresses;
|
|
|
|
Uri parsedEndpointUrl = Utils.ParseUri(endpointUrl);
|
|
if (parsedEndpointUrl != null)
|
|
{
|
|
baseAddresses = FilterByEndpointUrl(parsedEndpointUrl, baseAddresses);
|
|
}
|
|
|
|
ICollection<string> uriFilter = serverUris.IsNull
|
|
? Array.Empty<string>()
|
|
: serverUris.ToList();
|
|
|
|
// include the LDS itself unless filtered out.
|
|
if (baseAddresses.Count > 0 &&
|
|
(uriFilter.Count == 0 || uriFilter.Contains(ServerDescription.ApplicationUri)))
|
|
{
|
|
servers.Add(TranslateApplicationDescription(
|
|
parsedEndpointUrl,
|
|
ServerDescription,
|
|
baseAddresses,
|
|
ServerDescription.ApplicationName));
|
|
}
|
|
|
|
ICollection<string> requestedLocales = localeIds.IsNull
|
|
? Array.Empty<string>()
|
|
: localeIds.ToList();
|
|
|
|
// append registered servers that pass the filter.
|
|
servers.AddRange(Store.Find(uriFilter, requestedLocales));
|
|
}
|
|
finally
|
|
{
|
|
m_lock.Release();
|
|
}
|
|
|
|
return new FindServersResponse
|
|
{
|
|
ResponseHeader = CreateResponse(requestHeader, StatusCodes.Good),
|
|
Servers = servers
|
|
};
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override async ValueTask<GetEndpointsResponse> GetEndpointsAsync(
|
|
SecureChannelContext secureChannelContext,
|
|
RequestHeader requestHeader,
|
|
string endpointUrl,
|
|
ArrayOf<string> localeIds,
|
|
ArrayOf<string> profileUris,
|
|
RequestLifetime requestLifetime)
|
|
{
|
|
ValidateRequest(requestHeader);
|
|
|
|
ArrayOf<EndpointDescription> endpoints;
|
|
|
|
await m_lock.WaitAsync(requestLifetime.CancellationToken).ConfigureAwait(false);
|
|
try
|
|
{
|
|
IList<BaseAddress> baseAddresses = FilterByProfile(profileUris, BaseAddresses);
|
|
endpoints = BuildEndpointDescriptions(endpointUrl, baseAddresses);
|
|
}
|
|
finally
|
|
{
|
|
m_lock.Release();
|
|
}
|
|
|
|
return new GetEndpointsResponse
|
|
{
|
|
ResponseHeader = CreateResponse(requestHeader, StatusCodes.Good),
|
|
Endpoints = endpoints
|
|
};
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override async ValueTask<RegisterServerResponse> RegisterServerAsync(
|
|
SecureChannelContext secureChannelContext,
|
|
RequestHeader requestHeader,
|
|
RegisteredServer server,
|
|
RequestLifetime requestLifetime)
|
|
{
|
|
ValidateRequest(requestHeader);
|
|
|
|
ServiceResult validation = ValidateRegistration(secureChannelContext, server);
|
|
if (ServiceResult.IsBad(validation))
|
|
{
|
|
throw new ServiceResultException(validation);
|
|
}
|
|
|
|
_ = await Store
|
|
.RegisterAsync(server, mdnsConfig: null, requestLifetime.CancellationToken)
|
|
.ConfigureAwait(false);
|
|
|
|
return new RegisterServerResponse
|
|
{
|
|
ResponseHeader = CreateResponse(requestHeader, StatusCodes.Good)
|
|
};
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override async ValueTask<RegisterServer2Response> RegisterServer2Async(
|
|
SecureChannelContext secureChannelContext,
|
|
RequestHeader requestHeader,
|
|
RegisteredServer server,
|
|
ArrayOf<ExtensionObject> discoveryConfiguration,
|
|
RequestLifetime requestLifetime)
|
|
{
|
|
ValidateRequest(requestHeader);
|
|
|
|
ServiceResult validation = ValidateRegistration(secureChannelContext, server);
|
|
if (ServiceResult.IsBad(validation))
|
|
{
|
|
throw new ServiceResultException(validation);
|
|
}
|
|
|
|
var configResults = new List<StatusCode>();
|
|
|
|
// gather mdns configurations; non-mdns configs are returned as Good but ignored.
|
|
var mdnsConfigs = new List<MdnsDiscoveryConfiguration>();
|
|
if (!discoveryConfiguration.IsNull)
|
|
{
|
|
foreach (ExtensionObject ext in discoveryConfiguration)
|
|
{
|
|
if (!ext.IsNull && ext.TryGetValue(out MdnsDiscoveryConfiguration mdns))
|
|
{
|
|
mdnsConfigs.Add(mdns);
|
|
}
|
|
configResults.Add(StatusCodes.Good);
|
|
}
|
|
}
|
|
|
|
if (mdnsConfigs.Count == 0)
|
|
{
|
|
// no MdnsDiscoveryConfiguration provided — fall back to a plain registration.
|
|
_ = await Store
|
|
.RegisterAsync(server, mdnsConfig: null, requestLifetime.CancellationToken)
|
|
.ConfigureAwait(false);
|
|
}
|
|
else
|
|
{
|
|
foreach (MdnsDiscoveryConfiguration mdns in mdnsConfigs)
|
|
{
|
|
_ = await Store
|
|
.RegisterAsync(server, mdns, requestLifetime.CancellationToken)
|
|
.ConfigureAwait(false);
|
|
}
|
|
}
|
|
|
|
return new RegisterServer2Response
|
|
{
|
|
ResponseHeader = CreateResponse(requestHeader, StatusCodes.Good),
|
|
ConfigurationResults = configResults
|
|
};
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override async ValueTask<FindServersOnNetworkResponse> FindServersOnNetworkAsync(
|
|
SecureChannelContext secureChannelContext,
|
|
RequestHeader requestHeader,
|
|
uint startingRecordId,
|
|
uint maxRecordsToReturn,
|
|
ArrayOf<string> serverCapabilityFilter,
|
|
RequestLifetime requestLifetime)
|
|
{
|
|
ValidateRequest(requestHeader);
|
|
|
|
await Task.Yield();
|
|
|
|
ICollection<string> capFilter = serverCapabilityFilter.IsNull
|
|
? Array.Empty<string>()
|
|
: serverCapabilityFilter.ToList();
|
|
|
|
(IList<ServerOnNetwork> records, DateTime lastReset) =
|
|
Store.ListOnNetwork(startingRecordId, maxRecordsToReturn, capFilter);
|
|
|
|
return new FindServersOnNetworkResponse
|
|
{
|
|
ResponseHeader = CreateResponse(requestHeader, StatusCodes.Good),
|
|
LastCounterResetTime = lastReset,
|
|
Servers = records.ToArray()
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validates a <see cref="RegisteredServer"/> against Part 12 §6.4.2/§6.4.5
|
|
/// requirements, returning the appropriate status code on failure.
|
|
/// </summary>
|
|
/// <exception cref="ArgumentNullException"><paramref name="server"/> is null.</exception>
|
|
protected virtual ServiceResult ValidateRegistration(
|
|
SecureChannelContext secureChannelContext,
|
|
RegisteredServer server)
|
|
{
|
|
if (server == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(server));
|
|
}
|
|
|
|
// Per Part 12 §6.4.2: registration must occur on a secure channel
|
|
// (Sign or SignAndEncrypt). None is rejected.
|
|
MessageSecurityMode mode = secureChannelContext?.EndpointDescription?.SecurityMode
|
|
?? MessageSecurityMode.Invalid;
|
|
if (mode is MessageSecurityMode.None or MessageSecurityMode.Invalid)
|
|
{
|
|
return new ServiceResult(StatusCodes.BadSecurityChecksFailed,
|
|
new LocalizedText("RegisterServer requires a signed secure channel."));
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(server.ServerUri))
|
|
{
|
|
return new ServiceResult(StatusCodes.BadServerUriInvalid,
|
|
new LocalizedText("ServerUri is empty."));
|
|
}
|
|
|
|
if (server.ServerNames.IsNull || server.ServerNames.Count == 0)
|
|
{
|
|
return new ServiceResult(StatusCodes.BadServerNameMissing,
|
|
new LocalizedText("ServerNames is empty."));
|
|
}
|
|
|
|
if (server.DiscoveryUrls.IsNull || server.DiscoveryUrls.Count == 0)
|
|
{
|
|
return new ServiceResult(StatusCodes.BadDiscoveryUrlMissing,
|
|
new LocalizedText("DiscoveryUrls is empty."));
|
|
}
|
|
|
|
if (server.ServerType == ApplicationType.Client)
|
|
{
|
|
return new ServiceResult(StatusCodes.BadInvalidArgument,
|
|
new LocalizedText("ServerType=Client is not allowed."));
|
|
}
|
|
|
|
if ((int)server.ServerType is < 0 or > (int)ApplicationType.DiscoveryServer)
|
|
{
|
|
return new ServiceResult(StatusCodes.BadInvalidArgument,
|
|
new LocalizedText("ServerType is out of range."));
|
|
}
|
|
|
|
// Match the client cert ApplicationUri against the ServerUri.
|
|
if (secureChannelContext?.ClientChannelCertificate is { Length: > 0 } certBytes)
|
|
{
|
|
try
|
|
{
|
|
using var cert = Certificate.FromRawData(certBytes);
|
|
IReadOnlyList<string> applicationUris = X509Utils.GetApplicationUrisFromCertificate(cert);
|
|
if (applicationUris.Count > 0 &&
|
|
!applicationUris.Any(uri => string.Equals(uri, server.ServerUri, StringComparison.Ordinal)))
|
|
{
|
|
return new ServiceResult(StatusCodes.BadServerUriInvalid,
|
|
new LocalizedText("ServerUri does not match the certificate ApplicationUri."));
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
m_log?.LogDebug(ex, "Failed to inspect client cert ApplicationUri.");
|
|
}
|
|
}
|
|
|
|
return ServiceResult.Good;
|
|
}
|
|
|
|
private ArrayOf<EndpointDescription> BuildEndpointDescriptions(
|
|
string endpointUrl,
|
|
IList<BaseAddress> baseAddresses)
|
|
{
|
|
Uri parsedEndpointUrl = Utils.ParseUri(endpointUrl);
|
|
if (parsedEndpointUrl != null)
|
|
{
|
|
baseAddresses = FilterByEndpointUrl(parsedEndpointUrl, baseAddresses);
|
|
}
|
|
|
|
if (baseAddresses.Count == 0)
|
|
{
|
|
return new ArrayOf<EndpointDescription>(Array.Empty<EndpointDescription>());
|
|
}
|
|
|
|
ApplicationDescription application = TranslateApplicationDescription(
|
|
parsedEndpointUrl,
|
|
ServerDescription,
|
|
baseAddresses,
|
|
ServerDescription.ApplicationName);
|
|
|
|
return TranslateEndpointDescriptions(
|
|
parsedEndpointUrl,
|
|
baseAddresses,
|
|
Endpoints,
|
|
application);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
protected override IList<ServiceHost> InitializeServiceHosts(
|
|
ApplicationConfiguration configuration,
|
|
ITransportListenerBindings bindingFactory,
|
|
out ApplicationDescription serverDescription,
|
|
out ArrayOf<EndpointDescription> endpoints)
|
|
{
|
|
var hosts = new Dictionary<string, ServiceHost>();
|
|
|
|
// Mirror StandardServer's defaults so the LDS opens a real listener.
|
|
if (configuration.ServerConfiguration.SecurityPolicies.IsEmpty)
|
|
{
|
|
configuration.ServerConfiguration.SecurityPolicies =
|
|
configuration.ServerConfiguration.SecurityPolicies.AddItem(new ServerSecurityPolicy());
|
|
}
|
|
|
|
if (configuration.ServerConfiguration.UserTokenPolicies.IsEmpty)
|
|
{
|
|
var userTokenPolicy = new UserTokenPolicy { TokenType = UserTokenType.Anonymous };
|
|
userTokenPolicy.PolicyId = userTokenPolicy.TokenType.ToString();
|
|
|
|
configuration.ServerConfiguration.UserTokenPolicies += userTokenPolicy;
|
|
}
|
|
|
|
serverDescription = new ApplicationDescription
|
|
{
|
|
ApplicationUri = configuration.ApplicationUri,
|
|
ApplicationName = new LocalizedText("en-US", configuration.ApplicationName),
|
|
ApplicationType = configuration.ApplicationType,
|
|
ProductUri = configuration.ProductUri,
|
|
DiscoveryUrls = GetDiscoveryUrls()
|
|
};
|
|
|
|
var endpointsList = new List<EndpointDescription>();
|
|
ArrayOf<string> baseAddresses = configuration.ServerConfiguration.BaseAddresses;
|
|
foreach (string scheme in Utils.DefaultUriSchemes)
|
|
{
|
|
bool hasAddress = false;
|
|
foreach (string a in baseAddresses)
|
|
{
|
|
if (a.StartsWith(scheme, StringComparison.Ordinal))
|
|
{
|
|
hasAddress = true;
|
|
break;
|
|
}
|
|
}
|
|
if (!hasAddress)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
ITransportListenerFactory binding = bindingFactory.GetBinding(scheme, MessageContext.Telemetry);
|
|
if (binding != null)
|
|
{
|
|
List<EndpointDescription> endpointsForHost = binding.CreateServiceHost(
|
|
this,
|
|
hosts,
|
|
configuration,
|
|
configuration.ServerConfiguration.BaseAddresses,
|
|
serverDescription,
|
|
configuration.ServerConfiguration.SecurityPolicies,
|
|
CertificateManager,
|
|
configuration.CertificateManager);
|
|
endpointsList.AddRange(endpointsForHost);
|
|
}
|
|
}
|
|
|
|
endpoints = endpointsList.ToArray();
|
|
return [.. hosts.Values];
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override ServiceHost CreateServiceHost(ServerBase server, params Uri[] addresses)
|
|
{
|
|
return new ServiceHost(this, addresses);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
protected override EndpointBase GetEndpointInstance(ServerBase server)
|
|
{
|
|
return new DiscoveryEndpoint(server);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
protected override void Dispose(bool disposing)
|
|
{
|
|
if (disposing)
|
|
{
|
|
m_lock?.Dispose();
|
|
}
|
|
base.Dispose(disposing);
|
|
}
|
|
}
|
|
}
|