using Aip.Api.Service.Configurations; using Microsoft.AspNetCore.DataProtection.KeyManagement; using Newtonsoft.Json.Linq; using Serilog; using System.Runtime.InteropServices; using System.Text; namespace Aip.Api.Service; public class AipApiManager { [DllImport("AipApiManager.dll", CallingConvention = CallingConvention.StdCall)] public static extern int AipApiDecrypt([In] string ADecMessage, [Out] StringBuilder AEncMessage); public static AipSettings? LocalValidateLicense(IConfiguration configuration) { try { // 현재 실행 위치의 한 단계 위 경로를 얻어옵니다. string currDirectory = Directory.GetCurrentDirectory(); string licenseIniFile = Path.Combine(currDirectory, "License", "AipApiLicense.ini"); //Log.Information("LicenseFilePath: {0}, {1}", parentDirectory, licenseFilePath); if (!File.Exists(licenseIniFile)) { Log.Error("AIP API 라이센스 정보를 찾을 수 없습니다."); return null; } Dictionary> iniData = ReadIniFile(licenseIniFile); string ApplicationName = iniData["License"]["ApplicationName"].Trim(); string ApplicationVersion = iniData["License"]["ApplicationVersion"].Trim(); string Domain = iniData["License"]["Domain"].Trim(); string MipDataPath = iniData["License"]["MipDataPath"].Trim(); string TenantId = iniData["License"]["TenantId"].Trim(); string ClientId = iniData["License"]["ClientId"].Trim(); string EMail = iniData["License"]["EMail"].Trim(); string LoginType = iniData["License"]["LoginType"].Trim(); string SecretValue = iniData["License"]["SecretValue"].Trim(); string Thumbprint = iniData["License"]["Thumbprint"].Trim(); Log.Information(" ApplicationName: {0}", ApplicationName); Log.Information("ApplicationVersion: {0}", ApplicationVersion); Log.Information(" Domain: {0}", Domain); Log.Information(" MipDataPath: {0}", MipDataPath); Log.Information(" TenantId: {0}", TenantId); Log.Information(" ClientId: {0}", ClientId); Log.Information(" EMail: {0}", EMail); Log.Information(" LoginType: {0}", LoginType); Log.Information(" SecretValue: {0}", SecretValue); Log.Information(" Thumbprint: {0}", Thumbprint); int decResult; string DecTenantId = string.Empty; string DecClientId = string.Empty; string DecEMail = string.Empty; string DecSecretValue = string.Empty; string DecThumbprint = string.Empty; StringBuilder sDecTenantId = new StringBuilder(2048); StringBuilder sDecClientId = new StringBuilder(2048); StringBuilder sDecEMail = new StringBuilder(2048); StringBuilder sDecSecretValue = new StringBuilder(2048); StringBuilder sDecThumbprint = new StringBuilder(2048); decResult = AipApiDecrypt(TenantId, sDecTenantId); if (decResult == 0) { DecTenantId = sDecTenantId.ToString(); } decResult = AipApiDecrypt(ClientId, sDecClientId); if (decResult == 0) { DecClientId = sDecClientId.ToString(); } decResult = AipApiDecrypt(EMail, sDecEMail); if (decResult == 0) { DecEMail = sDecEMail.ToString(); } decResult = AipApiDecrypt(SecretValue, sDecSecretValue); if (decResult == 0) { DecSecretValue = sDecSecretValue.ToString(); } decResult = AipApiDecrypt(Thumbprint, sDecThumbprint); if (decResult == 0) { DecThumbprint = sDecThumbprint.ToString(); } //Log.Information("=================================="); //Log.Information(" DecTenantId: {0}", DecTenantId); //Log.Information(" DecClientId: {0}", DecClientId); //Log.Information(" DecEMail: {0}", DecEMail); //Log.Information(" DecSecretValue: {0}", DecSecretValue); //Log.Information(" DecThumbprint: {0}", DecThumbprint); //Log.Information("=================================="); AipSettings aipSettings = new AipSettings(); aipSettings.ProcessId = 0; aipSettings.Port = 0; aipSettings.Bindings = 0; aipSettings.AipPort = 0; aipSettings.AipBindings = 0; aipSettings.AppName = ApplicationName; // 어플리케이션 이름 aipSettings.AppVersion = ApplicationVersion; // 어플리케이션 버전 aipSettings.ClientId = DecClientId; // 어플리케이션 ID(클라이언트 ID) aipSettings.TenantId = DecTenantId; // 디렉토리(테넌트) ID aipSettings.MipData = MipDataPath; // Mip Data Path aipSettings.Domain = Domain; // Domain aipSettings.EMail = DecEMail; // User E-Mail aipSettings.SecretValue = DecSecretValue; // 인증 비밀번호 aipSettings.CertThumbPrint = DecThumbprint; // 인증서 지문 aipSettings.LoginType = LoginType; // 로그인 유형 // appsettings.json 설정정보 로딩 if (!int.TryParse(configuration["Id"], out int aipServerId)) { aipServerId = 1; } aipSettings.AipServerId = aipServerId; if (!int.TryParse(configuration["Port"], out int port)) { port = 9871; } aipSettings.Port = port; if (!int.TryParse(configuration["AipPort"], out int aipPort)) { aipPort = 7000; } aipSettings.AipPort = aipPort; int coreCount = Environment.ProcessorCount; if (!int.TryParse(configuration["AipBindings"], out int aipBindings)) { aipBindings = (coreCount * 2); } aipSettings.AipBindings = aipBindings; string? sourceFilePath = configuration["SourceFilePath"]; if (sourceFilePath == null) { sourceFilePath = "c:\\Data\\Source\\"; } aipSettings.SourceFileDir = sourceFilePath; string? targetFilePath = configuration["TargetFilePath"]; if (targetFilePath == null) { targetFilePath = "c:\\Data\\Target\\"; } aipSettings.TargetFileDir = targetFilePath; aipSettings.SupportedFileExt = ".doc;.docx;.docm;.dot;.dotm;.dotx;.xls;.xlt;.xlsx;.xltx;.xltm;.xlsm;.xlsb;.ppt;.pps;.pot;.pptx;.ppsx;.pptm;.ppsm;.potx;.potm;.pdf;.txt;.xml;.jpg;.jpeg;.png;.tif;.tiff;.bmp;.gif;.jpe;.jfif;.jt;"; aipSettings.ProtectedFileExt = ".txt-.ptxt;.xml-.pxml;.jpg-.pjpg;.jpeg-.pjpeg;.png-.ppng;.tif-.ptif;.tiff-.ptiff;.bmp-.pbmp;.gif-.pgif;.jpe-.pjpe;.jfif-.pjfif;.jt-.pjt;"; aipSettings.SetLabelId = "1098bdb0-f2bc-43d3-b9c7-e610431fc1a4"; aipSettings.DeleteLabelId = "878173ae-cc36-4881-af57-604af868314c"; aipSettings.SetTemplateId = "3c597cfe-ec81-4c18-add7-3867f2d3b4d4"; aipSettings.DeleteTemplateId = "0166b75f-6a93-47f3-8fd3-e1e7c59141ab"; return aipSettings; } catch (Exception ex) { Log.Error("AIP API 라이센스 파일 검증 오류..."); Log.Error("AIP API 라이센스 파일 검증 오류", ex); return null; } } private static Dictionary> ReadIniFile(string filePath) { var iniData = new Dictionary>(); string currentSection = null; foreach (string line in File.ReadLines(filePath)) { if (line.StartsWith("[") && line.EndsWith("]")) { currentSection = line.Trim('[', ']'); iniData[currentSection] = new Dictionary(); } else if (!string.IsNullOrWhiteSpace(currentSection)) { string[] parts = line.Split('='); if (parts.Length == 2) { string key = parts[0].Trim(); string value = parts[1].Trim(); iniData[currentSection][key] = value; } } } return iniData; } private static void WriteIniFile(string filePath, Dictionary> iniData) { using (var writer = new StreamWriter(filePath)) { foreach (var section in iniData) { writer.WriteLine($"[{section.Key}]"); foreach (var kvp in section.Value) { writer.WriteLine($"{kvp.Key}={kvp.Value}"); } } } } }