using AipGateway.AIP; using AipGateway.API.Application.Modules; using AipGateway.API.Configurations; using AipGateway.API.Domain.Common.Exceptions; using AipGateway.API.Models; using System.Collections; namespace AipGateway.API.Services { public static class ContainerService { public static int Port = 5000; public static ServiceProvider provider = null; public static AipFileManager aipFileManager = null; public static AipSettings aipConfig = null; public static Hashtable aipLableMap = new Hashtable(); public static Hashtable aipPolicyMap = new Hashtable(); public static Hashtable aipProtectionMap = new Hashtable(); public static Hashtable apiKeyMap = new Hashtable(); public static Hashtable decryptKeyMap = new Hashtable(); public static Hashtable apiIpAddrMap = new Hashtable(); public static Hashtable decryptIpAddrMap = new Hashtable(); public static Hashtable supportedFileExtMap = new Hashtable(); public static Hashtable protectedFileExtMap = new Hashtable(); public static Hashtable supportedDelFileExtMap = new Hashtable(); public static Hashtable protectedDelFileExtMap = new Hashtable(); public static string GetFileData(string fileName) { string result = string.Empty; try { if (File.Exists(fileName)) { byte[] fileBytes = File.ReadAllBytes(fileName); result = Convert.ToBase64String(fileBytes); #if false using (FileStream fs = System.IO.File.OpenRead(fileName)) { using (var reader = new BinaryReader(fs)) { byte[] fileBytes = reader.ReadBytes((int)fs.Length); result = Convert.ToBase64String(fileBytes); } } #endif } } catch (Exception) { return string.Empty; } return result; } public static void DeleteFile(string fileName) { try { if (File.Exists(fileName)) { File.Delete(fileName); } } catch (Exception) { } } public static string? GetSupportedFileType(string fileName) { string fileExt = Path.GetExtension(fileName).ToLower(); return ContainerService.supportedFileExtMap[fileExt] as string; } public static int ValidationApiAuthorization(HttpContext httpContext, string key) { string errMsg; string? ipAddress = httpContext.Connection.RemoteIpAddress?.ToString(); ipAddress = string.IsNullOrEmpty(ipAddress) ? "xx.xx.xx.xx" : ipAddress; try { if (apiKeyMap.ContainsKey(key)) { LinkedApiKey? apiKey = ContainerService.apiKeyMap[key] as LinkedApiKey; if (apiKey == null) { return 1; // 등록되어 있지 않은 키 정보입니다. } if (DateTime.Compare(apiKey.ExpiredAt, DateTime.Now) < 0) { return 2; // 등록된 키의 유효기간이 지났습니다. } if (apiKey.serverMap.Contains(ipAddress)) { errMsg = "성공."; return 0; // 유효한 키값과 IP Address } errMsg = "등록되어 있지 않은 IP Address에서 호출된 API KEY 입니다."; return 3; // 유효한 키값이나 등록되지 않은 IP Address } else { errMsg = "등록되어 있지 않은 API KEY 입니다."; return 1; // 등록되어 있지 않은 키 정보입니다. } } catch (Exception ex) { errMsg = ex.Message; return 4; } } public static ApiException CreateValidationApiAuthorizationAsync(HttpContext httpContext, int errorNo) { string errorMessage; switch (errorNo) { case 0: errorMessage = GlobalConstants.API_RESULT_SUCCESS; break; case 1: errorMessage = "등록되어 있지 않은 API KEY 입니다."; break; case 2: errorMessage = "API KEY 유효기간이 지났습니다."; break; case 3: errorMessage = "등록되어 있지 않은 IP Address에서 호출된 API KEY 입니다."; break; case 4: errorMessage = "API KEY 유효성 검사 중 시스템 내부 오류가 발생하였습니다."; break; default: errorMessage = errorNo + ", API KEY 유효성 검사 중 알수 없는 오류가 발생하였습니다."; break; } return new ApiException(errorNo, errorMessage); } } }