- Agent端优化: * 添加质量档位定义 (Low: 320x180@3fps, High: 1280x720@15fps) * H.264编码器支持动态质量切换 * 屏幕流服务支持按需推流和质量控制 * 添加SignalR信令客户端连接服务器 - 服务器端优化: * 添加StreamSignalingHub处理质量控制信令 * 支持设备注册/注销和监控状态管理 * 支持教师端监控控制和设备选中 - 前端组件: * 创建H264VideoPlayer组件支持H.264和JPEG模式 * 更新学生屏幕监控页面使用新组件 - 性能提升: * 带宽从120Mbps降至6-7Mbps (降低95%) * 监控墙模式: 60台100kbps=6Mbps * 单机放大模式: 1台1Mbps+59台100kbps=6.9Mbps * 无人观看时停止推流节省带宽
208 lines
6.9 KiB
C#
208 lines
6.9 KiB
C#
using AmtScanner.Api.Configuration;
|
|
using AmtScanner.Api.Data;
|
|
using AmtScanner.Api.Hubs;
|
|
using AmtScanner.Api.Middleware;
|
|
using AmtScanner.Api.Services;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using System.Text;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// 监听所有 IP 地址,允许外部访问
|
|
builder.WebHost.UseUrls("http://0.0.0.0:5000");
|
|
|
|
// Add services to the container
|
|
builder.Services.AddControllers();
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
builder.Services.AddSwaggerGen();
|
|
|
|
// Add SignalR
|
|
builder.Services.AddSignalR();
|
|
|
|
// Add CORS
|
|
builder.Services.AddCors(options =>
|
|
{
|
|
options.AddPolicy("AllowFrontend", policy =>
|
|
{
|
|
policy.WithOrigins("http://localhost:5173", "http://localhost:3000", "http://localhost:3001", "http://localhost:3006", "http://localhost:3007")
|
|
.AllowAnyHeader()
|
|
.AllowAnyMethod()
|
|
.AllowCredentials()
|
|
.SetIsOriginAllowed(_ => true); // SignalR 需要
|
|
});
|
|
});
|
|
|
|
// 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 JWT Configuration
|
|
builder.Services.Configure<JwtSettings>(builder.Configuration.GetSection(JwtSettings.SectionName));
|
|
builder.Services.AddScoped<IJwtService, JwtService>();
|
|
builder.Services.AddScoped<IAuthService, AuthService>();
|
|
builder.Services.AddScoped<IMenuService, MenuService>();
|
|
|
|
// Add AMT Services
|
|
builder.Services.AddSingleton<IAmtScanService, AmtScanService>();
|
|
builder.Services.AddScoped<IAmtWsmanService, AmtWsmanService>();
|
|
builder.Services.AddScoped<IAmtNetworkService, AmtNetworkService>();
|
|
builder.Services.AddScoped<IAmtPowerService, AmtPowerService>();
|
|
|
|
// Add Screen Stream Proxy Service
|
|
builder.Services.AddSingleton<ScreenStreamProxyService>();
|
|
|
|
// Add Guacamole Service
|
|
builder.Services.AddHttpClient("Guacamole");
|
|
builder.Services.AddSingleton<GuacamoleService>();
|
|
|
|
// Add JWT Authentication
|
|
var jwtSettings = builder.Configuration.GetSection(JwtSettings.SectionName).Get<JwtSettings>()!;
|
|
builder.Services.AddAuthentication(options =>
|
|
{
|
|
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
})
|
|
.AddJwtBearer(options =>
|
|
{
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
|
{
|
|
ValidateIssuer = true,
|
|
ValidateAudience = true,
|
|
ValidateLifetime = true,
|
|
ValidateIssuerSigningKey = true,
|
|
ValidIssuer = jwtSettings.Issuer,
|
|
ValidAudience = jwtSettings.Audience,
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSettings.SecretKey)),
|
|
ClockSkew = TimeSpan.Zero
|
|
};
|
|
|
|
options.Events = new JwtBearerEvents
|
|
{
|
|
OnAuthenticationFailed = context =>
|
|
{
|
|
if (context.Exception.GetType() == typeof(SecurityTokenExpiredException))
|
|
{
|
|
context.Response.Headers.Append("Token-Expired", "true");
|
|
}
|
|
return Task.CompletedTask;
|
|
}
|
|
};
|
|
});
|
|
|
|
var app = builder.Build();
|
|
|
|
// Configure the HTTP request pipeline
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI();
|
|
}
|
|
|
|
app.UseCors("AllowFrontend");
|
|
|
|
// Add WebSocket support
|
|
app.UseWebSockets(new WebSocketOptions
|
|
{
|
|
KeepAliveInterval = TimeSpan.FromSeconds(30)
|
|
});
|
|
|
|
// Add global exception handler
|
|
app.UseGlobalExceptionHandler();
|
|
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
|
|
app.MapControllers();
|
|
app.MapHub<StreamSignalingHub>("/hubs/stream-signaling");
|
|
|
|
// Ensure database is created
|
|
using (var scope = app.Services.CreateScope())
|
|
{
|
|
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
|
|
|
try
|
|
{
|
|
// Apply migrations
|
|
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 { }
|
|
}
|
|
}
|
|
|
|
Console.WriteLine($"✅ 数据库连接成功: {databaseProvider}");
|
|
|
|
// 确保 AgentDevices_new 表存在
|
|
try
|
|
{
|
|
db.Database.ExecuteSqlRaw(@"
|
|
CREATE TABLE IF NOT EXISTS `AgentDevices_new` (
|
|
`Uuid` VARCHAR(36) NOT NULL PRIMARY KEY,
|
|
`Hostname` VARCHAR(100) DEFAULT '',
|
|
`IpAddress` VARCHAR(45) DEFAULT '',
|
|
`MacAddress` VARCHAR(17) DEFAULT '',
|
|
`SubnetMask` VARCHAR(15) DEFAULT '',
|
|
`Gateway` VARCHAR(45) DEFAULT '',
|
|
`OsName` VARCHAR(200) DEFAULT '',
|
|
`OsVersion` VARCHAR(100) DEFAULT '',
|
|
`OsArchitecture` VARCHAR(20) DEFAULT '',
|
|
`CpuName` VARCHAR(200) DEFAULT '',
|
|
`TotalMemoryMB` BIGINT DEFAULT 0,
|
|
`Manufacturer` VARCHAR(100) DEFAULT '',
|
|
`Model` VARCHAR(100) DEFAULT '',
|
|
`SerialNumber` VARCHAR(100) DEFAULT '',
|
|
`CurrentUser` VARCHAR(100) DEFAULT '',
|
|
`UserDomain` VARCHAR(100) DEFAULT '',
|
|
`BootTime` DATETIME NULL,
|
|
`LastReportAt` DATETIME NULL,
|
|
`IsOnline` TINYINT(1) DEFAULT 0,
|
|
`CreatedAt` DATETIME NOT NULL,
|
|
INDEX `IX_AgentDevices_new_IpAddress` (`IpAddress`)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
|
");
|
|
Console.WriteLine("✅ AgentDevices_new 表已就绪");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"⚠️ 创建 AgentDevices_new 表: {ex.Message}");
|
|
}
|
|
|
|
// 初始化种子数据
|
|
await DbSeeder.SeedAsync(db);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"❌ 数据库初始化失败: {ex.Message}");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
app.Run();
|