Skip to content

Commit

Permalink
对服务的 meta 为空值的时候反序列化进行防御
Browse files Browse the repository at this point in the history
  • Loading branch information
kklldog committed Apr 16, 2022
1 parent f4b2d8e commit 7d7c3f3
Showing 1 changed file with 34 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Threading.Tasks;
using Agile.Config.Protocol;
using AgileConfig.Server.Common;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

namespace AgileConfig.Server.Apisite.Controllers.api
Expand All @@ -21,12 +22,16 @@ public class RegisterCenterController : Controller
{
private readonly IRegisterCenterService _registerCenterService;
private readonly IServiceInfoService _serviceInfoService;
private readonly ILogger<RegisterCenterController> _logger;

public RegisterCenterController(IRegisterCenterService registerCenterService
,IServiceInfoService serviceInfoService)
,IServiceInfoService serviceInfoService,
ILoggerFactory loggerFactory
)
{
_registerCenterService = registerCenterService;
_serviceInfoService = serviceInfoService;
_logger = loggerFactory.CreateLogger<RegisterCenterController>();
}

[HttpPost]
Expand All @@ -40,7 +45,7 @@ public async Task<RegisterResultVM> Register([FromBody]RegisterServiceInfoVM mod
entity.CheckUrl = model.CheckUrl;
entity.AlarmUrl = model.AlarmUrl;
entity.HeartBeatMode = model.HeartBeatMode;
entity.MetaData = JsonConvert.SerializeObject(model.MetaData);
entity.MetaData = model.MetaData is null ? "[]" : JsonConvert.SerializeObject(model.MetaData);
entity.RegisterWay = RegisterWay.Auto;

var id = await _registerCenterService.RegisterAsync(entity);
Expand Down Expand Up @@ -134,9 +139,17 @@ public async Task<List<ServiceInfoVM>> AllServices()
ServiceName = serviceInfo.ServiceName,
Ip = serviceInfo.Ip,
Port = serviceInfo.Port,
MetaData = JsonConvert.DeserializeObject<List<string>>(serviceInfo.MetaData),
MetaData = new List<string>(),
Status = serviceInfo.Status
};
try
{
vm.MetaData = JsonConvert.DeserializeObject<List<string>>(serviceInfo.MetaData);
}
catch (Exception e)
{
_logger.LogError(e, $"deserialize meta data error, serviceId:{serviceInfo.ServiceId}");
}
vms.Add(vm);
}

Expand All @@ -156,9 +169,17 @@ public async Task<List<ServiceInfoVM>> OnlineServices()
ServiceName = serviceInfo.ServiceName,
Ip = serviceInfo.Ip,
Port = serviceInfo.Port,
MetaData = JsonConvert.DeserializeObject<List<string>>(serviceInfo.MetaData),
MetaData = new List<string>(),
Status = serviceInfo.Status
};
try
{
vm.MetaData = JsonConvert.DeserializeObject<List<string>>(serviceInfo.MetaData);
}
catch (Exception e)
{
_logger.LogError(e, $"deserialize meta data error, serviceId:{serviceInfo.ServiceId}");
}
vms.Add(vm);
}

Expand All @@ -178,9 +199,17 @@ public async Task<List<ServiceInfoVM>> OfflineServices()
ServiceName = serviceInfo.ServiceName,
Ip = serviceInfo.Ip,
Port = serviceInfo.Port,
MetaData = JsonConvert.DeserializeObject<List<string>>(serviceInfo.MetaData),
MetaData = new List<string>(),
Status = serviceInfo.Status
};
try
{
vm.MetaData = JsonConvert.DeserializeObject<List<string>>(serviceInfo.MetaData);
}
catch (Exception e)
{
_logger.LogError(e, $"deserialize meta data error, serviceId:{serviceInfo.ServiceId}");
}
vms.Add(vm);
}

Expand Down

0 comments on commit 7d7c3f3

Please sign in to comment.