Skip to content

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

Refactor proxy configuration methods and add input validation

AnoF-Cybere1d9edd519ba
diff --git a/.sonarlint/connectedMode.json b/.sonarlint/connectedMode.json
new file mode 100644
index 0000000..5fa5667
--- /dev/null
+++ b/.sonarlint/connectedMode.json
@@ -0,0 +1,5 @@
+{
+ "sonarCloudOrganization": "interceptsuite",
+ "projectKey": "InterceptSuite_ProxyBridge",
+ "region": "EU"
+}
\ No newline at end of file
diff --git a/Windows/gui/Interop/ProxyBridgeNative.cs b/Windows/gui/Interop/ProxyBridgeNative.cs
index cd31a7a..df7c4b5 100644
--- a/Windows/gui/Interop/ProxyBridgeNative.cs
+++ b/Windows/gui/Interop/ProxyBridgeNative.cs
@@ -92,17 +92,31 @@ public static class ProxyBridgeNative
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ProxyBridge_MoveRuleToPosition(uint ruleId, uint newPosition);
- [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
- public static extern uint ProxyBridge_AddProxyConfig(
+ [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ProxyBridge_AddProxyConfig")]
+ private static extern uint ProxyBridge_AddProxyConfig_Native(
ProxyType type,
[MarshalAs(UnmanagedType.LPStr)] string proxyIp,
ushort proxyPort,
[MarshalAs(UnmanagedType.LPStr)] string username,
[MarshalAs(UnmanagedType.LPStr)] string password);
- [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
+ public static uint ProxyBridge_AddProxyConfig(
+ ProxyType type,
+ string proxyIp,
+ ushort proxyPort,
+ string username,
+ string password)
+ {
+ if (string.IsNullOrWhiteSpace(proxyIp))
+ throw new ArgumentException("Proxy IP must not be null or empty.", nameof(proxyIp));
+ username ??= string.Empty;
+ password ??= string.Empty;
+ return ProxyBridge_AddProxyConfig_Native(type, proxyIp, proxyPort, username, password);
+ }
+
+ [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ProxyBridge_EditProxyConfig")]
[return: MarshalAs(UnmanagedType.Bool)]
- public static extern bool ProxyBridge_EditProxyConfig(
+ private static extern bool ProxyBridge_EditProxyConfig_Native(
uint configId,
ProxyType type,
[MarshalAs(UnmanagedType.LPStr)] string proxyIp,
@@ -110,18 +124,44 @@ public static class ProxyBridgeNative
[MarshalAs(UnmanagedType.LPStr)] string username,
[MarshalAs(UnmanagedType.LPStr)] string password);
+ public static bool ProxyBridge_EditProxyConfig(
+ uint configId,
+ ProxyType type,
+ string proxyIp,
+ ushort proxyPort,
+ string username,
+ string password)
+ {
+ username ??= string.Empty;
+ password ??= string.Empty;
+ return ProxyBridge_EditProxyConfig_Native(configId, type, proxyIp, proxyPort, username, password);
+ }
+
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ProxyBridge_DeleteProxyConfig(uint configId);
- [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
- public static extern int ProxyBridge_TestProxyConfig(
+ [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, EntryPoint = "ProxyBridge_TestProxyConfig")]
+ private static extern int ProxyBridge_TestProxyConfig_Native(
uint configId,
[MarshalAs(UnmanagedType.LPStr)] string targetHost,
ushort targetPort,
[MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder resultBuffer,
UIntPtr bufferSize);
+ public static int ProxyBridge_TestProxyConfig(
+ uint configId,
+ string targetHost,
+ ushort targetPort,
+ System.Text.StringBuilder resultBuffer,
+ UIntPtr bufferSize)
+ {
+ ArgumentNullException.ThrowIfNull(targetHost);
+ ArgumentNullException.ThrowIfNull(resultBuffer);
+ var effectiveSize = bufferSize == UIntPtr.Zero ? (UIntPtr)resultBuffer.Capacity : bufferSize;
+ return ProxyBridge_TestProxyConfig_Native(configId, targetHost, targetPort, resultBuffer, effectiveSize);
+ }
+
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern void ProxyBridge_SetLogCallback(LogCallback callback);
diff --git a/Windows/gui/Services/ProfileManager.cs b/Windows/gui/Services/ProfileManager.cs
index 12855e8..e5aacb1 100644
--- a/Windows/gui/Services/ProfileManager.cs
+++ b/Windows/gui/Services/ProfileManager.cs
@@ -95,8 +95,8 @@ public static class ProfileManager
private static readonly string ProfilesDirectory = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
- "ProxyBridge",
- "profiles"
+ "ProxyBridge".TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar),
+ "profiles".TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)
);
public static string[] GetProfileNames()
@@ -208,7 +208,7 @@ public static class ProfileManager
}
private static string GetProfilePath(string name)
- => Path.Combine(ProfilesDirectory, name + ProfileExtension);
+ => Path.Combine(ProfilesDirectory, Path.GetFileName(name) + ProfileExtension);
private static void EnsureDirectory()
{
diff --git a/Windows/gui/Services/ProxyBridgeService.cs b/Windows/gui/Services/ProxyBridgeService.cs
index b973bc1..4157a8e 100644
--- a/Windows/gui/Services/ProxyBridgeService.cs
+++ b/Windows/gui/Services/ProxyBridgeService.cs
@@ -55,6 +55,11 @@ public class ProxyBridgeService : IDisposable
? ProxyBridgeNative.ProxyType.HTTP
: ProxyBridgeNative.ProxyType.SOCKS5;
+ return AddProxyConfigCore(proxyType, ip, port, username, password);
+ }
+
+ private static uint AddProxyConfigCore(ProxyBridgeNative.ProxyType proxyType, string ip, ushort port, string username, string password)
+ {
return ProxyBridgeNative.ProxyBridge_AddProxyConfig(proxyType, ip, port, username, password);
}