JdUnionPlus.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. namespace molilian.core
  21. {
  22. public partial class JdUnionPlus
  23. {
  24. public static string GetLink(string content)
  25. {
  26. if (string.IsNullOrEmpty(content)) return content;
  27. string result = content;
  28. //string pattern = @"https?:\/\/?([\da-z\.-]+)\.([a-z]{2,6})(:\d{1,5})?([\/\w\.-]*)*\/?(#[\S]+)?(\?[\S]+)?";
  29. Regex regex = new(AlimamaPlus._url_pattern);
  30. MatchCollection matches = regex.Matches(content);
  31. foreach (Match m in matches.Cast<Match>())
  32. {
  33. string url = m.Value;
  34. return url;
  35. }
  36. return string.Empty;
  37. }
  38. public static string GetItemId(string url)
  39. {
  40. if (string.IsNullOrEmpty(url)) return string.Empty;
  41. // 定义正则表达式模式,使用捕获组提取数字ID
  42. string pattern = @"https?://item(?:\.m)?\.jd\.com/(?:product/)?(\d+)\.html(?:\?.*)?";
  43. // 使用正则表达式进行匹配
  44. Match match = Regex.Match(url, pattern);
  45. if (match.Success)
  46. {
  47. string idNumber = match.Groups[1].Value; // 捕获组1包含数字ID
  48. return idNumber;
  49. }
  50. return string.Empty;
  51. }
  52. public static string GetDeeplink(string url)
  53. {
  54. if (string.IsNullOrEmpty(url)) return "openapp.jdmobile://";
  55. string virtual_params = $"{{\"category\":\"jump\",\"des\":\"m\",\"sourceValue\":\"babel-act\",\"sourceType\":\"babel\",\"url\":\"{url}\",\"M_sourceFrom\":\"h5auto\",\"msf_type\":\"auto\"}}";
  56. string result = $"openapp.jdmobile://virtual?params={virtual_params.UrlEncode()}";
  57. return result;
  58. }
  59. public static JdDataDTO GetFormattedObject(string content, string ip = "", string oaid = "")
  60. {
  61. string shortLinkurl = GetLink(content);
  62. string deeplink_url = GetDeeplink(shortLinkurl);
  63. return new JdDataDTO()
  64. {
  65. message = string.Empty,
  66. link_type = LinkTypeEnum.unknown,
  67. channel = TkChannelEnum.jd,
  68. accountName = string.Empty,
  69. success = false,
  70. content = content,
  71. ip = ip,
  72. oaid = oaid,
  73. elapsedTime = 0,
  74. itemName = "点击打开京东APP",
  75. shortLinkurl = shortLinkurl,
  76. deeplink_url = deeplink_url,
  77. create_time = DateTime.Now,
  78. end_point = _end_point,
  79. };
  80. }
  81. public static bool IsAffLlink(string url)
  82. {
  83. Uri uri = new(url);
  84. if (uri.Host.Equals("u.jd.com", StringComparison.OrdinalIgnoreCase))
  85. {
  86. return true;
  87. }
  88. return false;
  89. }
  90. public static string? GetDesiredUrl(string url)
  91. {
  92. try
  93. {
  94. WebClientUtility client = new()
  95. {
  96. AllowAutoRedirect = false
  97. };
  98. var response = client.Request(url);
  99. if (!response.Successed) return null;
  100. var target_url = response.ResponseMessage?.Headers?.Location?.ToString();
  101. if (!url.StartsWith("https://3.cn")) return null;
  102. return target_url;
  103. }
  104. catch (Exception ex)
  105. {
  106. }
  107. return null;
  108. }
  109. public static LinkTypeEnum GetLinkType(string url, out string desiredUrl, out string itemId)
  110. {
  111. desiredUrl = url;
  112. itemId = string.Empty;
  113. if (string.IsNullOrEmpty(url)) return LinkTypeEnum.unknown;
  114. url = url.ToLower();
  115. if (url.StartsWith("https://u.jd.com") ||
  116. url.StartsWith("http://u.jd.com"))
  117. return LinkTypeEnum.other_aff;
  118. if (url.StartsWith("https://lives.jd.com/") ||
  119. url.StartsWith("http://lives.jd.com/"))
  120. return LinkTypeEnum.live;
  121. if (url.StartsWith("https://h5.m.jd.com/active/faxian/video/index.html") ||
  122. url.StartsWith("http://h5.m.jd.com/active/faxian/video/index.html"))
  123. return LinkTypeEnum.video;
  124. if (url.StartsWith("https://eco.m.jd.com/content/dr_home/index.html") ||
  125. url.StartsWith("http://eco.m.jd.com/content/dr_home/index.html")) return LinkTypeEnum.profile;
  126. if (url.StartsWith("https://3.cn") ||
  127. url.StartsWith("http://3.cn"))
  128. {
  129. var target_url = GetDesiredUrl(url);
  130. return GetLinkType(target_url, out desiredUrl, out itemId);
  131. }
  132. if (url.Contains("jd.com"))
  133. {
  134. itemId = GetItemId(url);
  135. if (!string.IsNullOrEmpty(itemId)) return LinkTypeEnum.goods;
  136. return LinkTypeEnum.baseDomain;
  137. }
  138. return LinkTypeEnum.unknown;
  139. }
  140. static bool ContainsSpecialFormat(string content)
  141. {
  142. // 使用正则表达式匹配内容中是否含有特定格式的字符串
  143. string pattern = @"[!$¥%]+[a-zA-Z0-9]+[!$¥%]]*";
  144. return Regex.IsMatch(content, pattern);
  145. }
  146. //public static JdDataDTO JdParse(JdPoolDTO account, string content, ref JdDataDTO result)
  147. //{
  148. // string message = "OK";
  149. // string url = GetLink(content);
  150. // var plus = new JdUnionPlus(account.app_key, account.app_secret);
  151. // if (ContainsSpecialFormat(content))
  152. // {
  153. // result.link_type = LinkTypeEnum.other_aff;
  154. // result.success = false;
  155. // result.link_type = LinkTypeEnum.unknown;
  156. // result.channel_type = ChannelTypeEnum.jd;
  157. // result.message = "其他推广链接";
  158. // result.accountName = string.Empty;
  159. // result.content = content;
  160. // result.shortLinkurl = url;
  161. // result.deeplink_url = GetDeeplink(null);
  162. // return result;
  163. // }
  164. // //result.link_type = plus.IsAffLlink(url) ? LinkTypeEnum.other_aff : LinkTypeEnum.goods;
  165. // if (!string.IsNullOrEmpty(url))
  166. // {
  167. // (result.link_type, result.content) = GetLinkType(url);
  168. // if (result.link_type == LinkTypeEnum.other_aff)
  169. // {
  170. // result.success = false;
  171. // result.link_type = LinkTypeEnum.unknown;
  172. // result.channel_type = ChannelTypeEnum.jd;
  173. // result.message = "其他推广链接";
  174. // result.accountName = string.Empty;
  175. // result.content = content;
  176. // result.shortLinkurl = url;
  177. // result.deeplink_url = GetDeeplink(null);
  178. // return result;
  179. // }
  180. // }
  181. // string shortLinkurl = plus.promotion_get(account.site_id, string.IsNullOrEmpty(url) ? content : url);
  182. // var success = !string.IsNullOrEmpty(shortLinkurl);
  183. // if (!success) message = "转链失败";
  184. // result.content = shortLinkurl;
  185. // result.shortLinkurl = shortLinkurl;
  186. // result.success = success;
  187. // result.message = message;
  188. // result.deeplink_url = GetDeeplink(url);
  189. // return result;
  190. //}
  191. public async Task<JdDataDTO> JdParseAsync(string content, JdDataDTO result, CancellationToken cancellationToken = default)
  192. {
  193. string message = "OK";
  194. string url = GetLink(content);
  195. if (string.IsNullOrEmpty(url))
  196. {
  197. result.success = false;
  198. result.link_type = LinkTypeEnum.unknown;
  199. result.channel_type = ChannelTypeEnum.jd;
  200. result.message = "放弃转链";
  201. result.reason = "纯口令";
  202. result.accountId = 0;
  203. result.accountName = string.Empty;
  204. result.content = content;
  205. result.shortLinkurl = url;
  206. result.deeplink_url = GetDeeplink(url);
  207. return result;
  208. }
  209. string shortLinkurl = url;
  210. //result.link_type = plus.IsAffLlink(url) ? LinkTypeEnum.other_aff : LinkTypeEnum.goods;
  211. result.link_type = GetLinkType(url, out string out_url, out string itemId);
  212. url = out_url;
  213. result.itemId = itemId;
  214. if (result.link_type == LinkTypeEnum.other_aff)
  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 = GetDeeplink(url);
  226. return result;
  227. }
  228. if (string.IsNullOrEmpty(url) || string.IsNullOrEmpty(result.itemId) ||
  229. (!url.StartsWith("https://item.jd.com/") &&
  230. !url.StartsWith("https://item.m.jd.com/product/") &&
  231. !url.StartsWith("http://item.m.jd.com/product/") &&
  232. !url.StartsWith("http://item.m.jd.com/product/")))
  233. {
  234. result.success = false;
  235. result.link_type = LinkTypeEnum.unknown;
  236. result.channel_type = ChannelTypeEnum.jd;
  237. result.message = "放弃转链";
  238. result.reason = "非标链接";
  239. result.accountId = 0;
  240. result.accountName = string.Empty;
  241. result.content = content;
  242. result.shortLinkurl = url;
  243. result.deeplink_url = GetDeeplink(url);
  244. return result;
  245. }
  246. if (!result.ip.StartsWith("127.0.0") && FlowControlIgnoreRequest(_config, out string reason))
  247. {
  248. result.success = false;
  249. result.link_type = LinkTypeEnum.unknown;
  250. result.channel_type = ChannelTypeEnum.jd;
  251. result.message = "放弃转链";
  252. result.reason = reason;
  253. result.accountId = 0;
  254. result.accountName = string.Empty;
  255. result.content = content;
  256. result.shortLinkurl = url;
  257. result.deeplink_url = GetDeeplink(url);
  258. return result;
  259. }
  260. switch (_account.work_mode)
  261. {
  262. case JdUnionWorkMode.SiteApi:
  263. result = await GetPromotionBySiteAsync(result, url, cancellationToken);
  264. break;
  265. case JdUnionWorkMode.AffApi:
  266. result = await GetPromotionByAffAsync(result, url, cancellationToken);
  267. break;
  268. case JdUnionWorkMode.Crawler:
  269. default:
  270. result = await GetPromotionByCrawlerAsync(result, url, cancellationToken);
  271. break;
  272. }
  273. return result;
  274. }
  275. }
  276. }