TkController.cs 21 KB

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