DeeplinkParseCore.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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.Xml.Linq;
  9. namespace molilian.core
  10. {
  11. public partial class DeeplinkParseCore
  12. {
  13. private static string _end_point;
  14. private static readonly string[] separators = ["\r\n", "\n"];
  15. static DeeplinkParseCore()
  16. {
  17. _end_point = Environment.GetEnvironmentVariable("EndPoint");
  18. }
  19. public static DeeplinkParseDataDTO GetFormattedObject(string content, string channel, string ip = "", string oaid = "")
  20. {
  21. return new DeeplinkParseDataDTO()
  22. {
  23. message = string.Empty,
  24. success = false,
  25. content = content,
  26. ip = ip,
  27. oaid = oaid,
  28. elapsedTime = 0,
  29. create_time = DateTime.Now,
  30. end_point = _end_point,
  31. };
  32. }
  33. public static async Task<ActionResult> ParseAsync(string content, string channel, string ip, string oaid)
  34. {
  35. var result = GetFormattedObject(content, channel, ip, oaid);
  36. content = content.Trim();
  37. var rule = DeeplinkParseRuleCore.GetOne(channel);
  38. if (rule == null)
  39. {
  40. result.success = false;
  41. result.message = "转链失败";
  42. result.reason = "无效平台";
  43. _ = TkLogCore.ParseLogAsync(result);
  44. return new APIResult(new
  45. {
  46. result.success,
  47. result.message,
  48. result.reason,
  49. });
  50. }
  51. result.channel_id = rule.channel_id;
  52. result.channel_name = rule.channel_name;
  53. result = await GenerateDeeplink(content, rule, result);
  54. _ = TkLogCore.ParseLogAsync(result);
  55. return new APIResult(new
  56. {
  57. result.success,
  58. result.message,
  59. channel = result.channel_name,
  60. result.deeplink_url,
  61. result.itemName,
  62. });
  63. }
  64. public static async Task<DeeplinkParseDataDTO> GenerateDeeplink(string text, DeeplinkParseRuleDTO config, DeeplinkParseDataDTO result)
  65. {
  66. try
  67. {
  68. var list = config.rules.Convert2Object<List<DeeplinkParseRule>>();
  69. foreach (var rule in list)
  70. {
  71. var e = await WorkEachRule(text, config, result, rule);
  72. if (e != null) return e;
  73. }
  74. }
  75. catch (Exception ex)
  76. {
  77. }
  78. result.success = false;
  79. return result;
  80. }
  81. private static async Task<DeeplinkParseDataDTO?> WorkEachRule(string text, DeeplinkParseRuleDTO config, DeeplinkParseDataDTO result, DeeplinkParseRule rule)
  82. {
  83. if (!rule.enable) return null;
  84. string content = text;
  85. if (!string.IsNullOrEmpty(rule.url_pattern))
  86. {
  87. var regex = new Regex(rule.url_pattern);
  88. var match = regex.Match(content);
  89. if (!match.Success) return null;
  90. content = match.Groups[1].Value;
  91. }
  92. string url = ToolParsePlus.GetLink(text);
  93. try
  94. {
  95. switch (rule.url_action)
  96. {
  97. case UrlAction.Redirect:
  98. {
  99. var response = await new WebClientUtility
  100. {
  101. Proxy = ProxyNodesCore.RandomOne(),
  102. AllowAutoRedirect = false
  103. }.RequestAsync(url);
  104. if (response.ResponseMessage != null)
  105. {
  106. content = response.ResponseMessage.Headers.Location.ToString();
  107. content = content.UrlDecode();
  108. }
  109. }
  110. break;
  111. case UrlAction.Body:
  112. {
  113. var response = await new WebClientUtility
  114. {
  115. Proxy = ProxyNodesCore.RandomOne(),
  116. UserAgent = ProviderFakeUserAgent.RandomMobile,
  117. AllowAutoRedirect = true
  118. }.RequestAsync(url);
  119. content = response.Body();
  120. }
  121. break;
  122. case UrlAction.None:
  123. //{
  124. // content = url;
  125. //}
  126. break;
  127. }
  128. }
  129. catch (Exception ex)
  130. {
  131. //Console.WriteLine($"{url}\t{rule.url_action}\t{ex.Message}");
  132. }
  133. if (rule.childs.Count > 0)
  134. {
  135. foreach (var sub_rule in rule.childs)
  136. {
  137. var e = await WorkEachRule(content, config, result, sub_rule);
  138. if (e != null) return e;
  139. }
  140. return null;
  141. }
  142. if (!string.IsNullOrEmpty(rule.body_pattern))
  143. {
  144. var regex = new Regex(rule.body_pattern);
  145. var match = regex.Match(content);
  146. if (!match.Success) return null;
  147. }
  148. var deeplink = rule.deeplink_template;
  149. var prompt_text = rule.prompt_text;
  150. try
  151. {
  152. var regex = new Regex(rule.pattern);
  153. var match = regex.Match(content);
  154. if (match.Success)
  155. {
  156. if (match.Groups.Count > 1)
  157. {
  158. for (var i = 1; i < match.Groups.Count; i++)
  159. {
  160. string val = match.Groups[i].Value;
  161. if (deeplink.Contains("{url:"))
  162. {
  163. deeplink = deeplink.Replace($"{{url:{i - 1}}}", val.UrlEncode());
  164. }
  165. if (prompt_text.Contains("{url:"))
  166. {
  167. prompt_text = prompt_text.Replace($"{{url:{i - 1}}}", val.UrlEncode());
  168. }
  169. //atob
  170. if (deeplink.Contains("{atob:"))
  171. {
  172. deeplink = deeplink.Replace($"{{atob:{i - 1}}}", val.FromBase64());
  173. }
  174. if (prompt_text.Contains("{atob:"))
  175. {
  176. prompt_text = prompt_text.Replace($"{{atob:{i - 1}}}", val.FromBase64());
  177. }
  178. //btoa
  179. if (deeplink.Contains("{btoa:"))
  180. {
  181. deeplink = deeplink.Replace($"{{btoa:{i - 1}}}", val.ToBase64());
  182. }
  183. if (prompt_text.Contains("{btoa:"))
  184. {
  185. prompt_text = prompt_text.Replace($"{{btoa:{i - 1}}}", val.ToBase64());
  186. }
  187. deeplink = deeplink.Replace($"{{{i - 1}}}", val);
  188. prompt_text = prompt_text.Replace($"{{{i - 1}}}", val);
  189. }
  190. deeplink = Regex.Replace(deeplink, @"\{\d+\}", string.Empty);
  191. prompt_text = Regex.Replace(prompt_text, @"\{\d+\}", string.Empty);
  192. if (!string.IsNullOrEmpty(deeplink))
  193. {
  194. result.deeplink_url = deeplink;
  195. result.itemName = prompt_text;
  196. result.success = true;
  197. return result;
  198. }
  199. }
  200. else
  201. {
  202. result.deeplink_url = deeplink;
  203. result.itemName = prompt_text;
  204. result.success = true;
  205. return result;
  206. }
  207. }
  208. }
  209. catch (Exception ex)
  210. {
  211. Console.WriteLine($"Error processing pattern '{rule.pattern}': {ex.Message}");
  212. }
  213. return null;
  214. }
  215. }
  216. }