DeeplinkParseCore.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. 
  2. using dodohold.core;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.Extensions.FileSystemGlobbing.Internal;
  5. using Sayaka.Common;
  6. using Spire.Pdf.Annotations;
  7. using System.Text.RegularExpressions;
  8. using System.Web;
  9. using System.Xml.Linq;
  10. namespace molilian.core
  11. {
  12. public partial class DeeplinkParseCore
  13. {
  14. private static string _end_point;
  15. private static readonly string[] separators = ["\r\n", "\n"];
  16. static DeeplinkParseCore()
  17. {
  18. _end_point = Environment.GetEnvironmentVariable("EndPoint");
  19. }
  20. public static DeeplinkParseDataDTO GetFormattedObject(string content, string channel, string ip = "", string oaid = "")
  21. {
  22. return new DeeplinkParseDataDTO()
  23. {
  24. message = string.Empty,
  25. success = false,
  26. content = content,
  27. ip = ip,
  28. oaid = oaid,
  29. elapsedTime = 0,
  30. create_time = DateTime.Now,
  31. end_point = _end_point,
  32. };
  33. }
  34. public static async Task<ActionResult> ParseAsync(string content, string channel, string ip, string oaid)
  35. {
  36. var result = GetFormattedObject(content, channel, ip, oaid);
  37. content = content.Trim();
  38. var rule = DeeplinkParseRuleCore.GetOne(channel);
  39. if (rule == null)
  40. {
  41. result.success = false;
  42. result.message = "转链失败";
  43. result.reason = "无效平台";
  44. _ = TkLogCore.ParseLogAsync(result);
  45. return new APIResult(new
  46. {
  47. result.success,
  48. result.message,
  49. result.reason,
  50. });
  51. }
  52. if (ShouldIgnoreRequest(rule.IgnorePercentageCity, ip, oaid, out string reason))
  53. {
  54. result.success = false;
  55. result.message = "放弃转链";
  56. result.reason = reason;
  57. _ = TkLogCore.ParseLogAsync(result);
  58. return new APIResult(new
  59. {
  60. result.success,
  61. result.message,
  62. result.reason,
  63. });
  64. }
  65. result.channel_id = rule.channel_id;
  66. result.channel_name = rule.channel_name;
  67. result = await GenerateDeeplink(content, rule, result);
  68. _ = TkLogCore.ParseLogAsync(result);
  69. return new APIResult(new
  70. {
  71. result.success,
  72. result.message,
  73. channel = result.channel_name,
  74. result.deeplink_url,
  75. result.itemName,
  76. });
  77. }
  78. public static bool ShouldIgnoreRequest(string ignorePercentageCity, string ip, string oaid, out string reason)
  79. {
  80. reason = string.Empty;
  81. try
  82. {
  83. // IP和流量控制
  84. string? ipInfo = IP2RegionPlus.Search(ip);
  85. if (AlimamaPlus.IgnoreRegionIncluded(ignorePercentageCity, ipInfo, out string regionInfo))
  86. {
  87. reason = $"地区控制:{regionInfo}";
  88. return true;
  89. }
  90. }
  91. catch (Exception ex)
  92. {
  93. reason = "配置异常";
  94. }
  95. return false;
  96. }
  97. public static async Task<DeeplinkParseDataDTO> GenerateDeeplink(string text, DeeplinkParseRuleDTO config, DeeplinkParseDataDTO result)
  98. {
  99. try
  100. {
  101. var list = config.rules.Convert2Object<List<DeeplinkParseRule>>();
  102. foreach (var rule in list)
  103. {
  104. var e = await WorkEachRule(text, config, result, rule);
  105. if (e != null) return e;
  106. }
  107. }
  108. catch (Exception ex)
  109. {
  110. }
  111. result.success = false;
  112. return result;
  113. }
  114. private static async Task<DeeplinkParseDataDTO?> WorkEachRule(string text, DeeplinkParseRuleDTO config, DeeplinkParseDataDTO result, DeeplinkParseRule rule)
  115. {
  116. if (!rule.enable) return null;
  117. string content = text;
  118. if (!string.IsNullOrEmpty(rule.url_pattern))
  119. {
  120. var regex = new Regex(rule.url_pattern);
  121. var match = regex.Match(content);
  122. if (!match.Success) return null;
  123. content = match.Groups[1].Value;
  124. }
  125. string url = ToolParsePlus.GetLink(text);
  126. try
  127. {
  128. switch (rule.url_action)
  129. {
  130. case UrlAction.Redirect:
  131. {
  132. var response = await new WebClientUtility
  133. {
  134. Proxy = ProxyNodesCore.RandomOne(),
  135. UserAgent = ProviderFakeUserAgent.RandomMobile,
  136. AllowAutoRedirect = false
  137. }.RequestAsync(url);
  138. if (response.ResponseMessage != null)
  139. {
  140. content = response.ResponseMessage.Headers.Location.ToString();
  141. content = content.UrlDecode();
  142. }
  143. }
  144. break;
  145. case UrlAction.Body:
  146. {
  147. var response = await new WebClientUtility
  148. {
  149. Proxy = ProxyNodesCore.RandomOne(),
  150. UserAgent = ProviderFakeUserAgent.RandomMobile,
  151. AllowAutoRedirect = true
  152. }.RequestAsync(url);
  153. content = response.Body();
  154. }
  155. break;
  156. case UrlAction.None:
  157. //{
  158. // content = url;
  159. //}
  160. break;
  161. }
  162. }
  163. catch (Exception ex)
  164. {
  165. //Console.WriteLine($"{url}\t{rule.url_action}\t{ex.Message}");
  166. }
  167. if (rule.childs.Count > 0)
  168. {
  169. foreach (var sub_rule in rule.childs)
  170. {
  171. var e = await WorkEachRule(content, config, result, sub_rule);
  172. if (e != null) return e;
  173. }
  174. return null;
  175. }
  176. if (!string.IsNullOrEmpty(rule.body_pattern))
  177. {
  178. var regex = new Regex(rule.body_pattern);
  179. var match = regex.Match(content);
  180. if (!match.Success) return null;
  181. }
  182. var deeplink = rule.deeplink_template;
  183. var prompt_text = rule.prompt_text;
  184. try
  185. {
  186. var regex = new Regex(rule.pattern);
  187. var match = regex.Match(content);
  188. if (match.Success)
  189. {
  190. if (match.Groups.Count > 1)
  191. {
  192. for (var i = 1; i < match.Groups.Count; i++)
  193. {
  194. string val = match.Groups[i].Value;
  195. if (deeplink.Contains("{url:"))
  196. {
  197. string encodedUrl = HttpUtility.UrlEncode(val);
  198. deeplink = deeplink.Replace($"{{url:{i - 1}}}", encodedUrl);
  199. //deeplink = deeplink.Replace($"{{url:{i - 1}}}", val.UrlEncode());
  200. }
  201. if (prompt_text.Contains("{url:"))
  202. {
  203. string encodedUrl = HttpUtility.UrlEncode(val);
  204. prompt_text = prompt_text.Replace($"{{url:{i - 1}}}", encodedUrl);
  205. //prompt_text = prompt_text.Replace($"{{url:{i - 1}}}", val.UrlEncode());
  206. }
  207. if (deeplink.Contains("{decode:"))
  208. {
  209. string encodedUrl = HttpUtility.UrlDecode(val);
  210. deeplink = deeplink.Replace($"{{decode:{i - 1}}}", encodedUrl);
  211. //deeplink = deeplink.Replace($"{{url:{i - 1}}}", val.UrlEncode());
  212. }
  213. if (prompt_text.Contains("{decode:"))
  214. {
  215. string encodedUrl = HttpUtility.UrlDecode(val);
  216. prompt_text = prompt_text.Replace($"{{decode:{i - 1}}}", encodedUrl);
  217. }
  218. if (deeplink.Contains("{url2:"))
  219. {
  220. string encodedUrl = HttpUtility.UrlEncode(val);
  221. encodedUrl = HttpUtility.UrlEncode(encodedUrl);
  222. deeplink = deeplink.Replace($"{{url2:{i - 1}}}", encodedUrl);
  223. }
  224. if (prompt_text.Contains("{url2:"))
  225. {
  226. string encodedUrl = HttpUtility.UrlEncode(val);
  227. encodedUrl = HttpUtility.UrlEncode(encodedUrl);
  228. prompt_text = prompt_text.Replace($"{{url2:{i - 1}}}", encodedUrl);
  229. }
  230. if (deeplink.Contains("{url3:"))
  231. {
  232. string encodedUrl = HttpUtility.UrlEncode(val);
  233. encodedUrl = HttpUtility.UrlEncode(encodedUrl);
  234. deeplink = deeplink.Replace($"{{url3:{i - 1}}}", encodedUrl);
  235. }
  236. if (prompt_text.Contains("{url3:"))
  237. {
  238. string encodedUrl = HttpUtility.UrlEncode(val);
  239. encodedUrl = HttpUtility.UrlEncode(encodedUrl);
  240. prompt_text = prompt_text.Replace($"{{url3:{i - 1}}}", encodedUrl);
  241. }
  242. //atob
  243. if (deeplink.Contains("{atob:"))
  244. {
  245. deeplink = deeplink.Replace($"{{atob:{i - 1}}}", val.FromBase64());
  246. }
  247. if (prompt_text.Contains("{atob:"))
  248. {
  249. prompt_text = prompt_text.Replace($"{{atob:{i - 1}}}", val.FromBase64());
  250. }
  251. //btoa
  252. if (deeplink.Contains("{btoa:"))
  253. {
  254. deeplink = deeplink.Replace($"{{btoa:{i - 1}}}", val.ToBase64());
  255. }
  256. if (prompt_text.Contains("{btoa:"))
  257. {
  258. prompt_text = prompt_text.Replace($"{{btoa:{i - 1}}}", val.ToBase64());
  259. }
  260. deeplink = deeplink.Replace($"{{{i - 1}}}", val);
  261. prompt_text = prompt_text.Replace($"{{{i - 1}}}", val);
  262. }
  263. deeplink = Regex.Replace(deeplink, @"\{\d+\}", string.Empty);
  264. prompt_text = Regex.Replace(prompt_text, @"\{\d+\}", string.Empty);
  265. if (!string.IsNullOrEmpty(deeplink))
  266. {
  267. result.deeplink_url = deeplink;
  268. result.itemName = prompt_text;
  269. result.success = true;
  270. return result;
  271. }
  272. }
  273. else
  274. {
  275. result.deeplink_url = deeplink;
  276. result.itemName = prompt_text;
  277. result.success = true;
  278. return result;
  279. }
  280. }
  281. }
  282. catch (Exception ex)
  283. {
  284. Console.WriteLine($"Error processing pattern '{rule.pattern}': {ex.Message}");
  285. }
  286. return null;
  287. }
  288. }
  289. }