[성현모] KeficoMailService 추가, KMS 인증서 컨피그 추가
This commit is contained in:
@ -12,6 +12,10 @@ namespace WebApi.Project.ProxyKMS.Models
|
||||
public string ApiName { get; set; } = string.Empty;
|
||||
|
||||
public List<Function> Functions { get; set; } = new List<Function>();
|
||||
|
||||
public bool CertificateVerify { get; set; } = false;
|
||||
public string CertPemPath { get; set; } = string.Empty;
|
||||
public string CertKeyPath { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public class Function
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
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;
|
||||
@ -51,5 +53,95 @@ namespace WebApi.Project.ProxyKMS.Services
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user