ToolParsePlus.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. public static string GetLink(string content)
  26. {
  27. if (string.IsNullOrEmpty(content)) return content;
  28. string result = content;
  29. string pattern = @"https?:\/\/?([\da-z\.-]+)\.([a-z]{2,6})(:\d{1,5})?([\/\w\.-]*)*\/?(#[\S]+)?(\?[\S]+)?";
  30. Regex regex = new(pattern);
  31. MatchCollection matches = regex.Matches(content);
  32. foreach (Match m in matches.Cast<Match>())
  33. {
  34. string url = m.Value;
  35. return url;
  36. }
  37. return string.Empty;
  38. }
  39. public static ToolParseDataDTO GetFormattedObject(TkChannelEnum type, string content, string ip = "", string oaid = "")
  40. {
  41. string itemName = string.Empty;
  42. switch (type)
  43. {
  44. case TkChannelEnum.bdpan:
  45. itemName = "打开百度网盘";
  46. break;
  47. case TkChannelEnum.wemeet:
  48. itemName = "加入腾讯会议";
  49. break;
  50. }
  51. return new ToolParseDataDTO()
  52. {
  53. message = string.Empty,
  54. success = false,
  55. content = content,
  56. ip = ip,
  57. oaid = oaid,
  58. elapsedTime = 0,
  59. create_time = DateTime.Now,
  60. itemName = itemName,
  61. };
  62. }
  63. public static string GetPanLink(string text, ref string surl, ref string pwd)
  64. {
  65. surl = string.Empty;
  66. pwd = string.Empty;
  67. string result = string.Empty;
  68. string linkPattern = @"(https://pan\.baidu\.com/s/[A-Za-z0-9?\-=]+)";
  69. string codePattern = @"提取码[::]([A-Za-z0-9]{4})";
  70. Regex linkRegex = new(linkPattern);
  71. Regex codeRegex = new(codePattern);
  72. Match linkMatch = linkRegex.Match(text);
  73. if (linkMatch.Success)
  74. {
  75. result = linkMatch.Groups[1].Value;
  76. if (result.Contains("?pwd="))
  77. {
  78. pwd = result.GetContentPart("?pwd=", "");
  79. surl = result.GetContentPart("/s/1", "?pwd=");
  80. }
  81. else
  82. {
  83. surl = result.GetContentPart("/s/1", "");
  84. // 匹配提取码
  85. Match codeMatch = codeRegex.Match(text);
  86. if (codeMatch.Success)
  87. {
  88. pwd = codeMatch.Groups[1].Value;
  89. }
  90. }
  91. }
  92. return result;
  93. }
  94. public static void PanParse(string content, ref ToolParseDataDTO result)
  95. {
  96. string message = "OK";
  97. string surl = string.Empty, pwd = string.Empty;
  98. string shortLinkurl = GetPanLink(content, ref surl, ref pwd);
  99. if (string.IsNullOrEmpty(surl))
  100. {
  101. result.channel = TkChannelEnum.bdpan;
  102. result.content = content;
  103. result.success = false;
  104. result.message = "转链失败";
  105. result.reason = "内容不匹配";
  106. return;
  107. }
  108. var deeplink_url = $"bdnetdisk://n/action.SHARE_LINK?surl={surl}&pwd={pwd}";
  109. result.channel = TkChannelEnum.bdpan;
  110. result.content = content;
  111. result.success = true;
  112. result.message = message;
  113. result.shortLinkurl = shortLinkurl;
  114. result.deeplink_url = deeplink_url;
  115. }
  116. public static void WemeetParse(string content, ref ToolParseDataDTO result)
  117. {
  118. string message = "OK";
  119. string wemeetId = GetWemeetId(content);
  120. if (string.IsNullOrEmpty(wemeetId))
  121. {
  122. result.channel = TkChannelEnum.wemeet;
  123. result.content = content;
  124. result.success = false;
  125. result.message = "转链失败";
  126. result.reason = "内容不匹配";
  127. return;
  128. }
  129. var deeplink_url = $"wemeet://page/inmeeting?meeting_code={wemeetId}";
  130. result.channel = TkChannelEnum.wemeet;
  131. result.content = wemeetId;
  132. result.success = true;
  133. result.message = message;
  134. result.deeplink_url = deeplink_url;
  135. }
  136. public static string GetWemeetId(string content)
  137. {
  138. string pattern = @"#腾讯会议:(\d{3}-\d{3}-\d{3})";
  139. Regex regex = new Regex(pattern);
  140. Match match = regex.Match(content);
  141. string meetingCode;
  142. // 判断是否匹配成功
  143. if (match.Success)
  144. {
  145. // 提取会议编码
  146. meetingCode = match.Groups[1].Value;
  147. meetingCode = meetingCode.Replace("-", "");
  148. if (!string.IsNullOrEmpty(meetingCode)) return meetingCode;
  149. }
  150. meetingCode = GetWemeetIdFromUrl(content);
  151. return meetingCode;
  152. }
  153. public static string? GetDesiredUrl(string url)
  154. {
  155. try
  156. {
  157. WebClientUtility client = new()
  158. {
  159. AllowAutoRedirect = false
  160. };
  161. var response = client.Request(url);
  162. if (!response.Successed) return null;
  163. return response.Body();
  164. }
  165. catch (Exception ex)
  166. {
  167. }
  168. return null;
  169. }
  170. public static string GetWemeetIdFromUrl(string content)
  171. {
  172. string pattern = @"https:\/\/meeting\.tencent\.com\/dm\/[a-zA-Z0-9]+";
  173. Regex regex = new Regex(pattern);
  174. Match match = regex.Match(content);
  175. // 判断是否匹配成功
  176. if (match.Success)
  177. {
  178. // 提取会议编码
  179. string meeting_url = match.Value;
  180. var body = GetDesiredUrl(meeting_url);
  181. if (string.IsNullOrEmpty(body)) return string.Empty;
  182. string meetingCode = body.GetContentPart("\"meeting_code\":\"", "\"");
  183. return meetingCode;
  184. }
  185. return string.Empty;
  186. }
  187. }
  188. }