老九 3 năm trước cách đây
mục cha
commit
4db6ab6b3f

+ 2 - 2
Directory.Build.props

@@ -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>		
+		<IsWebConfigTransformDisabled>true</IsWebConfigTransformDisabled> 		
 		<Description>github加速神器</Description>
 		<Copyright>https://github.com/dotnetcore/FastGithub</Copyright>
 		<RuntimeIdentifier>win-x64</RuntimeIdentifier>	

+ 0 - 3
FastGithub.Configuration/FastGithub.Configuration.csproj

@@ -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" />

+ 0 - 3
FastGithub.DomainResolve/FastGithub.DomainResolve.csproj

@@ -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" />

+ 4 - 8
FastGithub.FlowAnalyze/FastGithub.FlowAnalyze.csproj

@@ -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>
+	<ItemGroup>
+		<FrameworkReference Include="Microsoft.AspNetCore.App" />
+	</ItemGroup>
+
 </Project>

+ 1 - 4
FastGithub.Http/FastGithub.Http.csproj

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

+ 0 - 1
FastGithub.HttpServer/FastGithub.HttpServer.csproj

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

+ 1 - 2
FastGithub.PacketIntercept/FastGithub.PacketIntercept.csproj

@@ -1,7 +1,6 @@
 <Project Sdk="Microsoft.NET.Sdk">
 
 	<PropertyGroup>
-    <Nullable>enable</Nullable>
 		<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
 	</PropertyGroup>
 
@@ -10,7 +9,7 @@
 		<PackageReference Include="DNS" Version="6.1.0" />
 		<PackageReference Include="Microsoft.Extensions.Hosting" 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>

+ 31 - 29
FastGithub.UI/App.xaml.cs

@@ -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)
+        public App()
         {
-            this.globalMutex = new Mutex(true, "Global\\FastGithub.UI", out var firstInstance);
-            if (firstInstance == false)
-            {
-                this.Shutdown();
-            }
-            else
-            {
-                AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve;
-                SetWebBrowserVersion(9000);
-                StartFastGithub();
-            }
-
-            base.OnStartup(e);
+            this.globalMutex = new Mutex(true, "Global\\FastGithub.UI", out this.isFirstInstance);
+            AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve;
         }
 
         /// <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>

+ 28 - 26
FastGithub.UI/FastGithub.UI.csproj

@@ -1,32 +1,34 @@
 <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>
-    <ApplicationIcon>app.ico</ApplicationIcon>
-    <AutoGenerateBindingRedirects>False</AutoGenerateBindingRedirects>
-  </PropertyGroup>
+	<PropertyGroup>
+		<UseWPF>true</UseWPF>
+		<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>
 
-  <ItemGroup>
-    <None Remove="Resource\*.*" />
-    <Resource Include="Resource\*.*" />
-  </ItemGroup>
+	<ItemGroup>
+		<None Remove="Resource\*.*" />
+		<Resource Include="Resource\*.*" />
+	</ItemGroup>
 
-  <ItemGroup>
-    <Reference Include="System.Net.Http" />
-  </ItemGroup>
+	<ItemGroup>
+		<Reference Include="System.Net.Http" />
+	</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>

+ 11 - 6
FastGithub.UI/FlowChart.xaml.cs

@@ -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 - 0
FastGithub.UI/LogLevel.cs

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

+ 2 - 2
FastGithub.UI/MainWindow.xaml.cs

@@ -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)

+ 5 - 5
FastGithub.UI/UdpLog.cs

@@ -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}");
         }
     }
+
 }

+ 2 - 2
FastGithub.UI/UdpLogListBox.xaml

@@ -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>

+ 11 - 8
FastGithub.UI/UdpLogListBox.xaml.cs

@@ -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();
-                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);
             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);

+ 43 - 43
FastGithub/FastGithub.csproj

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