TestController.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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.Threading;
  9. using Microsoft.AspNetCore.Mvc.RazorPages;
  10. using TencentCloud.Soe.V20180724.Models;
  11. using static dodohold.core.ZTOExpress.CreateOrderArgs;
  12. namespace molilian.api.Controllers
  13. {
  14. [ApiController]
  15. [Route("[controller]/[action]")]
  16. public class TestController : ControllerBase
  17. {
  18. protected IHttpContextAccessor _accessor;
  19. public TestController(IHttpContextAccessor accessor)
  20. {
  21. _accessor = accessor;
  22. }
  23. [HttpGet]
  24. public async Task<ActionResult> ip(string ip)
  25. {
  26. string result = IP2RegionPlus.Search(ip);
  27. return new APIResult(new
  28. {
  29. success = true,
  30. message = result
  31. });
  32. }
  33. [HttpPost]
  34. public async Task<ActionResult> testreg([FromBody] JsonElement form)
  35. {
  36. var content = form.Read("s", string.Empty);
  37. string shortLinkurl = AlimamaPlus.GetTaobaoLink(content);
  38. bool is_tao_token = AlimamaPlus.MatchRegexes(content, []);
  39. bool is_other = AlimamaPlus.MatchOtherInfo(content, []);
  40. return new APIResult(new
  41. {
  42. shortLinkurl,
  43. is_tao_token,
  44. is_other
  45. });
  46. }
  47. //[HttpGet]
  48. //public async Task<ActionResult> comparison_tk([FromQuery] int count = 100)
  49. //{
  50. // string cacheKey = ":lock_key:comparison_tk_logs";
  51. // int last_id = RedisHelper.Get<int>(cacheKey);
  52. // string filter = "id>@last_id";
  53. // var result = new DBContext.Table("comparison_tk_logs")
  54. // .Where(filter, new { last_id })
  55. // .Page(count, 1)
  56. // .Order("ID")
  57. // .PageList<TkDataDTO>(false);
  58. // var ip = "127.0.0.1";
  59. // var oaid = "test-comparison_tk_logs";
  60. // foreach (var item in result.List)
  61. // {
  62. // await UnionParseCore.TaobaoParseAsync(item.rawContent, ip, oaid);
  63. // RedisHelper.Set(cacheKey, item.id, 10 * 86400);
  64. // }
  65. // if (result.Count < count)
  66. // {
  67. // cacheKey = ":lock_key:start_comparison_tk";
  68. // RedisHelper.Set(cacheKey, 1, 600);
  69. // }
  70. // return new APIResult(new
  71. // {
  72. // success = true,
  73. // message = "ok"
  74. // });
  75. //}
  76. //[HttpGet]
  77. //public async Task<ActionResult> comparison_tk_raw([FromQuery] int count = 100)
  78. //{
  79. // string cacheKey = ":lock_key:comparison_tk_logs_raw";
  80. // int last_id = RedisHelper.Get<int>(cacheKey);
  81. // string filter = "id>@last_id";
  82. // var result = new DBContext.Table("comparison_tk_logs")
  83. // .Where(filter, new { last_id })
  84. // .Page(count, 1)
  85. // .Order("ID")
  86. // .PageList<TkDataDTO>(false);
  87. // var ip = "127.0.0.1";
  88. // var oaid = "test-comparison_tk_logs_raw";
  89. // foreach (var item in result.List)
  90. // {
  91. // await UnionParseCore.TaobaoParseAsync(item.rawContent, ip, oaid);
  92. // RedisHelper.Set(cacheKey, item.id, 10 * 86400);
  93. // }
  94. // if (result.Count < count)
  95. // {
  96. // cacheKey = ":lock_key:start_comparison_tk";
  97. // RedisHelper.Set(cacheKey, 1, 600);
  98. // }
  99. // return new APIResult(new
  100. // {
  101. // success = true,
  102. // message = "ok"
  103. // });
  104. //}
  105. [HttpGet]
  106. public async Task<ActionResult> testTask()
  107. {
  108. string url = "https://www.taobao.com";
  109. TkDataDTO result = new TkDataDTO();
  110. result.message = "init";
  111. result.success = true;
  112. int timeout = 1000;
  113. using var cts = new CancellationTokenSource();
  114. cts.CancelAfter(timeout);
  115. var requestTask = Task.Run(() => AlimamaPlus.testTaskAsync(result, cts.Token), cts.Token);
  116. var delayTask = Task.Delay(timeout, cts.Token);
  117. var completedTask = await Task.WhenAny(requestTask, delayTask);
  118. if (completedTask == requestTask)
  119. {
  120. result = await requestTask;
  121. }
  122. else
  123. {
  124. cts.Cancel();
  125. result.success = false;
  126. result.message = "放弃转链";
  127. result.reason = "请求超时";
  128. result.itemName = "点击打开淘宝APP";
  129. }
  130. return new APIResult(new
  131. {
  132. success = true,
  133. message = "ok",
  134. result,
  135. });
  136. }
  137. [HttpGet]
  138. public async Task<ActionResult> testThread()
  139. {
  140. // 查看默认的最小和最大线程数
  141. ThreadPool.GetMinThreads(out int defaultMinWorker, out int defaultMinIOC);
  142. ThreadPool.GetMaxThreads(out int defaultMaxWorker, out int defaultMaxIOC);
  143. string tmp = $"Default Min worker threads: {defaultMinWorker}, Min I/O completion threads: {defaultMinIOC}";
  144. string tmp2 = $"Default Max worker threads: {defaultMaxWorker}, Max I/O completion threads: {defaultMaxIOC}";
  145. // 获取当前线程池中可用的工作线程数和 I/O 完成端口线程数
  146. ThreadPool.GetAvailableThreads(out int availableWorkerThreads, out int availableIOCompletionThreads);
  147. string tmp3 = $"Current available worker threads: {availableWorkerThreads}, available I/O completion threads: {availableIOCompletionThreads}";
  148. return new APIResult(new
  149. {
  150. tmp,
  151. tmp2,
  152. tmp3,
  153. });
  154. }
  155. [HttpPost]
  156. public async Task<ActionResult> test1([FromBody] JsonElement form)
  157. {
  158. NotifyCore.Notify(new NifyMessage
  159. {
  160. message = $"【淘宝联盟:test】cookie 掉线",
  161. priority = NifyMessagePriority.high,
  162. tags = ["red_circle"]
  163. });
  164. return new APIResult(new
  165. {
  166. success = true,
  167. message = "ok"
  168. });
  169. }
  170. [HttpGet]
  171. public async Task<ActionResult> xxx()
  172. {
  173. var list = TkPoolCore.List();
  174. if (list == null) return new APIResult(new { success = false, message = "没有有效账号", });
  175. string message = string.Empty;
  176. foreach (var account in list)
  177. {
  178. try
  179. {
  180. var alimama = new AlimamaPlus(account);
  181. alimama.RenewCookie();
  182. }
  183. catch (Exception ex)
  184. {
  185. message = $"【cookie续期】xxxx\n{ex.Message}\n{ex.StackTrace}";
  186. NotifyCore.Notify(new NifyMessage
  187. {
  188. message = message,
  189. priority = NifyMessagePriority.high,
  190. tags = ["red_circle"]
  191. });
  192. continue;
  193. }
  194. }
  195. return new APIResult(new { success = true, message = "ok" });
  196. }
  197. [HttpPost]
  198. public async Task<ActionResult> Upfile(IFormFile file, [FromQuery] string dir = "")
  199. {
  200. // 检查文件是否为空
  201. if (file == null || file.Length == 0) throw new APIException("无效文件");
  202. using MemoryStream stream = new();
  203. file.CopyTo(stream);
  204. var bytes = stream.ToArray();
  205. var result = await ImageHostingPlus.Dianping(file.FileName, bytes);
  206. //result = ImageHostingPlus.Dianping2(file);
  207. return new APIResult(new { data = result });
  208. }
  209. }
  210. }