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>
<PropertyGroup>
<Version>2.0.5</Version>
<Nullable>enable</Nullable>
<TargetFramework>net6.0</TargetFramework>
<DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>
<IsWebConfigTransformDisabled>true</IsWebConfigTransformDisabled>
<EnableCompressionInSingleFile>true</EnableCompressionInSingleFile>
<Description>github加速神器</Description>
<Copyright>https://github.com/dotnetcore/FastGithub</Copyright>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>

View File

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

View File

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

View File

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

View File

@ -1,7 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Portable.BouncyCastle" Version="1.8.10" />

View File

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

View File

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

View File

@ -13,27 +13,13 @@ namespace FastGithub.UI
/// </summary>
public partial class App : Application
{
private Mutex globalMutex;
private readonly Mutex globalMutex;
private readonly bool isFirstInstance;
/// <summary>
/// 程序启动
/// </summary>
/// <param name="e"></param>
protected override void OnStartup(StartupEventArgs e)
{
this.globalMutex = new Mutex(true, "Global\\FastGithub.UI", out var firstInstance);
if (firstInstance == false)
{
this.Shutdown();
}
else
public App()
{
this.globalMutex = new Mutex(true, "Global\\FastGithub.UI", out this.isFirstInstance);
AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve;
SetWebBrowserVersion(9000);
StartFastGithub();
}
base.OnStartup(e);
}
/// <summary>
@ -42,25 +28,41 @@ namespace FastGithub.UI
/// <param name="sender"></param>
/// <param name="args"></param>
/// <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;
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 buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
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>

View File

@ -1,13 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net45</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<UseWPF>true</UseWPF>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<ApplicationManifest>app.manifest</ApplicationManifest>
<UseWindowsForms>true</UseWindowsForms>
<OutputType>WinExe</OutputType>
<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>
<TargetFramework>net45</TargetFramework>
<ApplicationIcon>app.ico</ApplicationIcon>
<ApplicationManifest>app.manifest</ApplicationManifest>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<AutoGenerateBindingRedirects>False</AutoGenerateBindingRedirects>
</PropertyGroup>

View File

@ -1,5 +1,6 @@
using LiveCharts;
using LiveCharts.Wpf;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net.Http;
@ -40,18 +41,18 @@ namespace FastGithub.UI
this.Series.Add(this.readSeries);
this.Series.Add(this.writeSeries);
DataContext = this;
this.DataContext = this;
this.InitFlowChart();
}
private async void InitFlowChart()
{
var httpClient = new HttpClient();
while (true)
using var httpClient = new HttpClient();
while (this.Dispatcher.HasShutdownStarted == false)
{
try
{
await this.GetFlowStatisticsAsync(httpClient);
await this.FlushFlowStatisticsAsync(httpClient);
}
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 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.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>
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 PROJECT_URI = "https://github.com/dotnetcore/FastGithub";
@ -78,7 +78,7 @@ namespace FastGithub.UI
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
var hwndSource = PresentationSource.FromVisual(this) as HwndSource;
var hwndSource = (HwndSource)PresentationSource.FromVisual(this);
hwndSource.AddHook(WndProc);
IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)

View File

@ -1,6 +1,5 @@
using System;
using System.Windows;
using System.Windows.Media;
namespace FastGithub.UI
{
@ -8,17 +7,18 @@ namespace FastGithub.UI
{
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()
{
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" />
</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="14" Margin="0 3" FontWeight="Light" Text="{Binding Message}">
<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 2" FontWeight="Light" Text="{Binding Message}">
<TextBlock.Foreground>
<SolidColorBrush Color="{Binding Color}"/>
</TextBlock.Foreground>

View File

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

View File

@ -1,11 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<Nullable>enable</Nullable>
<AssemblyName>fastgithub</AssemblyName>
<OutputType>Exe</OutputType>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<ApplicationManifest>app.manifest</ApplicationManifest>
<EnableCompressionInSingleFile>true</EnableCompressionInSingleFile>
</PropertyGroup>
<ItemGroup>