DeeplinkParseCore.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. result.channel_id = rule.channel_id;
  53. result.channel_name = rule.channel_name;
  54. result = await GenerateDeeplink(content, rule, result);
  55. _ = TkLogCore.ParseLogAsync(result);
  56. return new APIResult(new
  57. {
  58. result.success,
  59. result.message,
  60. channel = result.channel_name,
  61. result.deeplink_url,
  62. result.itemName,
  63. });
  64. }
  65. public static async Task<DeeplinkParseDataDTO> GenerateDeeplink(string text, DeeplinkParseRuleDTO config, DeeplinkParseDataDTO result)
  66. {
  67. try
  68. {
  69. var list = config.rules.Convert2Object<List<DeeplinkParseRule>>();
  70. foreach (var rule in list)
  71. {
  72. var e = await WorkEachRule(text, config, result, rule);
  73. if (e != null) return e;
  74. }
  75. }
  76. catch (Exception ex)
  77. {
  78. }
  79. result.success = false;
  80. return result;
  81. }
  82. private static async Task<DeeplinkParseDataDTO?> WorkEachRule(string text, DeeplinkParseRuleDTO config, DeeplinkParseDataDTO result, DeeplinkParseRule rule)
  83. {
  84. if (!rule.enable) return null;
  85. string content = text;
  86. if (!string.IsNullOrEmpty(rule.url_pattern))
  87. {
  88. var regex = new Regex(rule.url_pattern);
  89. var match = regex.Match(content);
  90. if (!match.Success) return null;
  91. content = match.Groups[1].Value;
  92. }
  93. string url = ToolParsePlus.GetLink(text);
  94. try
  95. {
  96. switch (rule.url_action)
  97. {
  98. case UrlAction.Redirect:
  99. {
  100. var response = await new WebClientUtility
  101. {
  102. Proxy = ProxyNodesCore.RandomOne(),
  103. AllowAutoRedirect = false
  104. }.RequestAsync(url);
  105. if (response.ResponseMessage != null)
  106. {
  107. content = response.ResponseMessage.Headers.Location.ToString();
  108. content = content.UrlDecode();
  109. }
  110. }
  111. break;
  112. case UrlAction.Body:
  113. {
  114. var response = await new WebClientUtility
  115. {
  116. Proxy = ProxyNodesCore.RandomOne(),
  117. UserAgent = ProviderFakeUserAgent.RandomMobile,
  118. AllowAutoRedirect = true
  119. }.RequestAsync(url);
  120. content = response.Body();
  121. }
  122. break;
  123. case UrlAction.None:
  124. //{
  125. // content = url;
  126. //}
  127. break;
  128. }
  129. }
  130. catch (Exception ex)
  131. {
  132. //Console.WriteLine($"{url}\t{rule.url_action}\t{ex.Message}");
  133. }
  134. if (rule.childs.Count > 0)
  135. {
  136. foreach (var sub_rule in rule.childs)
  137. {
  138. var e = await WorkEachRule(content, config, result, sub_rule);
  139. if (e != null) return e;
  140. }
  141. return null;
  142. }
  143. if (!string.IsNullOrEmpty(rule.body_pattern))
  144. {
  145. var regex = new Regex(rule.body_pattern);
  146. var match = regex.Match(content);
  147. if (!match.Success) return null;
  148. }
  149. var deeplink = rule.deeplink_template;
  150. var prompt_text = rule.prompt_text;
  151. try
  152. {
  153. var regex = new Regex(rule.pattern);
  154. var match = regex.Match(content);
  155. if (match.Success)
  156. {
  157. if (match.Groups.Count > 1)
  158. {
  159. for (var i = 1; i < match.Groups.Count; i++)
  160. {
  161. string val = match.Groups[i].Value;
  162. if (deeplink.Contains("{url:"))
  163. {
  164. string encodedUrl = HttpUtility.UrlEncode(val);
  165. deeplink = deeplink.Replace($"{{url:{i - 1}}}", encodedUrl);
  166. //deeplink = deeplink.Replace($"{{url:{i - 1}}}", val.UrlEncode());
  167. }
  168. if (prompt_text.Contains("{url:"))
  169. {
  170. string encodedUrl = HttpUtility.UrlEncode(val);
  171. prompt_text = prompt_text.Replace($"{{url:{i - 1}}}", encodedUrl);
  172. //prompt_text = prompt_text.Replace($"{{url:{i - 1}}}", val.UrlEncode());
  173. }
  174. if (deeplink.Contains("{url2:"))
  175. {
  176. string encodedUrl = HttpUtility.UrlEncode(val);
  177. encodedUrl = HttpUtility.UrlEncode(encodedUrl);
  178. deeplink = deeplink.Replace($"{{url2:{i - 1}}}", encodedUrl);
  179. }
  180. if (prompt_text.Contains("{url2:"))
  181. {
  182. string encodedUrl = HttpUtility.UrlEncode(val);
  183. encodedUrl = HttpUtility.UrlEncode(encodedUrl);
  184. prompt_text = prompt_text.Replace($"{{url2:{i - 1}}}", encodedUrl);
  185. }
  186. if (deeplink.Contains("{url3:"))
  187. {
  188. string encodedUrl = HttpUtility.UrlEncode(val);
  189. encodedUrl = HttpUtility.UrlEncode(encodedUrl);
  190. deeplink = deeplink.Replace($"{{url3:{i - 1}}}", encodedUrl);
  191. }
  192. if (prompt_text.Contains("{url3:"))
  193. {
  194. string encodedUrl = HttpUtility.UrlEncode(val);
  195. encodedUrl = HttpUtility.UrlEncode(encodedUrl);
  196. prompt_text = prompt_text.Replace($"{{url3:{i - 1}}}", encodedUrl);
  197. }
  198. //atob
  199. if (deeplink.Contains("{atob:"))
  200. {
  201. deeplink = deeplink.Replace($"{{atob:{i - 1}}}", val.FromBase64());
  202. }
  203. if (prompt_text.Contains("{atob:"))
  204. {
  205. prompt_text = prompt_text.Replace($"{{atob:{i - 1}}}", val.FromBase64());
  206. }
  207. //btoa
  208. if (deeplink.Contains("{btoa:"))
  209. {
  210. deeplink = deeplink.Replace($"{{btoa:{i - 1}}}", val.ToBase64());
  211. }
  212. if (prompt_text.Contains("{btoa:"))
  213. {
  214. prompt_text = prompt_text.Replace($"{{btoa:{i - 1}}}", val.ToBase64());
  215. }
  216. deeplink = deeplink.Replace($"{{{i - 1}}}", val);
  217. prompt_text = prompt_text.Replace($"{{{i - 1}}}", val);
  218. }
  219. deeplink = Regex.Replace(deeplink, @"\{\d+\}", string.Empty);
  220. prompt_text = Regex.Replace(prompt_text, @"\{\d+\}", string.Empty);
  221. if (!string.IsNullOrEmpty(deeplink))
  222. {
  223. result.deeplink_url = deeplink;
  224. result.itemName = prompt_text;
  225. result.success = true;
  226. return result;
  227. }
  228. }
  229. else
  230. {
  231. result.deeplink_url = deeplink;
  232. result.itemName = prompt_text;
  233. result.success = true;
  234. return result;
  235. }
  236. }
  237. }
  238. catch (Exception ex)
  239. {
  240. Console.WriteLine($"Error processing pattern '{rule.pattern}': {ex.Message}");
  241. }
  242. return null;
  243. }
  244. }
  245. }