lvfengfree 9e3b1f3c03 feat: 添加远程桌面Token分享功能
- 新增 WindowsCredential 模型和控制器,用于管理 Windows 凭据
- 新增 RemoteAccessToken 模型,支持生成可分享的远程访问链接
- 更新 RemoteDesktopController,添加 Token 生成、验证、撤销等 API
- 更新前端 RemoteDesktopModal,支持4种连接方式:快速连接、生成分享链接、手动输入、链接管理
- 新增 WindowsCredentialManager 组件用于管理 Windows 凭据
- 新增 RemoteAccessPage 用于通过 Token 访问远程桌面
- 添加 Vue Router 支持 /remote/:token 路由
- 更新数据库迁移,添加 WindowsCredentials 和 RemoteAccessTokens 表
2026-01-20 15:00:44 +08:00

138 lines
4.5 KiB
C#

using AmtScanner.Api.Data;
using AmtScanner.Api.Models;
using AmtScanner.Api.Services;
using AmtScanner.Api.Repositories;
using Microsoft.EntityFrameworkCore;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
// Disable SSL certificate validation for AMT devices (they use self-signed certs)
ServicePointManager.ServerCertificateValidationCallback =
(sender, certificate, chain, sslPolicyErrors) => true;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// Add CORS
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowFrontend", policy =>
{
policy.WithOrigins("http://localhost:5173", "http://localhost:3000", "http://localhost:3001")
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
});
});
// Add SignalR for WebSocket
builder.Services.AddSignalR();
// Add Database
var databaseProvider = builder.Configuration.GetValue<string>("DatabaseProvider", "SQLite");
if (databaseProvider == "MySQL")
{
var connectionString = builder.Configuration.GetConnectionString("MySqlConnection");
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString)));
}
else
{
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseSqlite(builder.Configuration.GetConnectionString("DefaultConnection")));
}
// Add Services
builder.Services.AddScoped<IAmtScannerService, AmtScannerService>();
builder.Services.AddScoped<ICredentialService, CredentialService>();
builder.Services.AddScoped<IAmtHardwareQueryService, AmtHardwareQueryService>();
builder.Services.AddScoped<IHardwareInfoService, HardwareInfoService>();
builder.Services.AddScoped<IHardwareInfoRepository, HardwareInfoRepository>();
builder.Services.AddScoped<IAmtPowerService, AmtPowerService>();
builder.Services.AddHttpClient<IGuacamoleService, GuacamoleService>();
var app = builder.Build();
// Configure the HTTP request pipeline
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseCors("AllowFrontend");
app.UseAuthorization();
app.MapControllers();
// Map SignalR Hub
app.MapHub<ScanProgressHub>("/scanHub");
// Ensure database is created
using (var scope = app.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
try
{
// Apply migrations (create database and tables)
// 如果迁移失败(表已存在),尝试手动标记迁移为已完成
try
{
db.Database.Migrate();
}
catch (Exception migrationEx) when (migrationEx.Message.Contains("already exists"))
{
Console.WriteLine("⚠️ 表已存在,尝试标记迁移为已完成...");
var pendingMigrations = db.Database.GetPendingMigrations().ToList();
foreach (var migration in pendingMigrations)
{
try
{
db.Database.ExecuteSqlRaw(
"INSERT IGNORE INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`) VALUES ({0}, {1})",
migration, "8.0.0");
Console.WriteLine($"✅ 已标记迁移: {migration}");
}
catch { }
}
}
// Add default credential if not exists
if (!db.AmtCredentials.Any())
{
var credentialService = scope.ServiceProvider.GetRequiredService<ICredentialService>();
var defaultCredential = new AmtCredential
{
Name = "Default Admin",
Username = "admin",
Password = credentialService.EncryptPassword("Guo1wu3shi4!"),
IsDefault = true,
Description = "默认管理员凭据",
CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow
};
db.AmtCredentials.Add(defaultCredential);
db.SaveChanges();
Console.WriteLine("✅ 默认凭据已创建: admin / Guo1wu3shi4!");
}
Console.WriteLine($"✅ 数据库连接成功: {databaseProvider}");
}
catch (Exception ex)
{
Console.WriteLine($"❌ 数据库初始化失败: {ex.Message}");
throw;
}
}
app.Run();