TkController.cs 21 KB

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