ToolParsePlus.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. string deeplink_url = $"bdnetdisk://n/action.SHARE_LINK?surl={surl}";
  118. if (!string.IsNullOrEmpty(pwd)) deeplink_url += $"&pwd={pwd}";
  119. result.deeplink_url = deeplink_url;
  120. result.channel = TkChannelEnum.bdpan;
  121. result.content = content;
  122. result.success = true;
  123. result.message = message;
  124. }
  125. public static async Task<ToolParseDataDTO> WemeetParseAsync(string content, ToolParseDataDTO result, CancellationToken cancellationToken = default)
  126. {
  127. string message = "OK";
  128. string wemeetId = await GetWemeetIdAsync(content);
  129. var deeplink_url = "";
  130. result.deeplink_url = deeplink_url;
  131. if (!string.IsNullOrEmpty(wemeetId))
  132. {
  133. result.deeplink_url = $"wemeet://page/inmeeting?meeting_code={wemeetId}";
  134. result.channel = TkChannelEnum.wemeet;
  135. result.content = wemeetId;
  136. result.success = true;
  137. result.message = message;
  138. return result;
  139. }
  140. var url = await GetWemeetUrlAsync(content);
  141. if (!string.IsNullOrEmpty(url))
  142. {
  143. result.deeplink_url = $"wemeet://page/external_link?url={url.UrlEncode()}";
  144. result.channel = TkChannelEnum.wemeet;
  145. result.content = wemeetId;
  146. result.success = true;
  147. result.message = message;
  148. return result;
  149. }
  150. result.channel = TkChannelEnum.wemeet;
  151. result.content = content;
  152. result.success = false;
  153. result.message = "转链失败";
  154. result.reason = "内容不匹配";
  155. return result;
  156. }
  157. public static async Task<string> GetWemeetUrlAsync(string content)
  158. {
  159. string pattern = @"https?:\/\/meeting\.tencent\.com[^\s]*";
  160. Regex regex = new Regex(pattern);
  161. Match match = regex.Match(content);
  162. string url = string.Empty;
  163. // 判断是否匹配成功
  164. if (match.Success)
  165. {
  166. // 提取会议编码
  167. url = match.Groups[0].Value;
  168. }
  169. return url;
  170. }
  171. public static async Task<string> GetWemeetIdAsync(string content)
  172. {
  173. string pattern = @"#腾讯会议:(\d{3}-\d{3}-\d{3})";
  174. Regex regex = new Regex(pattern);
  175. Match match = regex.Match(content);
  176. string meetingCode;
  177. // 判断是否匹配成功
  178. if (match.Success)
  179. {
  180. // 提取会议编码
  181. meetingCode = match.Groups[1].Value;
  182. meetingCode = meetingCode.Replace("-", "");
  183. if (!string.IsNullOrEmpty(meetingCode)) return meetingCode;
  184. }
  185. meetingCode = await GetWemeetIdFromUrlAsync(content);
  186. return meetingCode;
  187. }
  188. public static async Task<string> GetDesiredUrlAsync(string url)
  189. {
  190. try
  191. {
  192. WebClientUtility client = new()
  193. {
  194. AllowAutoRedirect = false
  195. };
  196. var response = await client.RequestAsync(url);
  197. if (!response.Successed) return null;
  198. return response.Body();
  199. }
  200. catch (Exception ex)
  201. {
  202. }
  203. return null;
  204. }
  205. public static async Task<string> GetMeetingCodeAsync(string url)
  206. {
  207. try
  208. {
  209. url = url.Replace("/dm/", "/l/");
  210. WebClientUtility client = new()
  211. {
  212. AllowAutoRedirect = false
  213. };
  214. var response = await client.RequestAsync(url);
  215. if (!response.Successed) return null;
  216. if (response.ResponseMessage.StatusCode == System.Net.HttpStatusCode.Redirect)
  217. {
  218. string redirectUrl = response.ResponseMessage?.Headers?.Location.ToString();
  219. //https://meeting.tencent.com/detail.html?mtoken=uGjRUgQFhvDCowb28odPQ__&meetingcode=116399514&sdkappid=1400115281&show8k=0
  220. string meetingCode = redirectUrl.GetContentPart("&meetingcode=", "&");
  221. return meetingCode;
  222. }
  223. }
  224. catch (Exception ex)
  225. {
  226. }
  227. return null;
  228. }
  229. public static async Task<string> GetWemeetIdFromUrlAsync(string content)
  230. {
  231. {
  232. string pattern = @"https?:\/\/meeting\.tencent\.com\/p\/([0-9]+)";
  233. Regex regex = new Regex(pattern);
  234. Match match = regex.Match(content);
  235. // 判断是否匹配成功
  236. if (match.Success)
  237. {
  238. // 提取会议编码
  239. string meetingCode = match.Groups[1].Value;
  240. return meetingCode;
  241. }
  242. }
  243. {
  244. content = content.Replace("meeting.tencent.com/dw/", "meeting.tencent.com/dm/");
  245. string pattern = @"https?:\/\/meeting\.tencent\.com\/dm\/[a-zA-Z0-9]+";
  246. Regex regex = new Regex(pattern);
  247. Match match = regex.Match(content);
  248. // 判断是否匹配成功
  249. if (match.Success)
  250. {
  251. // 提取会议编码
  252. string meeting_url = match.Value;
  253. //重定向获取会议Code
  254. string meetingCode = await GetMeetingCodeAsync(meeting_url);
  255. if (string.IsNullOrEmpty(meetingCode))
  256. {
  257. //正文获取Code
  258. string body = await GetDesiredUrlAsync(meeting_url);
  259. if (string.IsNullOrEmpty(body)) return string.Empty;
  260. meetingCode = body.GetContentPart("\"meeting_code\":\"", "\"");
  261. }
  262. return meetingCode;
  263. }
  264. return string.Empty;
  265. }
  266. }
  267. }
  268. }