TkController.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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. namespace molilian.api.Controllers
  14. {
  15. [ApiController]
  16. [Route("[controller]/[action]")]
  17. public class TkController : ControllerBase
  18. {
  19. protected IHttpContextAccessor _accessor;
  20. public TkController(IHttpContextAccessor accessor)
  21. {
  22. _accessor = accessor;
  23. }
  24. /// <summary>
  25. /// oppo调用
  26. /// </summary>
  27. /// <param name="s">正文</param>
  28. /// <param name="c">渠道。tb/jd</param>
  29. /// <param name="a">分配的客户端账号</param>
  30. /// <param name="t">11位时间戳</param>
  31. /// <param name="sign">签名</param>
  32. /// <param name="ip">客户的IP</param>
  33. /// <param name="oaid">客户的唯一ID</param>
  34. /// <returns></returns>
  35. [HttpPost]
  36. public async Task<ActionResult> unionParse([FromBody] JsonElement form, [FromQuery] string a = "", [FromQuery] int t = 0, [FromQuery] string sign = "")
  37. {
  38. //正文
  39. var content = form.Read("s", string.Empty);
  40. var channel = form.Read("c", string.Empty);
  41. var ip = form.Read("ip", string.Empty);
  42. var oaid = form.Read("oaid", string.Empty);
  43. //验证签名
  44. if (string.IsNullOrEmpty(a) || string.IsNullOrEmpty(sign))
  45. {
  46. return new APIResult(new { success = false, message = "验证错误" }, APIResultCodeEnum.Unauthorized);
  47. }
  48. DateTime time1 = t.Convert2Datetime();
  49. if (time1 < DateTime.Now.AddMinutes(-10))
  50. {
  51. return new APIResult(new { success = false, message = "验证错误2" }, APIResultCodeEnum.Unauthorized);
  52. }
  53. var account = ApiAccountCore.GetOne(a);
  54. if (account == null)
  55. {
  56. return new APIResult(new { success = false, message = "验证错误3" }, APIResultCodeEnum.Unauthorized);
  57. }
  58. string app_sign = $"{account.api_secret}@{t}".MD5();
  59. if (sign != app_sign)
  60. {
  61. return new APIResult(new { success = false, message = "验证错误4" }, APIResultCodeEnum.Unauthorized);
  62. }
  63. return await UnionParseCore.UnionParseAsync(content, channel, ip, oaid);
  64. }
  65. [HttpPost]
  66. public async Task<ActionResult> unsafeParse([FromBody] JsonElement form)
  67. {
  68. var content = form.Read("s", string.Empty);
  69. var channel = form.Read("c", string.Empty);
  70. var ip = form.Read("ip", string.Empty);
  71. var oaid = form.Read("oaid", string.Empty);
  72. #if DEBUG
  73. ip = "127.0.0.1";
  74. oaid = "test-oaid";
  75. #endif
  76. return await UnionParseCore.UnionParseAsync(content, channel, ip, oaid);
  77. }
  78. [HttpPost]
  79. public async Task<ActionResult> unionCoupon([FromBody] JsonElement form, [FromQuery] string a = "", [FromQuery] int t = 0, [FromQuery] string sign = "")
  80. {
  81. //正文
  82. var content = form.Read("s", string.Empty);
  83. var channel = form.Read("c", string.Empty);
  84. var ip = form.Read("ip", string.Empty);
  85. var oaid = form.Read("oaid", string.Empty);
  86. //验证签名
  87. if (string.IsNullOrEmpty(a) || string.IsNullOrEmpty(sign))
  88. {
  89. return new APIResult(new { success = false, message = "验证错误" }, APIResultCodeEnum.Unauthorized);
  90. }
  91. DateTime time1 = t.Convert2Datetime();
  92. if (time1 < DateTime.Now.AddMinutes(-10))
  93. {
  94. return new APIResult(new { success = false, message = "验证错误2" }, APIResultCodeEnum.Unauthorized);
  95. }
  96. var account = ApiAccountCore.GetOne(a);
  97. if (account == null)
  98. {
  99. return new APIResult(new { success = false, message = "验证错误3" }, APIResultCodeEnum.Unauthorized);
  100. }
  101. string app_sign = $"{account.api_secret}@{t}".MD5();
  102. if (sign != app_sign)
  103. {
  104. return new APIResult(new { success = false, message = "验证错误4" }, APIResultCodeEnum.Unauthorized);
  105. }
  106. return await UnionCouponCore.UnionCouponAsync(content, channel, ip, oaid);
  107. }
  108. [HttpPost]
  109. public async Task<ActionResult> unsafeCoupon([FromBody] JsonElement form)
  110. {
  111. var content = form.Read("s", string.Empty);
  112. var channel = form.Read("c", string.Empty);
  113. var ip = form.Read("ip", string.Empty);
  114. var oaid = form.Read("oaid", string.Empty);
  115. #if DEBUG
  116. ip = "127.0.0.1";
  117. oaid = "test-oaid";
  118. #endif
  119. return await UnionCouponCore.UnionCouponAsync(content, channel, ip, oaid);
  120. }
  121. /// <summary>
  122. /// 第三方调用
  123. /// </summary>
  124. /// <param name="s"></param>
  125. /// <param name="s2"></param>
  126. /// <returns></returns>
  127. [HttpGet]
  128. public async Task<ActionResult> parse(string s, string s2 = "", string ip = "", string oaid = "")
  129. {
  130. AlimamaPlus alimama = null;
  131. ip = AlimamaPlus.OppoDecode(ip);
  132. oaid = AlimamaPlus.OppoDecode(oaid);
  133. var result = AlimamaPlus.GetFormattedObject(s, ip, oaid);
  134. string body = "";
  135. string content = s.UrlDecode();
  136. string content2 = s2.UrlDecode();
  137. string account_name = string.Empty;
  138. result.rawContent = content;
  139. result.rawContent2 = content2;
  140. try
  141. {
  142. if (AlimamaPlus.ShouldIgnoreRequest(ip, oaid, out string reason))
  143. {
  144. result.success = false;
  145. result.message = "放弃转链";
  146. result.reason = reason;
  147. _ = TkLogCore.LogAsync(result);
  148. return new APIResult(new
  149. {
  150. result.success,
  151. result.message,
  152. result.content,
  153. result.couponAmount,
  154. result.taoToken,
  155. result.shortLinkurl,
  156. result.deeplink_url,
  157. });
  158. }
  159. var account = TkPoolCore.GetOne(TkPoolCore.TkAction.parse);
  160. if (account == null)
  161. {
  162. result.success = false;
  163. result.message = "放弃转链";
  164. result.reason = "没有匹配账号";
  165. _ = TkLogCore.LogAsync(result);
  166. return new APIResult(new
  167. {
  168. result.success,
  169. result.message,
  170. result.content,
  171. result.couponAmount,
  172. result.taoToken,
  173. result.shortLinkurl,
  174. result.deeplink_url,
  175. });
  176. }
  177. result.accountId = account.id;
  178. account_name = account.name;
  179. alimama = new AlimamaPlus(account);
  180. if (alimama.ShouldIgnoreOtherAff(s2))
  181. {
  182. result.success = false;
  183. result.message = "放弃转链";
  184. result.reason = "其他推广链接";
  185. _ = TkLogCore.LogAsync(result);
  186. return new APIResult(new
  187. {
  188. result.success,
  189. result.message,
  190. result.content,
  191. result.couponAmount,
  192. result.taoToken,
  193. result.shortLinkurl,
  194. result.deeplink_url,
  195. });
  196. }
  197. result = await alimama.UnionParseAsync(content, result);
  198. }
  199. catch (Exception ex)
  200. {
  201. if (ex.Message.Contains("A task was canceled"))
  202. {
  203. result.success = false;
  204. result.message = "放弃转链";
  205. result.reason = "请求超时";
  206. }
  207. else
  208. {
  209. //new LoggerLibrary("api_error", "parse")
  210. //.Info(s, s2)
  211. //.Info($"【taobao】{account_name}", body)
  212. //.Info(ex.Message, ex.StackTrace)
  213. //.SaveAsync();
  214. NotifyCore.Notify(new NifyMessage
  215. {
  216. message = $"【转链接口异常】{account_name}\n{content}\n{content2}\n{body}\n\n{ex.Message}\n{ex.StackTrace}",
  217. priority = NifyMessagePriority.high,
  218. tags = ["red_circle"]
  219. });
  220. result.success = false;
  221. result.message = "放弃转链";
  222. result.reason = "转链接口异常";
  223. }
  224. }
  225. _ = TkLogCore.LogAsync(result, alimama);
  226. return new APIResult(new
  227. {
  228. result.success,
  229. result.message,
  230. result.content,
  231. result.couponAmount,
  232. result.taoToken,
  233. result.shortLinkurl,
  234. result.deeplink_url,
  235. });
  236. }
  237. [HttpGet]
  238. public ActionResult parse2(string s, string s2 = "", string ip = "", string oaid = "")
  239. {
  240. AlimamaPlus alimama = null;
  241. ip = AlimamaPlus.OppoDecode(ip);
  242. oaid = AlimamaPlus.OppoDecode(oaid);
  243. var result = AlimamaPlus.GetFormattedObject(s, ip, oaid);
  244. string body = "";
  245. string content = s.UrlDecode();
  246. string content2 = s2.UrlDecode();
  247. string account_name = string.Empty;
  248. result.rawContent = content;
  249. result.rawContent2 = content2;
  250. try
  251. {
  252. if (AlimamaPlus.ShouldIgnoreRequest(ip, oaid, out string reason))
  253. {
  254. result.success = false;
  255. result.message = "放弃转链";
  256. result.reason = reason;
  257. _ = TkLogCore.LogAsync(result);
  258. return new APIResult(new
  259. {
  260. result.success,
  261. result.message,
  262. result.content,
  263. result.couponAmount,
  264. result.taoToken,
  265. result.shortLinkurl,
  266. result.deeplink_url,
  267. });
  268. }
  269. var account = TkPoolCore.GetOne(TkPoolCore.TkAction.parse);
  270. if (account == null)
  271. {
  272. result.success = false;
  273. result.message = "放弃转链";
  274. result.reason = "没有匹配账号";
  275. _ = TkLogCore.LogAsync(result);
  276. return new APIResult(new
  277. {
  278. result.success,
  279. result.message,
  280. result.content,
  281. result.couponAmount,
  282. result.taoToken,
  283. result.shortLinkurl,
  284. result.deeplink_url,
  285. });
  286. }
  287. account_name = account.name;
  288. alimama = new AlimamaPlus(account);
  289. if (alimama.ShouldIgnoreOtherAff(s2))
  290. {
  291. result.success = false;
  292. result.message = "放弃转链";
  293. result.reason = "其他推广链接";
  294. _ = TkLogCore.LogAsync(result);
  295. return new APIResult(new
  296. {
  297. result.success,
  298. result.message,
  299. result.content,
  300. result.couponAmount,
  301. result.taoToken,
  302. result.shortLinkurl,
  303. result.deeplink_url,
  304. });
  305. }
  306. alimama.UnionParse(content, ref result);
  307. //result.ip = ip;
  308. //result.oaid = oaid;
  309. }
  310. catch (Exception ex)
  311. {
  312. new LoggerLibrary("api_error", "parse")
  313. .Info(s, s2)
  314. .Info($"【taobao】{account_name}", body)
  315. .Info(ex.Message, ex.StackTrace)
  316. .SaveAsync();
  317. NotifyCore.Notify(new NifyMessage
  318. {
  319. message = $"【转链接口异常】{account_name}\n{content}\n{content2}\n{body}\n\n{ex.Message}\n{ex.StackTrace}",
  320. priority = NifyMessagePriority.high,
  321. tags = ["red_circle"]
  322. });
  323. result.success = false;
  324. result.message = "放弃转链";
  325. result.reason = "转链接口异常";
  326. }
  327. _ = TkLogCore.LogAsync(result, alimama);
  328. return new APIResult(new
  329. {
  330. result.success,
  331. result.message,
  332. result.content,
  333. result.couponAmount,
  334. result.taoToken,
  335. result.shortLinkurl,
  336. result.deeplink_url,
  337. });
  338. }
  339. [HttpGet]
  340. public ActionResult jd_parse(string s, string s2 = "", string ip = "", string oaid = "")
  341. {
  342. var result = JdUnionPlus.GetFormattedObject(s, ip, oaid);
  343. string body = "";
  344. string content = s.UrlDecode();
  345. string content2 = s2.UrlDecode();
  346. try
  347. {
  348. if (AlimamaPlus.ShouldIgnoreRequest(ip, oaid, out string reason))
  349. {
  350. result.success = false;
  351. result.message = "放弃转链";
  352. result.reason = reason;
  353. result.rawContent = content;
  354. result.rawContent2 = content2;
  355. TkLogCore.LogAsync(result);
  356. return new APIResult(result);
  357. }
  358. var api = DataokeCore.GetOne();
  359. if (api == null)
  360. {
  361. result.success = false;
  362. result.message = "放弃转链";
  363. result.reason = "没有匹配账号";
  364. result.rawContent = content;
  365. result.rawContent2 = content2;
  366. TkLogCore.LogAsync(result);
  367. return new APIResult(result);
  368. }
  369. var account = JdPoolCore.GetOne();
  370. if (account == null)
  371. {
  372. result.success = false;
  373. result.message = "放弃转链";
  374. result.reason = "没有匹配账号";
  375. result.rawContent = content;
  376. result.rawContent2 = content2;
  377. TkLogCore.LogAsync(result);
  378. return new APIResult(result);
  379. }
  380. result = DataokeCore.JdParse(result, api, account, content);
  381. }
  382. catch (Exception ex)
  383. {
  384. new LoggerLibrary("api_error")
  385. .Info(s, s2)
  386. .Info("【jd】", body)
  387. .Info(ex.Message, ex.StackTrace)
  388. .SaveAsync();
  389. NotifyCore.Notify(new NifyMessage
  390. {
  391. message = $"【转链接口异常】\n{content}\n{content2}\n{body}\n\n{ex.Message}\n{ex.StackTrace}",
  392. priority = NifyMessagePriority.high,
  393. tags = ["red_circle"]
  394. });
  395. result.success = false;
  396. result.message = "放弃转链";
  397. result.reason = "转链接口异常";
  398. }
  399. result.rawContent = content;
  400. result.rawContent2 = content2;
  401. TkLogCore.LogAsync(result);
  402. return new APIResult(new
  403. {
  404. result.success,
  405. result.message,
  406. result.shortLinkurl,
  407. result.deeplink_url,
  408. });
  409. }
  410. //[HttpGet]
  411. //public Task<ActionResult> parse3(string s, string s2, string ip = "", string oaid = "")
  412. //{
  413. // var result = AlimamaPlus.GetFormattedObject(s, ip, oaid);
  414. // string body = "";
  415. // string content = s.UrlDecode();
  416. // string content2 = s2.UrlDecode();
  417. // try
  418. // {
  419. // if (AlimamaPlus.ShouldIgnoreRequest("", ""))
  420. // {
  421. // result.success = false;
  422. // result.message = "放弃转链";
  423. // }
  424. // else
  425. // {
  426. // var account = VeapiPoolCore.GetOne();
  427. // if (account == null)
  428. // {
  429. // result.success = false;
  430. // result.message = "内部错误(500)";
  431. // }
  432. // else
  433. // {
  434. // var core = new VeapiPlus(account.name, account.key, account.sessionKey);
  435. // result = core.UnionParse(content);
  436. // }
  437. // }
  438. // }
  439. // catch (Exception ex)
  440. // {
  441. // NotifyCore.Notify(new NifyMessage
  442. // {
  443. // message = $"【转链接口异常】\n{body}",
  444. // priority = NifyMessagePriority.high,
  445. // tags = ["red_circle"]
  446. // });
  447. // result.success = false;
  448. // result.message = "转链接口异常";
  449. // }
  450. // _ = TkLogCore.Log(content, content2, result);
  451. // return Task.FromResult<ActionResult>(new APIResult(result));
  452. //}
  453. [HttpGet("{ignorePercentage}")]
  454. public ActionResult set_ignore(int ignorePercentage = 0)
  455. {
  456. TkConfigCore.Update(ignorePercentage);
  457. return new APIResult(new { success = true, message = "ok" });
  458. }
  459. [HttpGet("{ignorePercentage}")]
  460. public ActionResult set_jd_ignore(int ignorePercentage = 0)
  461. {
  462. TkConfigCore.JdUpdate(ignorePercentage);
  463. return new APIResult(new { success = true, message = "ok" });
  464. }
  465. [HttpGet]
  466. public ActionResult reload()
  467. {
  468. ApiAccountCore.Refresh();
  469. ProxyNodesCore.Refresh();
  470. DataokeCore.Refresh();
  471. JdPoolCore.Refresh();
  472. PangolinPoolCore.Refresh();
  473. TkConfigCore.Refresh();
  474. TkPoolCore.Refresh();
  475. VeapiPoolCore.Refresh();
  476. return new APIResult(new
  477. {
  478. success = true,
  479. message = "ok",
  480. });
  481. }
  482. [HttpPost]
  483. public ActionResult updateCookies([FromBody] JsonElement form)
  484. {
  485. string cookie = form.Read<string>("cookie", string.Empty);
  486. var user_agent = _accessor.HttpContext.Request.UserAgent();
  487. TkPoolCore.UpdateCookies(cookie, user_agent);
  488. return new APIResult(new
  489. {
  490. success = true,
  491. message = "ok"
  492. });
  493. }
  494. [HttpGet]
  495. public async Task<ActionResult> dplist([FromQuery] string query = "", [FromQuery] string a = "", [FromQuery] int t = 0, [FromQuery] string sign = "")
  496. {
  497. #if DEBUG
  498. #else
  499. //验证签名
  500. if (string.IsNullOrEmpty(a) || string.IsNullOrEmpty(sign))
  501. {
  502. return new APIResult(new { success = false, message = "验证错误" }, APIResultCodeEnum.Unauthorized);
  503. }
  504. DateTime time1 = t.Convert2Datetime();
  505. if (time1 < DateTime.Now.AddMinutes(-10))
  506. {
  507. return new APIResult(new { success = false, message = "验证错误2" }, APIResultCodeEnum.Unauthorized);
  508. }
  509. var account = ApiAccountCore.GetOne(a);
  510. if (account == null)
  511. {
  512. return new APIResult(new { success = false, message = "验证错误3" }, APIResultCodeEnum.Unauthorized);
  513. }
  514. string app_sign = $"{account.api_secret}@{t}".MD5();
  515. if (sign != app_sign)
  516. {
  517. return new APIResult(new { success = false, message = "验证错误4" }, APIResultCodeEnum.Unauthorized);
  518. }
  519. if (string.IsNullOrEmpty(query))
  520. {
  521. return new APIResult(new { success = false, message = "查询条件不能为空" });
  522. }
  523. #endif
  524. string filter = $"batchId=@query";
  525. var account_list = TkPoolCore.List();
  526. List<int> account_ids = [];
  527. if (account_list.Any())
  528. {
  529. account_ids = account_list.Where(e => e.enable_fake_click).Select(e => e.id).ToList();
  530. filter += $" AND accountId IN @account_ids";
  531. }
  532. string orderBy = "paid_time ASC";
  533. var data = new DBContext.Table("tk_order_tracking")
  534. .Where(filter, new { query, account_ids })
  535. .Order(orderBy)
  536. .Select<TkOrderTrackingDTO>();
  537. List<object> list = [];
  538. foreach (var item in data)
  539. {
  540. list.Add(new { url = item.deeplinkUrl, count = item.click_num, time = item.exp_time });
  541. }
  542. var clientIp = _accessor.HttpContext.GetUserIp();
  543. LoggerLibrary log = new LoggerLibrary("api", "dplist");
  544. log.Info($"{clientIp}\t{query}\tcount:{list.Count}");
  545. var ids = data.Select(e => e.Id).ToArray();
  546. log.Info(string.Join(',', ids));
  547. log.SaveAsync();
  548. return new APIResult(new
  549. {
  550. success = true,
  551. message = list.Count == 0 ? "none" : "ok",
  552. count = list.Count,
  553. data = list,
  554. });
  555. }
  556. [HttpGet]
  557. public async Task<ActionResult> unsafeParseDpList([FromQuery] string query = "")
  558. {
  559. string filter = $"batchId=@query";
  560. var account_list = TkPoolCore.List();
  561. List<int> account_ids = [];
  562. if (account_list.Any())
  563. {
  564. account_ids = account_list.Where(e => e.enable_fake_click).Select(e => e.id).ToList();
  565. filter += $" AND accountId IN @account_ids";
  566. }
  567. string orderBy = "paid_time ASC";
  568. var data = new DBContext.Table("tk_order_tracking")
  569. .Where(filter, new { query, account_ids })
  570. .Order(orderBy)
  571. .Select<TkOrderTrackingDTO>();
  572. List<object> list = [];
  573. foreach (var item in data)
  574. {
  575. list.Add(new { url = item.deeplinkUrl, count = item.click_num, time = item.exp_time });
  576. }
  577. var clientIp = _accessor.HttpContext.GetUserIp();
  578. LoggerLibrary log = new LoggerLibrary("api", "unsafeParseDpList");
  579. log.Info($"{clientIp}\t{query}\tcount:{list.Count}");
  580. var ids = data.Select(e => e.Id).ToArray();
  581. log.Info(string.Join(',', ids));
  582. log.SaveAsync();
  583. return new APIResult(new
  584. {
  585. success = true,
  586. message = list.Count == 0 ? "none" : "ok",
  587. count = list.Count,
  588. data = list,
  589. });
  590. }
  591. }
  592. }