JdUnionPlus.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. };
  163. }
  164. public static bool IsAffLlink(string url)
  165. {
  166. Uri uri = new(url);
  167. if (uri.Host.Equals("`u.jd.com", StringComparison.OrdinalIgnoreCase))
  168. {
  169. return true;
  170. }
  171. return false;
  172. }
  173. public static string? GetDesiredUrl(string url)
  174. {
  175. try
  176. {
  177. WebClientUtility client = new()
  178. {
  179. AllowAutoRedirect = false,
  180. Referer = "http://www.jd.com",
  181. Proxy = ProxyNodesCore.RandomOne()
  182. };
  183. #if DEBUG
  184. client.Proxy = null;
  185. #endif
  186. var response = client.Request(url);
  187. if (!response.Successed) return null;
  188. var target_url = response.ResponseMessage?.Headers?.Location?.ToString();
  189. if (!url.StartsWith("https://3.cn")) return null;
  190. return target_url;
  191. }
  192. catch (Exception ex)
  193. {
  194. }
  195. return null;
  196. }
  197. public static LinkTypeEnum GetLinkType(string url, out string desiredUrl, out string itemId)
  198. {
  199. desiredUrl = url;
  200. itemId = string.Empty;
  201. if (string.IsNullOrEmpty(url)) return LinkTypeEnum.unknown;
  202. string lowerUrl = url.ToLower();
  203. Uri uri = new(url);
  204. if ("3.cn".Equals(uri.Host))
  205. {
  206. var target_url = GetDesiredUrl(url);
  207. return GetLinkType(target_url, out desiredUrl, out itemId);
  208. }
  209. string[] arr = ["mall.jd.com", "shop.m.jd.com", "shop.jd.com"];
  210. if (arr.Contains(uri.Host)) return LinkTypeEnum.profile;
  211. arr = ["lives.jd.com"];
  212. if (arr.Contains(uri.Host)) return LinkTypeEnum.live;
  213. arr = ["u.jd.com"];
  214. if (arr.Contains(uri.Host)) return LinkTypeEnum.other_aff;
  215. arr = ["h5.m.jd.com"];
  216. if (arr.Contains(uri.Host)) return LinkTypeEnum.video;
  217. if (lowerUrl.Contains("h5.m.jd.com/active/faxian/video/index.html"))
  218. return LinkTypeEnum.video;
  219. if (lowerUrl.Contains("eco.m.jd.com/content/dr_home/index.html"))
  220. return LinkTypeEnum.profile;
  221. if (uri.Host.Contains("jd.com"))
  222. {
  223. itemId = GetItemId(url);
  224. if (!string.IsNullOrEmpty(itemId)) return LinkTypeEnum.goods;
  225. return LinkTypeEnum.baseDomain;
  226. }
  227. return LinkTypeEnum.unknown;
  228. }
  229. static bool ContainsSpecialFormat(string content)
  230. {
  231. // 使用正则表达式匹配内容中是否含有特定格式的字符串
  232. string pattern = @"[!$¥%]+[a-zA-Z0-9]+[!$¥%]]*";
  233. return Regex.IsMatch(content, pattern);
  234. }
  235. public async Task<JdDataDTO> JdParseAsync(string content, int commerceType, string clickId, JdDataDTO result, CancellationToken cancellationToken = default)
  236. {
  237. string message = "OK";
  238. string url = GetLink(content);
  239. if (string.IsNullOrEmpty(url))
  240. {
  241. result.success = false;
  242. result.link_type = LinkTypeEnum.unknown;
  243. result.channel_type = ChannelTypeEnum.jd;
  244. result.message = "放弃转链";
  245. result.reason = "纯口令";
  246. result.accountId = 0;
  247. result.accountName = string.Empty;
  248. result.content = content;
  249. result.shortLinkurl = url;
  250. result.deeplink_url = GetDefaultStyleDeeplink(url);
  251. return result;
  252. }
  253. string shortLinkurl = url;
  254. //result.link_type = plus.IsAffLlink(url) ? LinkTypeEnum.other_aff : LinkTypeEnum.goods;
  255. result.shortLinkurl = url;
  256. result.link_type = GetLinkType(url, out string out_url, out string itemId);
  257. url = out_url;
  258. result.itemId = itemId;
  259. switch (result.link_type)
  260. {
  261. case LinkTypeEnum.profile:
  262. case LinkTypeEnum.live:
  263. case LinkTypeEnum.video:
  264. result.success = false;
  265. result.channel_type = ChannelTypeEnum.jd;
  266. result.message = "放弃转链";
  267. result.reason = result.link_type switch
  268. {
  269. LinkTypeEnum.profile => "店铺",
  270. LinkTypeEnum.live => "直播",
  271. LinkTypeEnum.video => "视频",
  272. _ => "非标链接",
  273. };
  274. result.accountId = 0;
  275. result.accountName = string.Empty;
  276. result.content = content;
  277. result.shortLinkurl = url;
  278. result.deeplink_url = GetDefaultStyleDeeplink(url);
  279. return result;
  280. case LinkTypeEnum.other_aff:
  281. result.success = false;
  282. result.link_type = LinkTypeEnum.unknown;
  283. result.channel_type = ChannelTypeEnum.jd;
  284. result.message = "放弃转链";
  285. result.reason = "其他推广链接";
  286. result.accountId = 0;
  287. result.accountName = string.Empty;
  288. result.content = content;
  289. result.shortLinkurl = url;
  290. result.deeplink_url = GetDefaultStyleDeeplink(url);
  291. return result;
  292. default:
  293. if (string.IsNullOrEmpty(url) || string.IsNullOrEmpty(result.itemId) ||
  294. (!url.StartsWith("https://item.jd.com/") &&
  295. !url.StartsWith("http://item.jd.com/") &&
  296. !url.StartsWith("https://item.m.jd.com/product/") &&
  297. !url.StartsWith("http://item.m.jd.com/product/") &&
  298. !url.StartsWith("http://item.m.jd.com/product/")))
  299. {
  300. result.success = false;
  301. //result.link_type = LinkTypeEnum.unknown;
  302. result.channel_type = ChannelTypeEnum.jd;
  303. result.message = "放弃转链";
  304. result.reason = "非标链接";
  305. result.accountId = 0;
  306. result.accountName = string.Empty;
  307. result.content = content;
  308. result.shortLinkurl = shortLinkurl;
  309. result.deeplink_url = result.link_type switch
  310. {
  311. LinkTypeEnum.other_aff or
  312. LinkTypeEnum.baseDomain or
  313. LinkTypeEnum.goods => GetDefaultStyleDeeplink(result.shortLinkurl),
  314. _ => GetDefaultStyleDeeplink(result.shortLinkurl),
  315. };
  316. return result;
  317. }
  318. break;
  319. }
  320. if (!TestParseCore.InWhitelist(result.ip, result.oaid) && FlowControlIgnoreRequest(_config, result.riskStrategy, result.launchScene, out string reason))
  321. {
  322. result.success = false;
  323. result.link_type = LinkTypeEnum.unknown;
  324. result.channel_type = ChannelTypeEnum.jd;
  325. result.message = "放弃转链";
  326. result.reason = reason;
  327. result.accountId = 0;
  328. result.accountName = string.Empty;
  329. result.content = content;
  330. result.shortLinkurl = url;
  331. result.deeplink_url = result.link_type switch
  332. {
  333. LinkTypeEnum.other_aff or LinkTypeEnum.goods => GetDefaultStyleDeeplink(result.shortLinkurl),
  334. _ => GetDefaultStyleDeeplink(result.shortLinkurl),
  335. };
  336. return result;
  337. }
  338. switch (commerceType)
  339. {
  340. case 1:
  341. case 2:
  342. result.success = false;
  343. result.message = "放弃转链";
  344. result.reason = $"厂商放弃{commerceType}";
  345. return result;
  346. }
  347. switch (_account.work_mode)
  348. {
  349. case JdUnionWorkMode.SiteApi:
  350. result = await GetPromotionBySiteAsync(result, url, clickId, cancellationToken);
  351. break;
  352. case JdUnionWorkMode.AffApi:
  353. result = await GetPromotionByAffAsync(result, url, clickId, cancellationToken);
  354. break;
  355. case JdUnionWorkMode.Ddx:
  356. result = await GetPromotionByDdxAsync(result, url, clickId, cancellationToken);
  357. break;
  358. case JdUnionWorkMode.JingFen:
  359. result = await GetPromotionByCrawlerJingfenAsync(result, url, clickId, cancellationToken);
  360. break;
  361. case JdUnionWorkMode.Crawler:
  362. default:
  363. result = await GetPromotionByCrawlerAsync(result, url, clickId, cancellationToken);
  364. break;
  365. }
  366. return result;
  367. }
  368. }
  369. }