ToolParsePlus.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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(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. string result = string.Empty;
  74. string linkPattern = @"(https://pan\.baidu\.com/s/[A-Za-z0-9?\-_=]+)";
  75. string codePattern = @"提取码[::]\s*([A-Za-z0-9]{4})";
  76. Regex linkRegex = new(linkPattern);
  77. Regex codeRegex = new(codePattern);
  78. Match linkMatch = linkRegex.Match(text);
  79. if (linkMatch.Success)
  80. {
  81. result = linkMatch.Groups[1].Value;
  82. if (result.Contains("?pwd="))
  83. {
  84. pwd = result.GetContentPart("?pwd=", "");
  85. surl = result.GetContentPart("/s/1", "?pwd=");
  86. }
  87. else
  88. {
  89. surl = result.GetContentPart("/s/1", "");
  90. // 匹配提取码
  91. Match codeMatch = codeRegex.Match(text);
  92. if (codeMatch.Success)
  93. {
  94. pwd = codeMatch.Groups[1].Value;
  95. }
  96. }
  97. }
  98. return result;
  99. }
  100. public static void PanParse(string content, ref ToolParseDataDTO result, CancellationToken cancellationToken = default)
  101. {
  102. string message = "OK";
  103. string surl = string.Empty, pwd = string.Empty;
  104. string shortLinkurl = GetPanLink(content, ref surl, ref pwd);
  105. if (string.IsNullOrEmpty(surl))
  106. {
  107. result.channel = TkChannelEnum.bdpan;
  108. result.content = content;
  109. result.success = false;
  110. result.message = "转链失败";
  111. result.reason = "内容不匹配";
  112. return;
  113. }
  114. var deeplink_url = $"bdnetdisk://n/action.SHARE_LINK?surl={surl}&pwd={pwd}";
  115. result.channel = TkChannelEnum.bdpan;
  116. result.content = content;
  117. result.success = true;
  118. result.message = message;
  119. result.shortLinkurl = shortLinkurl;
  120. result.deeplink_url = deeplink_url;
  121. }
  122. public static void WemeetParse(string content, ref ToolParseDataDTO result, CancellationToken cancellationToken = default)
  123. {
  124. string message = "OK";
  125. string wemeetId = GetWemeetId(content);
  126. if (string.IsNullOrEmpty(wemeetId))
  127. {
  128. result.channel = TkChannelEnum.wemeet;
  129. result.content = content;
  130. result.success = false;
  131. result.message = "转链失败";
  132. result.reason = "内容不匹配";
  133. return;
  134. }
  135. var deeplink_url = $"wemeet://page/inmeeting?meeting_code={wemeetId}";
  136. result.channel = TkChannelEnum.wemeet;
  137. result.content = wemeetId;
  138. result.success = true;
  139. result.message = message;
  140. result.deeplink_url = deeplink_url;
  141. }
  142. public static string GetWemeetId(string content)
  143. {
  144. string pattern = @"#腾讯会议:(\d{3}-\d{3}-\d{3})";
  145. Regex regex = new Regex(pattern);
  146. Match match = regex.Match(content);
  147. string meetingCode;
  148. // 判断是否匹配成功
  149. if (match.Success)
  150. {
  151. // 提取会议编码
  152. meetingCode = match.Groups[1].Value;
  153. meetingCode = meetingCode.Replace("-", "");
  154. if (!string.IsNullOrEmpty(meetingCode)) return meetingCode;
  155. }
  156. meetingCode = GetWemeetIdFromUrl(content);
  157. return meetingCode;
  158. }
  159. public static string? GetDesiredUrl(string url)
  160. {
  161. try
  162. {
  163. WebClientUtility client = new()
  164. {
  165. AllowAutoRedirect = false
  166. };
  167. var response = client.Request(url);
  168. if (!response.Successed) return null;
  169. return response.Body();
  170. }
  171. catch (Exception ex)
  172. {
  173. }
  174. return null;
  175. }
  176. public static string GetWemeetIdFromUrl(string content)
  177. {
  178. {
  179. string pattern = @"https:\/\/meeting\.tencent\.com\/p\/([0-9]+)";
  180. Regex regex = new Regex(pattern);
  181. Match match = regex.Match(content);
  182. // 判断是否匹配成功
  183. if (match.Success)
  184. {
  185. // 提取会议编码
  186. string meetingCode = match.Groups[1].Value;
  187. return meetingCode;
  188. }
  189. }
  190. {
  191. string pattern = @"https:\/\/meeting\.tencent\.com\/dm\/[a-zA-Z0-9]+";
  192. Regex regex = new Regex(pattern);
  193. Match match = regex.Match(content);
  194. // 判断是否匹配成功
  195. if (match.Success)
  196. {
  197. // 提取会议编码
  198. string meeting_url = match.Value;
  199. var body = GetDesiredUrl(meeting_url);
  200. if (string.IsNullOrEmpty(body)) return string.Empty;
  201. string meetingCode = body.GetContentPart("\"meeting_code\":\"", "\"");
  202. return meetingCode;
  203. }
  204. return string.Empty;
  205. }
  206. }
  207. }
  208. }