TestController.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. using TencentCloud.Oceanus.V20190422.Models;
  17. namespace molilian.api.Controllers
  18. {
  19. [ApiController]
  20. [Route("[controller]/[action]")]
  21. public class TestController : ControllerBase
  22. {
  23. protected IHttpContextAccessor _accessor;
  24. public TestController(IHttpContextAccessor accessor)
  25. {
  26. _accessor = accessor;
  27. }
  28. [HttpGet]
  29. public async Task<ActionResult> ChangePublicIp(int id)
  30. {
  31. id = id >= 20000 ? id - 20000 : id;
  32. var proxy_node = new DBContext.Table("proxy_nodes").Get<dynamic>(id);
  33. if (proxy_node == null) return new APIResult(new { success = false, msg = "没有匹配的 proxy_nodes 记录" });
  34. int aliyun_id = proxy_node.aliyun_id;
  35. string proxy_server = proxy_node.server;
  36. var account = new DBContext.Table("aliyun_pool").Get<dynamic>(aliyun_id);
  37. if (account == null) return new APIResult(new { success = false, msg = "没有匹配的 aliyun_pool 记录" });
  38. var uri = new Uri(proxy_server);
  39. string privateIp = uri.Host;
  40. AliyunCore core = new AliyunCore(account.id);
  41. var success = core.ChangePublicIp(privateIp);
  42. return new APIResult(new { success = "ok" });
  43. }
  44. [HttpGet]
  45. //第一部 先加一个网卡,
  46. public async Task<ActionResult> CreateNetworkInterface(int id, string ecsid)
  47. {
  48. AliyunCore core = new AliyunCore(id);
  49. //创建弹性网卡并绑定公网IP
  50. var success = core.EcsCreateNetworkInterface("cn-beijing", ecsid);
  51. return new APIResult(new { success });
  52. }
  53. [HttpGet]
  54. //第二部 给网卡绑定很多个辅助ip
  55. public async Task<ActionResult> EcsAssignPrivateIpAddresses(int id, string ecsid, int count)
  56. {
  57. AliyunCore core = new AliyunCore(id);
  58. //创建弹性网卡并绑定公网IP
  59. var success = core.EcsAssignPrivateIpAddresses("cn-beijing", ecsid, count);
  60. return new APIResult(new { success });
  61. }
  62. [HttpGet]
  63. public async Task<ActionResult> QueryAccountBalance()
  64. {
  65. AliyunPlus plus = new AliyunPlus("LTAI5tQbkTjtULQcrWGaw2VJ", "UIkkolVVEddooOKOIUByCqymZkK6ZA");
  66. var response = plus.QueryAccountBalance();
  67. var balance = response.Body.Data.AvailableAmount;
  68. return new APIResult(new { response });
  69. }
  70. //[HttpGet]
  71. //public async Task<ActionResult> test(string table = "tk_parse_logs_shop", int count = 100)
  72. //{
  73. // //table = "tk_parse_logs_shop";
  74. // //table = "tk_parse_logs_live";
  75. // //table = "tk_parse_logs_video";
  76. // string filter = "success=0 AND message='放弃转链' AND reason='非标准链接'";
  77. // for (int i = 0; i < 100; i++)
  78. // {
  79. // try
  80. // {
  81. // var list = new DBContext.Table(table)
  82. // .Where(filter, new { })
  83. // .Limit(count).Select<TkDataDTO>();
  84. // if (!list.Any())
  85. // {
  86. // return new APIResult(new
  87. // {
  88. // success = false,
  89. // message = "所有任务完成"
  90. // });
  91. // }
  92. // foreach (var item in list)
  93. // {
  94. // TkPoolDTO? account = TkPoolCore.GetOne(TkPoolCore.TkAction.parse);
  95. // if (account == null)
  96. // {
  97. // return new APIResult(new
  98. // {
  99. // success = false,
  100. // message = "没有工作账号"
  101. // });
  102. // }
  103. // var result = item.Convert2Json().Convert2Object<TkDataDTO>();
  104. // result.accountId = account.id;
  105. // result.accountName = account.company;
  106. // var alimama = new AlimamaPlus(account);
  107. // int timeout = alimama._config.rt_max;
  108. // using var cts = new CancellationTokenSource();
  109. // cts.CancelAfter(timeout);
  110. // result = await alimama.UnionParseAsync(item.content, result);
  111. // string reason = "非标准链接".Equals(result.reason) ? "非标准链接ok" : result.reason;
  112. // new DBContext.Table(table)
  113. // .Add("linkType", (int)result.link_type)
  114. // .Add("rawContent", result.rawContent)
  115. // .Add("success", result.success)
  116. // .Add("message", result.message)
  117. // .Add("reason", reason)
  118. // .Add("content", result.content)
  119. // .Add("itemId", result.itemId)
  120. // .Add("itemName", result.itemName)
  121. // .Add("pic", result.pic)
  122. // .Add("couponAmount", result.couponAmount)
  123. // .Add("promotionPrice", result.promotionPrice)
  124. // .Add("taoToken", result.taoToken)
  125. // .Add("shortLinkurl", result.shortLinkurl)
  126. // .Add("deeplink_url", result.deeplink_url)
  127. // .Add("num_iid", result.num_iid)
  128. // .Add("elapsedTime", result.elapsedTime)
  129. // .Add("elapsedTime2", result.elapsedTime2)
  130. // .Add("elapsedTime3", result.elapsedTime3)
  131. // .Add("subCode", result.subCode)
  132. // .Where("id=@id", new { item.id })
  133. // .Update();
  134. // }
  135. // }
  136. // catch
  137. // {
  138. // }
  139. // }
  140. // return new APIResult(new
  141. // {
  142. // success = true,
  143. // message = "ok"
  144. // });
  145. //}
  146. [HttpGet]
  147. public async Task<ActionResult> redu_douyin(string command)
  148. {
  149. ReduPlus plus = new ReduPlus("", "", "");
  150. var result = await plus.DouyinParse(command);
  151. return Content(result.Convert2Json());
  152. }
  153. [HttpGet]
  154. public async Task<ActionResult> redu_kuaishou(string command)
  155. {
  156. ReduPlus plus = new ReduPlus("", "", "");
  157. var result = await plus.KuaishouParse(command);
  158. return Content(result.Convert2Json());
  159. }
  160. [HttpPost]
  161. public async Task<ActionResult> test2([FromForm] string jsonContent, [FromForm] string testText)
  162. {
  163. // 用于存储输出结果的StringBuilder
  164. StringBuilder outputBuilder = new StringBuilder();
  165. // 解析JSON数据
  166. JsonDocument jsonDoc = JsonDocument.Parse(jsonContent);
  167. // 获取根元素
  168. JsonElement root = jsonDoc.RootElement;
  169. // 遍历规则
  170. foreach (JsonElement ruleSet in root.EnumerateArray())
  171. {
  172. string platformType = ruleSet.Read("platformType", string.Empty);
  173. string supplier = ruleSet.Read("supplier", string.Empty);
  174. outputBuilder.AppendLine($"平台类型: {platformType}, 供应商: {supplier}");
  175. JsonElement pwdRules = ruleSet.GetProperty("pwdRules");
  176. int idx = 0;
  177. foreach (JsonElement patternElement in pwdRules.EnumerateArray())
  178. {
  179. string pattern = patternElement.GetString();
  180. try
  181. {
  182. // 使用Regex类来编译正则表达式
  183. Regex compiledPattern = new Regex(pattern);
  184. if (compiledPattern.IsMatch(testText))
  185. {
  186. outputBuilder.AppendLine($"规则 {idx + 1}: 匹配\t{pattern}");
  187. }
  188. else
  189. {
  190. outputBuilder.AppendLine($"规则 {idx + 1}: 不匹配");
  191. }
  192. }
  193. catch (Exception e)
  194. {
  195. outputBuilder.AppendLine($"规则 {idx + 1}: 正则表达式错误 - {e.Message}\t{pattern}");
  196. }
  197. idx++;
  198. }
  199. }
  200. outputBuilder.AppendLine("测试完成");
  201. return Content(outputBuilder.ToString());
  202. }
  203. [HttpGet]
  204. public async Task<ActionResult> ip(string ip)
  205. {
  206. string result = IP2RegionPlus.Search(ip);
  207. return new APIResult(new
  208. {
  209. success = true,
  210. message = result
  211. });
  212. }
  213. [HttpPost]
  214. public async Task<ActionResult> testreg([FromBody] JsonElement form)
  215. {
  216. var content = form.Read("s", string.Empty);
  217. string shortLinkurl = AlimamaPlus.GetTaobaoLink(content);
  218. bool is_tao_token = AlimamaPlus.MatchRegexes(content, []);
  219. bool is_other = AlimamaPlus.MatchOtherInfo(content, []);
  220. return new APIResult(new
  221. {
  222. shortLinkurl,
  223. is_tao_token,
  224. is_other
  225. });
  226. }
  227. //[HttpGet]
  228. //public async Task<ActionResult> comparison_tk([FromQuery] int count = 100)
  229. //{
  230. // string cacheKey = ":lock_key:comparison_tk_logs";
  231. // int last_id = RedisHelper.Get<int>(cacheKey);
  232. // string filter = "id>@last_id";
  233. // var result = new DBContext.Table("comparison_tk_logs")
  234. // .Where(filter, new { last_id })
  235. // .Page(count, 1)
  236. // .Order("ID")
  237. // .PageList<TkDataDTO>(false);
  238. // var ip = "127.0.0.1";
  239. // var oaid = "test-comparison_tk_logs";
  240. // foreach (var item in result.List)
  241. // {
  242. // await UnionParseCore.TaobaoParseAsync(item.rawContent, ip, oaid);
  243. // RedisHelper.Set(cacheKey, item.id, 10 * 86400);
  244. // }
  245. // if (result.Count < count)
  246. // {
  247. // cacheKey = ":lock_key:start_comparison_tk";
  248. // RedisHelper.Set(cacheKey, 1, 600);
  249. // }
  250. // return new APIResult(new
  251. // {
  252. // success = true,
  253. // message = "ok"
  254. // });
  255. //}
  256. //[HttpGet]
  257. //public async Task<ActionResult> comparison_tk_raw([FromQuery] int count = 100)
  258. //{
  259. // string cacheKey = ":lock_key:comparison_tk_logs_raw";
  260. // int last_id = RedisHelper.Get<int>(cacheKey);
  261. // string filter = "id>@last_id";
  262. // var result = new DBContext.Table("comparison_tk_logs")
  263. // .Where(filter, new { last_id })
  264. // .Page(count, 1)
  265. // .Order("ID")
  266. // .PageList<TkDataDTO>(false);
  267. // var ip = "127.0.0.1";
  268. // var oaid = "test-comparison_tk_logs_raw";
  269. // foreach (var item in result.List)
  270. // {
  271. // await UnionParseCore.TaobaoParseAsync(item.rawContent, ip, oaid);
  272. // RedisHelper.Set(cacheKey, item.id, 10 * 86400);
  273. // }
  274. // if (result.Count < count)
  275. // {
  276. // cacheKey = ":lock_key:start_comparison_tk";
  277. // RedisHelper.Set(cacheKey, 1, 600);
  278. // }
  279. // return new APIResult(new
  280. // {
  281. // success = true,
  282. // message = "ok"
  283. // });
  284. //}
  285. [HttpGet]
  286. public async Task<ActionResult> testTask()
  287. {
  288. string url = "https://www.taobao.com";
  289. TkDataDTO result = new TkDataDTO();
  290. result.message = "init";
  291. result.success = true;
  292. int timeout = 1000;
  293. using var cts = new CancellationTokenSource();
  294. cts.CancelAfter(timeout);
  295. var requestTask = Task.Run(() => AlimamaPlus.testTaskAsync(result, cts.Token), cts.Token);
  296. var delayTask = Task.Delay(timeout, cts.Token);
  297. var completedTask = await Task.WhenAny(requestTask, delayTask);
  298. if (completedTask == requestTask)
  299. {
  300. result = await requestTask;
  301. }
  302. else
  303. {
  304. cts.Cancel();
  305. result.success = false;
  306. result.message = "放弃转链";
  307. result.reason = "请求超时";
  308. result.itemName = "点击打开淘宝APP";
  309. }
  310. return new APIResult(new
  311. {
  312. success = true,
  313. message = "ok",
  314. result,
  315. });
  316. }
  317. [HttpGet]
  318. public async Task<ActionResult> testThread()
  319. {
  320. // 查看默认的最小和最大线程数
  321. ThreadPool.GetMinThreads(out int defaultMinWorker, out int defaultMinIOC);
  322. ThreadPool.GetMaxThreads(out int defaultMaxWorker, out int defaultMaxIOC);
  323. string tmp = $"Default Min worker threads: {defaultMinWorker}, Min I/O completion threads: {defaultMinIOC}";
  324. string tmp2 = $"Default Max worker threads: {defaultMaxWorker}, Max I/O completion threads: {defaultMaxIOC}";
  325. // 获取当前线程池中可用的工作线程数和 I/O 完成端口线程数
  326. ThreadPool.GetAvailableThreads(out int availableWorkerThreads, out int availableIOCompletionThreads);
  327. string tmp3 = $"Current available worker threads: {availableWorkerThreads}, available I/O completion threads: {availableIOCompletionThreads}";
  328. return new APIResult(new
  329. {
  330. tmp,
  331. tmp2,
  332. tmp3,
  333. });
  334. }
  335. [HttpPost]
  336. public async Task<ActionResult> test1([FromBody] JsonElement form)
  337. {
  338. NotifyCore.Notify(new NifyMessage
  339. {
  340. message = $"【淘宝联盟:test】cookie 掉线",
  341. priority = NifyMessagePriority.high,
  342. tags = ["red_circle"]
  343. });
  344. return new APIResult(new
  345. {
  346. success = true,
  347. message = "ok"
  348. });
  349. }
  350. [HttpGet]
  351. public async Task<ActionResult> xxx()
  352. {
  353. var list = await TkPoolCore.ListAsync();
  354. if (list == null) return new APIResult(new { success = false, message = "没有有效账号", });
  355. string message = string.Empty;
  356. foreach (var account in list)
  357. {
  358. try
  359. {
  360. var alimama = new AlimamaPlus(account);
  361. alimama.RenewCookie();
  362. }
  363. catch (Exception ex)
  364. {
  365. message = $"【cookie续期】xxxx\n{ex.Message}\n{ex.StackTrace}";
  366. NotifyCore.Notify(new NifyMessage
  367. {
  368. message = message,
  369. priority = NifyMessagePriority.high,
  370. tags = ["red_circle"]
  371. });
  372. continue;
  373. }
  374. }
  375. return new APIResult(new { success = true, message = "ok" });
  376. }
  377. }
  378. }