DeeplinkParseCore.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. UserAgent = ProviderFakeUserAgent.RandomMobile,
  104. AllowAutoRedirect = false
  105. }.RequestAsync(url);
  106. if (response.ResponseMessage != null)
  107. {
  108. content = response.ResponseMessage.Headers.Location.ToString();
  109. content = content.UrlDecode();
  110. }
  111. }
  112. break;
  113. case UrlAction.Body:
  114. {
  115. var response = await new WebClientUtility
  116. {
  117. Proxy = ProxyNodesCore.RandomOne(),
  118. UserAgent = ProviderFakeUserAgent.RandomMobile,
  119. AllowAutoRedirect = true
  120. }.RequestAsync(url);
  121. content = response.Body();
  122. }
  123. break;
  124. case UrlAction.None:
  125. //{
  126. // content = url;
  127. //}
  128. break;
  129. }
  130. }
  131. catch (Exception ex)
  132. {
  133. //Console.WriteLine($"{url}\t{rule.url_action}\t{ex.Message}");
  134. }
  135. if (rule.childs.Count > 0)
  136. {
  137. foreach (var sub_rule in rule.childs)
  138. {
  139. var e = await WorkEachRule(content, config, result, sub_rule);
  140. if (e != null) return e;
  141. }
  142. return null;
  143. }
  144. if (!string.IsNullOrEmpty(rule.body_pattern))
  145. {
  146. var regex = new Regex(rule.body_pattern);
  147. var match = regex.Match(content);
  148. if (!match.Success) return null;
  149. }
  150. var deeplink = rule.deeplink_template;
  151. var prompt_text = rule.prompt_text;
  152. try
  153. {
  154. var regex = new Regex(rule.pattern);
  155. var match = regex.Match(content);
  156. if (match.Success)
  157. {
  158. if (match.Groups.Count > 1)
  159. {
  160. for (var i = 1; i < match.Groups.Count; i++)
  161. {
  162. string val = match.Groups[i].Value;
  163. if (deeplink.Contains("{url:"))
  164. {
  165. string encodedUrl = HttpUtility.UrlEncode(val);
  166. deeplink = deeplink.Replace($"{{url:{i - 1}}}", encodedUrl);
  167. //deeplink = deeplink.Replace($"{{url:{i - 1}}}", val.UrlEncode());
  168. }
  169. if (prompt_text.Contains("{url:"))
  170. {
  171. string encodedUrl = HttpUtility.UrlEncode(val);
  172. prompt_text = prompt_text.Replace($"{{url:{i - 1}}}", encodedUrl);
  173. //prompt_text = prompt_text.Replace($"{{url:{i - 1}}}", val.UrlEncode());
  174. }
  175. if (deeplink.Contains("{decode:"))
  176. {
  177. string encodedUrl = HttpUtility.UrlDecode(val);
  178. deeplink = deeplink.Replace($"{{decode:{i - 1}}}", encodedUrl);
  179. //deeplink = deeplink.Replace($"{{url:{i - 1}}}", val.UrlEncode());
  180. }
  181. if (prompt_text.Contains("{decode:"))
  182. {
  183. string encodedUrl = HttpUtility.UrlDecode(val);
  184. prompt_text = prompt_text.Replace($"{{decode:{i - 1}}}", encodedUrl);
  185. }
  186. if (deeplink.Contains("{url2:"))
  187. {
  188. string encodedUrl = HttpUtility.UrlEncode(val);
  189. encodedUrl = HttpUtility.UrlEncode(encodedUrl);
  190. deeplink = deeplink.Replace($"{{url2:{i - 1}}}", encodedUrl);
  191. }
  192. if (prompt_text.Contains("{url2:"))
  193. {
  194. string encodedUrl = HttpUtility.UrlEncode(val);
  195. encodedUrl = HttpUtility.UrlEncode(encodedUrl);
  196. prompt_text = prompt_text.Replace($"{{url2:{i - 1}}}", encodedUrl);
  197. }
  198. if (deeplink.Contains("{url3:"))
  199. {
  200. string encodedUrl = HttpUtility.UrlEncode(val);
  201. encodedUrl = HttpUtility.UrlEncode(encodedUrl);
  202. deeplink = deeplink.Replace($"{{url3:{i - 1}}}", encodedUrl);
  203. }
  204. if (prompt_text.Contains("{url3:"))
  205. {
  206. string encodedUrl = HttpUtility.UrlEncode(val);
  207. encodedUrl = HttpUtility.UrlEncode(encodedUrl);
  208. prompt_text = prompt_text.Replace($"{{url3:{i - 1}}}", encodedUrl);
  209. }
  210. //atob
  211. if (deeplink.Contains("{atob:"))
  212. {
  213. deeplink = deeplink.Replace($"{{atob:{i - 1}}}", val.FromBase64());
  214. }
  215. if (prompt_text.Contains("{atob:"))
  216. {
  217. prompt_text = prompt_text.Replace($"{{atob:{i - 1}}}", val.FromBase64());
  218. }
  219. //btoa
  220. if (deeplink.Contains("{btoa:"))
  221. {
  222. deeplink = deeplink.Replace($"{{btoa:{i - 1}}}", val.ToBase64());
  223. }
  224. if (prompt_text.Contains("{btoa:"))
  225. {
  226. prompt_text = prompt_text.Replace($"{{btoa:{i - 1}}}", val.ToBase64());
  227. }
  228. deeplink = deeplink.Replace($"{{{i - 1}}}", val);
  229. prompt_text = prompt_text.Replace($"{{{i - 1}}}", val);
  230. }
  231. deeplink = Regex.Replace(deeplink, @"\{\d+\}", string.Empty);
  232. prompt_text = Regex.Replace(prompt_text, @"\{\d+\}", string.Empty);
  233. if (!string.IsNullOrEmpty(deeplink))
  234. {
  235. result.deeplink_url = deeplink;
  236. result.itemName = prompt_text;
  237. result.success = true;
  238. return result;
  239. }
  240. }
  241. else
  242. {
  243. result.deeplink_url = deeplink;
  244. result.itemName = prompt_text;
  245. result.success = true;
  246. return result;
  247. }
  248. }
  249. }
  250. catch (Exception ex)
  251. {
  252. Console.WriteLine($"Error processing pattern '{rule.pattern}': {ex.Message}");
  253. }
  254. return null;
  255. }
  256. }
  257. }