ApiController.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using molilian.core;
  2. using dodohold.core;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Org.BouncyCastle.Ocsp;
  5. using System.Text.Json;
  6. using System.Runtime.InteropServices;
  7. using System.Net;
  8. using System.Security.Cryptography;
  9. using Microsoft.AspNetCore.Authentication;
  10. using TencentCloud.Ecm.V20190719.Models;
  11. using COSXML.Network;
  12. using System.Linq;
  13. using static dodohold.core.ZTOExpress.CreateOrderArgs;
  14. using TencentCloud.Lighthouse.V20200324.Models;
  15. using TencentCloud.Ame.V20190916.Models;
  16. using TencentCloud.Omics.V20221128.Models;
  17. namespace molilian.api.Controllers
  18. {
  19. [ApiController]
  20. [Route("api/[action]")]
  21. public class ApiController : ControllerBase
  22. {
  23. protected IHttpContextAccessor _accessor;
  24. TaokeOpenCore core;
  25. public ApiController(IHttpContextAccessor accessor)
  26. {
  27. _accessor = accessor;
  28. core = new TaokeOpenCore();
  29. }
  30. //[HttpGet]
  31. //public async Task<ActionResult> RequestAsync([FromQuery] string url, [FromQuery] int proxyId)
  32. //{
  33. // if (string.IsNullOrEmpty(url))
  34. // {
  35. // return BadRequest("URL cannot be empty");
  36. // }
  37. // var proxy = ProxyNodesCore.GetOne(proxyId);
  38. // if (proxy == null)
  39. // {
  40. // return Unauthorized("Invalid proxy configuration");
  41. // }
  42. // WebClientUtility cli = new()
  43. // {
  44. // Proxy = proxy
  45. // };
  46. // await cli.RequestAsync(url);
  47. // var content = cli.Body();
  48. // var contentType = cli.ResponseMessage.Content.Headers.ContentType?.MediaType ?? "text/plain";
  49. // return Content(content, contentType);
  50. //}
  51. [HttpGet]
  52. public async Task<ActionResult> RequestAsync([FromQuery] string url, [FromQuery] string proxy)
  53. {
  54. if (string.IsNullOrEmpty(url))
  55. {
  56. return BadRequest("URL cannot be empty");
  57. }
  58. WebProxy _proxy = ProxyManager.Parse(proxy);
  59. WebClientUtility cli = new();
  60. cli.Proxy = _proxy;
  61. cli.Timeout = TimeSpan.FromSeconds(10);
  62. await cli.RequestAsync(url);
  63. if (cli.Successed)
  64. {
  65. var content = cli.Body();
  66. var contentType = cli.ResponseMessage.Content.Headers.ContentType?.MediaType ?? "text/plain";
  67. return Content(content, contentType);
  68. }
  69. throw cli.ResponseException;
  70. }
  71. [HttpPost]
  72. public async Task<ActionResult> PromotionQuery([FromBody] JsonElement form, [FromQuery] string a = "", [FromQuery] int t = 0, [FromQuery] string sign = "")
  73. {
  74. var img = form.Read("img", string.Empty);
  75. var url = form.Read("url", string.Empty);
  76. var ip = form.Read("ip", string.Empty);
  77. var oaid = form.Read("oaid", string.Empty);
  78. var scene = form.Read("scene", string.Empty);
  79. var recommand = form.Read("recommand", true);
  80. var aid = form.Read("aid", 0);
  81. //验证签名
  82. if (string.IsNullOrEmpty(a) || string.IsNullOrEmpty(sign))
  83. {
  84. return new APIResult(new { success = false, message = "验证错误" }, APIResultCodeEnum.Unauthorized);
  85. }
  86. DateTime time1 = t.Convert2Datetime();
  87. if (time1 < DateTime.Now.AddMinutes(-10))
  88. {
  89. return new APIResult(new { success = false, message = "验证错误2" }, APIResultCodeEnum.Unauthorized);
  90. }
  91. var account = ApiAccountCore.GetOne(a);
  92. if (account == null)
  93. {
  94. return new APIResult(new { success = false, message = "验证错误3" }, APIResultCodeEnum.Unauthorized);
  95. }
  96. string app_sign = $"{account.api_secret}@{t}".MD5();
  97. if (sign != app_sign)
  98. {
  99. return new APIResult(new { success = false, message = "验证错误4" }, APIResultCodeEnum.Unauthorized);
  100. }
  101. if (!string.IsNullOrEmpty(url))
  102. {
  103. var bytes = new WebClientUtility().Request(url).ResponseBody;
  104. img = Convert.ToBase64String(bytes);
  105. }
  106. return await core.PromotionQueryAsync(img, oaid, ip, scene, recommand, false, aid);
  107. }
  108. [HttpPost]
  109. public async Task<ActionResult> unsafePromotionQuery([FromBody] JsonElement form)
  110. {
  111. var img = form.Read("img", string.Empty);
  112. var url = form.Read("url", string.Empty);
  113. var ip = form.Read("ip", string.Empty);
  114. var oaid = form.Read("oaid", string.Empty);
  115. var scene = form.Read("scene", string.Empty);
  116. var recommand = form.Read("recommand", true);
  117. var debug = form.Read("debug", false);
  118. var aid = form.Read("aid", 0);
  119. if (!string.IsNullOrEmpty(url))
  120. {
  121. var bytes = new WebClientUtility().Request(url).ResponseBody;
  122. img = Convert.ToBase64String(bytes);
  123. }
  124. if (string.IsNullOrEmpty(oaid))
  125. {
  126. Random rand = new Random();
  127. oaid = $"{rand.Next(100000, 999999)}-{rand.Next(100000, 999999)}-{rand.Next(100000, 999999)}";
  128. }
  129. return await core.PromotionQueryAsync(img, oaid, ip, scene, recommand, debug, aid);
  130. }
  131. }
  132. }