Skip to content

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

#55 fixed memory issues - added C# garbage, updated disable logging to remove all garbage from memory, updated C connection track to remove all connection more efficiently

Sourav Kalal47892a7efb2e
diff --git a/Windows/gui/Interop/ProxyBridgeNative.cs b/Windows/gui/Interop/ProxyBridgeNative.cs
index 5953257..4c1c514 100644
--- a/Windows/gui/Interop/ProxyBridgeNative.cs
+++ b/Windows/gui/Interop/ProxyBridgeNative.cs
@@ -98,6 +98,9 @@ public static class ProxyBridgeNative
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern void ProxyBridge_SetConnectionCallback(ConnectionCallback callback);
+ [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
+ public static extern void ProxyBridge_SetTrafficLoggingEnabled([MarshalAs(UnmanagedType.Bool)] bool enable);
+
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern void ProxyBridge_SetDnsViaProxy([MarshalAs(UnmanagedType.Bool)] bool enable);
diff --git a/Windows/gui/Services/ProxyBridgeService.cs b/Windows/gui/Services/ProxyBridgeService.cs
index 7c3532d..6ddd5cc 100644
--- a/Windows/gui/Services/ProxyBridgeService.cs
+++ b/Windows/gui/Services/ProxyBridgeService.cs
@@ -118,6 +118,11 @@ public class ProxyBridgeService : IDisposable
ProxyBridgeNative.ProxyBridge_SetDnsViaProxy(enable);
}
+ public static void SetTrafficLoggingEnabled(bool enable)
+ {
+ ProxyBridgeNative.ProxyBridge_SetTrafficLoggingEnabled(enable);
+ }
+
public string TestConnection(string targetHost, ushort targetPort)
{
var buffer = new System.Text.StringBuilder(4096);
diff --git a/Windows/gui/ViewModels/MainWindowViewModel.cs b/Windows/gui/ViewModels/MainWindowViewModel.cs
index 2b096aa..ec413d0 100644
--- a/Windows/gui/ViewModels/MainWindowViewModel.cs
+++ b/Windows/gui/ViewModels/MainWindowViewModel.cs
@@ -15,6 +15,9 @@ namespace ProxyBridge.GUI.ViewModels;
public class MainWindowViewModel : ViewModelBase
{
+ private const int MAX_CONNECTION_LOG_LINES = 100;
+ private const int MAX_ACTIVITY_LOG_LINES = 100;
+
private string _title = "ProxyBridge";
private int _selectedTabIndex;
private string _connectionsLog = "";
@@ -66,9 +69,11 @@ public class MainWindowViewModel : ViewModelBase
});
};
- // Add to queue instead of updating UI immediately
_proxyService.ConnectionReceived += (processName, pid, destIp, destPort, proxyInfo) =>
{
+ if (!_isTrafficLoggingEnabled)
+ return;
+
string logEntry = $"[{DateTime.Now:HH:mm:ss}] {processName} (PID:{pid}) -> {destIp}:{destPort} via {proxyInfo}\n";
lock (_connectionLogLock)
{
@@ -100,6 +105,17 @@ public class MainWindowViewModel : ViewModelBase
sb.Append(log);
}
ConnectionsLog += sb.ToString();
+
+ var lines = ConnectionsLog.Split('\n');
+ if (lines.Length > MAX_CONNECTION_LOG_LINES)
+ {
+ var oldLog = ConnectionsLog;
+ var linesToKeep = lines.Skip(lines.Length - MAX_CONNECTION_LOG_LINES).ToArray();
+ ConnectionsLog = string.Join("\n", linesToKeep);
+
+ oldLog = null!;
+ GC.Collect(2, GCCollectionMode.Forced, false, false);
+ }
};
if (_isTrafficLoggingEnabled)
{
@@ -196,6 +212,20 @@ public class MainWindowViewModel : ViewModelBase
{
if (SetProperty(ref _activityLog, value))
{
+ if (!string.IsNullOrEmpty(_activityLog))
+ {
+ var lines = _activityLog.Split('\n');
+ if (lines.Length > MAX_ACTIVITY_LOG_LINES)
+ {
+ var oldLog = _activityLog;
+ var linesToKeep = lines.Skip(lines.Length - MAX_ACTIVITY_LOG_LINES).ToArray();
+ _activityLog = string.Join("\n", linesToKeep);
+
+ oldLog = null!;
+ GC.Collect(2, GCCollectionMode.Forced, false, false);
+ }
+ }
+
if (string.IsNullOrWhiteSpace(_activitySearchText))
FilteredActivityLog = _activityLog;
}
@@ -285,6 +315,7 @@ public class MainWindowViewModel : ViewModelBase
{
if (value)
{
+ ProxyBridgeService.SetTrafficLoggingEnabled(true);
_connectionLogTimer?.Start();
}
else
@@ -295,9 +326,19 @@ public class MainWindowViewModel : ViewModelBase
{
_pendingConnectionLogs.Clear();
}
+
+ ProxyBridgeService.SetTrafficLoggingEnabled(false);
+
+ ConnectionsLog = null!;
+ FilteredConnectionsLog = null!;
+ ConnectionsLog = "";
+ FilteredConnectionsLog = "";
+
+ GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true, true);
+ GC.WaitForPendingFinalizers();
+ GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true, true);
}
SaveConfigurationInternal();
- ActivityLog += $"[{DateTime.Now:HH:mm:ss}] Traffic Logging: {(value ? "Enabled" : "Disabled")}\n";
}
}
}
@@ -509,16 +550,41 @@ public class MainWindowViewModel : ViewModelBase
ClearConnectionsLogCommand = new RelayCommand(() =>
{
+ lock (_connectionLogLock)
+ {
+ _pendingConnectionLogs.Clear();
+ }
+
+ ConnectionsLog = null!;
+ ConnectionsSearchText = null!;
+ FilteredConnectionsLog = null!;
+
ConnectionsLog = "";
ConnectionsSearchText = "";
FilteredConnectionsLog = "";
+
+ GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true, true);
+ GC.WaitForPendingFinalizers();
+ GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true, true);
});
ClearActivityLogCommand = new RelayCommand(() =>
{
+ var oldActivity = ActivityLog;
+ var oldSearch = ActivitySearchText;
+ var oldFiltered = FilteredActivityLog;
+
ActivityLog = "ProxyBridge initialized successfully\n";
ActivitySearchText = "";
FilteredActivityLog = "ProxyBridge initialized successfully\n";
+
+ oldActivity = null!;
+ oldSearch = null!;
+ oldFiltered = null!;
+
+ GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true, true);
+ GC.WaitForPendingFinalizers();
+ GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true, true);
});
SearchConnectionsCommand = new RelayCommand(() =>
diff --git a/Windows/src/ProxyBridge.c b/Windows/src/ProxyBridge.c
index de439b7..d90a430 100644
--- a/Windows/src/ProxyBridge.c
+++ b/Windows/src/ProxyBridge.c
@@ -44,6 +44,7 @@ typedef struct CONNECTION_INFO {
UINT32 orig_dest_ip;
UINT16 orig_dest_port;
BOOL is_tracked;
+ DWORD last_activity; // GetTickCount() timestamp for cleanup
struct CONNECTION_INFO *next;
} CONNECTION_INFO;
@@ -97,6 +98,8 @@ static DWORD last_udp_connect_attempt = 0;
static BOOL running = FALSE;
static DWORD g_current_process_id = 0;
+static BOOL g_traffic_logging_enabled = TRUE;
+
static char g_proxy_host[256] = ""; // Can be IP address or hostname
static UINT16 g_proxy_port = 0;
static UINT16 g_local_relay_port = LOCAL_PROXY_PORT;
@@ -154,6 +157,7 @@ static void add_connection(UINT16 src_port, UINT32 src_ip, UINT32 dest_ip, UINT1
static BOOL get_connection(UINT16 src_port, UINT32 *dest_ip, UINT16 *dest_port);
static BOOL is_connection_tracked(UINT16 src_port);
static void remove_connection(UINT16 src_port);
+static void cleanup_stale_connections(void);
static BOOL is_connection_already_logged(DWORD pid, UINT32 dest_ip, UINT16 dest_port, RuleAction action);
static void add_logged_connection(DWORD pid, UINT32 dest_ip, UINT16 dest_port, RuleAction action);
static void clear_logged_connections(void);
@@ -172,9 +176,18 @@ static DWORD WINAPI packet_processor(LPVOID arg)
PWINDIVERT_IPHDR ip_header;
PWINDIVERT_TCPHDR tcp_header;
PWINDIVERT_UDPHDR udp_header;
+ DWORD last_cleanup = GetTickCount();
while (running)
{
+ DWORD now = GetTickCount();
+
+ if (now - last_cleanup > 30000)
+ {
+ cleanup_stale_connections();
+ last_cleanup = now;
+ }
+
if (!WinDivertRecv(windivert_handle, packet, sizeof(packet), &packet_len, &addr))
{
if (GetLastError() == ERROR_INVALID_HANDLE)
@@ -280,7 +293,11 @@ static DWORD WINAPI packet_processor(LPVOID arg)
const char* display_name = extract_filename(process_name);
g_connection_callback(display_name, pid, dest_ip_str, dest_port, proxy_info);
- add_logged_connection(pid, dest_ip, dest_port, action);
+
+ if (g_traffic_logging_enabled)
+ {
+ add_logged_connection(pid, dest_ip, dest_port, action);
+ }
}
}
}
@@ -413,7 +430,10 @@ static DWORD WINAPI packet_processor(LPVOID arg)
const char* display_name = extract_filename(process_name);
g_connection_callback(display_name, pid, dest_ip_str, orig_dest_port, proxy_info);
- add_logged_connection(pid, orig_dest_ip, orig_dest_port, action);
+ if (g_traffic_logging_enabled)
+ {
+ add_logged_connection(pid, orig_dest_ip, orig_dest_port, action);
+ }
}
}
}
@@ -1951,6 +1971,7 @@ static void add_connection(UINT16 src_port, UINT32 src_ip, UINT32 dest_ip, UINT1
conn->orig_dest_ip = dest_ip;
conn->orig_dest_port = dest_port;
conn->is_tracked = TRUE;
+ conn->last_activity = GetTickCount();
conn->next = connection_list;
connection_list = conn;
ReleaseMutex(lock);
@@ -2012,6 +2033,86 @@ static void remove_connection(UINT16 src_port)
ReleaseMutex(lock);
}
+static void cleanup_stale_connections(void)
+{
+ WaitForSingleObject(lock, INFINITE);
+
+ DWORD now = GetTickCount();
+ int removed = 0;
+ CONNECTION_INFO **conn_ptr = &connection_list;
+
+ while (*conn_ptr != NULL)
+ {
+ if (now - (*conn_ptr)->last_activity > 60000)
+ {
+ CONNECTION_INFO *to_free = *conn_ptr;
+ *conn_ptr = (*conn_ptr)->next;
+ free(to_free);
+ removed++;
+ }
+ else
+ {
+ conn_ptr = &(*conn_ptr)->next;
+ }
+ }
+
+ DWORD now_cache = GetTickCount();
+ int cache_removed = 0;
+ for (int i = 0; i < PID_CACHE_SIZE; i++)
+ {
+ PID_CACHE_ENTRY **entry_ptr = &pid_cache[i];
+ while (*entry_ptr != NULL)
+ {
+ if (now_cache - (*entry_ptr)->timestamp > 10000) /// need to remove it in 10sec
+ {
+ PID_CACHE_ENTRY *to_free = *entry_ptr;
+ *entry_ptr = (*entry_ptr)->next;
+ free(to_free);
+ cache_removed++;
+ }
+ else
+ {
+ entry_ptr = &(*entry_ptr)->next;
+ }
+ }
+ }
+
+ int logged_count = 0;
+ LOGGED_CONNECTION *temp = logged_connections;
+ while (temp != NULL)
+ {
+ logged_count++;
+ temp = temp->next;
+ }
+
+ if (logged_count > 500)
+ {
+ int to_remove = logged_count - 500;
+ int logged_removed = 0;
+
+ for (int i = 0; i < to_remove; i++)
+ {
+ LOGGED_CONNECTION **walk = &logged_connections;
+ LOGGED_CONNECTION *prev = NULL;
+
+ while (*walk != NULL && (*walk)->next != NULL)
+ {
+ walk = &(*walk)->next;
+ }
+
+ if (*walk != NULL)
+ {
+ LOGGED_CONNECTION *to_free = *walk;
+ *walk = NULL;
+ free(to_free);
+ logged_removed++;
+ }
+ }
+ }
+
+ ReleaseMutex(lock);
+}
+
PROXYBRIDGE_API UINT32 ProxyBridge_AddRule(const char* process_name, const char* target_hosts, const char* target_ports, RuleProtocol protocol, RuleAction action)
{
if (process_name == NULL || process_name[0] == '\0')
@@ -2248,6 +2349,15 @@ PROXYBRIDGE_API void ProxyBridge_SetConnectionCallback(ConnectionCallback callba
g_connection_callback = callback;
}
+PROXYBRIDGE_API void ProxyBridge_SetTrafficLoggingEnabled(BOOL enable)
+{
+ g_traffic_logging_enabled = enable;
+ if (!enable)
+ {
+ clear_logged_connections();
+ }
+}
+
// Check if connection already logged (deduplication)
static BOOL is_connection_already_logged(DWORD pid, UINT32 dest_ip, UINT16 dest_port, RuleAction action)
{
diff --git a/Windows/src/ProxyBridge.h b/Windows/src/ProxyBridge.h
index 91d1178..893d99d 100644
--- a/Windows/src/ProxyBridge.h
+++ b/Windows/src/ProxyBridge.h
@@ -42,6 +42,7 @@ PROXYBRIDGE_API BOOL ProxyBridge_SetProxyConfig(ProxyType type, const char* prox
PROXYBRIDGE_API void ProxyBridge_SetDnsViaProxy(BOOL enable);
PROXYBRIDGE_API void ProxyBridge_SetLogCallback(LogCallback callback);
PROXYBRIDGE_API void ProxyBridge_SetConnectionCallback(ConnectionCallback callback);
+PROXYBRIDGE_API void ProxyBridge_SetTrafficLoggingEnabled(BOOL enable);
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);