JdUnionPlus.cs 15 KB

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