ContainerService.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using AipGateway.AIP;
  2. using AipGateway.API.Application.Modules;
  3. using AipGateway.API.Configurations;
  4. using AipGateway.API.Domain.Common.Exceptions;
  5. using AipGateway.API.Models;
  6. using System.Collections;
  7. namespace AipGateway.API.Services
  8. {
  9. public static class ContainerService
  10. {
  11. public static int Port = 5000;
  12. public static ServiceProvider provider = null;
  13. public static AipFileManager aipFileManager = null;
  14. public static AipSettings aipConfig = null;
  15. public static Hashtable aipLableMap = new Hashtable();
  16. public static Hashtable aipPolicyMap = new Hashtable();
  17. public static Hashtable aipProtectionMap = new Hashtable();
  18. public static Hashtable apiKeyMap = new Hashtable();
  19. public static Hashtable decryptKeyMap = new Hashtable();
  20. public static Hashtable apiIpAddrMap = new Hashtable();
  21. public static Hashtable decryptIpAddrMap = new Hashtable();
  22. public static Hashtable supportedFileExtMap = new Hashtable();
  23. public static Hashtable protectedFileExtMap = new Hashtable();
  24. public static Hashtable supportedDelFileExtMap = new Hashtable();
  25. public static Hashtable protectedDelFileExtMap = new Hashtable();
  26. public static string GetFileData(string fileName)
  27. {
  28. string result = string.Empty;
  29. try
  30. {
  31. if (File.Exists(fileName))
  32. {
  33. byte[] fileBytes = File.ReadAllBytes(fileName);
  34. result = Convert.ToBase64String(fileBytes);
  35. #if false
  36. using (FileStream fs = System.IO.File.OpenRead(fileName))
  37. {
  38. using (var reader = new BinaryReader(fs))
  39. {
  40. byte[] fileBytes = reader.ReadBytes((int)fs.Length);
  41. result = Convert.ToBase64String(fileBytes);
  42. }
  43. }
  44. #endif
  45. }
  46. }
  47. catch (Exception)
  48. {
  49. return string.Empty;
  50. }
  51. return result;
  52. }
  53. public static void DeleteFile(string fileName)
  54. {
  55. try
  56. {
  57. if (File.Exists(fileName))
  58. {
  59. File.Delete(fileName);
  60. }
  61. }
  62. catch (Exception) { }
  63. }
  64. public static string? GetSupportedFileType(string fileName)
  65. {
  66. string fileExt = Path.GetExtension(fileName).ToLower();
  67. return ContainerService.supportedFileExtMap[fileExt] as string;
  68. }
  69. public static int ValidationApiAuthorization(HttpContext httpContext, string key)
  70. {
  71. string errMsg;
  72. string? ipAddress = httpContext.Connection.RemoteIpAddress?.ToString();
  73. ipAddress = string.IsNullOrEmpty(ipAddress) ? "xx.xx.xx.xx" : ipAddress;
  74. try
  75. {
  76. if (apiKeyMap.ContainsKey(key))
  77. {
  78. LinkedApiKey? apiKey = ContainerService.apiKeyMap[key] as LinkedApiKey;
  79. if (apiKey == null)
  80. {
  81. return 1; // 등록되어 있지 않은 키 정보입니다.
  82. }
  83. if (DateTime.Compare(apiKey.ExpiredAt, DateTime.Now) < 0)
  84. {
  85. return 2; // 등록된 키의 유효기간이 지났습니다.
  86. }
  87. if (apiKey.serverMap.Contains(ipAddress))
  88. {
  89. errMsg = "성공.";
  90. return 0; // 유효한 키값과 IP Address
  91. }
  92. errMsg = "등록되어 있지 않은 IP Address에서 호출된 API KEY 입니다.";
  93. return 3; // 유효한 키값이나 등록되지 않은 IP Address
  94. }
  95. else
  96. {
  97. errMsg = "등록되어 있지 않은 API KEY 입니다.";
  98. return 1; // 등록되어 있지 않은 키 정보입니다.
  99. }
  100. }
  101. catch (Exception ex)
  102. {
  103. errMsg = ex.Message;
  104. return 4;
  105. }
  106. }
  107. public static ApiException CreateValidationApiAuthorizationAsync(HttpContext httpContext, int errorNo)
  108. {
  109. string errorMessage;
  110. switch (errorNo)
  111. {
  112. case 0: errorMessage = GlobalConstants.API_RESULT_SUCCESS; break;
  113. case 1: errorMessage = "등록되어 있지 않은 API KEY 입니다."; break;
  114. case 2: errorMessage = "API KEY 유효기간이 지났습니다."; break;
  115. case 3: errorMessage = "등록되어 있지 않은 IP Address에서 호출된 API KEY 입니다."; break;
  116. case 4: errorMessage = "API KEY 유효성 검사 중 시스템 내부 오류가 발생하였습니다."; break;
  117. default: errorMessage = errorNo + ", API KEY 유효성 검사 중 알수 없는 오류가 발생하였습니다."; break;
  118. }
  119. return new ApiException(errorNo, errorMessage);
  120. }
  121. }
  122. }