Public Access
The relay checkbox said it was not wired up, and the plan said the server half was built and shipped. It was not: there was ADR 0004, the contract records, RelayOptions and a CHECK constraint, and RelayOptions said in its own remarks that there was no endpoint and no dialler. So both halves are built here, as step 3 of docs/reaching-a-host-you-cannot-dial.md. Server, in Features/Relay: - POST /api/v1/relay/tickets makes every decision: the host, Connect on its vault, the relay switched on, and the stored name resolved once and judged by RelayAddressPolicy. Loopback, link-local, 0.0.0.0/8, multicast and the metadata endpoints are refused with no configuration escape, with IPv4-mapped and NAT64 spellings unwrapped first; then the operator's denials, allow-list and private-network switch. What passes goes into a Data Protection ticket, and nothing resolves the name again. Every target refusal is the same 403, so the endpoint cannot be used to map the operator's DNS; the real reason is logged. - GET /api/v1/relay/connect takes the ticket as a second subprotocol, checks its expiry against TimeProvider and spends it with a unique insert into relay_ticket_use before accepting the upgrade. After the upgrade a refusal is a close code: 4502 for a host the server could not reach, 4429 for a limit, 4408 idle, 4409 lifetime, 1001 after a drain. - relay_session is the audit row, written from short-lived scopes. Client: - RelayBridge is a loopback SOCKS5 listener that accepts once and pipes to the relay socket. It rides on the new SshLoopbackProxy.Lifetime, so the connection disposes it on every way a session ends and a failed connect disposes it before throwing. - ServerRelay, exposed as IVaultServer.Relay from /api/v1/meta, asks for the ticket, opens the socket, starts the bridge and turns every refusal into a sentence. - VaultViewModel and TransfersViewModel reach a relayed host through it, never fall back to dialling it directly, and replace SSH.NET's transport error with the relay's own reason when it gave one. Neither side ever cancels a WebSocket operation. Cancelling one aborts the socket, and an aborted socket sends no close frame, which is the only way the client learns the server could not reach the host. AnIdleSession_IsClosedWithTheIdleCode asserts that the code arrives. Three departures from ADR 0004, recorded in it: awaited copy loops rather than Pipelines (same backpressure, two 32 KiB buffers), no ?ticket= fallback, and no device id in the ticket. Tests: API 245, shell 582 (5 skipped), layout 170, SSH 110 including a real login through the bridge, API client 30, contracts 25. Not run: the Android build (its change is copy only) and the Alpine layout oracle.
97 lines
4.2 KiB
C#
97 lines
4.2 KiB
C#
using DodoSSH.Api.Authorization;
|
|
using DodoSSH.Api.Features.Events;
|
|
using DodoSSH.Api.Features.Identity;
|
|
using DodoSSH.Api.Features.Sync;
|
|
using DodoSSH.Api.Features.Teams;
|
|
using DodoSSH.Api.Setup;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Authorization.Policy;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// Docker secrets, if mounted. Optional so the same image works with plain environment variables.
|
|
builder.Configuration.AddKeyPerFile("/run/secrets", optional: true);
|
|
builder.Configuration.AddEnvironmentVariables(prefix: "DODOSSH_");
|
|
|
|
builder.Services.AddDodoJson();
|
|
builder.Services.AddDodoOptions();
|
|
builder.Services.AddDodoPersistence(builder.Configuration);
|
|
builder.Services.AddDodoAuthentication();
|
|
builder.Services.AddDodoEndpoints();
|
|
builder.Services.AddDodoOpenApi();
|
|
builder.Services.AddDodoHealthChecks();
|
|
|
|
// DateTime.UtcNow is banned repo-wide (see BannedSymbols.txt); everything takes
|
|
// TimeProvider so time can be faked in tests.
|
|
builder.Services.AddSingleton(TimeProvider.System);
|
|
|
|
// FastEndpoints registers this too. Kept because CurrentUserContext needs it on its own merits, and
|
|
// the registration would be silently lost the day the endpoint framework changed again.
|
|
builder.Services.AddHttpContextAccessor();
|
|
builder.Services.AddScoped<ICurrentUserContext, CurrentUserContext>();
|
|
builder.Services.AddScoped<IVaultAccessService, VaultAccessService>();
|
|
builder.Services.AddScoped<SyncService>();
|
|
builder.Services.AddScoped<IdentityService>();
|
|
builder.Services.AddScoped<EnrollmentService>();
|
|
builder.Services.AddScoped<DeviceService>();
|
|
builder.Services.AddScoped<DirectoryService>();
|
|
builder.Services.AddScoped<KeyLogService>();
|
|
builder.Services.AddScoped<TeamService>();
|
|
builder.Services.AddScoped<VaultGrantService>();
|
|
builder.Services.AddScoped<IIdentityBindingVerifier, IdentityBindingVerifier>();
|
|
builder.Services.AddSingleton<ICursorKeyProvider, CursorKeyProvider>();
|
|
|
|
// A singleton, because the sockets it holds outlive the requests that opened them. Registered twice
|
|
// resolving to the same instance: the endpoint needs the whole hub — admit, remove, count — while the
|
|
// write paths that announce a change need only the two methods that announce one, and should not gain
|
|
// a reference to connection management to get them.
|
|
builder.Services.AddSingleton<VaultEventHub>();
|
|
builder.Services.AddSingleton<IVaultEventPublisher>(
|
|
provider => provider.GetRequiredService<VaultEventHub>());
|
|
|
|
// The relay's services, its ticket key ring, and a shutdown long enough for its drain. See ADR 0004.
|
|
builder.Services.AddDodoRelay();
|
|
|
|
// Scoped rather than the AddAuthorization default of singleton: the handler reads the request's
|
|
// DbContext, and a singleton would capture one for the lifetime of the process.
|
|
builder.Services.AddScoped<IAuthorizationHandler, EnrolledHandler>();
|
|
|
|
// Replaces the framework default, which is also registered as a singleton. Last registration wins.
|
|
builder.Services.AddSingleton<IAuthorizationMiddlewareResultHandler, DodoAuthorizationResultHandler>();
|
|
|
|
builder.Services.AddProblemDetails();
|
|
|
|
var app = builder.Build();
|
|
|
|
// Deliberately no UseHttpsRedirection: the API is always fronted by a reverse proxy
|
|
// (Caddy in the reference compose stack) which terminates TLS. Redirecting here
|
|
// produces redirect loops behind a proxy. HTTPS in development comes from the
|
|
// launch profile instead.
|
|
|
|
app.BlockFastEndpointsRouteTable();
|
|
|
|
// Before the authentication middleware, because the upgrade handshake has to survive it: the events
|
|
// endpoint answers an ordinary authenticated request that happens to become a socket, and without
|
|
// this the upgrade is never offered and the handler sees a plain GET. No allow-list of origins is
|
|
// configured, deliberately — every client here is a native application sending a bearer token, so
|
|
// there is no browser origin to trust and nothing a cross-site request could reach without one.
|
|
app.UseWebSockets();
|
|
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
|
|
app.MapDodoHealthChecks();
|
|
app.MapDodoEndpoints();
|
|
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.MapOpenApi();
|
|
}
|
|
|
|
app.WarnOnRiskyConfiguration();
|
|
|
|
// Before the port opens, not after. See Persistence.MigrateDodoDatabaseAsync.
|
|
await app.MigrateDodoDatabaseAsync().ConfigureAwait(false);
|
|
|
|
await app.RunAsync().ConfigureAwait(false);
|