TkController.cs 23 KB

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