A:主页面调整为WebSocket连接设置
This commit is contained in:
1 parent
fe4c71f392
commit
61ab65ee81
8 files changed
+87
-39
No files matched your search
+21
-13
@@ -4,8 +4,11 @@ using System.Net.Http;
|
|||||||
using System.Windows;
|
using System.Windows;
|
||||||
using System.Windows.Threading;
|
using System.Windows.Threading;
|
||||||
using Application.Utils;
|
using Application.Utils;
|
||||||
|
using Common;
|
||||||
using Common.Logger;
|
using Common.Logger;
|
||||||
|
using Common.Net.WebSocketConn;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Serilog.Events;
|
||||||
|
|
||||||
namespace Shell;
|
namespace Shell;
|
||||||
|
|
||||||
@@ -18,11 +21,15 @@ public partial class App : System.Windows.Application
|
|||||||
{
|
{
|
||||||
// 异常处理
|
// 异常处理
|
||||||
ExceptionCatch();
|
ExceptionCatch();
|
||||||
|
// 初始化日志
|
||||||
|
var configModelLogging = AppConfig.Instance.ConfigModel.Logging;
|
||||||
|
Log.Initialize(configModelLogging);
|
||||||
base.OnStartup(e);
|
base.OnStartup(e);
|
||||||
|
|
||||||
// ✅ 启动你的服务(如 WebSocket、HTTP、端口监听等)
|
// ✅ 启动你的服务(如 WebSocket、HTTP、端口监听等)
|
||||||
StartMyServer();
|
StartMyServer();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 异常捕捉
|
/// 异常捕捉
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -33,7 +40,7 @@ public partial class App : System.Windows.Application
|
|||||||
// 注册全局异常处理
|
// 注册全局异常处理
|
||||||
TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
|
TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 处理异常
|
/// 处理异常
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -48,6 +55,7 @@ public partial class App : System.Windows.Application
|
|||||||
MessageBox.Show($"恭喜你发现了BUG,请联系管理员解决:{exception.Message}");
|
MessageBox.Show($"恭喜你发现了BUG,请联系管理员解决:{exception.Message}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
|
private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
|
||||||
{
|
{
|
||||||
Log.Error(e.Exception, "UI线程异常");
|
Log.Error(e.Exception, "UI线程异常");
|
||||||
@@ -64,13 +72,14 @@ public partial class App : System.Windows.Application
|
|||||||
// 让应用正常关闭而不是崩溃
|
// 让应用正常关闭而不是崩溃
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
|
private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
|
||||||
{
|
{
|
||||||
foreach (var ex in e.Exception.InnerExceptions)
|
foreach (var ex in e.Exception.InnerExceptions)
|
||||||
{
|
{
|
||||||
Log.Error(e.Exception, "UI线程异常");
|
Log.Error(e.Exception, "UI线程异常");
|
||||||
}
|
}
|
||||||
|
|
||||||
e.SetObserved(); // 标记异常已被观察,防止应用终止
|
e.SetObserved(); // 标记异常已被观察,防止应用终止
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,7 +87,7 @@ public partial class App : System.Windows.Application
|
|||||||
{
|
{
|
||||||
var exception = e.ExceptionObject as Exception;
|
var exception = e.ExceptionObject as Exception;
|
||||||
Log.Error(exception, "非UI线程异常");
|
Log.Error(exception, "非UI线程异常");
|
||||||
|
|
||||||
if (e.IsTerminating)
|
if (e.IsTerminating)
|
||||||
{
|
{
|
||||||
// 应用即将终止,进行紧急清理
|
// 应用即将终止,进行紧急清理
|
||||||
@@ -86,6 +95,7 @@ public partial class App : System.Windows.Application
|
|||||||
Log.Fatal("应用程序即将因未处理异常而终止");
|
Log.Fatal("应用程序即将因未处理异常而终止");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool CanRecover(Exception exception)
|
private bool CanRecover(Exception exception)
|
||||||
{
|
{
|
||||||
return exception switch
|
return exception switch
|
||||||
@@ -104,17 +114,15 @@ public partial class App : System.Windows.Application
|
|||||||
private void StartMyServer()
|
private void StartMyServer()
|
||||||
{
|
{
|
||||||
int port = SysHelper.GetAvailablePort();
|
int port = SysHelper.GetAvailablePort();
|
||||||
Console.WriteLine($"服务启动在端口: {port}");
|
|
||||||
|
|
||||||
// 例如开启线程或任务监听 WS:
|
AppConfig.Instance.WebSocketPort = port;
|
||||||
Task.Run(() =>
|
|
||||||
{
|
|
||||||
StartWebSocketServer(port);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private void StartWebSocketServer(int port)
|
AppConfig.Instance.WebSocketPath = $"ws://127.0.0.1:{port}";
|
||||||
{
|
Log.Information4Ctx($"服务启动在端口: {AppConfig.Instance.WebSocketPort}");
|
||||||
// 示例服务逻辑,如上文 WebSocket 实现
|
Log.Information($"服务完整地址: {AppConfig.Instance.WebSocketPath}");
|
||||||
|
// TODO 2025年5月28日 22点25分 :开启 Http 网络本地服务,提供网页访问
|
||||||
|
|
||||||
|
// 启动WebSocket服务器
|
||||||
|
WebSocketManager.Instance.Start(AppConfig.Instance.WebSocketPath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+2
-7
@@ -8,13 +8,6 @@
|
|||||||
<UseWPF>true</UseWPF>
|
<UseWPF>true</UseWPF>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<Content Include="WinDivert.dll">
|
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
|
||||||
</Content>
|
|
||||||
<Content Include="WinDivert64.sys" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Folder Include="wwwroot\" />
|
<Folder Include="wwwroot\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
@@ -25,8 +18,10 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\Application\Application.csproj" />
|
<ProjectReference Include="..\Application\Application.csproj" />
|
||||||
|
<ProjectReference Include="..\Common.Net\Common.Net.csproj" />
|
||||||
<ProjectReference Include="..\Data\Data.csproj" />
|
<ProjectReference Include="..\Data\Data.csproj" />
|
||||||
<ProjectReference Include="..\Infrastructure\Infrastructure.csproj" />
|
<ProjectReference Include="..\Infrastructure\Infrastructure.csproj" />
|
||||||
|
<ProjectReference Include="..\Utils.ETW\Utils.ETW.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -2,7 +2,13 @@ using System.Windows.Controls;
|
|||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
|
using Application.Utils;
|
||||||
|
using Common;
|
||||||
|
using Common.Logger;
|
||||||
|
using Common.Net;
|
||||||
|
using Common.Net.WebSocketConn;
|
||||||
using Infrastructure.utils;
|
using Infrastructure.utils;
|
||||||
|
using Utils.ETW;
|
||||||
using WinDivertNet.WinDivertWrapper;
|
using WinDivertNet.WinDivertWrapper;
|
||||||
|
|
||||||
namespace Shell.Views;
|
namespace Shell.Views;
|
||||||
@@ -12,6 +18,9 @@ public partial class WebPanel : UserControl
|
|||||||
public WebPanel()
|
public WebPanel()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
|
|
||||||
|
Class1.Main2();
|
||||||
|
|
||||||
InitWebView();
|
InitWebView();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,8 +44,8 @@ public partial class WebPanel : UserControl
|
|||||||
// 反序列化为 C# 对象
|
// 反序列化为 C# 对象
|
||||||
var msg = JsonSerializer.Deserialize<JsMessage>(json);
|
var msg = JsonSerializer.Deserialize<JsMessage>(json);
|
||||||
|
|
||||||
Console.WriteLine(json);
|
Log.Information(json);
|
||||||
Console.WriteLine(msg?.Channel);
|
Log.Information(msg?.Channel);
|
||||||
|
|
||||||
switch (msg?.Channel)
|
switch (msg?.Channel)
|
||||||
{
|
{
|
||||||
@@ -49,7 +58,7 @@ public partial class WebPanel : UserControl
|
|||||||
break;
|
break;
|
||||||
case "GetProgramDiagnostics":
|
case "GetProgramDiagnostics":
|
||||||
var programDiagnostics = SysInfoUtils.GetProgramDiagnostics();
|
var programDiagnostics = SysInfoUtils.GetProgramDiagnostics();
|
||||||
Console.WriteLine(programDiagnostics);
|
Log.Information(programDiagnostics);
|
||||||
break;
|
break;
|
||||||
case "InspectProcess":
|
case "InspectProcess":
|
||||||
SysInfoUtils.InspectProcess(23192);
|
SysInfoUtils.InspectProcess(23192);
|
||||||
@@ -60,19 +69,54 @@ public partial class WebPanel : UserControl
|
|||||||
case "PacketSnifferStart":
|
case "PacketSnifferStart":
|
||||||
PacketSniffer.Start();
|
PacketSniffer.Start();
|
||||||
break;
|
break;
|
||||||
|
case "GetWebSocketPath":
|
||||||
|
WebSocketPath();
|
||||||
|
break;
|
||||||
|
case "CloseWebSocket":
|
||||||
|
CloseWebSocket();
|
||||||
|
break;
|
||||||
|
case "OpenWebSocket":
|
||||||
|
OpenWebSocket();
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
Console.WriteLine("没有任何函数被触发...");
|
Log.Information("没有任何函数被触发...");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void OpenWebSocket()
|
||||||
|
{
|
||||||
|
int port = SysHelper.GetAvailablePort();
|
||||||
|
AppConfig.Instance.WebSocketPort = port;
|
||||||
|
AppConfig.Instance.WebSocketPath = $"ws://127.0.0.1:{port}";
|
||||||
|
WebSocketManager.Instance.Start(AppConfig.Instance.WebSocketPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void CloseWebSocket()
|
||||||
|
{
|
||||||
|
WebSocketManager.Instance.Stop();
|
||||||
|
}
|
||||||
|
|
||||||
private async void InitWebView1()
|
private async void InitWebView1()
|
||||||
{
|
{
|
||||||
await webView.ExecuteScriptAsync("""
|
await webView.ExecuteScriptAsync("""
|
||||||
window.externalFunctions.__BRIDGE_LISTEN__('showMessage','来自 C# 的问候');
|
window.externalFunctions.__BRIDGE_LISTEN__('showMessage','来自 C# 的问候');
|
||||||
""");
|
""");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async void WebSocketPath()
|
||||||
|
{
|
||||||
|
var instanceWebSocketPath = AppConfig.Instance.WebSocketPath;
|
||||||
|
Log.Information($"instanceWebSocketPath -> :{instanceWebSocketPath}");
|
||||||
|
string result = await webView.ExecuteScriptAsync($"""
|
||||||
|
window.externalFunctions.__BRIDGE_LISTEN__('ListenWebSocketPath','{instanceWebSocketPath}');
|
||||||
|
""");
|
||||||
|
string result1 = await webView.ExecuteScriptAsync($"""
|
||||||
|
window.externalFunctions.__BRIDGE_LISTEN__('GetWebSocketPath','{instanceWebSocketPath}');
|
||||||
|
""");
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class JsMessage
|
public class JsMessage
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
@@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"AppSettings": {
|
"AppSettings": {
|
||||||
"MaxConnection": 99
|
"MaxConnection": 321
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"AppSettings": {
|
"AppSettings": {
|
||||||
"MaxConnection": 10
|
"MaxConnection": 123
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+14
-13
@@ -1,18 +1,19 @@
|
|||||||
{
|
{
|
||||||
"AppSettings": {
|
"AppSettings": {
|
||||||
"AppName": "MyApp",
|
"AppName": "MyApp",
|
||||||
"MaxConnection": 10
|
"MaxConnection": 444,
|
||||||
},
|
"Version": "0.0.1",
|
||||||
"Logging": {
|
"Logging": {
|
||||||
"EnableConsole": true,
|
"EnableConsole": true,
|
||||||
"EnableFile": true,
|
"EnableFile": false,
|
||||||
"LogPath": "logs",
|
"LogPath": "logs",
|
||||||
"MaxFileSizeMB": 1,
|
"MaxFileSizeMB": 1,
|
||||||
"RetentionDays": 30,
|
"RetentionDays": 30,
|
||||||
"CompressDaysThreshold": 3,
|
"CompressDaysThreshold": 3,
|
||||||
"MinimumLevel": "Information",
|
"MinimumLevel": "Information",
|
||||||
"OutputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] {Message:lj}{NewLine}{Exception}",
|
"OutputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] [{ThreadId}] {Message:lj}{NewLine}{Exception}",
|
||||||
"UseJsonFormat": false,
|
"UseJsonFormat": false,
|
||||||
"CompressCheckIntervalHours": 24
|
"CompressCheckIntervalHours": 24
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user