TkController.cs 20 KB

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