AipApiManager.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using Aip.Api.Service.Configurations;
  2. using Microsoft.AspNetCore.DataProtection.KeyManagement;
  3. using Newtonsoft.Json.Linq;
  4. using Serilog;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. namespace Aip.Api.Service;
  8. public class AipApiManager
  9. {
  10. [DllImport("AipApiManager.dll", CallingConvention = CallingConvention.StdCall)]
  11. public static extern int AipApiDecrypt([In] string ADecMessage, [Out] StringBuilder AEncMessage);
  12. public static AipSettings? LocalValidateLicense(IConfiguration configuration)
  13. {
  14. try
  15. {
  16. // 현재 실행 위치의 한 단계 위 경로를 얻어옵니다.
  17. string currDirectory = Directory.GetCurrentDirectory();
  18. string licenseIniFile = Path.Combine(currDirectory, "License", "AipApiLicense.ini");
  19. //Log.Information("LicenseFilePath: {0}, {1}", parentDirectory, licenseFilePath);
  20. if (!File.Exists(licenseIniFile))
  21. {
  22. Log.Error("AIP API 라이센스 정보를 찾을 수 없습니다.");
  23. return null;
  24. }
  25. Dictionary<string, Dictionary<string, string>> iniData = ReadIniFile(licenseIniFile);
  26. string ApplicationName = iniData["License"]["ApplicationName"].Trim();
  27. string ApplicationVersion = iniData["License"]["ApplicationVersion"].Trim();
  28. string Domain = iniData["License"]["Domain"].Trim();
  29. string MipDataPath = iniData["License"]["MipDataPath"].Trim();
  30. string TenantId = iniData["License"]["TenantId"].Trim();
  31. string ClientId = iniData["License"]["ClientId"].Trim();
  32. string EMail = iniData["License"]["EMail"].Trim();
  33. string LoginType = iniData["License"]["LoginType"].Trim();
  34. string SecretValue = iniData["License"]["SecretValue"].Trim();
  35. string Thumbprint = iniData["License"]["Thumbprint"].Trim();
  36. Log.Information(" ApplicationName: {0}", ApplicationName);
  37. Log.Information("ApplicationVersion: {0}", ApplicationVersion);
  38. Log.Information(" Domain: {0}", Domain);
  39. Log.Information(" MipDataPath: {0}", MipDataPath);
  40. Log.Information(" TenantId: {0}", TenantId);
  41. Log.Information(" ClientId: {0}", ClientId);
  42. Log.Information(" EMail: {0}", EMail);
  43. Log.Information(" LoginType: {0}", LoginType);
  44. Log.Information(" SecretValue: {0}", SecretValue);
  45. Log.Information(" Thumbprint: {0}", Thumbprint);
  46. int decResult;
  47. string DecTenantId = string.Empty;
  48. string DecClientId = string.Empty;
  49. string DecEMail = string.Empty;
  50. string DecSecretValue = string.Empty;
  51. string DecThumbprint = string.Empty;
  52. StringBuilder sDecTenantId = new StringBuilder(2048);
  53. StringBuilder sDecClientId = new StringBuilder(2048);
  54. StringBuilder sDecEMail = new StringBuilder(2048);
  55. StringBuilder sDecSecretValue = new StringBuilder(2048);
  56. StringBuilder sDecThumbprint = new StringBuilder(2048);
  57. decResult = AipApiDecrypt(TenantId, sDecTenantId);
  58. if (decResult == 0)
  59. {
  60. DecTenantId = sDecTenantId.ToString();
  61. }
  62. decResult = AipApiDecrypt(ClientId, sDecClientId);
  63. if (decResult == 0)
  64. {
  65. DecClientId = sDecClientId.ToString();
  66. }
  67. decResult = AipApiDecrypt(EMail, sDecEMail);
  68. if (decResult == 0)
  69. {
  70. DecEMail = sDecEMail.ToString();
  71. }
  72. decResult = AipApiDecrypt(SecretValue, sDecSecretValue);
  73. if (decResult == 0)
  74. {
  75. DecSecretValue = sDecSecretValue.ToString();
  76. }
  77. decResult = AipApiDecrypt(Thumbprint, sDecThumbprint);
  78. if (decResult == 0)
  79. {
  80. DecThumbprint = sDecThumbprint.ToString();
  81. }
  82. //Log.Information("==================================");
  83. //Log.Information(" DecTenantId: {0}", DecTenantId);
  84. //Log.Information(" DecClientId: {0}", DecClientId);
  85. //Log.Information(" DecEMail: {0}", DecEMail);
  86. //Log.Information(" DecSecretValue: {0}", DecSecretValue);
  87. //Log.Information(" DecThumbprint: {0}", DecThumbprint);
  88. //Log.Information("==================================");
  89. AipSettings aipSettings = new AipSettings();
  90. aipSettings.ProcessId = 0;
  91. aipSettings.Port = 0;
  92. aipSettings.Bindings = 0;
  93. aipSettings.AipPort = 0;
  94. aipSettings.AipBindings = 0;
  95. aipSettings.AppName = ApplicationName; // 어플리케이션 이름
  96. aipSettings.AppVersion = ApplicationVersion; // 어플리케이션 버전
  97. aipSettings.ClientId = DecClientId; // 어플리케이션 ID(클라이언트 ID)
  98. aipSettings.TenantId = DecTenantId; // 디렉토리(테넌트) ID
  99. aipSettings.MipData = MipDataPath; // Mip Data Path
  100. aipSettings.Domain = Domain; // Domain
  101. aipSettings.EMail = DecEMail; // User E-Mail
  102. aipSettings.SecretValue = DecSecretValue; // 인증 비밀번호
  103. aipSettings.CertThumbPrint = DecThumbprint; // 인증서 지문
  104. aipSettings.LoginType = LoginType; // 로그인 유형
  105. // appsettings.json 설정정보 로딩
  106. if (!int.TryParse(configuration["Id"], out int aipServerId))
  107. {
  108. aipServerId = 1;
  109. }
  110. aipSettings.AipServerId = aipServerId;
  111. if (!int.TryParse(configuration["Port"], out int port))
  112. {
  113. port = 9871;
  114. }
  115. aipSettings.Port = port;
  116. if (!int.TryParse(configuration["AipPort"], out int aipPort))
  117. {
  118. aipPort = 7000;
  119. }
  120. aipSettings.AipPort = aipPort;
  121. int coreCount = Environment.ProcessorCount;
  122. if (!int.TryParse(configuration["AipBindings"], out int aipBindings))
  123. {
  124. aipBindings = (coreCount * 2);
  125. }
  126. aipSettings.AipBindings = aipBindings;
  127. string? sourceFilePath = configuration["SourceFilePath"];
  128. if (sourceFilePath == null)
  129. {
  130. sourceFilePath = "c:\\Data\\Source\\";
  131. }
  132. aipSettings.SourceFileDir = sourceFilePath;
  133. string? targetFilePath = configuration["TargetFilePath"];
  134. if (targetFilePath == null)
  135. {
  136. targetFilePath = "c:\\Data\\Target\\";
  137. }
  138. aipSettings.TargetFileDir = targetFilePath;
  139. 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;";
  140. 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;";
  141. aipSettings.SetLabelId = "1098bdb0-f2bc-43d3-b9c7-e610431fc1a4";
  142. aipSettings.DeleteLabelId = "878173ae-cc36-4881-af57-604af868314c";
  143. aipSettings.SetTemplateId = "3c597cfe-ec81-4c18-add7-3867f2d3b4d4";
  144. aipSettings.DeleteTemplateId = "0166b75f-6a93-47f3-8fd3-e1e7c59141ab";
  145. return aipSettings;
  146. }
  147. catch (Exception ex)
  148. {
  149. Log.Error("AIP API 라이센스 파일 검증 오류...");
  150. Log.Error("AIP API 라이센스 파일 검증 오류", ex);
  151. return null;
  152. }
  153. }
  154. private static Dictionary<string, Dictionary<string, string>> ReadIniFile(string filePath)
  155. {
  156. var iniData = new Dictionary<string, Dictionary<string, string>>();
  157. string currentSection = null;
  158. foreach (string line in File.ReadLines(filePath))
  159. {
  160. if (line.StartsWith("[") && line.EndsWith("]"))
  161. {
  162. currentSection = line.Trim('[', ']');
  163. iniData[currentSection] = new Dictionary<string, string>();
  164. }
  165. else if (!string.IsNullOrWhiteSpace(currentSection))
  166. {
  167. string[] parts = line.Split('=');
  168. if (parts.Length == 2)
  169. {
  170. string key = parts[0].Trim();
  171. string value = parts[1].Trim();
  172. iniData[currentSection][key] = value;
  173. }
  174. }
  175. }
  176. return iniData;
  177. }
  178. private static void WriteIniFile(string filePath, Dictionary<string, Dictionary<string, string>> iniData)
  179. {
  180. using (var writer = new StreamWriter(filePath))
  181. {
  182. foreach (var section in iniData)
  183. {
  184. writer.WriteLine($"[{section.Key}]");
  185. foreach (var kvp in section.Value)
  186. {
  187. writer.WriteLine($"{kvp.Key}={kvp.Value}");
  188. }
  189. }
  190. }
  191. }
  192. }