浏览代码

housekeeping: Update framework to .net5.0

Bruce Wayne 4 年之前
父节点
当前提交
93159da643

+ 1 - 0
.gitattributes

@@ -0,0 +1 @@
+*.cs eol=crlf

+ 10 - 1
.github/dependabot.yml

@@ -22,4 +22,13 @@ updates:
       timezone: "Asia/Shanghai"
     labels:
       - "Automatic"
-    open-pull-requests-limit: 99
+    open-pull-requests-limit: 99
+  - package-ecosystem: "gitsubmodule"
+    directory: "/"
+    schedule:
+      interval: "daily"
+      time: "07:30"
+      timezone: "Asia/Shanghai"
+    labels:
+      - "Automatic"
+    open-pull-requests-limit: 99

+ 146 - 38
.github/workflows/CI.yaml

@@ -2,48 +2,156 @@ name: CI
 on: [push, pull_request]
 env:
   ProjectName: NatTypeTester
+  NET_TFM: net5.0-windows10.0.20348.0
+  Configuration: Release
 
 jobs:
+  check_format:
+    name: Check format
+    runs-on: windows-latest
+    steps:
+      - name: Setup .NET
+        uses: actions/setup-dotnet@v1
+        with:
+          dotnet-version: 5.0.x
+
+      - name: Install dotnet-format
+        run: dotnet tool update -g dotnet-format
+
+      - name: Checkout code
+        uses: actions/checkout@v2
+
+      - name: Build
+        run: dotnet build
+
+      - name: Run dotnet format check
+        run: dotnet format -wsa -v diag --check --no-restore
+
   build:
     name: Build
+    needs: check_format
+    if: ${{ !startsWith(github.ref, 'refs/tags/') }}
     runs-on: windows-latest
-    env:
-      NET_TFM: net48
-      Configuration: Release
+    strategy:
+      matrix:
+        Rid: [generic, win-x64, win-x86]
+    steps:
+      - name: Checkout code
+        uses: actions/checkout@v2
+        with:
+          submodules: true
+
+      - name: Setup .NET
+        uses: actions/setup-dotnet@v1
+        with:
+          dotnet-version: 5.0.x
+
+      - name: Build ${{ matrix.Rid }}
+        shell: pwsh
+        run: .\build.ps1 ${{ matrix.Rid }}
+
+      - name: Upload ${{ matrix.Rid }}
+        continue-on-error: true
+        uses: actions/upload-artifact@v2
+        with:
+          name: ${{ env.ProjectName }}-${{ matrix.Rid }}
+          path: ${{ env.ProjectName }}\bin\${{ env.Configuration }}\${{ env.NET_TFM }}\${{ matrix.Rid }}\publish\
+
+  nuget:
+    name: Nuget
+    needs: check_format
+    if: ${{ github.event_name == 'push' && startsWith(github.ref, 'refs/tags/') }}
+    runs-on: ubuntu-latest
+    strategy:
+      matrix:
+        PackageName: [STUN]
 
     steps:
-    - uses: actions/checkout@v2
-
-    - name: Add msbuild to PATH
-      uses: microsoft/[email protected]
-
-    - name: Build
-      shell: pwsh
-      run: msbuild -v:m -m -r -p:Configuration=Release NatTypeTester/NatTypeTester.csproj
-
-    - name: Upload
-      continue-on-error: true
-      if: ${{ !startsWith(github.ref, 'refs/tags/') }}
-      uses: actions/upload-artifact@v2
-      with:
-        name: ${{ env.ProjectName }}
-        path: ${{ env.ProjectName }}/bin/${{ env.Configuration }}/${{ env.NET_TFM }}/NatTypeTester.exe
-
-    - name: Get tag
-      if: ${{ github.event_name == 'push' && startsWith(github.ref, 'refs/tags/') }}
-      id: tag
-      uses: dawidd6/action-get-tag@v1
-
-    - name: Create a new GitHub release if a new tag is pushed
-      uses: softprops/action-gh-release@v1
-      if: ${{ github.event_name == 'push' && startsWith(github.ref, 'refs/tags/') }}
-      env:
-        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
-      with:
-        name: v${{ steps.tag.outputs.tag }}
-        prerelease: true
-        draft: false
-        files: ${{ env.ProjectName }}/bin/${{ env.Configuration }}/${{ env.NET_TFM }}/NatTypeTester.exe
-        body: |
-          ## 更新日志:
-          * 这是 GitHub Actions 自动化部署,更新日志应该很快会手动更新
+      - name: Checkout code
+        uses: actions/checkout@v2
+
+      - name: Setup .NET
+        uses: actions/setup-dotnet@v1
+        with:
+          dotnet-version: 5.0.x
+
+      - name: Build
+        shell: pwsh
+        run: dotnet build -c Release ${{ matrix.PackageName }}\${{ matrix.PackageName }}.csproj
+
+      - name: Push nuget packages
+        shell: pwsh
+        run: |
+          dotnet nuget push ${{ matrix.PackageName }}\bin\Release\*.nupkg -s https://nuget.pkg.github.com/HMBSbige -k ${{ secrets.GITHUB_TOKEN }} --skip-duplicate
+          dotnet nuget push ${{ matrix.PackageName }}\bin\Release\*.nupkg -s https://api.nuget.org/v3/index.json -k ${{ secrets.NuGetAPIKey }} --skip-duplicate
+
+  release:
+    name: Release
+    needs: nuget
+    runs-on: windows-latest
+
+    steps:
+      - name: Checkout code
+        uses: actions/checkout@v2
+        with:
+          submodules: true
+
+      - name: Setup .NET
+        uses: actions/setup-dotnet@v1
+        with:
+          dotnet-version: 5.0.x
+
+      - name: Build
+        shell: pwsh
+        run: .\build.ps1
+
+      - name: Get tag
+        id: tag
+        uses: dawidd6/action-get-tag@v1
+
+      - name: Package generic
+        shell: pwsh
+        run: |
+          New-Item -ItemType Directory -Path C:\builtfiles -Force > $null
+          $zip_path = "C:\builtfiles\$env:ProjectName-generic-${{ steps.tag.outputs.tag }}.7z"
+          7z a -mx9 "$zip_path" ".\$env:ProjectName\bin\$env:Configuration\$env:NET_TFM\generic\publish\"
+          7z rn "$zip_path" publish $env:ProjectName
+          echo "GENERIC_SHA256=$((Get-FileHash $zip_path -Algorithm SHA256).Hash)" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
+
+      - name: Package win-x64
+        shell: pwsh
+        run: |
+          New-Item -ItemType Directory -Path C:\builtfiles -Force > $null
+          $zip_path = "C:\builtfiles\$env:ProjectName-win-x64-${{ steps.tag.outputs.tag }}.7z"
+          7z a -mx9 "$zip_path" ".\$env:ProjectName\bin\$env:Configuration\$env:NET_TFM\win-x64\publish\"
+          7z rn "$zip_path" publish $env:ProjectName
+          echo "WINX64_SHA256=$((Get-FileHash $zip_path -Algorithm SHA256).Hash)" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
+
+      - name: Package win-x86
+        shell: pwsh
+        run: |
+          New-Item -ItemType Directory -Path C:\builtfiles -Force > $null
+          $zip_path = "C:\builtfiles\$env:ProjectName-win-x86-${{ steps.tag.outputs.tag }}.7z"
+          7z a -mx9 "$zip_path" ".\$env:ProjectName\bin\$env:Configuration\$env:NET_TFM\win-x86\publish\"
+          7z rn "$zip_path" publish $env:ProjectName
+          echo "WINX86_SHA256=$((Get-FileHash $zip_path -Algorithm SHA256).Hash)" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
+
+      - name: Changelog
+        uses: glennawatson/ChangeLog@v1
+        id: changelog
+
+      - name: Create a new GitHub release
+        uses: ncipollo/release-action@v1
+        with:
+          token: ${{ secrets.GITHUB_TOKEN }}
+          prerelease: true
+          draft: false
+          artifacts: C:\builtfiles\*
+          body: |
+            ${{ steps.changelog.outputs.commitLog }}
+            ## Hash
+            | Filename | SHA-256 |
+            | :- | :- |
+            | <sub>${{ env.ProjectName }}-generic-${{ steps.tag.outputs.tag }}.7z</sub> | <sub>${{ env.GENERIC_SHA256 }}</sub> |
+            | <sub>${{ env.ProjectName }}-win-x64-${{ steps.tag.outputs.tag }}.7z</sub> | <sub>${{ env.WINX64_SHA256 }}</sub> |
+            | <sub>${{ env.ProjectName }}-win-x86-${{ steps.tag.outputs.tag }}.7z</sub> | <sub>${{ env.WINX86_SHA256 }}</sub> |

+ 0 - 25
.github/workflows/nuget.yml

@@ -1,25 +0,0 @@
-name: Nuget
-on: [push, pull_request]
-
-jobs:
-  build:
-    name: Build
-    runs-on: ubuntu-latest
-    strategy:
-      matrix:
-        ProjectName: [ STUN ]
-
-    steps:
-    - name: Checkout code
-      uses: actions/checkout@v2
-
-    - name: Build
-      shell: pwsh
-      run: dotnet build -c Release ${{ matrix.ProjectName }}\${{ matrix.ProjectName }}.csproj
-
-    - name: Push nuget packages
-      if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' }}
-      shell: pwsh
-      run: |
-        dotnet nuget push ${{ matrix.ProjectName }}\bin\Release\*.nupkg -s https://nuget.pkg.github.com/HMBSbige -k ${{ secrets.GITHUB_TOKEN }} --skip-duplicate
-        dotnet nuget push ${{ matrix.ProjectName }}\bin\Release\*.nupkg -s https://api.nuget.org/v3/index.json -k ${{ secrets.NuGetAPIKey }} --skip-duplicate

+ 3 - 0
.gitmodules

@@ -0,0 +1,3 @@
+[submodule "Build/DotNetDllPathPatcher"]
+	path = Build/DotNetDllPathPatcher
+	url = https://github.com/HMBSbige/DotNetDllPathPatcher

+ 1 - 0
Build/DotNetDllPathPatcher

@@ -0,0 +1 @@
+Subproject commit 4f01b229820a8b9fc8ffc4525fced8f4ace140d0

+ 2 - 3
NatTypeTester-Console/NatTypeTester_Console.csproj

@@ -1,11 +1,10 @@
 <Project Sdk="Microsoft.NET.Sdk">
 
+  <Import Project="..\common.props" />
+
   <PropertyGroup>
     <OutputType>Exe</OutputType>
-    <TargetFrameworks>net48;net5.0</TargetFrameworks>
     <RootNamespace>NatTypeTester</RootNamespace>
-    <LangVersion>latest</LangVersion>
-    <Nullable>enable</Nullable>
     <AssemblyName>NatTypeTester.Console</AssemblyName>
   </PropertyGroup>
 

+ 0 - 4
NatTypeTester.Models/NatTypeTester.Models.csproj

@@ -2,10 +2,6 @@
 
   <Import Project="..\common.props" />
 
-  <PropertyGroup>
-    <TargetFramework>netstandard2.0</TargetFramework>
-  </PropertyGroup>
-
   <ItemGroup>
     <PackageReference Include="ReactiveUI.Fody" Version="14.3.10" />
     <PackageReference Include="Volo.Abp.Core" Version="4.4.0" />

+ 0 - 4
NatTypeTester.ViewModels/NatTypeTester.ViewModels.csproj

@@ -2,10 +2,6 @@
 
   <Import Project="..\common.props" />
 
-  <PropertyGroup>
-    <TargetFramework>netstandard2.0</TargetFramework>
-  </PropertyGroup>
-
   <ItemGroup>
     <PackageReference Include="ReactiveUI.Fody" Version="14.3.10" />
     <PackageReference Include="Volo.Abp.Core" Version="4.4.0" />

+ 0 - 3
NatTypeTester/FodyWeavers.xml

@@ -1,3 +0,0 @@
-<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
-  <Costura />
-</Weavers>

+ 4 - 14
NatTypeTester/NatTypeTester.csproj

@@ -1,26 +1,16 @@
-<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
+<Project Sdk="Microsoft.NET.Sdk">
 
   <Import Project="..\common.props" />
 
   <PropertyGroup>
+    <TargetFramework>net5.0-windows10.0.20348.0</TargetFramework>
     <OutputType>WinExe</OutputType>
-    <TargetFramework>net48</TargetFramework>
     <UseWPF>true</UseWPF>
+    <Version>3.4</Version>
     <ApplicationIcon>icon.ico</ApplicationIcon>
   </PropertyGroup>
 
-  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
-    <PlatformTarget>AnyCPU</PlatformTarget>
-  </PropertyGroup>
-
-  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
-    <PlatformTarget>AnyCPU</PlatformTarget>
-  </PropertyGroup>
-
   <ItemGroup>
-    <PackageReference Include="Costura.Fody" Version="5.4.0">
-      <PrivateAssets>All</PrivateAssets>
-    </PackageReference>
     <PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
     <PackageReference Include="ModernWpfUI" Version="0.9.4" />
     <PackageReference Include="ReactiveUI.Events.WPF" Version="14.3.10" />
@@ -33,4 +23,4 @@
     <ProjectReference Include="..\NatTypeTester.ViewModels\NatTypeTester.ViewModels.csproj" />
   </ItemGroup>
 
-</Project>
+</Project>

+ 12 - 9
README.md

@@ -1,16 +1,19 @@
 # NatTypeTester
-[![CI](https://github.com/HMBSbige/NatTypeTester/workflows/CI/badge.svg)](https://github.com/HMBSbige/NatTypeTester/actions)
+Channel | Status
+-|-
+CI | [![CI](https://github.com/HMBSbige/NatTypeTester/workflows/CI/badge.svg)](https://github.com/HMBSbige/NatTypeTester/actions)
+Stun.Net | [![NuGet.org](https://img.shields.io/nuget/v/Stun.Net.svg?logo=nuget)](https://www.nuget.org/packages/Stun.Net/)
 
 ## RFC supports
 
-- [x] [RFC 3489](https://tools.ietf.org/html/rfc3489)
-- [x] [RFC 5389](https://tools.ietf.org/html/rfc5389)
-- [x] [RFC 5769](https://tools.ietf.org/html/rfc5769)
-- [x] [RFC 5780](https://tools.ietf.org/html/rfc5780)
-- [ ] [RFC 7350](https://tools.ietf.org/html/rfc7350)
-- [ ] [RFC 7443](https://tools.ietf.org/html/rfc7443)
-- [ ] [RFC 7635](https://tools.ietf.org/html/rfc7635)
-- [ ] [RFC 8489](https://tools.ietf.org/html/rfc8489)
+- [x] [RFC 3489](https://datatracker.ietf.org/doc/html/rfc3489)
+- [x] [RFC 5389](https://datatracker.ietf.org/doc/html/rfc5389)
+- [x] [RFC 5769](https://datatracker.ietf.org/doc/html/rfc5769)
+- [x] [RFC 5780](https://datatracker.ietf.org/doc/html/rfc5780)
+- [ ] [RFC 7350](https://datatracker.ietf.org/doc/html/rfc7350)
+- [ ] [RFC 7443](https://datatracker.ietf.org/doc/html/rfc7443)
+- [ ] [RFC 7635](https://datatracker.ietf.org/doc/html/rfc7635)
+- [ ] [RFC 8489](https://datatracker.ietf.org/doc/html/rfc8489)
 
 ## Internet Protocol
 

+ 1 - 1
STUN/Message/Attributes/AddressAttribute.cs

@@ -69,7 +69,7 @@ namespace STUN.Message.Attributes
 			return true;
 		}
 
-		public override string ToString()
+		public override string? ToString()
 		{
 			return Address?.AddressFamily switch
 			{

+ 2 - 4
STUN/Proxy/NoneUdpProxy.cs

@@ -17,7 +17,7 @@ namespace STUN.Proxy
 			set => _udpClient.Client.ReceiveTimeout = Convert.ToInt32(value.TotalMilliseconds);
 		}
 
-		public IPEndPoint LocalEndPoint => (IPEndPoint)_udpClient.Client.LocalEndPoint;
+		public IPEndPoint LocalEndPoint => (IPEndPoint)_udpClient.Client.LocalEndPoint!;
 
 		private readonly UdpClient _udpClient;
 
@@ -38,9 +38,7 @@ namespace STUN.Proxy
 
 		public async Task<(byte[], IPEndPoint, IPAddress)> ReceiveAsync(byte[] bytes, IPEndPoint remote, EndPoint receive, CancellationToken token = default)
 		{
-			var localEndPoint = (IPEndPoint)_udpClient.Client.LocalEndPoint;
-
-			Debug.WriteLine($@"{localEndPoint} => {remote} {bytes.Length} 字节");
+			Debug.WriteLine($@"{LocalEndPoint} => {remote} {bytes.Length} 字节");
 
 			await _udpClient.SendAsync(bytes, bytes.Length, remote);
 

+ 2 - 2
STUN/Proxy/Socks5UdpProxy.cs

@@ -28,7 +28,7 @@ namespace STUN.Proxy
 			}
 		}
 
-		public IPEndPoint LocalEndPoint => (IPEndPoint)_udpClient.Client.LocalEndPoint;
+		public IPEndPoint LocalEndPoint => (IPEndPoint)_udpClient.Client.LocalEndPoint!;
 
 		private readonly UdpClient _udpClient;
 
@@ -53,7 +53,7 @@ namespace STUN.Proxy
 			try
 			{
 				var buf = new byte[1024];
-				await _assoc.ConnectAsync(_socksTcpEndPoint.Address, _socksTcpEndPoint.Port);
+				await _assoc.ConnectAsync(_socksTcpEndPoint.Address, _socksTcpEndPoint.Port, token);
 				var s = _assoc.GetStream();
 				using var _ = token.Register(() => s.Close());
 				var requestPasswordAuth = !string.IsNullOrEmpty(_user);

+ 2 - 3
STUN/STUN.csproj

@@ -1,9 +1,8 @@
 <Project Sdk="Microsoft.NET.Sdk">
 
+  <Import Project="..\common.props" />
+
   <PropertyGroup>
-    <TargetFramework>netstandard2.0</TargetFramework>
-    <LangVersion>latest</LangVersion>
-    <Nullable>enable</Nullable>
     <NoWarn>CS1591</NoWarn>
     <GenerateDocumentationFile>true</GenerateDocumentationFile>
     <GeneratePackageOnBuild>true</GeneratePackageOnBuild>

+ 2 - 3
UnitTest/UnitTest.csproj

@@ -1,9 +1,8 @@
 <Project Sdk="Microsoft.NET.Sdk">
 
+  <Import Project="..\common.props" />
+
   <PropertyGroup>
-    <TargetFramework>net5.0</TargetFramework>
-    <LangVersion>latest</LangVersion>
-    <Nullable>enable</Nullable>
     <IsPackable>false</IsPackable>
   </PropertyGroup>
 

+ 68 - 0
build.ps1

@@ -0,0 +1,68 @@
+param([string]$rid = 'all')
+$ErrorActionPreference = 'Stop'
+
+Write-Host 'dotnet SDK info'
+dotnet --info
+
+$proj = 'NatTypeTester'
+$exe = "$proj.exe"
+$net_tfm = 'net5.0-windows10.0.20348.0'
+$configuration = 'Release'
+$output_dir = "$PSScriptRoot\$proj\bin\$configuration"
+$proj_path = "$PSScriptRoot\$proj\$proj.csproj"
+
+$dllpatcher_tfm = 'net5.0'
+$dllpatcher_dir = "$PSScriptRoot\Build\DotNetDllPathPatcher"
+$dllpatcher_exe = "$dllpatcher_dir\bin\$configuration\$dllpatcher_tfm\DotNetDllPathPatcher.exe"
+
+function Build-Generic
+{
+    Write-Host 'Building generic'
+
+    $outdir = "$output_dir\$net_tfm\generic"
+    $publishDir = "$outdir\publish"
+
+    Remove-Item $publishDir -Recurse -Force -Confirm:$false -ErrorAction Ignore
+
+    dotnet publish -c $configuration -f $net_tfm $proj_path -o $publishDir
+    if ($LASTEXITCODE) { exit $LASTEXITCODE }
+
+    & "$dllpatcher_exe" "$publishDir\$exe" bin
+    if ($LASTEXITCODE) { exit $LASTEXITCODE }
+}
+
+function Build-SelfContained
+{
+    param([string]$rid)
+
+    Write-Host "Building $rid"
+
+    $outdir = "$output_dir\$net_tfm\$rid"
+    $publishDir = "$outdir\publish"
+
+    Remove-Item $publishDir -Recurse -Force -Confirm:$false -ErrorAction Ignore
+
+    dotnet publish -c $configuration -f $net_tfm -r $rid --self-contained true $proj_path
+    if ($LASTEXITCODE) { exit $LASTEXITCODE }
+
+    & "$dllpatcher_exe" "$publishDir\$exe" bin
+    if ($LASTEXITCODE) { exit $LASTEXITCODE }
+}
+
+dotnet build -c $configuration -f $dllpatcher_tfm $dllpatcher_dir\DotNetDllPathPatcher.csproj
+if ($LASTEXITCODE) { exit $LASTEXITCODE }
+
+if($rid -eq 'all' -or $rid -eq 'generic')
+{
+    Build-Generic
+}
+
+if($rid -eq 'all')
+{
+    Build-SelfContained win-x86
+    Build-SelfContained win-x64
+}
+elseif($rid -ne 'generic')
+{
+    Build-SelfContained $rid
+}

+ 3 - 3
common.props

@@ -1,8 +1,8 @@
 <Project>
-  <PropertyGroup>
+  <PropertyGroup>  
+    <TargetFramework>net5.0</TargetFramework>
     <LangVersion>latest</LangVersion>
-    <Version>3.4</Version>
-    <Nullable>enable</Nullable>    
+    <Nullable>enable</Nullable>
     <Authors>HMBSbige</Authors>
   </PropertyGroup>
 </Project>