ToolParsePlus.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 System.Threading.Channels;
  20. using TencentCloud.Tione.V20211111.Models;
  21. namespace molilian.core
  22. {
  23. public partial class ToolParsePlus
  24. {
  25. private static string _end_point;
  26. static ToolParsePlus()
  27. {
  28. _end_point = Environment.GetEnvironmentVariable("EndPoint");
  29. }
  30. public static string GetLink(string content)
  31. {
  32. if (string.IsNullOrEmpty(content)) return content;
  33. string result = content;
  34. //string pattern = @"https?:\/\/?([\da-z\.-]+)\.([a-z]{2,6})(:\d{1,5})?([\/\w\.-]*)*\/?(#[\S]+)?(\?[\S]+)?";
  35. string pattern = @"https?://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]";
  36. Regex regex = new(pattern);
  37. MatchCollection matches = regex.Matches(content);
  38. foreach (Match m in matches.Cast<Match>())
  39. {
  40. string url = m.Value;
  41. return url;
  42. }
  43. return string.Empty;
  44. }
  45. public static ToolParseDataDTO GetFormattedObject(TkChannelEnum type, string content, string ip = "", string oaid = "")
  46. {
  47. string itemName = string.Empty;
  48. switch (type)
  49. {
  50. case TkChannelEnum.bdpan:
  51. itemName = "打开百度网盘";
  52. break;
  53. case TkChannelEnum.wemeet:
  54. itemName = "加入腾讯会议";
  55. break;
  56. }
  57. return new ToolParseDataDTO()
  58. {
  59. message = string.Empty,
  60. success = false,
  61. content = content,
  62. ip = ip,
  63. oaid = oaid,
  64. elapsedTime = 0,
  65. create_time = DateTime.Now,
  66. itemName = itemName,
  67. end_point = _end_point,
  68. };
  69. }
  70. public static string GetPanLink(string text, ref string surl, ref string pwd)
  71. {
  72. surl = string.Empty;
  73. pwd = string.Empty;
  74. text = text.Replace("pan。baidu。com", "pan.baidu.com");
  75. string result = string.Empty;
  76. string linkPattern = @"(https?://pan\.baidu\.com/s/[A-Za-z0-9?\-_=]+)";
  77. string codePattern = @"提取码[::]\s*([A-Za-z0-9]{4})";
  78. Regex linkRegex = new(linkPattern);
  79. Regex codeRegex = new(codePattern);
  80. Match linkMatch = linkRegex.Match(text);
  81. if (linkMatch.Success)
  82. {
  83. result = linkMatch.Groups[1].Value;
  84. if (result.Contains("?pwd="))
  85. {
  86. pwd = result.GetContentPart("?pwd=", "");
  87. surl = result.GetContentPart("/s/1", "?pwd=");
  88. }
  89. else
  90. {
  91. surl = result.GetContentPart("/s/1", "");
  92. // 匹配提取码
  93. Match codeMatch = codeRegex.Match(text);
  94. if (codeMatch.Success)
  95. {
  96. pwd = codeMatch.Groups[1].Value;
  97. }
  98. }
  99. }
  100. return result;
  101. }
  102. public static void PanParse(string content, ref ToolParseDataDTO result, CancellationToken cancellationToken = default)
  103. {
  104. string message = "OK";
  105. string surl = string.Empty, pwd = string.Empty;
  106. string shortLinkurl = GetPanLink(content, ref surl, ref pwd);
  107. result.shortLinkurl = shortLinkurl;
  108. result.deeplink_url = string.IsNullOrEmpty(shortLinkurl) ? "" : "bdnetdisk://n/action.EXTERNAL_ACTIVITY";
  109. if (string.IsNullOrEmpty(surl))
  110. {
  111. result.channel = TkChannelEnum.bdpan;
  112. result.content = content;
  113. result.success = false;
  114. result.message = "转链失败";
  115. result.reason = "内容不匹配";
  116. return;
  117. }
  118. result.deeplink_url = $"bdnetdisk://n/action.SHARE_LINK?surl={surl}&pwd={pwd}";
  119. result.channel = TkChannelEnum.bdpan;
  120. result.content = content;
  121. result.success = true;
  122. result.message = message;
  123. }
  124. public static async Task<ToolParseDataDTO> WemeetParseAsync(string content, ToolParseDataDTO result, CancellationToken cancellationToken = default)
  125. {
  126. string message = "OK";
  127. string wemeetId = await GetWemeetIdAsync(content);
  128. var deeplink_url = "";
  129. result.deeplink_url = deeplink_url;
  130. if (!string.IsNullOrEmpty(wemeetId))
  131. {
  132. result.deeplink_url = $"wemeet://page/inmeeting?meeting_code={wemeetId}";
  133. result.channel = TkChannelEnum.wemeet;
  134. result.content = wemeetId;
  135. result.success = true;
  136. result.message = message;
  137. return result;
  138. }
  139. var url = await GetWemeetUrlAsync(content);
  140. if (!string.IsNullOrEmpty(url))
  141. {
  142. result.deeplink_url = $"wemeet://page/external_link?url={url.UrlEncode()}";
  143. result.channel = TkChannelEnum.wemeet;
  144. result.content = wemeetId;
  145. result.success = true;
  146. result.message = message;
  147. return result;
  148. }
  149. result.channel = TkChannelEnum.wemeet;
  150. result.content = content;
  151. result.success = false;
  152. result.message = "转链失败";
  153. result.reason = "内容不匹配";
  154. return result;
  155. }
  156. public static async Task<string> GetWemeetUrlAsync(string content)
  157. {
  158. string pattern = @"https?:\/\/meeting\.tencent\.com[^\s]*";
  159. Regex regex = new Regex(pattern);
  160. Match match = regex.Match(content);
  161. string url = string.Empty;
  162. // 判断是否匹配成功
  163. if (match.Success)
  164. {
  165. // 提取会议编码
  166. url = match.Groups[0].Value;
  167. }
  168. return url;
  169. }
  170. public static async Task<string> GetWemeetIdAsync(string content)
  171. {
  172. string pattern = @"#腾讯会议:(\d{3}-\d{3}-\d{3})";
  173. Regex regex = new Regex(pattern);
  174. Match match = regex.Match(content);
  175. string meetingCode;
  176. // 判断是否匹配成功
  177. if (match.Success)
  178. {
  179. // 提取会议编码
  180. meetingCode = match.Groups[1].Value;
  181. meetingCode = meetingCode.Replace("-", "");
  182. if (!string.IsNullOrEmpty(meetingCode)) return meetingCode;
  183. }
  184. meetingCode = await GetWemeetIdFromUrlAsync(content);
  185. return meetingCode;
  186. }
  187. public static async Task<string> GetDesiredUrlAsync(string url)
  188. {
  189. try
  190. {
  191. WebClientUtility client = new()
  192. {
  193. AllowAutoRedirect = false
  194. };
  195. var response = await client.RequestAsync(url);
  196. if (!response.Successed) return null;
  197. return response.Body();
  198. }
  199. catch (Exception ex)
  200. {
  201. }
  202. return null;
  203. }
  204. public static async Task<string> GetMeetingCodeAsync(string url)
  205. {
  206. try
  207. {
  208. url = url.Replace("/dm/", "/l/");
  209. WebClientUtility client = new()
  210. {
  211. AllowAutoRedirect = false
  212. };
  213. var response = await client.RequestAsync(url);
  214. if (!response.Successed) return null;
  215. if (response.ResponseMessage.StatusCode == System.Net.HttpStatusCode.Redirect)
  216. {
  217. string redirectUrl = response.ResponseMessage?.Headers?.Location.ToString();
  218. //https://meeting.tencent.com/detail.html?mtoken=uGjRUgQFhvDCowb28odPQ__&meetingcode=116399514&sdkappid=1400115281&show8k=0
  219. string meetingCode = redirectUrl.GetContentPart("&meetingcode=", "&");
  220. return meetingCode;
  221. }
  222. }
  223. catch (Exception ex)
  224. {
  225. }
  226. return null;
  227. }
  228. public static async Task<string> GetWemeetIdFromUrlAsync(string content)
  229. {
  230. {
  231. string pattern = @"https?:\/\/meeting\.tencent\.com\/p\/([0-9]+)";
  232. Regex regex = new Regex(pattern);
  233. Match match = regex.Match(content);
  234. // 判断是否匹配成功
  235. if (match.Success)
  236. {
  237. // 提取会议编码
  238. string meetingCode = match.Groups[1].Value;
  239. return meetingCode;
  240. }
  241. }
  242. {
  243. content = content.Replace("meeting.tencent.com/dw/", "meeting.tencent.com/dm/");
  244. string pattern = @"https?:\/\/meeting\.tencent\.com\/dm\/[a-zA-Z0-9]+";
  245. Regex regex = new Regex(pattern);
  246. Match match = regex.Match(content);
  247. // 判断是否匹配成功
  248. if (match.Success)
  249. {
  250. // 提取会议编码
  251. string meeting_url = match.Value;
  252. //重定向获取会议Code
  253. string meetingCode = await GetMeetingCodeAsync(meeting_url);
  254. if (string.IsNullOrEmpty(meetingCode))
  255. {
  256. //正文获取Code
  257. string body = await GetDesiredUrlAsync(meeting_url);
  258. if (string.IsNullOrEmpty(body)) return string.Empty;
  259. meetingCode = body.GetContentPart("\"meeting_code\":\"", "\"");
  260. }
  261. return meetingCode;
  262. }
  263. return string.Empty;
  264. }
  265. }
  266. }
  267. }