Files
SystemX.Web/Projects/WebApi/WebApi.Project.ProxyKMS/Services/KmsService.cs

148 lines
6.3 KiB
C#

using System;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Security.Cryptography.X509Certificates;
using SystemX.Core.DB;
using SystemX.Core.Services;
using WebApi.Library.Enums;
using WebApi.Project.ProxyKMS.Models;
namespace WebApi.Project.ProxyKMS.Services
{
public class KmsService : HttpService
{
private readonly IServiceScopeFactory _scopeFactory;
private readonly ConfigService<ProxyKMSConfig>? _configService;
private API KmsApi = new API();
public KmsService(IServiceProvider serviceProvider, IServiceScopeFactory scopeFactory, ConfigService<ProxyKMSConfig> configSerice)
{
_scopeFactory = scopeFactory;
_configService = configSerice;
KmsApi = _configService.GetConfig().API.Find(x=>x.ApiName.ToLower() == "kms");
}
public async Task<RESPONSE?> PostKms<REQUEST,RESPONSE>(REQUEST request, string guid = "", [CallerMemberName] string memberName = "") where REQUEST : class where RESPONSE : class
{
RESPONSE? response = default(RESPONSE);
LogXnet.WriteLine($"KmsService.PostKms::{memberName}", LogXLabel.Debug);
if (request != null)
{
var function = KmsApi.Functions.Find(x => x.Name == memberName);
response = await PostJsonAsync<REQUEST, RESPONSE>($"{function.Url}", request);
}
return response;
}
public async Task<RESPONSE?> PutKms<REQUEST, RESPONSE>(REQUEST request, string guid = "", [CallerMemberName] string memberName = "") where REQUEST : class where RESPONSE : class
{
RESPONSE? response = default(RESPONSE);
LogXnet.WriteLine($"KmsService.PutKms::{memberName}", LogXLabel.Debug);
if (request != null)
{
var function = KmsApi.Functions.Find(x => x.Name == memberName);
response = await PutJsonAsync<REQUEST, RESPONSE>($"{function.Url}", request);
}
return response;
}
public override async Task<RESPONSE?> PostJsonAsync<REQUEST, RESPONSE>(string url, REQUEST request, short timeOutSeconds = 5) where REQUEST : class where RESPONSE : class
{
RESPONSE response = null;
Guid guid = Guid.NewGuid();
using (HttpClient httpClient = new HttpClient(GetClientHandler()))
{
try
{
short timeOutSec = SetTimeout(timeOutSeconds);
httpClient.Timeout = new TimeSpan(0, 0, timeOutSec);
httpClient.BaseAddress = new Uri(url ?? "");
LogXnet.WriteLine($"[POST] Request({guid})::{url}{Environment.NewLine}{request?.ToJson()}", LogXLabel.HTTP);
DateTime requestTime = DateTime.Now;
response = await (await httpClient.PostAsJsonAsync(url, request)).Content.ReadFromJsonAsync<RESPONSE>();
LogXnet.WriteLine($"[POST] Rseponse({guid}) ({(DateTime.Now - requestTime).TotalSeconds} sec)::{url}{Environment.NewLine}{response?.ToJson()}", LogXLabel.HTTP);
}
catch (Exception e)
{
LogXnet.WriteLine(e);
LogXnet.WriteLine(e?.InnerException?.InnerException?.Message, LogXLabel.Exception);
}
}
return response;
}
public virtual async Task<RESPONSE?> PutJsonAsync<REQUEST, RESPONSE>(string url, REQUEST request, short timeOutSeconds = 5) where REQUEST : class where RESPONSE : class
{
RESPONSE? response = default(RESPONSE);
Guid guid = Guid.NewGuid();
using (HttpClient httpClient = new HttpClient(GetClientHandler()))
{
try
{
var timeOutSec = SetTimeout(timeOutSeconds);
httpClient.Timeout = new TimeSpan(0, 0, timeOutSec);
httpClient.BaseAddress = new Uri($"{url}");
LogXnet.WriteLine($"[PUT] Request({guid})::{url}{Environment.NewLine}{request?.ToJson()}", LogXLabel.HTTP);
DateTime requestTime = DateTime.Now;
var res = await httpClient.PutAsJsonAsync(url, request);
response = await res.Content.ReadFromJsonAsync<RESPONSE>();
LogXnet.WriteLine($"[PUT] Rseponse({guid}) ({(DateTime.Now - requestTime).TotalSeconds} sec)::{url}{Environment.NewLine}{response?.ToJson()}", LogXLabel.HTTP);
}
catch (Exception e)
{
LogXnet.WriteLine(e);
LogXnet.WriteLine(e?.InnerException?.InnerException?.Message, LogXLabel.Exception);
}
}
return response;
}
protected override HttpClientHandler GetClientHandler()
{
HttpClientHandler clientHandler = new HttpClientHandler();
//인증서가 경로에 있으면
if (File.Exists(KmsApi.CertPemPath) == true && File.Exists(KmsApi.CertKeyPath) == true)
{
Console.WriteLine($"Cert.Pem Path:{KmsApi.CertPemPath}", LogXLabel.Debug);
Console.WriteLine($"Cert.Key Path:{KmsApi.CertKeyPath}", LogXLabel.Debug);
var cert = X509Certificate2.CreateFromPemFile(KmsApi.CertPemPath, KmsApi.CertKeyPath);
cert = new X509Certificate2(cert.Export(X509ContentType.Pkcs12));
clientHandler.ClientCertificates.Add(cert);
}
else
{
Console.WriteLine($"File not exist. Cert.Pem Path:{KmsApi.CertPemPath}", LogXLabel.Warning);
Console.WriteLine($"File not exist. Cert.Key Path:{KmsApi.CertKeyPath}", LogXLabel.Warning);
}
//ssl 인증서 무시
Console.WriteLine($"CertificateVerify:{KmsApi.CertificateVerify}", LogXLabel.Debug);
if (KmsApi.CertificateVerify == false)
{
clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, sslPolicyErrors) =>
{
return true;
};
}
return clientHandler;
}
}
}