Skip to content

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

WIP - Added Proxy Test

Sourav Kalal3476a846a81e
diff --git a/gui/Interop/ProxyBridgeNative.cs b/gui/Interop/ProxyBridgeNative.cs
index 1b7f361..174bf32 100644
--- a/gui/Interop/ProxyBridgeNative.cs
+++ b/gui/Interop/ProxyBridgeNative.cs
@@ -91,4 +91,11 @@ public static class ProxyBridgeNative
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ProxyBridge_Stop();
+
+ [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
+ public static extern int ProxyBridge_TestConnection(
+ [MarshalAs(UnmanagedType.LPStr)] string targetHost,
+ ushort targetPort,
+ [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder resultBuffer,
+ UIntPtr bufferSize);
}
diff --git a/gui/Services/ProxyBridgeService.cs b/gui/Services/ProxyBridgeService.cs
index 6f3dcaa..47cfe60 100644
--- a/gui/Services/ProxyBridgeService.cs
+++ b/gui/Services/ProxyBridgeService.cs
@@ -88,6 +88,18 @@ public class ProxyBridgeService : IDisposable
return ProxyBridgeNative.ProxyBridge_DisableRule(ruleId);
}
+ public string TestConnection(string targetHost, ushort targetPort)
+ {
+ var buffer = new System.Text.StringBuilder(4096);
+ int result = ProxyBridgeNative.ProxyBridge_TestConnection(
+ targetHost,
+ targetPort,
+ buffer,
+ (UIntPtr)buffer.Capacity);
+
+ return buffer.ToString();
+ }
+
public void Dispose()
{
Stop();
diff --git a/gui/ViewModels/MainWindowViewModel.cs b/gui/ViewModels/MainWindowViewModel.cs
index d77c387..9cd7726 100644
--- a/gui/ViewModels/MainWindowViewModel.cs
+++ b/gui/ViewModels/MainWindowViewModel.cs
@@ -217,7 +217,8 @@ public class MainWindowViewModel : ViewModelBase
onClose: () =>
{
window.Close();
- }
+ },
+ proxyService: _proxyService
);
window.DataContext = viewModel;
diff --git a/gui/ViewModels/ProxySettingsViewModel.cs b/gui/ViewModels/ProxySettingsViewModel.cs
index 027d291..762082d 100644
--- a/gui/ViewModels/ProxySettingsViewModel.cs
+++ b/gui/ViewModels/ProxySettingsViewModel.cs
@@ -14,8 +14,14 @@ public class ProxySettingsViewModel : ViewModelBase
private string _proxyPassword = "";
private string _ipError = "";
private string _portError = "";
+ private bool _isTestViewOpen = false;
+ private string _testTargetHost = "google.com";
+ private string _testTargetPort = "80";
+ private string _testOutput = "";
+ private bool _isTesting = false;
private Action<string, string, string, string, string>? _onSave;
private Action? _onClose;
+ private Services.ProxyBridgeService? _proxyService;
public string ProxyIp
{
@@ -67,8 +73,41 @@ public class ProxySettingsViewModel : ViewModelBase
set => SetProperty(ref _portError, value);
}
+ public bool IsTestViewOpen
+ {
+ get => _isTestViewOpen;
+ set => SetProperty(ref _isTestViewOpen, value);
+ }
+
+ public string TestTargetHost
+ {
+ get => _testTargetHost;
+ set => SetProperty(ref _testTargetHost, value);
+ }
+
+ public string TestTargetPort
+ {
+ get => _testTargetPort;
+ set => SetProperty(ref _testTargetPort, value);
+ }
+
+ public string TestOutput
+ {
+ get => _testOutput;
+ set => SetProperty(ref _testOutput, value);
+ }
+
+ public bool IsTesting
+ {
+ get => _isTesting;
+ set => SetProperty(ref _isTesting, value);
+ }
+
public ICommand SaveCommand { get; }
public ICommand CancelCommand { get; }
+ public ICommand OpenTestCommand { get; }
+ public ICommand CloseTestCommand { get; }
+ public ICommand StartTestCommand { get; }
private bool IsValidIpOrDomain(string input)
{
@@ -81,10 +120,11 @@ public class ProxySettingsViewModel : ViewModelBase
return domainRegex.IsMatch(input);
}
- public ProxySettingsViewModel(string initialType, string initialIp, string initialPort, string initialUsername, string initialPassword, Action<string, string, string, string, string> onSave, Action onClose)
+ public ProxySettingsViewModel(string initialType, string initialIp, string initialPort, string initialUsername, string initialPassword, Action<string, string, string, string, string> onSave, Action onClose, Services.ProxyBridgeService? proxyService)
{
_onSave = onSave;
_onClose = onClose;
+ _proxyService = proxyService;
ProxyType = initialType;
ProxyIp = initialIp;
@@ -128,5 +168,75 @@ public class ProxySettingsViewModel : ViewModelBase
{
_onClose?.Invoke();
});
+
+ OpenTestCommand = new RelayCommand(() =>
+ {
+ IsTestViewOpen = true;
+ TestOutput = "";
+ });
+
+ CloseTestCommand = new RelayCommand(() =>
+ {
+ IsTestViewOpen = false;
+ });
+
+ StartTestCommand = new RelayCommand(async () =>
+ {
+ if (IsTesting) return;
+
+ // Validate inputs first
+ if (string.IsNullOrWhiteSpace(ProxyIp))
+ {
+ TestOutput = "ERROR: Please configure proxy IP address first";
+ return;
+ }
+
+ if (string.IsNullOrWhiteSpace(ProxyPort) || !ushort.TryParse(ProxyPort, out ushort proxyPortNum))
+ {
+ TestOutput = "ERROR: Please configure valid proxy port first";
+ return;
+ }
+
+ if (string.IsNullOrWhiteSpace(TestTargetHost))
+ {
+ TestOutput = "ERROR: Please enter target host";
+ return;
+ }
+
+ if (!ushort.TryParse(TestTargetPort, out ushort targetPortNum))
+ {
+ TestOutput = "ERROR: Invalid target port";
+ return;
+ }
+
+ IsTesting = true;
+ TestOutput = "Testing connection...\n";
+
+ try
+ { // First apply current settings temporarily
+ if (_proxyService != null)
+ {
+ _proxyService.SetProxyConfig(ProxyType, ProxyIp, proxyPortNum, ProxyUsername ?? "", ProxyPassword ?? "");
+
+ await System.Threading.Tasks.Task.Run(() =>
+ {
+ var result = _proxyService.TestConnection(TestTargetHost, targetPortNum);
+ TestOutput = result;
+ });
+ }
+ else
+ {
+ TestOutput = "ERROR: Proxy service not available";
+ }
+ }
+ catch (Exception ex)
+ {
+ TestOutput += $"\nERROR: {ex.Message}";
+ }
+ finally
+ {
+ IsTesting = false;
+ }
+ });
}
}
diff --git a/gui/Views/ProxySettingsWindow.axaml b/gui/Views/ProxySettingsWindow.axaml
index 4b3bd27..c31679c 100644
--- a/gui/Views/ProxySettingsWindow.axaml
+++ b/gui/Views/ProxySettingsWindow.axaml
@@ -3,14 +3,15 @@
xmlns:vm="using:ProxyBridge.GUI.ViewModels"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d" d:DesignWidth="500" d:DesignHeight="550"
+ mc:Ignorable="d" d:DesignWidth="500" d:DesignHeight="650"
x:Class="ProxyBridge.GUI.Views.ProxySettingsWindow"
x:DataType="vm:ProxySettingsViewModel"
x:CompileBindings="True"
Title="Proxy Settings"
Icon="/Assets/logo.ico"
- Width="500" Height="550"
- CanResize="False"
+ Width="500" Height="650"
+ MinHeight="550"
+ CanResize="True"
Background="#FF2D2D30"
WindowStartupLocation="CenterOwner">
@@ -83,8 +84,10 @@
</StackPanel>
</Border>
- <!-- Settings Form -->
- <StackPanel Margin="24,20" Spacing="20">
+ <!-- Settings Form with ScrollViewer -->
+ <ScrollViewer VerticalScrollBarVisibility="Auto"
+ HorizontalScrollBarVisibility="Disabled">
+ <StackPanel Margin="24,20" Spacing="20">
<StackPanel Spacing="6">
<TextBlock Text="Proxy Type" Classes="label"/>
<ComboBox HorizontalAlignment="Stretch"
@@ -130,10 +133,86 @@
FontSize="12"
Margin="0,8,0,0"/>
+ <!-- Test Connection Section -->
+ <Border BorderBrush="#FF3F3F46"
+ BorderThickness="0,1,0,0"
+ Padding="0,16,0,0"
+ Margin="0,16,0,0"
+ IsVisible="{Binding !IsTestViewOpen}">
+ <StackPanel Spacing="12">
+ <TextBlock Text="Test Connection"
+ FontSize="14"
+ FontWeight="SemiBold"
+ Foreground="#FFE0E0E0"/>
+ <Button Content="🧪 Test Proxy Connection"
+ Command="{Binding OpenTestCommand}"
+ Classes="secondary"
+ HorizontalAlignment="Left"/>
+ </StackPanel>
+ </Border>
+
+ <!-- Test Connection Form -->
+ <Border BorderBrush="#FF3F3F46"
+ BorderThickness="0,1,0,0"
+ Padding="0,16,0,0"
+ Margin="0,16,0,0"
+ IsVisible="{Binding IsTestViewOpen}">
+ <StackPanel Spacing="12">
+ <Grid ColumnDefinitions="*,Auto">
+ <TextBlock Text="Test Connection"
+ FontSize="14"
+ FontWeight="SemiBold"
+ Foreground="#FFE0E0E0"
+ VerticalAlignment="Center"/>
+ <Button Grid.Column="1"
+ Content="✕"
+ Classes="secondary"
+ Command="{Binding CloseTestCommand}"
+ Width="32"
+ Height="32"
+ Padding="0"/>
+ </Grid>
+
+ <Grid ColumnDefinitions="3*,2*">
+ <StackPanel Grid.Column="0" Spacing="6" Margin="0,0,6,0">
+ <TextBlock Text="Target Host" Classes="label"/>
+ <TextBox Text="{Binding TestTargetHost}"/>
+ </StackPanel>
+ <StackPanel Grid.Column="1" Spacing="6" Margin="6,0,0,0">
+ <TextBlock Text="Port" Classes="label"/>
+ <TextBox Text="{Binding TestTargetPort}"/>
+ </StackPanel>
+ </Grid>
+
+ <Button Content="▶ Start Test"
+ Command="{Binding StartTestCommand}"
+ IsEnabled="{Binding !IsTesting}"
+ HorizontalAlignment="Left"/>
+
+ <StackPanel Spacing="6">
+ <TextBlock Text="Output" Classes="label"/>
+ <Border Background="#FF1E1E1E"
+ BorderBrush="#FF3F3F46"
+ BorderThickness="1"
+ Padding="10"
+ MinHeight="150"
+ MaxHeight="200">
+ <ScrollViewer VerticalScrollBarVisibility="Auto">
+ <TextBlock Text="{Binding TestOutput}"
+ FontFamily="Consolas,monospace"
+ FontSize="12"
+ Foreground="#FFE0E0E0"
+ TextWrapping="Wrap"/>
+ </ScrollViewer>
+ </Border>
+ </StackPanel>
+ </StackPanel>
+ </Border>
+
<StackPanel Orientation="Horizontal"
HorizontalAlignment="Right"
Spacing="12"
- Margin="0,16,0,0">
+ Margin="0,16,0,20">
<Button Content="Cancel"
Classes="secondary"
Command="{Binding CancelCommand}"
@@ -142,6 +221,7 @@
Command="{Binding SaveCommand}"
Width="130"/>
</StackPanel>
- </StackPanel>
+ </StackPanel>
+ </ScrollViewer>
</DockPanel>
</Window>
diff --git a/src/ProxyBridge.c b/src/ProxyBridge.c
index 1e486a7..f0ca9ab 100644
--- a/src/ProxyBridge.c
+++ b/src/ProxyBridge.c
@@ -1471,6 +1471,198 @@ PROXYBRIDGE_API BOOL ProxyBridge_Stop(void)
return TRUE;
}
+
+PROXYBRIDGE_API int ProxyBridge_TestConnection(const char* target_host, UINT16 target_port, char* result_buffer, size_t buffer_size)
+{
+ WSADATA wsa;
+ SOCKET test_sock = INVALID_SOCKET;
+ struct sockaddr_in proxy_addr;
+ struct hostent* host_info;
+ UINT32 target_ip;
+ int ret = -1;
+ char temp_buffer[512];
+
+ if (g_proxy_ip[0] == '\0' || g_proxy_port == 0)
+ {
+ snprintf(result_buffer, buffer_size, "ERROR: No proxy configured");
+ return -1;
+ }
+
+ if (target_host == NULL || target_host[0] == '\0')
+ {
+ snprintf(result_buffer, buffer_size, "ERROR: Invalid target host");
+ return -1;
+ }
+
+ // Initialize Winsock
+ if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
+ {
+ snprintf(result_buffer, buffer_size, "ERROR: WSAStartup failed (%d)", WSAGetLastError());
+ return -1;
+ }
+
+ snprintf(temp_buffer, sizeof(temp_buffer), "Testing connection to %s:%d via %s proxy %s:%d...\n",
+ target_host, target_port,
+ g_proxy_type == PROXY_TYPE_HTTP ? "HTTP" : "SOCKS5",
+ g_proxy_ip, g_proxy_port);
+ strncpy(result_buffer, temp_buffer, buffer_size - 1);
+
+
+ host_info = gethostbyname(target_host);
+ if (host_info == NULL)
+ {
+ snprintf(temp_buffer, sizeof(temp_buffer), "ERROR: Failed to resolve hostname %s (%d)\n", target_host, WSAGetLastError());
+ strncat(result_buffer, temp_buffer, buffer_size - strlen(result_buffer) - 1);
+ WSACleanup();
+ return -1;
+ }
+ target_ip = *(UINT32*)host_info->h_addr_list[0];
+
+ snprintf(temp_buffer, sizeof(temp_buffer), "Resolved %s to %d.%d.%d.%d\n",
+ target_host,
+ (target_ip >> 0) & 0xFF, (target_ip >> 8) & 0xFF,
+ (target_ip >> 16) & 0xFF, (target_ip >> 24) & 0xFF);
+ strncat(result_buffer, temp_buffer, buffer_size - strlen(result_buffer) - 1);
+
+ // Create socket
+ test_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+ if (test_sock == INVALID_SOCKET)
+ {
+ snprintf(temp_buffer, sizeof(temp_buffer), "ERROR: Socket creation failed (%d)\n", WSAGetLastError());
+ strncat(result_buffer, temp_buffer, buffer_size - strlen(result_buffer) - 1);
+ WSACleanup();
+ return -1;
+ }
+
+ DWORD timeout = 10000;
+ setsockopt(test_sock, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout));
+ setsockopt(test_sock, SOL_SOCKET, SO_SNDTIMEO, (char*)&timeout, sizeof(timeout));
+
+ memset(&proxy_addr, 0, sizeof(proxy_addr));
+ proxy_addr.sin_family = AF_INET;
+ proxy_addr.sin_addr.s_addr = parse_ipv4(g_proxy_ip);
+ proxy_addr.sin_port = htons(g_proxy_port);
+
+ snprintf(temp_buffer, sizeof(temp_buffer), "Connecting to proxy %s:%d...\n", g_proxy_ip, g_proxy_port);
+ strncat(result_buffer, temp_buffer, buffer_size - strlen(result_buffer) - 1);
+
+ if (connect(test_sock, (struct sockaddr*)&proxy_addr, sizeof(proxy_addr)) == SOCKET_ERROR)
+ {
+ snprintf(temp_buffer, sizeof(temp_buffer), "ERROR: Failed to connect to proxy (%d)\n", WSAGetLastError());
+ strncat(result_buffer, temp_buffer, buffer_size - strlen(result_buffer) - 1);
+ closesocket(test_sock);
+ WSACleanup();
+ return -1;
+ }
+
+ strncat(result_buffer, "Connected to proxy server\n", buffer_size - strlen(result_buffer) - 1);
+
+ if (g_proxy_type == PROXY_TYPE_SOCKS5)
+ {
+ if (socks5_connect(test_sock, target_ip, target_port) != 0)
+ {
+ snprintf(temp_buffer, sizeof(temp_buffer), "ERROR: SOCKS5 handshake failed\n");
+ strncat(result_buffer, temp_buffer, buffer_size - strlen(result_buffer) - 1);
+ closesocket(test_sock);
+ WSACleanup();
+ return -1;
+ }
+ strncat(result_buffer, "SOCKS5 handshake successful\n", buffer_size - strlen(result_buffer) - 1);
+ }
+ else
+ {
+ if (http_connect(test_sock, target_ip, target_port) != 0)
+ {
+ snprintf(temp_buffer, sizeof(temp_buffer), "ERROR: HTTP CONNECT failed\n");
+ strncat(result_buffer, temp_buffer, buffer_size - strlen(result_buffer) - 1);
+ closesocket(test_sock);
+ WSACleanup();
+ return -1;
+ }
+ strncat(result_buffer, "HTTP CONNECT successful\n", buffer_size - strlen(result_buffer) - 1);
+ }
+
+
+ char http_request[512];
+ snprintf(http_request, sizeof(http_request),
+ "GET / HTTP/1.1\r\n"
+ "Host: %s\r\n"
+ "Connection: close\r\n"
+ "User-Agent: ProxyBridge/1.0\r\n"
+ "\r\n", target_host);
+
+ if (send(test_sock, http_request, strlen(http_request), 0) == SOCKET_ERROR)
+ {
+ snprintf(temp_buffer, sizeof(temp_buffer), "ERROR: Failed to send test request (%d)\n", WSAGetLastError());
+ strncat(result_buffer, temp_buffer, buffer_size - strlen(result_buffer) - 1);
+ closesocket(test_sock);
+ WSACleanup();
+ return -1;
+ }
+
+ strncat(result_buffer, "Sent HTTP GET request\n", buffer_size - strlen(result_buffer) - 1);
+ char response[1024];
+ int bytes_received = recv(test_sock, response, sizeof(response) - 1, 0);
+ if (bytes_received > 0)
+ {
+ response[bytes_received] = '\0';
+
+ if (strstr(response, "HTTP/") != NULL)
+ {
+ char* status_line = strstr(response, "HTTP/");
+ int status_code = 0;
+ if (status_line != NULL)
+ {
+ sscanf(status_line, "HTTP/%*s %d", &status_code);
+ }
+
+ snprintf(temp_buffer, sizeof(temp_buffer), "SUCCESS: Received HTTP %d response (%d bytes)\n", status_code, bytes_received);
+ strncat(result_buffer, temp_buffer, buffer_size - strlen(result_buffer) - 1);
+ ret = 0; // Success
+ }
+ else
+ {
+ snprintf(temp_buffer, sizeof(temp_buffer), "ERROR: Received data but not valid HTTP response\n");
+ strncat(result_buffer, temp_buffer, buffer_size - strlen(result_buffer) - 1);
+ ret = -1; // Failure - not a valid HTTP response
+ }
+ }
+ else if (bytes_received == 0)
+ {
+ snprintf(temp_buffer, sizeof(temp_buffer), "ERROR: Connection closed by remote host (no data received)\n");
+ strncat(result_buffer, temp_buffer, buffer_size - strlen(result_buffer) - 1);
+ ret = -1; // Failure
+ }
+ else
+ {
+ int error_code = WSAGetLastError();
+ if (error_code == WSAETIMEDOUT)
+ {
+ snprintf(temp_buffer, sizeof(temp_buffer), "ERROR: Connection timeout - no response received\n");
+ }
+ else
+ {
+ snprintf(temp_buffer, sizeof(temp_buffer), "ERROR: Failed to receive response (%d)\n", error_code);
+ }
+ strncat(result_buffer, temp_buffer, buffer_size - strlen(result_buffer) - 1);
+ ret = -1; // Failure
+ }
+
+ closesocket(test_sock);
+ WSACleanup();
+
+ if (ret == 0)
+ {
+ strncat(result_buffer, "\n✓ Proxy connection test PASSED\n", buffer_size - strlen(result_buffer) - 1);
+ }
+ else
+ {
+ strncat(result_buffer, "\n✗ Proxy connection test FAILED\n", buffer_size - strlen(result_buffer) - 1);
+ }
+
+ return ret;
+}
+
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
{
switch (fdwReason)
diff --git a/src/ProxyBridge.h b/src/ProxyBridge.h
index 1e7f17a..6271dad 100644
--- a/src/ProxyBridge.h
+++ b/src/ProxyBridge.h
@@ -41,6 +41,7 @@ PROXYBRIDGE_API void ProxyBridge_SetLogCallback(LogCallback callback);
PROXYBRIDGE_API void ProxyBridge_SetConnectionCallback(ConnectionCallback callback);
PROXYBRIDGE_API BOOL ProxyBridge_Start(void);
PROXYBRIDGE_API BOOL ProxyBridge_Stop(void);
+PROXYBRIDGE_API int ProxyBridge_TestConnection(const char* target_host, UINT16 target_port, char* result_buffer, size_t buffer_size);
#ifdef __cplusplus
}