TkController.cs 21 KB

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