In TLS mode proxy the server accepted X-Forwarded-For/Proto/Host from any peer, so a client reaching the HTTP port directly could pick a new address per sign-in (defeating the 5/min/IP limit), forge audit addresses and claim HTTPS; the admin never warned (SRS-03, SRV-3). Forwarded headers now count only from TITANO_TRUSTED_PROXIES (addresses or CIDR networks; default loopback, 10/8, 172.16/12, 192.168/16, fc00::/7), and the Dashboard and Settings show a "Not via proxy" chip with a tooltip when a page did not come through a trusted proxy over HTTPS (ProxyWarning in DashboardDto and ServerSettingsDto). TITANO_PUBLIC_HTTP_PORT and TITANO_PUBLIC_HTTPS_PORT name the ports clients reach when a container's ports are published as other host ports: the admin's HTTP-to-HTTPS redirect and the LAN beacon use them instead of the bound container ports (DOCKER-4, DOCKER-5). --help lists TITANO_LOG_LEVEL, the new variables and the container's PUID, PGID and TZ (DOCKER-7); a test keeps it in step with the variables the server reads. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
104 lines
4.4 KiB
C#
104 lines
4.4 KiB
C#
using System.Net;
|
|
using System.Net.NetworkInformation;
|
|
using System.Net.Sockets;
|
|
using Microsoft.AspNetCore.Hosting.Server;
|
|
using Microsoft.AspNetCore.Hosting.Server.Features;
|
|
using Titano.Protocol;
|
|
using Titano.Server.Api;
|
|
using Titano.Server.Configuration;
|
|
|
|
namespace Titano.Server.Hosting;
|
|
|
|
/// <summary>
|
|
/// LAN discovery (BRIEF §14.9, <c>TITANO_DISCOVERY</c>): every few seconds a <see cref="DiscoveryBeacon"/> is broadcast on
|
|
/// UDP port <see cref="DiscoveryBeacon.Port"/> of every IPv4 network, so clients can offer this server on first run.
|
|
/// Switching discovery off in the settings stops it without a restart. In a container the broadcasts reach the LAN
|
|
/// only with host networking; on a bridge network they stay on the bridge.
|
|
/// </summary>
|
|
public sealed class DiscoveryBeaconService(ServerOptions options, ServerState state, IServer server, ILogger<DiscoveryBeaconService> logger) : BackgroundService
|
|
{
|
|
private static readonly TimeSpan Interval = TimeSpan.FromSeconds(5);
|
|
|
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
|
{
|
|
using var udp = new UdpClient(AddressFamily.InterNetwork) { EnableBroadcast = true };
|
|
var warned = false;
|
|
while (!stoppingToken.IsCancellationRequested)
|
|
{
|
|
if (options.Discovery)
|
|
{
|
|
try
|
|
{
|
|
await SendOnceAsync(udp, BroadcastTargets(), stoppingToken);
|
|
}
|
|
catch (SocketException ex) when (!warned)
|
|
{
|
|
warned = true;
|
|
logger.LogInformation("LAN discovery beacon cannot be sent: {Reason}", ex.Message);
|
|
}
|
|
catch (SocketException)
|
|
{
|
|
// Already reported once; networks may come and go.
|
|
}
|
|
}
|
|
try
|
|
{
|
|
await Task.Delay(Interval, stoppingToken);
|
|
}
|
|
catch (OperationCanceledException)
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// The beacon for the ports clients reach: the public ports when set, else the ports Kestrel actually listens on
|
|
/// (HTTPS 0 when TLS is terminated elsewhere).
|
|
/// </summary>
|
|
public DiscoveryBeacon CreateBeacon() => new(
|
|
options.ServerName,
|
|
SystemEndpoints.Version,
|
|
options.PublicHttpPort ?? BoundPort(Uri.UriSchemeHttp) ?? options.HttpPort,
|
|
state.TlsFingerprint is null ? 0 : options.PublicHttpsPort ?? BoundPort(Uri.UriSchemeHttps) ?? options.HttpsPort,
|
|
state.TlsFingerprint);
|
|
|
|
public async Task SendOnceAsync(UdpClient udp, IEnumerable<IPEndPoint> targets, CancellationToken cancellationToken)
|
|
{
|
|
var datagram = CreateBeacon().ToBytes();
|
|
foreach (var target in targets) await udp.SendAsync(datagram, target, cancellationToken);
|
|
}
|
|
|
|
/// <summary>The limited broadcast address plus the directed broadcast of each IPv4 interface that is up.</summary>
|
|
private static List<IPEndPoint> BroadcastTargets()
|
|
{
|
|
var targets = new List<IPEndPoint> { new(IPAddress.Broadcast, DiscoveryBeacon.Port) };
|
|
foreach (var nic in NetworkInterface.GetAllNetworkInterfaces())
|
|
{
|
|
if (nic.OperationalStatus != OperationalStatus.Up || nic.NetworkInterfaceType == NetworkInterfaceType.Loopback) continue;
|
|
foreach (var unicast in nic.GetIPProperties().UnicastAddresses)
|
|
{
|
|
if (unicast.Address.AddressFamily != AddressFamily.InterNetwork || unicast.IPv4Mask is not { } mask) continue;
|
|
var address = unicast.Address.GetAddressBytes();
|
|
var bits = mask.GetAddressBytes();
|
|
for (var i = 0; i < 4; i++) address[i] = (byte)(address[i] | ~bits[i]);
|
|
var broadcast = new IPEndPoint(new IPAddress(address), DiscoveryBeacon.Port);
|
|
if (!targets.Contains(broadcast)) targets.Add(broadcast);
|
|
}
|
|
}
|
|
return targets;
|
|
}
|
|
|
|
private int? BoundPort(string scheme)
|
|
{
|
|
var addresses = server.Features.Get<IServerAddressesFeature>()?.Addresses ?? [];
|
|
foreach (var address in addresses)
|
|
{
|
|
if (Uri.TryCreate(address.Replace("[::]", "localhost", StringComparison.Ordinal).Replace("0.0.0.0", "localhost", StringComparison.Ordinal),
|
|
UriKind.Absolute, out var uri) && uri.Scheme == scheme)
|
|
return uri.Port;
|
|
}
|
|
return null;
|
|
}
|
|
}
|