AipFileController.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. using AipGateway.AIP;
  2. using AipGateway.API.Service.Models;
  3. using AipGateway.API.Service.Services;
  4. using Microsoft.AspNetCore.Http.Features;
  5. using Microsoft.AspNetCore.Http;
  6. using Microsoft.AspNetCore.Mvc;
  7. using System;
  8. using System.Buffers.Text;
  9. using System.IO;
  10. using System.Net;
  11. using System.Text;
  12. using static AipGateway.API.Service.Models.ReqFileInfo;
  13. using Microsoft.Extensions.Logging;
  14. using Microsoft.Extensions.Primitives;
  15. using AipGateway.API.Service.Utils;
  16. using Microsoft.AspNetCore.Http.HttpResults;
  17. using AipGateway.Data;
  18. using AipGateway.Data.Entities;
  19. using System.Reflection.Metadata.Ecma335;
  20. using Microsoft.EntityFrameworkCore;
  21. namespace AipGateway.API.Service.Controllers
  22. {
  23. [ApiController]
  24. [Route("v1/aip-files")]
  25. //[Route("v1/[controller]")]
  26. public class AipFileController : ControllerBase
  27. {
  28. private Microsoft.AspNetCore.Http.IHttpContextAccessor _accessor = null;
  29. private readonly ILogger<AipFileController> _logger = null;
  30. private readonly IAipFileManagerService _aipFileService = null;
  31. private readonly AipDbContext _aipDb = null;
  32. public AipFileController(Microsoft.AspNetCore.Http.IHttpContextAccessor accessor,
  33. ILogger<AipFileController> logger,
  34. IAipFileManagerService aipFileService,
  35. AipDbContext aipDb)
  36. {
  37. _accessor = accessor;
  38. _logger = logger;
  39. _aipFileService = aipFileService;
  40. _aipDb = aipDb;
  41. _logger.LogError("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: AipFileController");
  42. }
  43. private string GetRequestIpAddress()
  44. {
  45. string remoteIpAddress = "";
  46. if (_accessor.HttpContext.Request.Headers != null)
  47. {
  48. //XFF(X-Forwarded-For) HTTP 헤더 필드는 클라이언트의 원래 IP 주소를 식별하기 위한 표준
  49. //HTTP 프록시 또는 로드 밸런서를 통해 웹 서버에 연결
  50. var forwardedHeader = _accessor.HttpContext.Request.Headers["X-Forwarded-For"];
  51. if (!Microsoft.Extensions.Primitives.StringValues.IsNullOrEmpty(forwardedHeader))
  52. {
  53. remoteIpAddress = forwardedHeader.FirstOrDefault();
  54. }
  55. }
  56. //헤더가 없는 경우 연결 원격 IP 주소를 가져오기
  57. if (string.IsNullOrEmpty(remoteIpAddress) && _accessor.HttpContext.Connection.RemoteIpAddress != null)
  58. {
  59. remoteIpAddress = _accessor.HttpContext.Connection.RemoteIpAddress.ToString();
  60. }
  61. remoteIpAddress = remoteIpAddress.Replace("::1", "192.168.0.1").Replace("::ffff:", "");
  62. return remoteIpAddress;
  63. }
  64. [HttpGet("linked-systems")]
  65. [Produces("application/json")]
  66. public async Task<IEnumerable<TbLinkedSystem>> GetLinkedSystems()
  67. {
  68. var linkedSystems = await _aipDb.LinkedSystems.ToListAsync();
  69. var linkedServers = await _aipDb.LinkedServers.ToListAsync();
  70. var linkedDecryptKeys = await _aipDb.LinkedDecryptKeys.ToListAsync();
  71. var linkedApiKeys = await _aipDb.LinkedApiKeys.ToListAsync();
  72. var aipLabels = await _aipDb.AipLabels.ToListAsync();
  73. var aipPolicies = await _aipDb.AipPolicies.ToListAsync();
  74. var aipProtections = await _aipDb.AipProtections.ToListAsync();
  75. var aipServers = await _aipDb.AipServers.ToListAsync();
  76. var aipConfig = await _aipDb.AipConfigs.ToListAsync();
  77. _logger.LogInformation(" linkedSystems: {0} EA.", linkedSystems.Count);
  78. foreach (var v in linkedSystems)
  79. {
  80. _logger.LogInformation(" linkedSystems: {0}", v.ToString());
  81. }
  82. _logger.LogInformation(" linkedServers: {0} EA.", linkedServers.Count);
  83. foreach (var v in linkedServers)
  84. {
  85. _logger.LogInformation(" linkedServers: {0}", v.ToString());
  86. }
  87. _logger.LogInformation("linkedDecryptKeys: {0} EA.", linkedDecryptKeys.Count);
  88. foreach (var v in linkedDecryptKeys)
  89. {
  90. _logger.LogInformation("linkedDecryptKeys: {0}", v.ToString());
  91. }
  92. _logger.LogInformation(" linkedApiKeys: {0} EA.", linkedApiKeys.Count);
  93. foreach (var v in linkedApiKeys)
  94. {
  95. _logger.LogInformation(" linkedApiKeys: {0}", v.ToString());
  96. }
  97. _logger.LogInformation(" aipLabels: {0} EA.", aipLabels.Count);
  98. foreach (var v in aipLabels)
  99. {
  100. _logger.LogInformation(" aipLabels: {0}", v.ToString());
  101. }
  102. _logger.LogInformation(" aipPolicies: {0} EA.", aipPolicies.Count);
  103. foreach (var v in aipPolicies)
  104. {
  105. _logger.LogInformation(" aipPolicies: {0}", v.ToString());
  106. }
  107. _logger.LogInformation("aipProtections: {0} EA.", aipProtections.Count);
  108. foreach (var v in aipProtections)
  109. {
  110. _logger.LogInformation("aipProtections: {0}", v.ToString());
  111. }
  112. _logger.LogInformation("aipServers: {0} EA.", aipServers.Count);
  113. foreach (var v in aipServers)
  114. {
  115. _logger.LogInformation("aipServers: {0}", v.ToString());
  116. }
  117. _logger.LogInformation(" aipConfig: {0} EA.", aipConfig.Count);
  118. foreach (var v in aipConfig)
  119. {
  120. _logger.LogInformation(" aipConfig: {0}", v.ToString());
  121. }
  122. return linkedSystems;
  123. }
  124. [HttpGet("ip-address")]
  125. [Produces("application/json")]
  126. public Task<IpAddress> GetIpAddress()
  127. {
  128. string ipAddress = "";
  129. //IHeaderDictionary header = Request.HttpContext.Request.Headers;
  130. //foreach (KeyValuePair<string, StringValues> item in header)
  131. //{
  132. //_logger.LogInformation("{0}", item.ToString());
  133. //}
  134. //_logger.LogInformation(" HTTP_X_FORWARDED_FOR: {0}", HttpContext.GetServerVariable("HTTP_X_FORWARDED_FOR"));
  135. //_logger.LogInformation(" Forwarded: {0}", HttpContext.GetServerVariable("Forwarded"));
  136. //_logger.LogInformation(" X-Forwarded-For: {0}", HttpContext.GetServerVariable("X-Forwarded-For"));
  137. //_logger.LogInformation(" X-FORWARDED-FOR: {0}", HttpContext.GetServerVariable("X-FORWARDED-FOR"));
  138. //_logger.LogInformation("X-Forwarded-Client-Ip: {0}", HttpContext.GetServerVariable("X-Forwarded-Client-Ip"));
  139. //_logger.LogInformation(" user-agent: {0}", HttpContext.GetServerVariable("user-agent"));
  140. //_logger.LogInformation(" Proxy-Client-IP: {0}", HttpContext.GetServerVariable("Proxy-Client-IP"));
  141. //_logger.LogInformation(" WL-Proxy-Client-IP: {0}", HttpContext.GetServerVariable("WL-Proxy-Client-IP"));
  142. _logger.LogInformation(" REMOTE_ADDR: {0}", Microsoft.AspNetCore.Http.HttpContextServerVariableExtensions.GetServerVariable(HttpContext, "REMOTE_ADDR"));
  143. ipAddress = GetRequestIpAddress();
  144. //IPHelper.GetSourceIp(Request);
  145. //string ipAddress2 = Request.HttpContext.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
  146. //string temp = "";
  147. //if (!string.IsNullOrEmpty(ipAddress2))
  148. //{
  149. // string[] addresses = ipAddress2.Split(',');
  150. // if (addresses.Length != 0)
  151. // {
  152. // temp = addresses[0];
  153. // }
  154. //}
  155. //temp = context.Request.ServerVariables["REMOTE_ADDR"];
  156. //IPAddress remoteIpAddress = Request.HttpContext.Connection.RemoteIpAddress;
  157. //if (remoteIpAddress != null)
  158. //{
  159. // _logger.LogInformation("RemoteIpAddress_1: {0}", remoteIpAddress.ToString());
  160. // if (remoteIpAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
  161. // {
  162. // _logger.LogInformation("RemoteIpAddress_2: Looking for DNS");
  163. // remoteIpAddress = System.Net.Dns.GetHostEntry(remoteIpAddress)
  164. // .AddressList
  165. // .First(x => x.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);
  166. // }
  167. // ipAddress = remoteIpAddress.ToString();
  168. //}
  169. //else
  170. //{
  171. // _logger.LogInformation("RemoteIpAddress_3: Unknown");
  172. //}
  173. _logger.LogInformation("RemoteIpAddress_4: {0}", ipAddress);
  174. return Task.FromResult(new IpAddress()
  175. {
  176. ipAddress = ipAddress
  177. });
  178. }
  179. [HttpGet("config")]
  180. [Produces("application/json")]
  181. public async Task<AipConfig> GetConfig()
  182. {
  183. try
  184. {
  185. return await _aipFileService.GetConfig();
  186. }
  187. catch (Exception ex)
  188. {
  189. _logger.LogError(ex, "AipFileController::GetConfig.");
  190. return null;
  191. }
  192. }
  193. [HttpGet("aip-config")]
  194. [Produces("application/json")]
  195. public async Task<List<TbAipConfig>> GetAipConfig()
  196. {
  197. try
  198. {
  199. return await _aipDb.AipConfigs.ToListAsync();
  200. }
  201. catch (Exception ex)
  202. {
  203. _logger.LogError(ex, "AipFileController::GetAipConfig.");
  204. return null;
  205. }
  206. }
  207. [HttpGet("labels")]
  208. [Produces("application/json")]
  209. public async Task<List<AipLabel>?> GetLabels()
  210. {
  211. try
  212. {
  213. return await _aipFileService.GetLabels();
  214. }
  215. catch (Exception ex)
  216. {
  217. _logger.LogError(ex, "AipFileController::GetLabels.");
  218. return null;
  219. }
  220. }
  221. [HttpGet("policies")]
  222. [Produces("application/json")]
  223. public async Task<List<AipLabel>?> GetPolicies()
  224. {
  225. try
  226. {
  227. return await _aipFileService.GetPolicies();
  228. }
  229. catch (Exception ex)
  230. {
  231. _logger.LogError(ex, "AipFileController::GetPolicies.");
  232. return null;
  233. }
  234. }
  235. [HttpGet("protections")]
  236. [Produces("application/json")]
  237. public async Task<List<AipTemplate>?> GetProtections()
  238. {
  239. try
  240. {
  241. return await _aipFileService.GetProtections();
  242. }
  243. catch (Exception ex)
  244. {
  245. _logger.LogError(ex, "AipFileController::GetProtections.");
  246. return null;
  247. }
  248. }
  249. [HttpGet("file-info")]
  250. [Produces("application/json")]
  251. public async Task<AipFileInfo> GetFileInfo([FromQuery] ReqFileInfo.ByFileName file)
  252. {
  253. try
  254. {
  255. return await _aipFileService.GetFileInfo(file.name);
  256. }
  257. catch (Exception ex)
  258. {
  259. _logger.LogError(ex, "AipFileController::GetFileInfo-byFileName.");
  260. return null;
  261. }
  262. }
  263. [HttpPost("stream-info")]
  264. [Produces("application/json")]
  265. public async Task<AipFileInfo> GetStreamInfo([FromBody] ReqFileInfo.ByStream file)
  266. {
  267. try
  268. {
  269. //string base64String = Encoding.Default.GetString(file.stream);
  270. //Console.WriteLine("0.::::: {0}", file.fileData);
  271. //string str1 = Encoding.UTF8.GetString(file.stream);
  272. //Console.WriteLine("0.::::: {0}", str1);
  273. using (var stream = new MemoryStream(Convert.FromBase64String(file.fileData))) {
  274. return await _aipFileService.GetFileInfo(stream);
  275. }
  276. }
  277. catch (Exception ex)
  278. {
  279. _logger.LogError(ex, "AipFileController::GetFileInfo-byFileStream.");
  280. return null;
  281. }
  282. }
  283. [HttpGet("file-data")]
  284. [Produces("application/json")]
  285. public FileData GetFileData ([FromQuery] ReqFileInfo.ByFileName file)
  286. {
  287. FileData result = new FileData();
  288. try
  289. {
  290. //Console.WriteLine("0.::::::::::::::::::::::::::::::::::::::::::::::::::::::: {0}", file.name);
  291. if (System.IO.File.Exists(file.name))
  292. {
  293. using (FileStream fs = System.IO.File.OpenRead(file.name))
  294. {
  295. using (var reader = new BinaryReader(fs))
  296. {
  297. result.byteDataArr = reader.ReadBytes((int)fs.Length);
  298. result.base64Data = Convert.ToBase64String(result.byteDataArr);
  299. //result.streamData = new MemoryStream();
  300. //result.streamData.Write(result.byteDataArr, 0, result.byteDataArr.Length);
  301. }
  302. var bytes = Convert.FromBase64String(result.base64Data);
  303. var contents = new StreamContent(new MemoryStream(bytes));
  304. //Console.WriteLine("{0}", contents);
  305. var stream = new MemoryStream(Convert.FromBase64String(result.base64Data));
  306. if (file.name.Contains(".txt"))
  307. {
  308. System.IO.File.WriteAllBytes(@"c:\data\sample.txt.txt", Convert.FromBase64String(result.base64Data));
  309. }
  310. else
  311. {
  312. System.IO.File.WriteAllBytes(@"c:\data\sample.txt.pptx", Convert.FromBase64String(result.base64Data));
  313. }
  314. }
  315. }
  316. }
  317. catch (Exception ex)
  318. {
  319. //Console.WriteLine("{0}", ex.Message);
  320. _logger.LogError(ex, "AipFileController::GetFileData.");
  321. }
  322. return result;
  323. }
  324. }
  325. }