using System.Net; using LumaTunnel.Server.Core.Runtime; using LumaTunnel.Shared.Models; using LumaTunnel.Shared.Protocol; using XFEExtension.NetCore.ServerInteractive.Attributes; using XFEExtension.NetCore.ServerInteractive.Implements.CoreService; namespace LumaTunnel.Server.Core.Services; public partial class EnrollmentService : ServerCoreStandardServiceBase { [EntryPoint("v1/device/enroll")] public async Task EnrollAsync() { string? pairingCode = Json > "pairingCode"; string? deviceId = Json > "deviceId"; string? deviceName = Json > "deviceName"; string? clientVersion = Json > "clientVersion"; if (string.IsNullOrWhiteSpace(pairingCode) || string.IsNullOrWhiteSpace(deviceId)) { await CloseWithError("PairingCode and deviceId are required.", HttpStatusCode.BadRequest).ConfigureAwait(false); return; } var runtime = ServerRuntime.Current; if (!await runtime.PairingCodes.ConsumeAsync(pairingCode).ConfigureAwait(false)) { await CloseWithError("The pairing code is invalid, expired, or already used.", HttpStatusCode.Unauthorized).ConfigureAwait(false); return; } var (device, token) = await runtime.Devices.EnrollAsync( deviceId, deviceName ?? Environment.MachineName, clientVersion ?? "unknown").ConfigureAwait(false); await Close(new DeviceEnrollmentResponse( runtime.Settings.NodeId, runtime.Settings.NodeName, device.DeviceId, token, new Uri(runtime.Settings.PublicTunnelUrl), TunnelProtocol.Version)).ConfigureAwait(false); } }