TestController.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. using static Spire.Xls.Core.Spreadsheet.HTMLOptions;
  13. using System.Net.Http.Json;
  14. using System.Text.RegularExpressions;
  15. using System.Text;
  16. namespace molilian.api.Controllers
  17. {
  18. [ApiController]
  19. [Route("[controller]/[action]")]
  20. public class TestController : ControllerBase
  21. {
  22. protected IHttpContextAccessor _accessor;
  23. public TestController(IHttpContextAccessor accessor)
  24. {
  25. _accessor = accessor;
  26. }
  27. [HttpPost]
  28. public async Task<ActionResult> test([FromForm] string jsonContent, [FromForm] string testText)
  29. {
  30. // 用于存储输出结果的StringBuilder
  31. StringBuilder outputBuilder = new StringBuilder();
  32. // 解析JSON数据
  33. JsonDocument jsonDoc = JsonDocument.Parse(jsonContent);
  34. // 获取根元素
  35. JsonElement root = jsonDoc.RootElement;
  36. // 遍历规则
  37. foreach (JsonElement ruleSet in root.EnumerateArray())
  38. {
  39. string platformType = ruleSet.Read("platformType", string.Empty);
  40. string supplier = ruleSet.Read("supplier", string.Empty);
  41. outputBuilder.AppendLine($"平台类型: {platformType}, 供应商: {supplier}");
  42. JsonElement pwdRules = ruleSet.GetProperty("pwdRules");
  43. int idx = 0;
  44. foreach (JsonElement patternElement in pwdRules.EnumerateArray())
  45. {
  46. string pattern = patternElement.GetString();
  47. try
  48. {
  49. // 使用Regex类来编译正则表达式
  50. Regex compiledPattern = new Regex(pattern);
  51. if (compiledPattern.IsMatch(testText))
  52. {
  53. outputBuilder.AppendLine($"规则 {idx + 1}: 匹配\t{pattern}");
  54. }
  55. else
  56. {
  57. outputBuilder.AppendLine($"规则 {idx + 1}: 不匹配");
  58. }
  59. }
  60. catch (Exception e)
  61. {
  62. outputBuilder.AppendLine($"规则 {idx + 1}: 正则表达式错误 - {e.Message}\t{pattern}");
  63. }
  64. idx++;
  65. }
  66. }
  67. outputBuilder.AppendLine("测试完成");
  68. return Content(outputBuilder.ToString());
  69. }
  70. [HttpGet]
  71. public async Task<ActionResult> ip(string ip)
  72. {
  73. string result = IP2RegionPlus.Search(ip);
  74. return new APIResult(new
  75. {
  76. success = true,
  77. message = result
  78. });
  79. }
  80. [HttpPost]
  81. public async Task<ActionResult> testreg([FromBody] JsonElement form)
  82. {
  83. var content = form.Read("s", string.Empty);
  84. string shortLinkurl = AlimamaPlus.GetTaobaoLink(content);
  85. bool is_tao_token = AlimamaPlus.MatchRegexes(content, []);
  86. bool is_other = AlimamaPlus.MatchOtherInfo(content, []);
  87. return new APIResult(new
  88. {
  89. shortLinkurl,
  90. is_tao_token,
  91. is_other
  92. });
  93. }
  94. //[HttpGet]
  95. //public async Task<ActionResult> comparison_tk([FromQuery] int count = 100)
  96. //{
  97. // string cacheKey = ":lock_key:comparison_tk_logs";
  98. // int last_id = RedisHelper.Get<int>(cacheKey);
  99. // string filter = "id>@last_id";
  100. // var result = new DBContext.Table("comparison_tk_logs")
  101. // .Where(filter, new { last_id })
  102. // .Page(count, 1)
  103. // .Order("ID")
  104. // .PageList<TkDataDTO>(false);
  105. // var ip = "127.0.0.1";
  106. // var oaid = "test-comparison_tk_logs";
  107. // foreach (var item in result.List)
  108. // {
  109. // await UnionParseCore.TaobaoParseAsync(item.rawContent, ip, oaid);
  110. // RedisHelper.Set(cacheKey, item.id, 10 * 86400);
  111. // }
  112. // if (result.Count < count)
  113. // {
  114. // cacheKey = ":lock_key:start_comparison_tk";
  115. // RedisHelper.Set(cacheKey, 1, 600);
  116. // }
  117. // return new APIResult(new
  118. // {
  119. // success = true,
  120. // message = "ok"
  121. // });
  122. //}
  123. //[HttpGet]
  124. //public async Task<ActionResult> comparison_tk_raw([FromQuery] int count = 100)
  125. //{
  126. // string cacheKey = ":lock_key:comparison_tk_logs_raw";
  127. // int last_id = RedisHelper.Get<int>(cacheKey);
  128. // string filter = "id>@last_id";
  129. // var result = new DBContext.Table("comparison_tk_logs")
  130. // .Where(filter, new { last_id })
  131. // .Page(count, 1)
  132. // .Order("ID")
  133. // .PageList<TkDataDTO>(false);
  134. // var ip = "127.0.0.1";
  135. // var oaid = "test-comparison_tk_logs_raw";
  136. // foreach (var item in result.List)
  137. // {
  138. // await UnionParseCore.TaobaoParseAsync(item.rawContent, ip, oaid);
  139. // RedisHelper.Set(cacheKey, item.id, 10 * 86400);
  140. // }
  141. // if (result.Count < count)
  142. // {
  143. // cacheKey = ":lock_key:start_comparison_tk";
  144. // RedisHelper.Set(cacheKey, 1, 600);
  145. // }
  146. // return new APIResult(new
  147. // {
  148. // success = true,
  149. // message = "ok"
  150. // });
  151. //}
  152. [HttpGet]
  153. public async Task<ActionResult> testTask()
  154. {
  155. string url = "https://www.taobao.com";
  156. TkDataDTO result = new TkDataDTO();
  157. result.message = "init";
  158. result.success = true;
  159. int timeout = 1000;
  160. using var cts = new CancellationTokenSource();
  161. cts.CancelAfter(timeout);
  162. var requestTask = Task.Run(() => AlimamaPlus.testTaskAsync(result, cts.Token), cts.Token);
  163. var delayTask = Task.Delay(timeout, cts.Token);
  164. var completedTask = await Task.WhenAny(requestTask, delayTask);
  165. if (completedTask == requestTask)
  166. {
  167. result = await requestTask;
  168. }
  169. else
  170. {
  171. cts.Cancel();
  172. result.success = false;
  173. result.message = "放弃转链";
  174. result.reason = "请求超时";
  175. result.itemName = "点击打开淘宝APP";
  176. }
  177. return new APIResult(new
  178. {
  179. success = true,
  180. message = "ok",
  181. result,
  182. });
  183. }
  184. [HttpGet]
  185. public async Task<ActionResult> testThread()
  186. {
  187. // 查看默认的最小和最大线程数
  188. ThreadPool.GetMinThreads(out int defaultMinWorker, out int defaultMinIOC);
  189. ThreadPool.GetMaxThreads(out int defaultMaxWorker, out int defaultMaxIOC);
  190. string tmp = $"Default Min worker threads: {defaultMinWorker}, Min I/O completion threads: {defaultMinIOC}";
  191. string tmp2 = $"Default Max worker threads: {defaultMaxWorker}, Max I/O completion threads: {defaultMaxIOC}";
  192. // 获取当前线程池中可用的工作线程数和 I/O 完成端口线程数
  193. ThreadPool.GetAvailableThreads(out int availableWorkerThreads, out int availableIOCompletionThreads);
  194. string tmp3 = $"Current available worker threads: {availableWorkerThreads}, available I/O completion threads: {availableIOCompletionThreads}";
  195. return new APIResult(new
  196. {
  197. tmp,
  198. tmp2,
  199. tmp3,
  200. });
  201. }
  202. [HttpPost]
  203. public async Task<ActionResult> test1([FromBody] JsonElement form)
  204. {
  205. NotifyCore.Notify(new NifyMessage
  206. {
  207. message = $"【淘宝联盟:test】cookie 掉线",
  208. priority = NifyMessagePriority.high,
  209. tags = ["red_circle"]
  210. });
  211. return new APIResult(new
  212. {
  213. success = true,
  214. message = "ok"
  215. });
  216. }
  217. [HttpGet]
  218. public async Task<ActionResult> xxx()
  219. {
  220. var list = TkPoolCore.List();
  221. if (list == null) return new APIResult(new { success = false, message = "没有有效账号", });
  222. string message = string.Empty;
  223. foreach (var account in list)
  224. {
  225. try
  226. {
  227. var alimama = new AlimamaPlus(account);
  228. alimama.RenewCookie();
  229. }
  230. catch (Exception ex)
  231. {
  232. message = $"【cookie续期】xxxx\n{ex.Message}\n{ex.StackTrace}";
  233. NotifyCore.Notify(new NifyMessage
  234. {
  235. message = message,
  236. priority = NifyMessagePriority.high,
  237. tags = ["red_circle"]
  238. });
  239. continue;
  240. }
  241. }
  242. return new APIResult(new { success = true, message = "ok" });
  243. }
  244. [HttpPost]
  245. public async Task<ActionResult> Upfile(IFormFile file, [FromQuery] string dir = "")
  246. {
  247. // 检查文件是否为空
  248. if (file == null || file.Length == 0) throw new APIException("无效文件");
  249. using MemoryStream stream = new();
  250. file.CopyTo(stream);
  251. var bytes = stream.ToArray();
  252. var result = await ImageHostingPlus.Dianping(file.FileName, bytes);
  253. //result = ImageHostingPlus.Dianping2(file);
  254. return new APIResult(new { data = result });
  255. }
  256. }
  257. }