XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

LumaTunnel

【WinUI】LumaTunnel 是一个面向个人多设备的 Windows 代理系统。客户端在本机提供 HTTP/HTTPS CONNECT 与 SOCKS5 TCP 代理,并通过一个受信任 TLS 证书保护的 WSS 会话,将多个 TCP 流复用到自建 Windows Server 节点。

公开
关注 0 Fork 0 Star 0
UTF-8
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);
    }
}