JdUnionPlus.cs 15 KB

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