WebConsoleHomeController.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using Edge.WebHost.Models;
  9. using Edge.Core;
  10. using Microsoft.AspNetCore.Mvc;
  11. using Microsoft.AspNetCore.Mvc.ApiExplorer;
  12. using Microsoft.Extensions.Configuration;
  13. using Microsoft.Extensions.Logging;
  14. using Newtonsoft.Json;
  15. using RestSharp;
  16. namespace Edge.WebHost.Controllers
  17. {
  18. public class WebConsoleHomeController : Controller
  19. {
  20. private readonly ILogger<WebConsoleHomeController> _logger;
  21. JsonSerializerSettings _jsonSetting =
  22. new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore };
  23. private readonly IApiDescriptionGroupCollectionProvider _apiDescriptionsProvider;
  24. private string _apiBaseAddress;
  25. private string _checkLicenseUrl;
  26. private string _verifyLicenseUrl;
  27. private readonly string _serviceDiscoveryUrl;
  28. public WebConsoleHomeController(ILogger<WebConsoleHomeController> logger,
  29. IApiDescriptionGroupCollectionProvider apiDescriptionsProvider,
  30. IConfiguration configuration)
  31. {
  32. _logger = logger;
  33. _apiDescriptionsProvider = apiDescriptionsProvider;
  34. _apiBaseAddress = configuration.GetValue<string>("BaseUrl");
  35. _checkLicenseUrl = configuration.GetValue<string>("CheckLicenseUrl");
  36. _verifyLicenseUrl = configuration.GetValue<string>("VerifyLicenseUrl");
  37. _serviceDiscoveryUrl = configuration.GetValue<string>("ServiceDiscoveryUrl");
  38. }
  39. public async Task<IActionResult> Index()
  40. {
  41. var applicableApiDescriptions = _apiDescriptionsProvider.ApiDescriptionGroups.Items
  42. .SelectMany(group => group.Items);
  43. if (HttpContext != null)
  44. {
  45. _apiBaseAddress = "http://"+HttpContext.Request.Host.Value+"/";
  46. }
  47. if (await CheckLicense() == true)
  48. {
  49. return View("../WebConsole/Home/Index", _apiDescriptionsProvider.ApiDescriptionGroups.Items
  50. .SelectMany(group => group.Items));
  51. }
  52. else
  53. {
  54. return View("../WebConsole/Home/InputLicense", Licensing.Instance().GetMacAddress(_logger));
  55. }
  56. }
  57. public IActionResult Privacy()
  58. {
  59. return View();
  60. }
  61. [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
  62. public IActionResult Error()
  63. {
  64. return View(new ErrorViewModelWeb { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
  65. }
  66. public async Task<IActionResult> InputLicense()
  67. {
  68. return View("InputLicense");
  69. }
  70. public async Task<IActionResult> SaveLicense(string license)
  71. {
  72. if (await LicenseVerify(license) == true)
  73. {
  74. return RedirectToAction(nameof(Index), _apiDescriptionsProvider.ApiDescriptionGroups.Items.SelectMany(group => group.Items));
  75. }
  76. else
  77. {
  78. return View("InputLicense");
  79. }
  80. }
  81. private async Task<bool> CheckLicense()
  82. {
  83. var result = false;
  84. var path = await getServiceUrl("webapi", "CheckLicense", "LicensingApp");
  85. if (path != string.Empty)
  86. {
  87. _checkLicenseUrl = path;
  88. }
  89. try
  90. {
  91. var restClient = new RestClient(_apiBaseAddress);
  92. RestRequest request = new RestRequest(_checkLicenseUrl, Method.POST);
  93. request.AddHeader("Content-Type", "application/json");
  94. request.AddJsonBody("");
  95. request.RequestFormat = DataFormat.Json;
  96. var response = await restClient.ExecuteTaskAsync(request);
  97. if (response.StatusCode != System.Net.HttpStatusCode.OK)
  98. {
  99. _logger.LogInformation($"CheckLicense {_checkLicenseUrl} with Status code: " + response.StatusCode);
  100. }
  101. else
  102. {
  103. result = JsonConvert.DeserializeObject<bool>(response.Content);
  104. }
  105. }
  106. catch (Exception _)
  107. {
  108. }
  109. return result;
  110. }
  111. private async Task<bool> LicenseVerify(string license)
  112. {
  113. var result = false;
  114. try
  115. {
  116. var path = await getServiceUrl("webapi", "VerifyLicense", "LicensingApp");
  117. if (path != string.Empty)
  118. {
  119. _verifyLicenseUrl = path;
  120. }
  121. var restClient = new RestClient(_apiBaseAddress);
  122. RestRequest request = new RestRequest(_verifyLicenseUrl, Method.POST);
  123. request.AddHeader("Content-Type", "application/json");
  124. request.AddJsonBody(license);
  125. request.RequestFormat = DataFormat.Json;
  126. var response = await restClient.ExecuteTaskAsync(request);
  127. if (response.StatusCode != System.Net.HttpStatusCode.OK)
  128. {
  129. _logger.LogInformation($"LicenseVerify {_verifyLicenseUrl} with Status code: " + response.StatusCode);
  130. }
  131. else
  132. {
  133. result = JsonConvert.DeserializeObject<bool>(response.Content);
  134. }
  135. }
  136. catch (Exception _)
  137. {
  138. }
  139. return result;
  140. }
  141. /// <summary>
  142. /// serviceType canbe "webapi"/"localMqtt"/"remotemqtt"
  143. /// </summary>
  144. /// <param name="serviceType"></param>
  145. /// <param name="apiName"></param>
  146. /// <param name="tag"></param>
  147. /// <returns></returns>
  148. private async Task<string> getServiceUrl(string serviceType,string apiName,string tag)
  149. {
  150. var result = string.Empty;
  151. object[] requestBody = new object[]{ serviceType, new object[] { tag} };
  152. try
  153. {
  154. var restClient = new RestClient(_apiBaseAddress);
  155. RestRequest request = new RestRequest(_serviceDiscoveryUrl, Method.POST);
  156. request.AddHeader("Content-Type", "application/json");
  157. request.AddJsonBody(requestBody);
  158. request.RequestFormat = DataFormat.Json;
  159. var response = await restClient.ExecuteTaskAsync(request);
  160. if (response.StatusCode != System.Net.HttpStatusCode.OK)
  161. {
  162. _logger.LogInformation($"serviceDiscovery {_serviceDiscoveryUrl} with Status code: " + response.StatusCode);
  163. }
  164. else
  165. {
  166. var data = (Newtonsoft.Json.Linq.JArray)JsonConvert.DeserializeObject(response.Content);
  167. if (data.Count > 0)
  168. {
  169. foreach (var d in data)
  170. {
  171. if (d["ApiName"].ToString() == apiName)
  172. {
  173. result = d["Path"].ToString().Substring(1);
  174. break;
  175. }
  176. }
  177. }
  178. }
  179. }
  180. catch (Exception _)
  181. {
  182. }
  183. return result;
  184. }
  185. }
  186. }