TkController.cs 21 KB

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