洁 任 2 days ago
commit 0c85e8fd9e

@ -0,0 +1,187 @@
namespace Infrastructure.Auth;
using System;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
/// <summary>
/// 授权信息模型
/// </summary>
public class LicenseInfo
{
/// <summary>
/// 授权用户
/// </summary>
public string UserName { get; set; }
/// <summary>
/// 授权开始时间
/// </summary>
public DateTime StartTime { get; set; }
/// <summary>
/// 授权结束时间永久授权可设为DateTime.MaxValue
/// </summary>
public DateTime EndTime { get; set; }
/// <summary>
/// 授权类型(试用/正式/永久)
/// </summary>
public LicenseType LicenseType { get; set; }
/// <summary>
/// 硬件绑定信息(可选,用于防止多设备使用)
/// </summary>
public string HardwareId { get; set; }
}
/// <summary>
/// 授权类型
/// </summary>
public enum LicenseType
{
Trial, // 试用授权
Subscription, // 订阅授权
Permanent // 永久授权
}
/// <summary>
/// 授权管理核心类
/// </summary>
public class LicenseManager
{
// 加密密钥实际使用中建议替换为自己的密钥长度需为16/24/32字节
private readonly byte[] _key = Convert.FromBase64String("0TXis2sTDEnNAAe/6bYqG7xwapKQcXUlI+WwrMlKv5I=");
private readonly byte[] _iv = Convert.FromBase64String("UdDHCIRoqIn4shpJCGRUpg==");
/// <summary>
/// 生成授权文件
/// </summary>
/// <param name="info">授权信息</param>
/// <returns>加密后的授权字符串(可保存为文件)</returns>
public string GenerateLicense(LicenseInfo info)
{
// 序列化授权信息
string json = JsonSerializer.Serialize(info);
// 加密
return EncryptString(json);
}
/// <summary>
/// 验证授权有效性
/// </summary>
/// <param name="licenseString">加密的授权字符串</param>
/// <param name="errorMsg">验证失败时的错误信息</param>
/// <returns>是否有效</returns>
public bool ValidateLicense(string licenseString, out string errorMsg)
{
errorMsg = string.Empty;
try
{
// 解密授权信息
string json = DecryptString(licenseString);
var license = JsonSerializer.Deserialize<LicenseInfo>(json);
// 1. 检查硬件绑定(可选)
if (!string.IsNullOrEmpty(license.HardwareId) &&
license.HardwareId != GetHardwareId())
{
errorMsg = "授权与当前设备不匹配";
return false;
}
// 2. 检查时间有效性
DateTime now = DateTime.Now;
if (now < license.StartTime)
{
errorMsg = $"授权未生效,生效时间:{license.StartTime:yyyy-MM-dd}";
return false;
}
// 永久授权跳过结束时间检查
if (license.LicenseType != LicenseType.Permanent && now > license.EndTime)
{
errorMsg = $"授权已过期,过期时间:{license.EndTime:yyyy-MM-dd}";
return false;
}
return true;
}
catch (Exception ex)
{
errorMsg = $"授权验证失败:{ex.Message}";
return false;
}
}
/// <summary>
/// 获取授权剩余时间
/// </summary>
/// <param name="licenseString">加密的授权字符串</param>
/// <returns>剩余时间永久授权返回null</returns>
public TimeSpan? GetRemainingTime(string licenseString)
{
try
{
string json = DecryptString(licenseString);
var license = JsonSerializer.Deserialize<LicenseInfo>(json);
if (license.LicenseType == LicenseType.Permanent)
return null; // 永久授权无剩余时间
DateTime now = DateTime.Now;
if (now > license.EndTime)
return TimeSpan.Zero; // 已过期
return license.EndTime - now;
}
catch
{
return TimeSpan.Zero;
}
}
/// <summary>
/// 加密字符串
/// </summary>
private string EncryptString(string plainText)
{
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = _key;
aesAlg.IV = _iv;
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
byte[] encrypted = encryptor.TransformFinalBlock(Encoding.UTF8.GetBytes(plainText), 0, plainText.Length);
return Convert.ToBase64String(encrypted);
}
}
/// <summary>
/// 解密字符串
/// </summary>
private string DecryptString(string cipherText)
{
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = _key;
aesAlg.IV = _iv;
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
byte[] cipherBytes = Convert.FromBase64String(cipherText);
byte[] decrypted = decryptor.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length);
return Encoding.UTF8.GetString(decrypted);
}
}
/// <summary>
/// 获取硬件唯一标识(简化版,实际可增强)
/// </summary>
public string GetHardwareId()
{
// 实际应用中可结合CPU、硬盘、网卡等信息生成唯一ID
// 这里仅作为示例
return Environment.MachineName;
}
}

@ -35,6 +35,7 @@
<PackageReference Include="Quartz.Extensions.DependencyInjection" Version="3.6.2" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.0.1" />
<PackageReference Include="StackExchange.Redis" Version="2.6.111" />
<PackageReference Include="System.Management" Version="9.0.8" />
</ItemGroup>
<ItemGroup>

@ -653,7 +653,9 @@ public class ConfigSubscribe : IJob
}}";
_logger.LogDebug($"直播参数:{param}");
var topicRequest = $"thing/product/{sn}/services";
// 开启直播
await _mqttClientManager.PublishAsync(topicRequest, param);
// todo 关于直播是否开启成功
var req = new CallAiModel { TaskId = taskAssign1.TaskId, RtmpUrl = rtmp };
await _manageApp.CallAiModel(req);
}
@ -738,7 +740,8 @@ public class ConfigSubscribe : IJob
{
break;
}
// todo 已验证tid bid 是相同的
// todo 开启直播成功调用ai model
_logger.LogDebug($"开启直播成功 {message}");
break;
case "live_stop_push":

@ -1,4 +1,5 @@
using System.Reflection;
using System.ComponentModel;
using System.Reflection;
using Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
@ -7,6 +8,7 @@ using OpenAuth.App;
using OpenAuth.App.Interface;
using OpenAuth.Repository.Domain;
using OpenAuth.WebApi.Model.CustomAttribute;
using LicenseManager = Infrastructure.Auth.LicenseManager;
namespace OpenAuth.WebApi.Model
{
@ -47,6 +49,32 @@ namespace OpenAuth.WebApi.Model
return;
}
var licensePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "license.lic");
if (File.Exists(licensePath))
{
var licenseManager = new LicenseManager();
if (!licenseManager.ValidateLicense(System.IO.File.ReadAllText(licensePath), out var error))
{
context.HttpContext.Response.StatusCode = 401;
context.Result = new JsonResult(new Response
{
Code = 401,
Message = error
});
return;
}
}
else
{
context.HttpContext.Response.StatusCode = 401;
context.Result = new JsonResult(new Response
{
Code = 401,
Message = "无授权文件"
});
return;
}
var dataAttr = description.MethodInfo.GetCustomAttribute<AllDataAttribute>();
if (dataAttr != null)
{
@ -56,9 +84,11 @@ namespace OpenAuth.WebApi.Model
/*ar paras = description.MethodInfo.GetParameters();*/
var paras = context.HttpContext.Request.Query;
var routeValues = new RouteValueDictionary(paras.ToDictionary(x => x.Key, x => (object)x.Value.ToString()));
var routeValues =
new RouteValueDictionary(paras.ToDictionary(x => x.Key, x => (object)x.Value.ToString()));
context.Result = new RedirectToActionResult(dataAttr.ActionName, dataAttr.ControllerName, routeValues);
context.Result =
new RedirectToActionResult(dataAttr.ActionName, dataAttr.ControllerName, routeValues);
//return;
}
}
@ -80,4 +110,4 @@ namespace OpenAuth.WebApi.Model
return;
}
}
}
}
Loading…
Cancel
Save