ApiController.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. using System.Threading.Channels;
  18. namespace molilian.api.Controllers
  19. {
  20. [ApiController]
  21. [Route("api/[action]")]
  22. public class ApiController : ControllerBase
  23. {
  24. protected IHttpContextAccessor _accessor;
  25. TaokeOpenCore core;
  26. public ApiController(IHttpContextAccessor accessor)
  27. {
  28. _accessor = accessor;
  29. core = new TaokeOpenCore();
  30. }
  31. //[HttpGet]
  32. //public async Task<ActionResult> RequestAsync([FromQuery] string url, [FromQuery] int proxyId)
  33. //{
  34. // if (string.IsNullOrEmpty(url))
  35. // {
  36. // return BadRequest("URL cannot be empty");
  37. // }
  38. // var proxy = ProxyNodesCore.GetOne(proxyId);
  39. // if (proxy == null)
  40. // {
  41. // return Unauthorized("Invalid proxy configuration");
  42. // }
  43. // WebClientUtility cli = new()
  44. // {
  45. // Proxy = proxy
  46. // };
  47. // await cli.RequestAsync(url);
  48. // var content = cli.Body();
  49. // var contentType = cli.ResponseMessage.Content.Headers.ContentType?.MediaType ?? "text/plain";
  50. // return Content(content, contentType);
  51. //}
  52. [HttpGet]
  53. public async Task<ActionResult> RequestAsync([FromQuery] string url, [FromQuery] string proxy)
  54. {
  55. if (string.IsNullOrEmpty(url))
  56. {
  57. return BadRequest("URL cannot be empty");
  58. }
  59. WebProxy _proxy = ProxyManager.Parse(proxy);
  60. WebClientUtility cli = new();
  61. cli.Proxy = _proxy;
  62. cli.Timeout = TimeSpan.FromSeconds(10);
  63. await cli.RequestAsync(url);
  64. if (cli.Successed)
  65. {
  66. var content = cli.Body();
  67. var contentType = cli.ResponseMessage.Content.Headers.ContentType?.MediaType ?? "text/plain";
  68. return Content(content, contentType);
  69. }
  70. throw cli.ResponseException;
  71. }
  72. [HttpPost]
  73. public async Task<ActionResult> PromotionQuery([FromBody] JsonElement form, [FromQuery] string a = "", [FromQuery] int t = 0, [FromQuery] string sign = "")
  74. {
  75. var img = form.Read("img", string.Empty);
  76. var url = form.Read("url", string.Empty);
  77. var ip = form.Read("ip", string.Empty);
  78. var oaid = form.Read("oaid", string.Empty);
  79. var scene = form.Read("scene", string.Empty);
  80. var recommand = form.Read("recommand", true);
  81. var aid = form.Read("aid", 0);
  82. //验证签名
  83. if (string.IsNullOrEmpty(a) || string.IsNullOrEmpty(sign))
  84. {
  85. return new APIResult(new { success = false, message = "验证错误" }, APIResultCodeEnum.Unauthorized);
  86. }
  87. DateTime time1 = t.Convert2Datetime();
  88. if (time1 < DateTime.Now.AddMinutes(-10))
  89. {
  90. return new APIResult(new { success = false, message = "验证错误2" }, APIResultCodeEnum.Unauthorized);
  91. }
  92. var account = await ApiAccountCore.GetOneAsync(a);
  93. if (account == null)
  94. {
  95. return new APIResult(new { success = false, message = "验证错误3" }, APIResultCodeEnum.Unauthorized);
  96. }
  97. string app_sign = $"{account.api_secret}@{t}".MD5();
  98. if (sign != app_sign)
  99. {
  100. return new APIResult(new { success = false, message = "验证错误4" }, APIResultCodeEnum.Unauthorized);
  101. }
  102. if (!string.IsNullOrEmpty(url))
  103. {
  104. var bytes = new WebClientUtility().Request(url).ResponseBody;
  105. img = Convert.ToBase64String(bytes);
  106. }
  107. return await core.PromotionQueryAsync(img, oaid, ip, scene, recommand, false, aid);
  108. }
  109. [HttpPost]
  110. public async Task<ActionResult> unsafePromotionQuery([FromBody] JsonElement form)
  111. {
  112. var img = form.Read("img", string.Empty);
  113. var url = form.Read("url", string.Empty);
  114. var ip = form.Read("ip", string.Empty);
  115. var oaid = form.Read("oaid", string.Empty);
  116. var scene = form.Read("scene", string.Empty);
  117. var recommand = form.Read("recommand", true);
  118. var debug = form.Read("debug", false);
  119. var aid = form.Read("aid", 0);
  120. int count = form.Read("count", 0);
  121. if (!string.IsNullOrEmpty(url))
  122. {
  123. var bytes = new WebClientUtility().Request(url).ResponseBody;
  124. img = Convert.ToBase64String(bytes);
  125. }
  126. if (string.IsNullOrEmpty(oaid))
  127. {
  128. Random rand = new Random();
  129. oaid = $"{rand.Next(100000, 999999)}-{rand.Next(100000, 999999)}-{rand.Next(100000, 999999)}";
  130. }
  131. if (count > 1)
  132. {
  133. for (int i = 0; i < count; i++)
  134. {
  135. _ = core.PromotionQueryAsync(img, oaid, ip, scene, recommand, debug, aid);
  136. }
  137. return new APIResult(new { msg = "ok" });
  138. }
  139. return await core.PromotionQueryAsync(img, oaid, ip, scene, recommand, debug, aid);
  140. }
  141. }
  142. }