ToolParsePlus.cs 10 KB

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