Skip to content

Force Proxy to any Windows/MacOS/Linux Application TCP and UDP traffic

added update check on load

Sourav Kalal8f823bf085c4
diff --git a/gui/Services/SettingsService.cs b/gui/Services/SettingsService.cs
new file mode 100644
index 0000000..6dc21f1
--- /dev/null
+++ b/gui/Services/SettingsService.cs
@@ -0,0 +1,64 @@
+using System;
+using System.IO;
+using System.Text.Json;
+using System.Text.Json.Serialization;
+
+namespace ProxyBridge.GUI.Services;
+
+[JsonSourceGenerationOptions(WriteIndented = true)]
+[JsonSerializable(typeof(AppSettings))]
+internal partial class AppSettingsContext : JsonSerializerContext
+{
+}
+
+public class SettingsService
+{
+ private static readonly string SettingsPath = Path.Combine(
+ Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
+ "ProxyBridge",
+ "settings.json");
+
+ public AppSettings LoadSettings()
+ {
+ try
+ {
+ if (File.Exists(SettingsPath))
+ {
+ var json = File.ReadAllText(SettingsPath);
+ var settings = JsonSerializer.Deserialize(json, AppSettingsContext.Default.AppSettings);
+ return settings ?? new AppSettings();
+ }
+ }
+ catch
+ {
+ // If there's any error loading settings, return defaults
+ }
+
+ return new AppSettings();
+ }
+
+ public void SaveSettings(AppSettings settings)
+ {
+ try
+ {
+ var directory = Path.GetDirectoryName(SettingsPath);
+ if (directory != null && !Directory.Exists(directory))
+ {
+ Directory.CreateDirectory(directory);
+ }
+
+ var json = JsonSerializer.Serialize(settings, AppSettingsContext.Default.AppSettings);
+ File.WriteAllText(SettingsPath, json);
+ }
+ catch
+ {
+ // silently fail
+ }
+ }
+}
+
+public class AppSettings
+{
+ public bool CheckForUpdatesOnStartup { get; set; } = true;
+ public DateTime LastUpdateCheck { get; set; } = DateTime.MinValue;
+}
\ No newline at end of file
diff --git a/gui/ViewModels/MainWindowViewModel.cs b/gui/ViewModels/MainWindowViewModel.cs
index 4a5b86b..2eb1b63 100644
--- a/gui/ViewModels/MainWindowViewModel.cs
+++ b/gui/ViewModels/MainWindowViewModel.cs
@@ -4,6 +4,7 @@ using System.Collections.ObjectModel;
using System.Windows.Input;
using System.Linq;
using System.Text;
+using System.Threading.Tasks;
using Avalonia.Threading;
using Avalonia.Controls;
using ProxyBridge.GUI.Views;
@@ -108,6 +109,8 @@ public class MainWindowViewModel : ViewModelBase
{
ActivityLog += $"[{DateTime.Now:HH:mm:ss}] ERROR: {ex.Message}\n";
}
+
+ _ = CheckForUpdatesOnStartupAsync();
}
public string Title
@@ -458,6 +461,33 @@ public class MainWindowViewModel : ViewModelBase
IsProxySettingsDialogOpen = false;
}
+ private async Task CheckForUpdatesOnStartupAsync()
+ {
+ try
+ {
+ var settingsService = new SettingsService();
+ var settings = settingsService.LoadSettings();
+ if (!settings.CheckForUpdatesOnStartup)
+ return;
+
+ var updateService = new UpdateService();
+ var versionInfo = await updateService.CheckForUpdatesAsync();
+
+ if (versionInfo.IsUpdateAvailable && _mainWindow != null)
+ {
+ var notificationWindow = new UpdateNotificationWindow();
+ var viewModel = new UpdateNotificationViewModel(() => notificationWindow.Close(), versionInfo);
+ notificationWindow.DataContext = viewModel;
+
+ _ = notificationWindow.ShowDialog(_mainWindow);
+ }
+ }
+ catch
+ {
+ // silently fail
+ }
+ }
+
public void Cleanup()
{
_proxyService?.Dispose();
diff --git a/gui/ViewModels/UpdateNotificationViewModel.cs b/gui/ViewModels/UpdateNotificationViewModel.cs
new file mode 100644
index 0000000..fda8f5a
--- /dev/null
+++ b/gui/ViewModels/UpdateNotificationViewModel.cs
@@ -0,0 +1,172 @@
+using System;
+using System.Windows.Input;
+using System.Threading.Tasks;
+using System.Diagnostics;
+using System.IO;
+using System.Net.Http;
+using ProxyBridge.GUI.Services;
+
+namespace ProxyBridge.GUI.ViewModels;
+
+public class UpdateNotificationViewModel : ViewModelBase
+{
+ private readonly UpdateService _updateService;
+ private readonly SettingsService _settingsService;
+ private readonly Action _onClose;
+ private readonly VersionInfo _versionInfo;
+
+ private string _currentVersion = "";
+ private string _latestVersion = "";
+ private string _downloadStatus = "";
+ private string _errorMessage = "";
+ private double _downloadProgress = 0;
+ private bool _isDownloading = false;
+ private bool _hasError = false;
+
+ public UpdateNotificationViewModel() : this(() => { }, new VersionInfo())
+ {
+ }
+
+ public UpdateNotificationViewModel(Action onClose, VersionInfo versionInfo)
+ {
+ _updateService = new UpdateService();
+ _settingsService = new SettingsService();
+ _onClose = onClose;
+ _versionInfo = versionInfo;
+
+ CurrentVersion = versionInfo.CurrentVersionString;
+ LatestVersion = versionInfo.LatestVersionString;
+
+ UpdateNowCommand = new RelayCommand(async () => await DownloadAndInstallAsync());
+ DontAskAgainCommand = new RelayCommand(DontAskAgain);
+ LaterCommand = new RelayCommand(_onClose);
+ }
+
+ public string CurrentVersion
+ {
+ get => _currentVersion;
+ set => SetProperty(ref _currentVersion, value);
+ }
+
+ public string LatestVersion
+ {
+ get => _latestVersion;
+ set => SetProperty(ref _latestVersion, value);
+ }
+
+ public string DownloadStatus
+ {
+ get => _downloadStatus;
+ set => SetProperty(ref _downloadStatus, value);
+ }
+
+ public string ErrorMessage
+ {
+ get => _errorMessage;
+ set => SetProperty(ref _errorMessage, value);
+ }
+
+ public double DownloadProgress
+ {
+ get => _downloadProgress;
+ set => SetProperty(ref _downloadProgress, value);
+ }
+
+ public bool IsDownloading
+ {
+ get => _isDownloading;
+ set => SetProperty(ref _isDownloading, value);
+ }
+
+ public bool HasError
+ {
+ get => _hasError;
+ set => SetProperty(ref _hasError, value);
+ }
+
+ public ICommand UpdateNowCommand { get; }
+ public ICommand DontAskAgainCommand { get; }
+ public ICommand LaterCommand { get; }
+
+ private async Task DownloadAndInstallAsync()
+ {
+ IsDownloading = true;
+ HasError = false;
+ ErrorMessage = "";
+ DownloadProgress = 0;
+ DownloadStatus = "Starting download...";
+
+ try
+ {
+ if (string.IsNullOrEmpty(_versionInfo.DownloadUrl))
+ {
+ throw new Exception("Could not find download URL for the latest version");
+ }
+
+ var tempPath = Path.GetTempPath();
+ var fileName = _versionInfo.SetupFileName ?? "ProxyBridge-Setup.exe";
+ var filePath = Path.Combine(tempPath, fileName);
+
+ DownloadStatus = "Downloading update...";
+
+ using var httpClient = new HttpClient();
+ using var response = await httpClient.GetAsync(_versionInfo.DownloadUrl, HttpCompletionOption.ResponseHeadersRead);
+ response.EnsureSuccessStatusCode();
+
+ var totalBytes = response.Content.Headers.ContentLength ?? -1L;
+ using var contentStream = await response.Content.ReadAsStreamAsync();
+ using var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None, 8192, true);
+
+ var buffer = new byte[8192];
+ long totalBytesRead = 0;
+ int bytesRead;
+
+ while ((bytesRead = await contentStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
+ {
+ await fileStream.WriteAsync(buffer, 0, bytesRead);
+ totalBytesRead += bytesRead;
+
+ if (totalBytes > 0)
+ {
+ DownloadProgress = (double)totalBytesRead / totalBytes * 100;
+ DownloadStatus = $"Downloaded {totalBytesRead / 1024 / 1024:F1} MB of {totalBytes / 1024 / 1024:F1} MB";
+ }
+ else
+ {
+ DownloadStatus = $"Downloaded {totalBytesRead / 1024 / 1024:F1} MB";
+ }
+ }
+
+ DownloadStatus = "Download complete. Starting installation...";
+ DownloadProgress = 100;
+
+ // Start the installer and exit the current application
+ Process.Start(new ProcessStartInfo
+ {
+ FileName = filePath,
+ UseShellExecute = true
+ });
+
+ // Close the current application
+ Environment.Exit(0);
+ }
+ catch (Exception ex)
+ {
+ HasError = true;
+ ErrorMessage = $"Error downloading update: {ex.Message}";
+ DownloadStatus = "Download failed";
+ }
+ finally
+ {
+ IsDownloading = false;
+ }
+ }
+
+ private void DontAskAgain()
+ {
+ var settings = _settingsService.LoadSettings();
+ settings.CheckForUpdatesOnStartup = false;
+ _settingsService.SaveSettings(settings);
+ _onClose();
+ }
+}
\ No newline at end of file
diff --git a/gui/Views/UpdateNotificationWindow.axaml b/gui/Views/UpdateNotificationWindow.axaml
new file mode 100644
index 0000000..5515ea1
--- /dev/null
+++ b/gui/Views/UpdateNotificationWindow.axaml
@@ -0,0 +1,106 @@
+<Window xmlns="https://github.com/avaloniaui"
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+ xmlns:vm="using:ProxyBridge.GUI.ViewModels"
+ x:Class="ProxyBridge.GUI.Views.UpdateNotificationWindow"
+ x:DataType="vm:UpdateNotificationViewModel"
+ x:CompileBindings="True"
+ Title="Update Available"
+ Icon="/Assets/logo.ico"
+ Width="450" Height="250"
+ CanResize="False"
+ WindowStartupLocation="CenterOwner"
+ Background="#FF1E1E1E">
+
+ <Design.DataContext>
+ <vm:UpdateNotificationViewModel/>
+ </Design.DataContext>
+
+ <Grid RowDefinitions="Auto,*,Auto">
+ <!-- Header -->
+ <Border Grid.Row="0" Background="#FF252526" BorderBrush="#FF007ACC" BorderThickness="0,0,0,1" Padding="16,12">
+ <StackPanel Orientation="Horizontal" Spacing="10">
+ <TextBlock Text="Update Available"
+ FontSize="16"
+ FontWeight="SemiBold"
+ VerticalAlignment="Center"/>
+ </StackPanel>
+ </Border>
+
+ <!-- Content -->
+ <StackPanel Grid.Row="1" Margin="32,24" Spacing="16" VerticalAlignment="Center">
+ <!-- Version Info -->
+ <StackPanel Spacing="12">
+ <StackPanel Orientation="Horizontal" Spacing="8">
+ <TextBlock Text="Current Version:"
+ FontWeight="Medium"
+ Foreground="#FFB0B0B0"
+ Width="120"/>
+ <TextBlock Text="{Binding CurrentVersion}"
+ Foreground="#FF007ACC"/>
+ </StackPanel>
+
+ <StackPanel Orientation="Horizontal" Spacing="8">
+ <TextBlock Text="New Version:"
+ FontWeight="Medium"
+ Foreground="#FFB0B0B0"
+ Width="120"/>
+ <TextBlock Text="{Binding LatestVersion}"
+ Foreground="#FF4CAF50"/>
+ </StackPanel>
+ </StackPanel>
+
+ <!-- Update Message -->
+ <TextBlock Text="A new version of ProxyBridge is available. Would you like to download and install it now?"
+ TextWrapping="Wrap"
+ HorizontalAlignment="Center"
+ Foreground="#FFB0B0B0"
+ Margin="0,8,0,8"/>
+
+ <!-- Download Progress -->
+ <StackPanel IsVisible="{Binding IsDownloading}" Spacing="8">
+ <TextBlock Text="{Binding DownloadStatus}"
+ HorizontalAlignment="Center"
+ Foreground="#FF007ACC"/>
+ <ProgressBar Value="{Binding DownloadProgress}"
+ Maximum="100"
+ Height="8"
+ Background="#FF3C3C3C"
+ Foreground="#FF007ACC"/>
+ </StackPanel>
+
+ <!-- Error Message -->
+ <TextBlock Text="{Binding ErrorMessage}"
+ Foreground="#FFFF6B6B"
+ HorizontalAlignment="Center"
+ TextWrapping="Wrap"
+ IsVisible="{Binding HasError}"/>
+ </StackPanel>
+
+ <!-- Buttons -->
+ <Border Grid.Row="2" Background="#FF252526" BorderBrush="#FF007ACC" BorderThickness="0,1,0,0" Padding="16,12">
+ <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Spacing="10">
+ <Button Content="Update Now"
+ Command="{Binding UpdateNowCommand}"
+ IsEnabled="{Binding !IsDownloading}"
+ Background="#FF4CAF50"
+ Foreground="White"
+ Padding="16,6"
+ CornerRadius="3"/>
+ <Button Content="Don't Ask Again"
+ Command="{Binding DontAskAgainCommand}"
+ IsEnabled="{Binding !IsDownloading}"
+ Background="#FFFF6B6B"
+ Foreground="White"
+ Padding="16,6"
+ CornerRadius="3"/>
+ <Button Content="Later"
+ Command="{Binding LaterCommand}"
+ IsEnabled="{Binding !IsDownloading}"
+ Background="#FF3C3C3C"
+ Foreground="White"
+ Padding="16,6"
+ CornerRadius="3"/>
+ </StackPanel>
+ </Border>
+ </Grid>
+</Window>
\ No newline at end of file
diff --git a/gui/Views/UpdateNotificationWindow.axaml.cs b/gui/Views/UpdateNotificationWindow.axaml.cs
new file mode 100644
index 0000000..f52ae1d
--- /dev/null
+++ b/gui/Views/UpdateNotificationWindow.axaml.cs
@@ -0,0 +1,11 @@
+using Avalonia.Controls;
+
+namespace ProxyBridge.GUI.Views;
+
+public partial class UpdateNotificationWindow : Window
+{
+ public UpdateNotificationWindow()
+ {
+ InitializeComponent();
+ }
+}
\ No newline at end of file