Browse Source

ws 解析数据 转定义字符

master
root 3 days ago
parent
commit
79c8404448
  1. 69
      CoreAgent.Domain/Helpers/LinuxWsResponseParser.cs
  2. 5
      CoreAgent.Domain/Models/System/AppSettings.cs
  3. 8
      CoreAgent.Infrastructure/Command/Executors/LinuxCommandExecutor.cs

69
CoreAgent.Domain/Helpers/LinuxWsResponseParser.cs

@ -0,0 +1,69 @@
using System.Text.RegularExpressions;
namespace CoreAgent.Domain.Helpers;
/// <summary>
/// Linux WebSocket响应日志解析器
/// </summary>
public static class LinuxWsResponseParser
{
private static readonly Regex _regexWs = new Regex(@"\{(?:[^{}]*|\{(?:[^{}]*|\{[^{}]*\})*\})*\}");
/// <summary>
/// 解析Linux WebSocket响应日志
/// </summary>
/// <param name="input">输入日志字符串</param>
/// <returns>解析后的日志内容</returns>
public static string ParseWsResultLogs(this string input)
{
try
{
return GetContentBetweenOuterCurlyBraces(input);
}
catch (Exception ex)
{
string msg = ex.Message;
return "Parse Logs Data Is null";
}
}
private static string GetContentBetweenOuterCurlyBraces(string input)
{
int openBraceIndex = input.IndexOf('{');
if (openBraceIndex == -1)
{
return string.Empty; // 没有找到'{'
}
int closeBraceIndex = FindMatchingCloseBrace(input, openBraceIndex);
if (closeBraceIndex == -1)
{
return string.Empty; // 没有找到匹配的'}'
}
// 提取并返回内容
return input.Substring(openBraceIndex, closeBraceIndex - openBraceIndex + 1);
}
private static int FindMatchingCloseBrace(string input, int openBraceIndex)
{
int braceDepth = 1; // 从第一个'{'开始,深度为1
for (int i = openBraceIndex + 1; i < input.Length; i++)
{
if (input[i] == '{')
{
braceDepth++; // 遇到'{',深度增加
}
else if (input[i] == '}')
{
braceDepth--; // 遇到'}',深度减少
if (braceDepth == 0)
{
return i; // 找到匹配的'}',返回其索引
}
}
}
return -1; // 没有找到匹配的'}'
}
}

5
CoreAgent.Domain/Models/System/AppSettings.cs

@ -19,4 +19,9 @@ public class AppSettings
/// MME配置目录路径
/// </summary>
public string MmeConfigDirectory { get; set; } = "/root/mme/config/";
/// <summary>
/// WebSocket JavaScript文件路径
/// </summary>
public string WebSocketJsPath { get; set; } = "/root/enb/doc/ws.js";
}

8
CoreAgent.Infrastructure/Command/Executors/LinuxCommandExecutor.cs

@ -91,12 +91,8 @@ public class LinuxCommandExecutor : BaseCommandExecutor
throw new ArgumentException("命令不能为空", nameof(command));
}
// 使用双引号包裹整个命令,只对必要的特殊字符进行转义
var escapedCommand = command
.Replace("\"", "\\\"")
.Replace("`", "\\`")
.Replace("\\", "\\\\");
// 使用双引号包裹整个命令,只对双引号进行转义
var escapedCommand = command.Replace("\"", "\\\"");
return $"-c \"{escapedCommand}\"";
}
}
Loading…
Cancel
Save