Skip to content

Commit 603c973

Browse files
committedMar 16, 2025··
Fix Proxy Auth always results invalid password
1 parent c9bdca6 commit 603c973

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed
 

‎CollapseLauncher/Classes/Helper/HttpClientBuilder.cs

+8-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Net.Http;
77
using System.Net.Security;
88
using System.Runtime.InteropServices;
9+
using System.Security;
910
// ReSharper disable StringLiteralTypo
1011
// ReSharper disable UnusedMember.Global
1112

@@ -53,7 +54,7 @@ private static string GetDefaultUserAgent()
5354
+ $"WinAppSDK/{LauncherConfig.WindowsAppSdkVersion}";
5455
}
5556

56-
public HttpClientBuilder<THandler> UseExternalProxy(string host, string? username = null, string? password = null)
57+
public HttpClientBuilder<THandler> UseExternalProxy(string host, string? username = null, SecureString? password = null)
5758
{
5859
// Try to create the Uri
5960
if (Uri.TryCreate(host, UriKind.Absolute, out Uri? hostUri))
@@ -65,17 +66,16 @@ public HttpClientBuilder<THandler> UseExternalProxy(string host, string? usernam
6566
IsUseSystemProxy = false;
6667
ExternalProxy = null;
6768
return this;
68-
6969
}
7070

71-
public HttpClientBuilder<THandler> UseExternalProxy(Uri hostUri, string? username = null, string? password = null)
71+
public HttpClientBuilder<THandler> UseExternalProxy(Uri hostUri, string? username = null, SecureString? password = null)
7272
{
7373
IsUseSystemProxy = false;
7474

7575
// Initialize the proxy host
7676
ExternalProxy =
7777
!string.IsNullOrEmpty(username)
78-
&& !string.IsNullOrEmpty(password) ?
78+
&& password != null ?
7979
new WebProxy(hostUri, true, null, new NetworkCredential(username, password))
8080
: new WebProxy(hostUri, true);
8181

@@ -100,7 +100,10 @@ public HttpClientBuilder<THandler> UseLauncherConfig(int maxConnections = MaxCon
100100
UseProxy();
101101

102102
if (lIsUseProxy && isHttpProxyUrlValid && lProxyUri != null)
103-
UseExternalProxy(lProxyUri, lHttpProxyUsername, lHttpProxyPassword);
103+
{
104+
using SecureString? proxyPassword = SimpleProtectData.UnprotectStringAsSecureString(lHttpProxyPassword);
105+
UseExternalProxy(lProxyUri, lHttpProxyUsername, proxyPassword);
106+
}
104107

105108
AllowUntrustedCert(lIsAllowUntrustedCert);
106109
AllowCookies(lIsAllowHttpCookies);

‎CollapseLauncher/Classes/Helper/SimpleProtectData.cs

+24
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Hi3Helper;
22
using System;
33
using System.Buffers.Text;
4+
using System.Security;
45
using System.Security.Cryptography;
56
using System.Text;
67
using Hi3Helper.SentryHelper;
@@ -106,5 +107,28 @@ internal static class SimpleProtectData
106107
Array.Clear(unprotectedData);
107108
}
108109
}
110+
111+
internal static unsafe SecureString? UnprotectStringAsSecureString(string? input)
112+
{
113+
string? rawString = UnprotectString(input);
114+
115+
if (string.IsNullOrEmpty(rawString))
116+
return null;
117+
118+
int len = rawString.Length;
119+
fixed (char* charB = rawString)
120+
{
121+
try
122+
{
123+
SecureString secureString = new SecureString(charB, rawString.Length);
124+
return secureString;
125+
}
126+
finally
127+
{
128+
for (int i = 0; i < len; i++)
129+
*(charB + i) = '\0';
130+
}
131+
}
132+
}
109133
}
110134
}

0 commit comments

Comments
 (0)
Please sign in to comment.