diff --git a/CHANGELOG.md b/CHANGELOG.md index 15b268d4d..d19a9d290 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -495,5 +495,6 @@ + diff --git a/VpnHood.App.Launcher/Updater/PublishInfo2.cs b/VpnHood.App.Launcher/Updater/PublishInfo2.cs deleted file mode 100644 index e0beca23d..000000000 --- a/VpnHood.App.Launcher/Updater/PublishInfo2.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System; - -namespace VpnHood.App.Launcher.Updating; - -public class PublishInfo2 -{ - public Version Version { get; } - - public string? ExeFile { get; set; } - - public Uri UpdateInfoUrl { get; } - - public Uri UpdateScriptUrl { get; } - - public PublishInfo2(Uri updateScriptUrl, Uri updateInfoUrl, Version version) - { - UpdateScriptUrl = updateScriptUrl ?? throw new ArgumentNullException(nameof(updateScriptUrl)); - UpdateInfoUrl = updateInfoUrl ?? throw new ArgumentNullException(nameof(updateInfoUrl)); - Version = version ?? throw new ArgumentNullException(nameof(version)); - } - -} \ No newline at end of file diff --git a/VpnHood.App.Launcher/Updater/Updater2.cs b/VpnHood.App.Launcher/Updater/Updater2.cs deleted file mode 100644 index 3411df2ab..000000000 --- a/VpnHood.App.Launcher/Updater/Updater2.cs +++ /dev/null @@ -1,112 +0,0 @@ -using System; -using System.Diagnostics; -using System.IO; -using System.Net.Http; -using System.Runtime.InteropServices; -using System.Text.Json; -using System.Threading; -using System.Threading.Tasks; -using Microsoft.Extensions.Logging; - -namespace VpnHood.App.Launcher.Updating; - -public class Updater2 : IDisposable -{ - private readonly HttpClient _httpClient = new(); - private readonly Timer _timer; - private readonly ILogger _logger; - private string UpdateFolderPath { get; } - private readonly CancellationTokenSource _cancellationTokenSource; - private CancellationToken CancellationToken => _cancellationTokenSource.Token; - public PublishInfo2 LocalPublishInfo { get; } - public event EventHandler? InstallerScriptExecuted; - - - public Updater2(ILogger logger, UpdaterOptions2 options) - { - _logger = logger; - _cancellationTokenSource = new CancellationTokenSource(); - UpdateFolderPath = options.UpdateFolderPath; - - var localPublishInfoJson = File.ReadAllText(options.PublishInfoFilePath); - LocalPublishInfo = JsonSerializer.Deserialize(localPublishInfoJson) ?? - throw new Exception($"Could not load {nameof(PublishInfo)}!"); - - // make sure UpdateFolderPath exists - _timer = new Timer(CheckUpdateInterval, null, TimeSpan.Zero, options.CheckInterval); - Directory.CreateDirectory(UpdateFolderPath); - } - - private void CheckUpdateInterval(object? state) - { - CheckUpdate().ContinueWith(x => - { - if (x.Exception != null) - _logger.LogError(x.Exception, "Could not check for update."); - - }, CancellationToken); - } - - public async Task CheckUpdate() - { - // read online version - _logger.LogInformation("Checking for update on {UpdateInfoUrl}. CurrentVersion: {CurrentVersion}", LocalPublishInfo.UpdateInfoUrl, LocalPublishInfo.Version); - var onlinePublishInfoJson = await _httpClient.GetStringAsync(LocalPublishInfo.UpdateInfoUrl, CancellationToken); - var onlinePublishInfo = JsonSerializer.Deserialize(onlinePublishInfoJson) ?? - throw new Exception($"Could not load {nameof(PublishInfo)}!"); - - // download if newer - if (onlinePublishInfo.Version <= LocalPublishInfo.Version) - return; - - - _logger.LogInformation($"Updating has been started... " + - $"CurrentVersion: {LocalPublishInfo.Version}, " + - $"OnlineVersion: {onlinePublishInfo.Version}, " + - $"UpdateScriptUrl: {onlinePublishInfo.UpdateScriptUrl}"); - - var scriptFile = await DownloadFile(onlinePublishInfo.UpdateScriptUrl); - await RunScriptFile(scriptFile).WaitForExitAsync(CancellationToken); - InstallerScriptExecuted?.Invoke(this, EventArgs.Empty); - } - - private async Task DownloadFile(Uri fileUri) - { - _logger.LogInformation($"Downloading new version! Url: {fileUri}"); - var scriptFilePath = Path.Combine(UpdateFolderPath, Path.GetFileName(fileUri.LocalPath)); - - // open source stream from net - await using var readStream = await _httpClient.GetStreamAsync(fileUri, CancellationToken); - await using var writeStream = File.OpenWrite(scriptFilePath); - await readStream.CopyToAsync(writeStream, CancellationToken); - return scriptFilePath; - } - - public static Process RunScriptFile(string scriptFilePath) - { - var process = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) - ? RunWindowsScriptFile(scriptFilePath) - : RunLinuxScriptFile(scriptFilePath); - - if (process == null) - throw new Exception("Could not launch the updater."); - - return process; - } - - public static Process RunLinuxScriptFile(string scriptFilePath) - { - return Process.Start("sh", scriptFilePath); - } - - private static Process RunWindowsScriptFile(string scriptFilePath) - { - return Process.Start("powershell", scriptFilePath); - } - - public void Dispose() - { - _cancellationTokenSource.Cancel(); - _timer.Dispose(); - } -} diff --git a/VpnHood.App.Launcher/Updater/UpdaterOptions2.cs b/VpnHood.App.Launcher/Updater/UpdaterOptions2.cs deleted file mode 100644 index c844fca52..000000000 --- a/VpnHood.App.Launcher/Updater/UpdaterOptions2.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Logging.Abstractions; - -namespace VpnHood.App.Launcher.Updating; - -public class UpdaterOptions2 -{ - public TimeSpan CheckInterval { get; set; } = TimeSpan.FromHours(12); - public string PublishInfoFilePath { get; set; } - public string UpdateFolderPath { get; set; } - - public UpdaterOptions2(string publishInfoFilePath, string updateFolderPath) - { - PublishInfoFilePath = publishInfoFilePath; - UpdateFolderPath = updateFolderPath; - } -} \ No newline at end of file diff --git a/VpnHood.Server.App.Net/Install/VpnHoodServer-linux.run.sh b/VpnHood.Server.App.Net/Install/VpnHoodServer-linux.run.sh deleted file mode 100644 index 584315d9c..000000000 --- a/VpnHood.Server.App.Net/Install/VpnHoodServer-linux.run.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash -curDir="$(dirname "$0")"; -exeFile="$curDir/{exeFileParam}"; -chmod +x "$exeFile"; - -# Executing VpnHoodServer -"$exeFile" "$@"; \ No newline at end of file diff --git a/VpnHood.Server.App.Net/Install/VpnHoodServer-linux.sh b/VpnHood.Server.App.Net/Install/VpnHoodServer-linux.sh deleted file mode 100644 index b00146806..000000000 --- a/VpnHood.Server.App.Net/Install/VpnHoodServer-linux.sh +++ /dev/null @@ -1,116 +0,0 @@ -#!/bin/bash -echo "VpnHood Installation for linux"; - -# Default arguments -packageUrl="$packageUrlParam"; -versionTag="$versionTagParam"; -destinationPath="/opt/VpnHoodServer"; -packageFile=""; - -# Read arguments -for i; -do -if [ "$i" = "-autostart" ]; then - autostart="y"; - lastArg=""; continue; - -elif [ "$i" = "-q" ]; then - setDotNet="y"; - quiet="y"; - lastArg=""; continue; - -elif [ "$lastArg" = "-restBaseUrl" ]; then - restBaseUrl=$i; - lastArg=""; continue; - -elif [ "$lastArg" = "-restAuthorization" ]; then - restAuthorization=$i; - lastArg=""; continue; - -elif [ "$lastArg" = "-packageFile" ]; then - packageFile=$i; - lastArg=""; continue; - -elif [ "$lastArg" = "-versionTag" ]; then - versionTag=$i; - lastArg=""; continue; - - -elif [ "$lastArg" != "" ]; then - echo "Unknown argument! argument: $lastArg"; - exit; -fi; -lastArg=$i; -done; - -# User interaction -if [ "$quiet" != "y" ]; then - read -p "Auto Start (y/n)?" autostart; -fi; - -# point to latest version if $packageUrl is not set -if [ "$packageUrl" = "" ]; then - packageUrl="https://github.com/vpnhood/VpnHood/releases/latest/download/VpnHoodServer-linux.tar.gz"; -fi - -# download & install VpnHoodServer -if [ "$packageFile" = "" ]; then - echo "Downloading VpnHoodServer..."; - packageFile="VpnHoodServer-linux.tar.gz"; - wget -O $packageFile $packageUrl; -fi - -# extract -echo "Extracting to $destinationPath"; -mkdir -p $destinationPath; -tar -xzvf "$packageFile" -C /opt/VpnHoodServer - -# override publish info -if [ "$versionTag" != ""]; then - infoDir="/opt/VpnHoodServer/$versionTag/publish_info"; - cp "$infoDir/vhserver" "/opt/VpnHoodServer/" -f; - cp "$infoDir/publish.json" "/opt/VpnHoodServer/" -f; - chmod +x "$destinationPath/vhserver" -fi - -# init service -if [ "$autostart" = "y" ]; then - echo "creating autostart service. Name: VpnHoodService..."; - service=" -[Unit] -Description=VpnHood Server -After=network.target - -[Service] -Type=simple -ExecStart="$destinationPath/vhserver" -ExecStop="$destinationPath/vhserver" stop -TimeoutStartSec=0 -Restart=always -RestartSec=10 - -[Install] -WantedBy=default.target -"; - - echo "$service" > "/etc/systemd/system/VpnHoodServer.service"; - - # run service - echo "run VpnHoodServer service..."; - systemctl daemon-reload; - systemctl enable VpnHoodServer.service; - systemctl restart VpnHoodServer.service; -fi - -# Write AppSettingss -if [ "$restBaseUrl" != "" ]; then -appSettings="{ - \"HttpAccessServer\": { - \"BaseUrl\": \"$restBaseUrl\", - \"Authorization\": \"$restAuthorization\" - } -} -"; -echo "$appSettings" > "$destinationPath/appsettings.json" -fi - diff --git a/VpnHood.Server.App.Net/_publish.ps1 b/VpnHood.Server.App.Net/_publish.ps1 index 0f9b33c47..2f43e28b5 100644 --- a/VpnHood.Server.App.Net/_publish.ps1 +++ b/VpnHood.Server.App.Net/_publish.ps1 @@ -12,51 +12,70 @@ $projectDir = $PSScriptRoot; $projectFile = (Get-ChildItem -path $projectDir -file -Filter "*.csproj").FullName; # Creating linux package +$templateDir = "$PSScriptRoot/Install/Linux"; +$template_installScriptFile = "$templateDir/install.sh"; +$template_launcherFile = "$templateDir/vhserver.sh"; +$template_updaterFile = "$templateDir/updater.sh"; + $publishDir = "$projectDir/bin/release/publish-linux"; -$packageFileName = "VpnHoodServer-linux.tar.gz"; -$installScriptFileName = "VpnHoodServer-linux.sh"; -$publishInfoPackageFileName = "VpnHoodServer-linux.json"; -$releaseInfoDir = "$publishDir/$versionTag/publish_info"; -$publishInfoFileName = "publish.json"; -$launcherFileName = "vhserver"; +$publish_infoDir = "$publishDir/$versionTag/publish_info"; +$publish_updaterFile = "$publish_infoDir/update"; +$publish_launcherFile = "$publish_infoDir/vhserver"; +$publish_InfoFile = "$publish_infoDir/publish.json"; + +$module_InfoFile = "$moduleDir/VpnHoodServer-linux.json"; +$module_InstallerFile = "$moduleDir/VpnHoodServer-linux.sh"; +$module_PackageFile = "$moduleDir/VpnHoodServer-linux.tar.gz"; + +# Calcualted Path +$module_InfoFileName = $(Split-Path "$module_InfoFile" -leaf); +$module_PackageFileName = $(Split-Path "$module_PackageFile" -leaf); +$module_InstallerFileName = $(Split-Path "$module_InstallerFile" -leaf); + +# prepare publish folder +try { Remove-Item -path "$publishDir" -Force -Recurse } catch {} +New-Item -ItemType Directory -Path $publish_infoDir -Force | Out-Null; # publish echo "Build Linux Server..."; if (-not $noclean) { dotnet clean "$projectDir" -c "Release" --output $publishDir | Out-Null } -try { Remove-Item -path "$publishDir" -Force -Recurse } catch {} -dotnet publish "$projectDir" -c "Release" --output "$publishDir/$versionTag" --framework "net7.0" --self-contained --runtime "linux-x64" /p:Version=$versionParam +dotnet publish "$projectDir" -c "Release" --output "$publishDir/$versionTag" --framework "net7.0" --self-contained --runtime "win-x64" /p:Version=$versionParam if ($LASTEXITCODE -gt 0) { Throw "The publish exited with error code: " + $lastexitcode; } -New-Item -ItemType Directory -Path $releaseInfoDir -Force | Out-Null; -# server install-linux.sh +# create installation script echo "Creating Linux Server installation script..."; -$linuxScript = Get-Content -Path "$PSScriptRoot/Install/$installScriptFileName" -Raw; -$linuxScript = $linuxScript.Replace('$packageUrlParam', "https://github.com/vpnhood/VpnHood/releases/download/$versionTag/$packageFileName"); +$linuxScript = Get-Content -Path "$template_installScriptFile" -Raw; +$linuxScript = $linuxScript.Replace('$packageUrlParam', "https://github.com/vpnhood/VpnHood/releases/download/$versionTag/$module_packageFileName"); $linuxScript = $linuxScript.Replace('$versionTagParam', "$versionTag"); $linuxScript = $linuxScript -replace "`r`n", "`n"; -$linuxScript | Out-File -FilePath "$moduleDir/$installScriptFileName" -Encoding ASCII -Force -NoNewline; +$linuxScript | Out-File -FilePath "$module_InstallerFile" -Encoding ASCII -Force -NoNewline; # launcher script echo "Creating Linux Server launcher script..."; $exeFile = "$versionTag/VpnHoodServer"; -$linuxScript = (Get-Content -Path "$PSScriptRoot/Install/VpnHoodServer-linux.run.sh" -Raw).Replace('{exeFileParam}', $exeFile); +$linuxScript = (Get-Content -Path "$template_launcherFile" -Raw).Replace('{exeFileParam}', $exeFile); $linuxScript = $linuxScript -replace "`r`n", "`n"; -$linuxScript | Out-File -FilePath "$releaseInfoDir/$launcherFileName" -Encoding ASCII -Force -NoNewline; +$linuxScript | Out-File -FilePath "$publish_launcherFile" -Encoding ASCII -Force -NoNewline; + +# updater script +echo "Creating Linux Server updater script..."; +Copy-Item -path "$template_updaterFile" -Destination "$publish_updaterFile" -Force -Recurse # publish info $json = @{ Version=$versionParam; ExeFile=$exeFile; - UpdateInfoUrl="https://github.com/vpnhood/VpnHood/releases/latest/download/$publishInfoPackageFileName"; - UpdateScriptUrl="https://github.com/vpnhood/VpnHood/releases/latest/download/$installScriptFileName"; + UpdateInfoUrl="https://github.com/vpnhood/VpnHood/releases/latest/download/$module_InfoFileName"; + InstallScriptUrl="https://github.com/vpnhood/VpnHood/releases/download/$versionTag/$module_InstallerFileName"; + UpdateCode="5EE5047D-6E67-43D4-A90D-665813CA1E7F" }; -$json | ConvertTo-Json | Out-File "$releaseInfoDir/$publishInfoFileName" -Encoding utf8; -$json | ConvertTo-Json | Out-File "$moduleDir/$publishInfoPackageFileName" -Encoding utf8; +$json | ConvertTo-Json | Out-File "$publish_InfoFile" -Encoding utf8; +$json | ConvertTo-Json | Out-File "$module_InfoFile" -Encoding utf8; # zip echo "Compressing Linux Server package..."; -tar -czf $moduleDir/$packageFileName -C "$publishDir\" *; +tar -czf "$module_PackageFile" -C "$publishDir\" *; if ($isLatest) { diff --git a/VpnHood.sln b/VpnHood.sln index b215cb368..1cbb0b456 100644 --- a/VpnHood.sln +++ b/VpnHood.sln @@ -54,6 +54,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VpnHood.Samples.SimpleClien EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VpnHood.Samples.SimpleClient.Win", "Samples\VpnHood.Samples.SimpleClient.Win\VpnHood.Samples.SimpleClient.Win.csproj", "{1247900C-916A-426B-8733-D43B17FAA694}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VpnHood.App.Updater", "VpnHood.App.Updater\VpnHood.App.Updater.csproj", "{4175AF71-61EA-461D-A1E3-575D777CD7D2}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -132,6 +134,10 @@ Global {1247900C-916A-426B-8733-D43B17FAA694}.Debug|Any CPU.Build.0 = Debug|Any CPU {1247900C-916A-426B-8733-D43B17FAA694}.Release|Any CPU.ActiveCfg = Release|Any CPU {1247900C-916A-426B-8733-D43B17FAA694}.Release|Any CPU.Build.0 = Release|Any CPU + {4175AF71-61EA-461D-A1E3-575D777CD7D2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4175AF71-61EA-461D-A1E3-575D777CD7D2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4175AF71-61EA-461D-A1E3-575D777CD7D2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4175AF71-61EA-461D-A1E3-575D777CD7D2}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -145,6 +151,7 @@ Global {611ABBE6-E2FB-43E2-BCC2-FA5BC3E98EE3} = {B3EAE8F1-8ECF-4FC9-9941-28F65A2E7F3E} {DE35DB53-EABB-489D-9A4F-6B22D31E4A95} = {5194E054-10CD-4832-B733-33B02A08E4B6} {1247900C-916A-426B-8733-D43B17FAA694} = {5194E054-10CD-4832-B733-33B02A08E4B6} + {4175AF71-61EA-461D-A1E3-575D777CD7D2} = {B3EAE8F1-8ECF-4FC9-9941-28F65A2E7F3E} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {19BAEB35-FD38-4E41-9963-46313AF87C41}