using Admin.NET.Plugin.AiDOP.Entity.S8; using System.Text.RegularExpressions; namespace Admin.NET.Plugin.AiDOP.Service.S8; public class S8DataSourceService : ITransient { private readonly SqlSugarRepository _rep; public S8DataSourceService(SqlSugarRepository rep) => _rep = rep; public async Task> ListAsync(long tenantId, long factoryId) { var rows = await _rep.AsQueryable() .Where(x => x.TenantId == tenantId && x.FactoryId == factoryId) .ToListAsync(); foreach (var r in rows) r.Endpoint = MaskSecret(r.Endpoint); return rows; } public async Task CreateAsync(AdoS8DataSource body) { if (string.IsNullOrWhiteSpace(body.DataSourceCode) || string.IsNullOrWhiteSpace(body.Type)) throw new S8BizException("数据源编码和类型必填"); var exists = await _rep.AsQueryable() .AnyAsync(x => x.TenantId == body.TenantId && x.FactoryId == body.FactoryId && x.DataSourceCode == body.DataSourceCode); if (exists) throw new S8BizException("数据源编码已存在"); body.Id = 0; body.CreatedAt = DateTime.Now; await _rep.InsertAsync(body); body.Endpoint = MaskSecret(body.Endpoint); return body; } public async Task UpdateAsync(long id, AdoS8DataSource body) { var e = await _rep.GetByIdAsync(id) ?? throw new S8BizException("记录不存在"); if (string.IsNullOrWhiteSpace(body.DataSourceCode) || string.IsNullOrWhiteSpace(body.Type)) throw new S8BizException("数据源编码和类型必填"); var exists = await _rep.AsQueryable() .AnyAsync(x => x.Id != id && x.TenantId == body.TenantId && x.FactoryId == body.FactoryId && x.DataSourceCode == body.DataSourceCode); if (exists) throw new S8BizException("数据源编码已存在"); // 入参 endpoint 含掩码占位符(Pwd=****** / Password=******)时保留旧值的真实密码段,避免前端 // 回填脱敏值后误覆盖。Endpoint 全空时也不覆盖原密码。 body.Endpoint = MergeEndpointPreservingSecret(body.Endpoint, e.Endpoint); body.Id = id; body.CreatedAt = e.CreatedAt; body.UpdatedAt = DateTime.Now; await _rep.UpdateAsync(body); body.Endpoint = MaskSecret(body.Endpoint); return body; } public async Task DeleteAsync(long id) => await _rep.DeleteByIdAsync(id); public async Task TestAsync(long id) { var entity = await _rep.GetByIdAsync(id) ?? throw new S8BizException("记录不存在"); var success = !string.IsNullOrWhiteSpace(entity.Endpoint); entity.LastCheckAt = DateTime.Now; entity.LastCheckStatus = success ? "SUCCESS" : "FAILED: endpoint is empty"; entity.UpdatedAt = DateTime.Now; await _rep.UpdateAsync(entity); return new { id, success, message = success ? "连接信息校验通过" : "连接地址为空,未通过校验", entity.LastCheckAt, entity.LastCheckStatus }; } // BUG-13:endpoint 中的 Pwd=xxx / Password=xxx(大小写不敏感)替换为 ******,保留其它字段。 private static readonly Regex SecretPattern = new( @"(?i)(Pwd|Password)\s*=\s*([^;]*)", RegexOptions.Compiled); private static string? MaskSecret(string? endpoint) { if (string.IsNullOrWhiteSpace(endpoint)) return endpoint; return SecretPattern.Replace(endpoint, m => $"{m.Groups[1].Value}=******"); } private static string? MergeEndpointPreservingSecret(string? incoming, string? existing) { if (string.IsNullOrWhiteSpace(incoming)) return existing; if (string.IsNullOrWhiteSpace(existing)) return incoming; // 提取旧 endpoint 中的真实密码值(首个匹配为准) var oldMatch = SecretPattern.Match(existing); if (!oldMatch.Success) return incoming; var realSecret = oldMatch.Groups[2].Value; // 把入参里 Pwd=****** 之类的占位还原为真实密码 return SecretPattern.Replace(incoming, m => { var v = m.Groups[2].Value; return IsMaskedPlaceholder(v) ? $"{m.Groups[1].Value}={realSecret}" : m.Value; }); } private static bool IsMaskedPlaceholder(string? v) => !string.IsNullOrEmpty(v) && v.All(c => c == '*'); }