JdUnionPlus.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc.Controllers;
  3. using Microsoft.AspNetCore.Mvc.Filters;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using dodohold.core;
  9. using Dataoke;
  10. using System.Text.RegularExpressions;
  11. using Org.BouncyCastle.Ocsp;
  12. using System.Diagnostics;
  13. using TencentCloud.Fmu.V20191213.Models;
  14. using Microsoft.VisualBasic;
  15. using System.Text.Json;
  16. using static System.Runtime.InteropServices.JavaScript.JSType;
  17. using System.Security.Cryptography;
  18. using Google.Protobuf.WellKnownTypes;
  19. using Microsoft.Extensions.FileSystemGlobbing.Internal;
  20. using System.Web;
  21. namespace molilian.core
  22. {
  23. public partial class JdUnionPlus
  24. {
  25. private const string VIDEO_URL_PREFIX = "https://h5.m.jd.com/active/faxian/video/index.html";
  26. public static string GetLink(string content)
  27. {
  28. if (string.IsNullOrEmpty(content)) return content;
  29. string result = content;
  30. //string pattern = @"https?:\/\/?([\da-z\.-]+)\.([a-z]{2,6})(:\d{1,5})?([\/\w\.-]*)*\/?(#[\S]+)?(\?[\S]+)?";
  31. //openjd://virtual?params={%22category%22:%22jump%22,%20%22des%22:%22m%22,%20%22url%22:%22https://item.m.jd.com/product/10197819484796.html%22}
  32. //openapp.jdmobile://virtual?params={%22category%22:%22jump%22,%20%22des%22:%22m%22,%20%22url%22:%22https://item.m.jd.com/product/10197819484796.html%22}
  33. // 处理 openjd:// 和 openapp.jdmobile:// 格式的 deeplink
  34. if (content.StartsWith("openjd://virtual?params=") || content.StartsWith("openapp.jdmobile://virtual?params="))
  35. {
  36. try
  37. {
  38. string paramsStr = content.GetContentPart("params={", "}");
  39. if (!string.IsNullOrEmpty(paramsStr))
  40. {
  41. paramsStr = $"{{{paramsStr}}}";
  42. string decodedParams = HttpUtility.UrlDecode(paramsStr);
  43. // 使用正则提取 "url" 字段的值
  44. var urlMatch = Regex.Match(decodedParams, @"""url""\s*:\s*""([^""]+)""");
  45. if (urlMatch.Success)
  46. {
  47. return urlMatch.Groups[1].Value;
  48. }
  49. }
  50. }
  51. catch
  52. {
  53. // 如果解析失败,继续使用默认逻辑
  54. }
  55. }
  56. Regex regex = new(AlimamaPlus._url_pattern);
  57. MatchCollection matches = regex.Matches(content);
  58. foreach (Match m in matches.Cast<Match>())
  59. {
  60. string url = m.Value;
  61. return url;
  62. }
  63. return string.Empty;
  64. }
  65. public static string GetItemId(string url)
  66. {
  67. if (string.IsNullOrEmpty(url)) return string.Empty;
  68. string[] patterns = [
  69. @"https?://(?:i-)?item(?:\.m)?\.jd\.com/(?:product/)?(\d+)\.html(?:\?.*)?"
  70. //@"https?:\/\/b2b\.m\.jd\.com\/goods-detail\/.*[?&]skuId=(\d+)"
  71. ];
  72. foreach (var pattern in patterns)
  73. {
  74. Match match = Regex.Match(url, pattern);
  75. if (match.Success)
  76. {
  77. string idNumber = match.Groups[1].Value; // 捕获组1包含数字ID
  78. return idNumber;
  79. }
  80. }
  81. return string.Empty;
  82. }
  83. public static string GetDeeplink(string url, string clickId)
  84. {
  85. var config = TkConfigCore.Get();
  86. if (string.IsNullOrEmpty(config.dp_style)) return GetDefaultStyleDeeplink(url);
  87. Random rand = new();
  88. int randomValue = rand.Next(0, 101);
  89. if (randomValue > config.dp_style_percentage) return GetDefaultStyleDeeplink(url);
  90. if (string.IsNullOrEmpty(clickId)) clickId = config.dp_style_default_str;
  91. var ts = DateTime.Now.Convert2UnixTimestamp(true);
  92. string virtual_params;
  93. if (!string.IsNullOrEmpty(url) && url.StartsWith(VIDEO_URL_PREFIX))
  94. {
  95. // 处理视频URL的情况
  96. var queryString = url.Substring(VIDEO_URL_PREFIX.Length + 1); // +1 for the '?' character
  97. var queryParams = HttpUtility.ParseQueryString(queryString);
  98. var paramsDict = queryParams.AllKeys.ToDictionary(k => k, k => queryParams[k]);
  99. paramsDict["category"] = "jump";
  100. paramsDict["m_param"] = $"{{\"jdv\":\"0|oppo_seedling|-|widget|{clickId}|{ts}\"}}";
  101. virtual_params = JsonSerializer.Serialize(paramsDict);
  102. }
  103. else if (string.IsNullOrEmpty(url))
  104. {
  105. virtual_params = $"{{\"category\":\"jump\",\"des\":\"HomePage\",\"m_param\":{{\"jdv\":\"0|oppo_seedling|-|widget|{clickId}|{ts}\"}}";
  106. }
  107. else
  108. {
  109. virtual_params = $"{{\"category\":\"jump\",\"des\":\"m\",\"url\":\"{url}\",\"m_param\":{{\"jdv\":\"0|oppo_seedling|-|widget|{clickId}|{ts}\"}}}}";
  110. }
  111. string result = $"openapp.jdmobile://virtual?params={virtual_params.UrlEncode()}";
  112. return result;
  113. }
  114. public static string GetDefaultStyleDeeplink(string url)
  115. {
  116. if (string.IsNullOrEmpty(url)) return "openapp.jdmobile://";
  117. string virtual_params;
  118. if (url.StartsWith(VIDEO_URL_PREFIX))
  119. {
  120. // 处理视频URL的情况
  121. var queryString = url.Substring(VIDEO_URL_PREFIX.Length + 1);
  122. var queryParams = HttpUtility.ParseQueryString(queryString);
  123. var paramsDict = queryParams.AllKeys.ToDictionary(k => k, k => queryParams[k]);
  124. paramsDict["category"] = "jump";
  125. virtual_params = JsonSerializer.Serialize(paramsDict);
  126. }
  127. else
  128. {
  129. virtual_params = $"{{\"category\":\"jump\",\"des\":\"m\",\"sourceValue\":\"babel-act\",\"sourceType\":\"babel\",\"url\":\"{url}\",\"M_sourceFrom\":\"h5auto\",\"msf_type\":\"auto\"}}";
  130. }
  131. string result = $"openapp.jdmobile://virtual?params={virtual_params.UrlEncode()}";
  132. return result;
  133. }
  134. //public static string GetDefaultStyleDeeplink(string url)
  135. //{
  136. // if (string.IsNullOrEmpty(url)) return "openapp.jdmobile://";
  137. // string virtual_params = $"{{\"category\":\"jump\",\"des\":\"m\",\"sourceValue\":\"babel-act\",\"sourceType\":\"babel\",\"url\":\"{url}\",\"M_sourceFrom\":\"h5auto\",\"msf_type\":\"auto\"}}";
  138. // string result = $"openapp.jdmobile://virtual?params={virtual_params.UrlEncode()}";
  139. // return result;
  140. //}
  141. public static JdDataDTO GetFormattedObject(UnionParseRequest request)
  142. {
  143. string shortLinkurl = GetLink(request.Content);
  144. string deeplink_url = GetDefaultStyleDeeplink(shortLinkurl);
  145. return new JdDataDTO()
  146. {
  147. message = string.Empty,
  148. link_type = LinkTypeEnum.unknown,
  149. channel = TkChannelEnum.jd,
  150. accountName = string.Empty,
  151. success = false,
  152. content = request.Content,
  153. ip = request.Ip,
  154. oaid = request.Oaid,
  155. elapsedTime = 0,
  156. itemName = "点击打开京东APP",
  157. shortLinkurl = shortLinkurl,
  158. deeplink_url = deeplink_url,
  159. create_time = DateTime.Now,
  160. end_point = _end_point,
  161. parse_type = request.Type,
  162. riskStrategy = request.RiskStrategy,
  163. launchScene = request.LaunchScene,
  164. };
  165. }
  166. public static bool IsAffLlink(string url)
  167. {
  168. Uri uri = new(url);
  169. if (uri.Host.Equals("`u.jd.com", StringComparison.OrdinalIgnoreCase))
  170. {
  171. return true;
  172. }
  173. return false;
  174. }
  175. public static string? GetDesiredUrl(string url)
  176. {
  177. try
  178. {
  179. WebClientUtility client = new()
  180. {
  181. AllowAutoRedirect = false,
  182. Referer = "http://www.jd.com",
  183. Proxy = ProxyNodesCore.RandomOne()
  184. };
  185. #if DEBUG
  186. client.Proxy = null;
  187. #endif
  188. var response = client.Request(url);
  189. if (!response.Successed) return null;
  190. var target_url = response.ResponseMessage?.Headers?.Location?.ToString();
  191. if (!url.StartsWith("https://3.cn")) return null;
  192. return target_url;
  193. }
  194. catch (Exception ex)
  195. {
  196. }
  197. return null;
  198. }
  199. public static LinkTypeEnum GetLinkType(string url, out string desiredUrl, out string itemId)
  200. {
  201. desiredUrl = url;
  202. itemId = string.Empty;
  203. if (string.IsNullOrEmpty(url)) return LinkTypeEnum.unknown;
  204. string lowerUrl = url.ToLower();
  205. Uri uri = new(url);
  206. if ("3.cn".Equals(uri.Host))
  207. {
  208. var target_url = GetDesiredUrl(url);
  209. return GetLinkType(target_url, out desiredUrl, out itemId);
  210. }
  211. string[] arr = ["mall.jd.com", "shop.m.jd.com", "shop.jd.com"];
  212. if (arr.Contains(uri.Host)) return LinkTypeEnum.profile;
  213. arr = ["lives.jd.com"];
  214. if (arr.Contains(uri.Host)) return LinkTypeEnum.live;
  215. arr = ["u.jd.com"];
  216. if (arr.Contains(uri.Host)) return LinkTypeEnum.other_aff;
  217. arr = ["h5.m.jd.com"];
  218. if (arr.Contains(uri.Host)) return LinkTypeEnum.video;
  219. if (lowerUrl.Contains("h5.m.jd.com/active/faxian/video/index.html"))
  220. return LinkTypeEnum.video;
  221. if (lowerUrl.Contains("eco.m.jd.com/content/dr_home/index.html"))
  222. return LinkTypeEnum.profile;
  223. if (uri.Host.Contains("jd.com"))
  224. {
  225. itemId = GetItemId(url);
  226. if (!string.IsNullOrEmpty(itemId)) return LinkTypeEnum.goods;
  227. return LinkTypeEnum.baseDomain;
  228. }
  229. return LinkTypeEnum.unknown;
  230. }
  231. static bool ContainsSpecialFormat(string content)
  232. {
  233. // 使用正则表达式匹配内容中是否含有特定格式的字符串
  234. string pattern = @"[!$¥%]+[a-zA-Z0-9]+[!$¥%]]*";
  235. return Regex.IsMatch(content, pattern);
  236. }
  237. public async Task<JdDataDTO> JdParseAsync(string content, UnionParseRequest request, JdDataDTO result, CancellationToken cancellationToken = default)
  238. {
  239. string message = "OK";
  240. string url = GetLink(content);
  241. if (string.IsNullOrEmpty(url))
  242. {
  243. result.success = false;
  244. result.link_type = LinkTypeEnum.unknown;
  245. result.channel_type = ChannelTypeEnum.jd;
  246. result.message = "放弃转链";
  247. result.reason = "纯口令";
  248. result.accountId = 0;
  249. result.accountName = string.Empty;
  250. result.content = content;
  251. result.shortLinkurl = url;
  252. result.deeplink_url = GetDefaultStyleDeeplink(url);
  253. return result;
  254. }
  255. string shortLinkurl = url;
  256. //result.link_type = plus.IsAffLlink(url) ? LinkTypeEnum.other_aff : LinkTypeEnum.goods;
  257. result.shortLinkurl = url;
  258. result.link_type = GetLinkType(url, out string out_url, out string itemId);
  259. url = out_url;
  260. result.itemId = itemId;
  261. switch (result.link_type)
  262. {
  263. case LinkTypeEnum.profile:
  264. case LinkTypeEnum.live:
  265. case LinkTypeEnum.video:
  266. result.success = false;
  267. result.channel_type = ChannelTypeEnum.jd;
  268. result.message = "放弃转链";
  269. result.reason = result.link_type switch
  270. {
  271. LinkTypeEnum.profile => "店铺",
  272. LinkTypeEnum.live => "直播",
  273. LinkTypeEnum.video => "视频",
  274. _ => "非标链接",
  275. };
  276. result.accountId = 0;
  277. result.accountName = string.Empty;
  278. result.content = content;
  279. result.shortLinkurl = url;
  280. result.deeplink_url = GetDefaultStyleDeeplink(url);
  281. return result;
  282. case LinkTypeEnum.other_aff:
  283. result.success = false;
  284. result.link_type = LinkTypeEnum.unknown;
  285. result.channel_type = ChannelTypeEnum.jd;
  286. result.message = "放弃转链";
  287. result.reason = "其他推广链接";
  288. result.accountId = 0;
  289. result.accountName = string.Empty;
  290. result.content = content;
  291. result.shortLinkurl = url;
  292. result.deeplink_url = GetDefaultStyleDeeplink(url);
  293. return result;
  294. default:
  295. if (string.IsNullOrEmpty(url) || string.IsNullOrEmpty(result.itemId) ||
  296. (!url.StartsWith("https://item.jd.com/") &&
  297. !url.StartsWith("http://item.jd.com/") &&
  298. !url.StartsWith("https://item.m.jd.com/product/") &&
  299. !url.StartsWith("http://item.m.jd.com/product/") &&
  300. !url.StartsWith("http://item.m.jd.com/product/")))
  301. {
  302. result.success = false;
  303. //result.link_type = LinkTypeEnum.unknown;
  304. result.channel_type = ChannelTypeEnum.jd;
  305. result.message = "放弃转链";
  306. result.reason = "非标链接";
  307. result.accountId = 0;
  308. result.accountName = string.Empty;
  309. result.content = content;
  310. result.shortLinkurl = shortLinkurl;
  311. result.deeplink_url = result.link_type switch
  312. {
  313. LinkTypeEnum.other_aff or
  314. LinkTypeEnum.baseDomain or
  315. LinkTypeEnum.goods => GetDefaultStyleDeeplink(result.shortLinkurl),
  316. _ => GetDefaultStyleDeeplink(result.shortLinkurl),
  317. };
  318. return result;
  319. }
  320. break;
  321. }
  322. if (!TestParseCore.InWhitelist(result.ip, result.oaid) && FlowControlIgnoreRequest(_config, request, out string reason))
  323. {
  324. result.success = false;
  325. result.link_type = LinkTypeEnum.unknown;
  326. result.channel_type = ChannelTypeEnum.jd;
  327. result.message = "放弃转链";
  328. result.reason = reason;
  329. result.accountId = 0;
  330. result.accountName = string.Empty;
  331. result.content = content;
  332. result.shortLinkurl = url;
  333. result.deeplink_url = result.link_type switch
  334. {
  335. LinkTypeEnum.other_aff or LinkTypeEnum.goods => GetDefaultStyleDeeplink(result.shortLinkurl),
  336. _ => GetDefaultStyleDeeplink(result.shortLinkurl),
  337. };
  338. return result;
  339. }
  340. switch (request.CommerceType)
  341. {
  342. case 1:
  343. case 2:
  344. result.success = false;
  345. result.message = "放弃转链";
  346. result.reason = $"厂商放弃{request.CommerceType}";
  347. return result;
  348. }
  349. switch (_account.work_mode)
  350. {
  351. case JdUnionWorkMode.SiteApi:
  352. result = await GetPromotionBySiteAsync(result, url, request.ClickId, cancellationToken);
  353. break;
  354. case JdUnionWorkMode.AffApi:
  355. result = await GetPromotionByAffAsync(result, url, request.ClickId, cancellationToken);
  356. break;
  357. case JdUnionWorkMode.Ddx:
  358. result = await GetPromotionByDdxAsync(result, url, request.ClickId, cancellationToken);
  359. break;
  360. case JdUnionWorkMode.JingFen:
  361. result = await GetPromotionByCrawlerJingfenAsync(result, url, request.ClickId, cancellationToken);
  362. break;
  363. case JdUnionWorkMode.Crawler:
  364. default:
  365. result = await GetPromotionByCrawlerAsync(result, url, request.ClickId, cancellationToken);
  366. break;
  367. }
  368. return result;
  369. }
  370. }
  371. }