This commit is contained in:
老九 2021-11-03 22:36:13 +08:00
parent c6f718f844
commit 4db6ab6b3f
16 changed files with 156 additions and 144 deletions

View File

@ -1,10 +1,10 @@
<Project> <Project>
<PropertyGroup> <PropertyGroup>
<Version>2.0.5</Version> <Version>2.0.5</Version>
<Nullable>enable</Nullable>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
<DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports> <DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>
<IsWebConfigTransformDisabled>true</IsWebConfigTransformDisabled> <IsWebConfigTransformDisabled>true</IsWebConfigTransformDisabled>
<EnableCompressionInSingleFile>true</EnableCompressionInSingleFile>
<Description>github加速神器</Description> <Description>github加速神器</Description>
<Copyright>https://github.com/dotnetcore/FastGithub</Copyright> <Copyright>https://github.com/dotnetcore/FastGithub</Copyright>
<RuntimeIdentifier>win-x64</RuntimeIdentifier> <RuntimeIdentifier>win-x64</RuntimeIdentifier>

View File

@ -1,7 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" /> <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" />

View File

@ -1,7 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="5.0.0" /> <PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="5.0.0" />

View File

@ -1,11 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <ItemGroup>
<TargetFramework>net6.0</TargetFramework> <FrameworkReference Include="Microsoft.AspNetCore.App" />
<Nullable>enable</Nullable> </ItemGroup>
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
</Project> </Project>

View File

@ -1,8 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Portable.BouncyCastle" Version="1.8.10" /> <PackageReference Include="Portable.BouncyCastle" Version="1.8.10" />
<ProjectReference Include="..\FastGithub.DomainResolve\FastGithub.DomainResolve.csproj" /> <ProjectReference Include="..\FastGithub.DomainResolve\FastGithub.DomainResolve.csproj" />

View File

@ -1,7 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup> </PropertyGroup>

View File

@ -1,7 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup> </PropertyGroup>
@ -10,7 +9,7 @@
<PackageReference Include="DNS" Version="6.1.0" /> <PackageReference Include="DNS" Version="6.1.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" /> <PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
<PackageReference Include="Microsoft.Win32.Registry" Version="5.0.0" /> <PackageReference Include="Microsoft.Win32.Registry" Version="5.0.0" />
<PackageReference Include="FastGithub.WinDiverts" Version="1.4.1" /> <PackageReference Include="FastGithub.WinDiverts" Version="1.4.1" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -13,27 +13,13 @@ namespace FastGithub.UI
/// </summary> /// </summary>
public partial class App : Application public partial class App : Application
{ {
private Mutex globalMutex; private readonly Mutex globalMutex;
private readonly bool isFirstInstance;
/// <summary> public App()
/// 程序启动
/// </summary>
/// <param name="e"></param>
protected override void OnStartup(StartupEventArgs e)
{ {
this.globalMutex = new Mutex(true, "Global\\FastGithub.UI", out var firstInstance); this.globalMutex = new Mutex(true, "Global\\FastGithub.UI", out this.isFirstInstance);
if (firstInstance == false) AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve;
{
this.Shutdown();
}
else
{
AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve;
SetWebBrowserVersion(9000);
StartFastGithub();
}
base.OnStartup(e);
} }
/// <summary> /// <summary>
@ -42,25 +28,41 @@ namespace FastGithub.UI
/// <param name="sender"></param> /// <param name="sender"></param>
/// <param name="args"></param> /// <param name="args"></param>
/// <returns></returns> /// <returns></returns>
private static Assembly OnAssemblyResolve(object sender, ResolveEventArgs args) private static Assembly? OnAssemblyResolve(object sender, ResolveEventArgs args)
{ {
var name = new AssemblyName(args.Name).Name; var name = new AssemblyName(args.Name).Name;
return name.EndsWith(".resources") ? null : LoadAssembly(name); if (name.EndsWith(".resources"))
} {
return default;
}
/// <summary>
/// 从资源加载程序集
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
private static Assembly LoadAssembly(string name)
{
var stream = GetResourceStream(new Uri($"Resource/{name}.dll", UriKind.Relative)).Stream; var stream = GetResourceStream(new Uri($"Resource/{name}.dll", UriKind.Relative)).Stream;
var buffer = new byte[stream.Length]; var buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length); stream.Read(buffer, 0, buffer.Length);
return Assembly.Load(buffer); return Assembly.Load(buffer);
} }
/// <summary>
/// 程序启动
/// </summary>
/// <param name="e"></param>
protected override void OnStartup(StartupEventArgs e)
{
if (this.isFirstInstance == false)
{
this.Shutdown();
}
else
{
StartFastGithub();
SetWebBrowserVersion(9000);
}
base.OnStartup(e);
}
/// <summary> /// <summary>
/// 设置浏览器版本 /// 设置浏览器版本
/// </summary> /// </summary>

View File

@ -1,32 +1,34 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<OutputType>WinExe</OutputType> <UseWPF>true</UseWPF>
<TargetFramework>net45</TargetFramework> <UseWindowsForms>true</UseWindowsForms>
<UseWindowsForms>true</UseWindowsForms> <OutputType>WinExe</OutputType>
<UseWPF>true</UseWPF> <LangVersion>8.0</LangVersion>
<PackageLicenseExpression>MIT</PackageLicenseExpression> <Nullable>enable</Nullable>
<ApplicationManifest>app.manifest</ApplicationManifest> <TargetFramework>net45</TargetFramework>
<ApplicationIcon>app.ico</ApplicationIcon> <ApplicationIcon>app.ico</ApplicationIcon>
<AutoGenerateBindingRedirects>False</AutoGenerateBindingRedirects> <ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup> <PackageLicenseExpression>MIT</PackageLicenseExpression>
<AutoGenerateBindingRedirects>False</AutoGenerateBindingRedirects>
</PropertyGroup>
<ItemGroup> <ItemGroup>
<None Remove="Resource\*.*" /> <None Remove="Resource\*.*" />
<Resource Include="Resource\*.*" /> <Resource Include="Resource\*.*" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Reference Include="System.Net.Http" /> <Reference Include="System.Net.Http" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<PackageReference Include="LiveCharts.Wpf" Version="0.9.7">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="LiveCharts.Wpf" Version="0.9.7">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
</Project> </Project>

View File

@ -1,5 +1,6 @@
using LiveCharts; using LiveCharts;
using LiveCharts.Wpf; using LiveCharts.Wpf;
using Newtonsoft.Json;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net.Http; using System.Net.Http;
@ -40,18 +41,18 @@ namespace FastGithub.UI
this.Series.Add(this.readSeries); this.Series.Add(this.readSeries);
this.Series.Add(this.writeSeries); this.Series.Add(this.writeSeries);
DataContext = this; this.DataContext = this;
this.InitFlowChart(); this.InitFlowChart();
} }
private async void InitFlowChart() private async void InitFlowChart()
{ {
var httpClient = new HttpClient(); using var httpClient = new HttpClient();
while (true) while (this.Dispatcher.HasShutdownStarted == false)
{ {
try try
{ {
await this.GetFlowStatisticsAsync(httpClient); await this.FlushFlowStatisticsAsync(httpClient);
} }
catch (Exception) catch (Exception)
{ {
@ -63,11 +64,15 @@ namespace FastGithub.UI
} }
} }
private async Task GetFlowStatisticsAsync(HttpClient httpClient) private async Task FlushFlowStatisticsAsync(HttpClient httpClient)
{ {
var response = await httpClient.GetAsync("http://127.0.0.1/flowStatistics"); var response = await httpClient.GetAsync("http://127.0.0.1/flowStatistics");
var json = await response.EnsureSuccessStatusCode().Content.ReadAsStringAsync(); var json = await response.EnsureSuccessStatusCode().Content.ReadAsStringAsync();
var flowStatistics = Newtonsoft.Json.JsonConvert.DeserializeObject<FlowStatistics>(json); var flowStatistics = JsonConvert.DeserializeObject<FlowStatistics>(json);
if (flowStatistics == null)
{
return;
}
this.textBlockRead.Text = FlowStatistics.ToNetworkSizeString(flowStatistics.TotalRead); this.textBlockRead.Text = FlowStatistics.ToNetworkSizeString(flowStatistics.TotalRead);
this.textBlockWrite.Text = FlowStatistics.ToNetworkSizeString(flowStatistics.TotalWrite); this.textBlockWrite.Text = FlowStatistics.ToNetworkSizeString(flowStatistics.TotalWrite);

15
FastGithub.UI/LogLevel.cs Normal file
View File

@ -0,0 +1,15 @@
namespace FastGithub.UI
{
/// <summary>
/// 日志等级
/// </summary>
public enum LogLevel
{
Verbose,
Debug,
Information,
Warning,
Error,
Fatal
}
}

View File

@ -12,7 +12,7 @@ namespace FastGithub.UI
/// </summary> /// </summary>
public partial class MainWindow : Window public partial class MainWindow : Window
{ {
private System.Windows.Forms.NotifyIcon notifyIcon; private readonly System.Windows.Forms.NotifyIcon notifyIcon;
private const string FAST_GITHUB = "FastGithub"; private const string FAST_GITHUB = "FastGithub";
private const string PROJECT_URI = "https://github.com/dotnetcore/FastGithub"; private const string PROJECT_URI = "https://github.com/dotnetcore/FastGithub";
@ -78,7 +78,7 @@ namespace FastGithub.UI
protected override void OnSourceInitialized(EventArgs e) protected override void OnSourceInitialized(EventArgs e)
{ {
base.OnSourceInitialized(e); base.OnSourceInitialized(e);
var hwndSource = PresentationSource.FromVisual(this) as HwndSource; var hwndSource = (HwndSource)PresentationSource.FromVisual(this);
hwndSource.AddHook(WndProc); hwndSource.AddHook(WndProc);
IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)

View File

@ -1,6 +1,5 @@
using System; using System;
using System.Windows; using System.Windows;
using System.Windows.Media;
namespace FastGithub.UI namespace FastGithub.UI
{ {
@ -8,17 +7,18 @@ namespace FastGithub.UI
{ {
public DateTime Timestamp { get; set; } public DateTime Timestamp { get; set; }
public string Level { get; set; } public LogLevel Level { get; set; }
public string Message { get; set; } public string Message { get; set; } = string.Empty;
public string SourceContext { get; set; } public string SourceContext { get; set; } = string.Empty;
public string Color => this.Level == "Information" ? "#333" : "IndianRed"; public string Color => this.Level <= LogLevel.Information ? "#333" : "IndianRed";
public void SetToClipboard() public void SetToClipboard()
{ {
Clipboard.SetText($"{this.Timestamp:yyyy-MM-dd HH:mm:ss.fff}\r\n{this.Message}"); Clipboard.SetText($"{this.Timestamp:yyyy-MM-dd HH:mm:ss.fff}\r\n{this.Message}");
} }
} }
} }

View File

@ -34,8 +34,8 @@
<MenuItem Header="清除所有" Click="MenuItem_Clear_Click" /> <MenuItem Header="清除所有" Click="MenuItem_Clear_Click" />
</ContextMenu> </ContextMenu>
</StackPanel.ContextMenu> </StackPanel.ContextMenu>
<TextBlock TextWrapping="Wrap" FontSize="12" Margin="0 3" Text="{Binding Timestamp, StringFormat={}{0:yyyy-MM-dd HH:mm:ss.fff}}"/> <TextBlock TextWrapping="Wrap" FontSize="13" Margin="0 2" Text="{Binding Timestamp, StringFormat={}{0:yyyy-MM-dd HH:mm:ss.fff}}"/>
<TextBlock TextWrapping="Wrap" FontSize="14" Margin="0 3" FontWeight="Light" Text="{Binding Message}"> <TextBlock TextWrapping="Wrap" FontSize="14" Margin="0 2" FontWeight="Light" Text="{Binding Message}">
<TextBlock.Foreground> <TextBlock.Foreground>
<SolidColorBrush Color="{Binding Color}"/> <SolidColorBrush Color="{Binding Color}"/>
</TextBlock.Foreground> </TextBlock.Foreground>

View File

@ -1,4 +1,5 @@
using System; using Newtonsoft.Json;
using System;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
@ -21,22 +22,25 @@ namespace FastGithub.UI
public UdpLogListBox() public UdpLogListBox()
{ {
InitializeComponent(); InitializeComponent();
DataContext = this;
this.DataContext = this;
this.InitUdpLoggerAsync(); this.InitUdpLoggerAsync();
} }
private async void InitUdpLoggerAsync() private async void InitUdpLoggerAsync()
{ {
this.socket.Bind(new IPEndPoint(IPAddress.Loopback, UdpLoggerPort.Value)); this.socket.Bind(new IPEndPoint(IPAddress.Loopback, UdpLoggerPort.Value));
while (true) while (this.Dispatcher.HasShutdownStarted == false)
{ {
var log = await this.GetUdpLogAsync(); var log = await this.GetUdpLogAsync();
this.LogList.Add(log); if (log != null)
{
this.LogList.Add(log);
}
} }
} }
private async Task<UdpLog> GetUdpLogAsync() private async Task<UdpLog?> GetUdpLogAsync()
{ {
EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0); EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
var taskCompletionSource = new TaskCompletionSource<int>(); var taskCompletionSource = new TaskCompletionSource<int>();
@ -44,10 +48,9 @@ namespace FastGithub.UI
var length = await taskCompletionSource.Task; var length = await taskCompletionSource.Task;
var json = Encoding.UTF8.GetString(buffer, 0, length); var json = Encoding.UTF8.GetString(buffer, 0, length);
return Newtonsoft.Json.JsonConvert.DeserializeObject<UdpLog>(json); return JsonConvert.DeserializeObject<UdpLog>(json);
} }
private void EndReceiveFrom(IAsyncResult ar) private void EndReceiveFrom(IAsyncResult ar)
{ {
EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0); EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);

View File

@ -1,52 +1,52 @@
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<Nullable>enable</Nullable> <AssemblyName>fastgithub</AssemblyName>
<AssemblyName>fastgithub</AssemblyName> <OutputType>Exe</OutputType>
<OutputType>Exe</OutputType> <PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageLicenseExpression>MIT</PackageLicenseExpression> <ApplicationManifest>app.manifest</ApplicationManifest>
<ApplicationManifest>app.manifest</ApplicationManifest> <EnableCompressionInSingleFile>true</EnableCompressionInSingleFile>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="PInvoke.AdvApi32" Version="0.7.104" /> <PackageReference Include="PInvoke.AdvApi32" Version="0.7.104" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="5.0.1" /> <PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="5.0.1" />
<PackageReference Include="Serilog.Sinks.Network" Version="2.0.2.68" /> <PackageReference Include="Serilog.Sinks.Network" Version="2.0.2.68" />
<ProjectReference Include="..\FastGithub.DomainResolve\FastGithub.DomainResolve.csproj" /> <ProjectReference Include="..\FastGithub.DomainResolve\FastGithub.DomainResolve.csproj" />
<ProjectReference Include="..\FastGithub.HttpServer\FastGithub.HttpServer.csproj" /> <ProjectReference Include="..\FastGithub.HttpServer\FastGithub.HttpServer.csproj" />
<ProjectReference Include="..\FastGithub.PacketIntercept\FastGithub.PacketIntercept.csproj" /> <ProjectReference Include="..\FastGithub.PacketIntercept\FastGithub.PacketIntercept.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Serilog.AspNetCore" Version="4.1.0" /> <PackageReference Include="Serilog.AspNetCore" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.0" /> <PackageReference Include="Serilog.Sinks.Console" Version="4.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" /> <PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.2.0" /> <PackageReference Include="Serilog.Settings.Configuration" Version="3.2.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="../README.md" Link="README.md"> <None Include="../README.md" Link="README.md">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None> </None>
<None Include="../README.html" Link="README.html"> <None Include="../README.html" Link="README.html">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None> </None>
<None Include="../LICENSE" Link="LICENSE"> <None Include="../LICENSE" Link="LICENSE">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None> </None>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Update="appsettings.json"> <None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None> </None>
<None Update="appsettings/appsettings.*.json"> <None Update="appsettings/appsettings.*.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None> </None>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="Properties\" /> <Folder Include="Properties\" />
</ItemGroup> </ItemGroup>
</Project> </Project>