WhoisController.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using Microsoft.AspNetCore.Mvc;
  2. using System.ComponentModel.DataAnnotations;
  3. using economy.Helpers;
  4. using economy.Models;
  5. using economy.Models.Whois;
  6. namespace economy.Controllers
  7. {
  8. public class WhoisController : Controller
  9. {
  10. [ViewData]
  11. [BindProperty(Name = "query", SupportsGet = true)]
  12. [Required(ErrorMessage = "검색어를 입력해주세요.")]
  13. [StringLength(120, ErrorMessage = "검색어 길이를 초과하였습니다.")]
  14. public required string Query { get; set; }
  15. private readonly DataGoKR _dataGoKR;
  16. private Dictionary<string, string> _queryString;
  17. public WhoisController(DataGoKR dataGoKR)
  18. {
  19. _dataGoKR = dataGoKR;
  20. }
  21. [HttpGet]
  22. public IActionResult Index()
  23. {
  24. ViewBag.isError = false;
  25. return View();
  26. }
  27. [HttpPost]
  28. public async Task<IActionResult> Search()
  29. {
  30. try
  31. {
  32. // 유효성 검사
  33. if (!ModelState.IsValid)
  34. {
  35. throw new Exception(
  36. string.Join("\n", ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage))
  37. );
  38. }
  39. if (string.IsNullOrWhiteSpace(Query))
  40. {
  41. throw new Exception("검색어를 입력해주세요.");
  42. }
  43. if (Common.IsDomain(Query))
  44. {
  45. ViewBag.result = await _Domain();
  46. ViewBag.type = 1;
  47. }
  48. else if (Common.IsIPv4(Query) || Common.IsIPv6(Query))
  49. {
  50. ViewBag.result = await _IP();
  51. ViewBag.type = 2;
  52. }
  53. else
  54. {
  55. throw new Exception("형식이 옳지 않습니다. 올바른 도메인 또는 IP 주소를 입력해주세요.");
  56. }
  57. ViewBag.isError = false;
  58. }
  59. catch (Exception ex)
  60. {
  61. ViewBag.isError = true;
  62. ViewBag.errorMessage = ex.Message;
  63. }
  64. ViewBag.Query = Query;
  65. return View("Index");
  66. }
  67. // 도메인 검색
  68. private async Task<Models.Whois.Domain.Response> _Domain()
  69. {
  70. WhoisModel whois = new WhoisModel(_dataGoKR);
  71. Models.Whois.Domain.Response domainInfo = await whois.GetDomainInfo(Query);
  72. return domainInfo;
  73. }
  74. // IP 검색
  75. private async Task<Models.Whois.IP.Response> _IP()
  76. {
  77. WhoisModel whois = new WhoisModel(_dataGoKR);
  78. Models.Whois.IP.Response IPInfo = await whois.GetIPInfo(Query);
  79. return IPInfo;
  80. }
  81. }
  82. }