-
Notifications
You must be signed in to change notification settings - Fork 5.5k
[Android] Bridge SocketsHttpHandler to java.net.ProxySelector #128031
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
simonrozsival
wants to merge
6
commits into
dotnet:main
Choose a base branch
from
simonrozsival:dev/simonrozsival/android-platform-proxy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
392726f
[Android] Bridge SocketsHttpHandler to java.net.ProxySelector
simonrozsival 6fdfd39
Address review feedback on AndroidPlatformProxy
simonrozsival 9123b9d
Address review feedback
simonrozsival 927bfbd
Preserve Android DIRECT proxy results
simonrozsival 5b9611a
Address Android proxy review feedback
simonrozsival 5d7a922
Merge branch 'main' into dev/simonrozsival/android-platform-proxy
simonrozsival File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
...s/Common/src/Interop/Android/System.Security.Cryptography.Native.Android/Interop.Proxy.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Runtime.InteropServices; | ||
|
|
||
| internal static partial class Interop | ||
| { | ||
| internal static partial class AndroidCrypto | ||
| { | ||
| internal enum AndroidProxyType | ||
| { | ||
| Direct = 0, | ||
| Http = 1, | ||
| Socks = 2, | ||
| } | ||
|
|
||
| [StructLayout(LayoutKind.Sequential)] | ||
| internal struct AndroidProxyInfo | ||
| { | ||
| public int Type; // AndroidProxyType | ||
| public IntPtr Host; // NUL-terminated UTF-16; read via Marshal.PtrToStringUni; freed by FreeProxyResult | ||
| public int Port; | ||
| } | ||
|
|
||
| [LibraryImport(Libraries.AndroidCryptoNative, | ||
| EntryPoint = "AndroidCryptoNative_GetProxyForUrl", | ||
| StringMarshalling = StringMarshalling.Utf8)] | ||
| internal static unsafe partial int GetProxyForUrl( | ||
| string url, | ||
| out int count, | ||
| out AndroidProxyInfo* proxies); | ||
|
|
||
| [LibraryImport(Libraries.AndroidCryptoNative, | ||
| EntryPoint = "AndroidCryptoNative_FreeProxyResult")] | ||
| internal static unsafe partial void FreeProxyResult( | ||
| AndroidProxyInfo* proxies, int count); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
...es/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/AndroidPlatformProxy.Android.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Net; | ||
| using System.Runtime.InteropServices; | ||
|
|
||
| namespace System.Net.Http | ||
| { | ||
| /// <summary> | ||
| /// An <see cref="IWebProxy"/> that defers proxy resolution to the | ||
| /// Android platform via <c>java.net.ProxySelector</c>. Honors Wi-Fi | ||
| /// proxy, MDM-deployed system proxy, PAC scripts, and per-network | ||
| /// or VPN proxies — the same source <c>java.net.HttpURLConnection</c> | ||
| /// consults. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// The Android proxy APIs (<c>ProxySelector</c>, <c>ProxyInfo</c>, | ||
| /// <c>ConnectivityManager</c>) do not surface credentials. The | ||
| /// <see cref="Credentials"/> property is required by | ||
| /// <see cref="IWebProxy"/> but is never populated by this class. | ||
| /// Apps that need to authenticate to a system-detected proxy should | ||
| /// set <c>HttpClientHandler.DefaultProxyCredentials</c> (or | ||
| /// <c>SocketsHttpHandler.DefaultProxyCredentials</c>) — the same | ||
| /// behavior as on macOS and Windows. | ||
| /// </para> | ||
| /// </remarks> | ||
| internal sealed class AndroidPlatformProxy : IWebProxy | ||
| { | ||
| public ICredentials? Credentials { get; set; } | ||
|
|
||
| public unsafe Uri? GetProxy(Uri destination) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(destination); | ||
|
|
||
| string url = destination.AbsoluteUri; | ||
|
|
||
| Interop.AndroidCrypto.AndroidProxyInfo* proxies = null; | ||
| int count = 0; | ||
| int rc = Interop.AndroidCrypto.GetProxyForUrl(url, out count, out proxies); | ||
|
|
||
| if (rc != 0 || proxies == null) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| try | ||
| { | ||
| // ProxySelector returns entries in preference order. We take the first. | ||
| // SocketsHttpHandler does not currently have an opt-in fallback API for | ||
| // multiple proxies; if the chosen proxy is unreachable the user sees the | ||
| // connect error rather than an automatic retry. | ||
| for (int i = 0; i < count; i++) | ||
| { | ||
| Interop.AndroidCrypto.AndroidProxyInfo entry = proxies[i]; | ||
| Interop.AndroidCrypto.AndroidProxyType type = (Interop.AndroidCrypto.AndroidProxyType)entry.Type; | ||
|
|
||
| if (type == Interop.AndroidCrypto.AndroidProxyType.Direct) | ||
| { | ||
| // Java's Proxy.NO_PROXY is represented as Proxy.Type.DIRECT. | ||
| // Preserve ProxySelector ordering by treating DIRECT as the | ||
| // selected result rather than skipping to a later fallback proxy. | ||
| return null; | ||
| } | ||
|
|
||
| // SOCKS is a transport-level proxy protocol (RFC 1928 for SOCKS5). | ||
| // Unlike HTTP CONNECT, SOCKS tunnels arbitrary TCP at the socket | ||
| // layer. SocketsHttpHandler accepts "socks5://" via | ||
| // HttpUtilities.IsSupportedProxyScheme. Android's | ||
| // java.net.Proxy.Type.SOCKS maps to SOCKS5 on modern Android. | ||
| string? scheme = type switch | ||
| { | ||
| Interop.AndroidCrypto.AndroidProxyType.Http => "http", | ||
| Interop.AndroidCrypto.AndroidProxyType.Socks => "socks5", | ||
| _ => null, | ||
| }; | ||
|
|
||
| if (scheme is null) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| // The native PAL allocates the host as NUL-terminated UTF-16 | ||
| // (Marshal.PtrToStringUni is a zero-conversion copy). | ||
| string? host = Marshal.PtrToStringUni(entry.Host); | ||
| if (string.IsNullOrEmpty(host)) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| return new UriBuilder(scheme, host, entry.Port).Uri; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| finally | ||
| { | ||
| Interop.AndroidCrypto.FreeProxyResult(proxies, count); | ||
| } | ||
| } | ||
|
|
||
| // SocketsHttpHandler's pattern is: call IsBypassed first; if it returns false, | ||
| // call GetProxy. For us, computing IsBypassed *correctly* would require calling | ||
| // GetProxy first, which would mean two JNI round-trips per request. | ||
| // Instead, we always return false (same approach as HttpWindowsProxy:349–360) so | ||
| // that SHH always calls GetProxy exactly once and discovers "no proxy" via a | ||
| // null return. | ||
| public bool IsBypassed(Uri host) => false; | ||
| } | ||
| } | ||
52 changes: 52 additions & 0 deletions
52
...braries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/SystemProxyInfo.Android.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Diagnostics.CodeAnalysis; | ||
|
|
||
| namespace System.Net.Http | ||
| { | ||
| internal static partial class SystemProxyInfo | ||
| { | ||
| public static IWebProxy ConstructSystemProxy() | ||
| { | ||
| // 1. Honor HTTP_PROXY / HTTPS_PROXY / NO_PROXY env vars first. | ||
| // This matches the Unix convention and lets dev/test flows | ||
| // (e.g. Charles/Fiddler) keep working unchanged. | ||
| if (HttpEnvironmentProxy.TryCreate(out IWebProxy? envProxy)) | ||
| { | ||
| return envProxy; | ||
| } | ||
|
|
||
| // 2. Defer to the Android platform proxy via java.net.ProxySelector. | ||
| // Wi-Fi proxy, MDM-deployed proxy, PAC scripts, and per-network | ||
| // or VPN-attached ProxyInfo are all surfaced through this path. | ||
| if (UseAndroidSystemProxy) | ||
| { | ||
| return new AndroidPlatformProxy(); | ||
| } | ||
|
|
||
| return new HttpNoProxy(); | ||
|
simonrozsival marked this conversation as resolved.
|
||
| } | ||
|
simonrozsival marked this conversation as resolved.
|
||
|
|
||
| // Feature switch: System.Net.Http.UseAndroidSystemProxy | ||
| // | ||
| // Defaults to true. Apps may opt out (set to "false" via | ||
| // <RuntimeHostConfigurationOption>) for the following reasons: | ||
| // | ||
| // * Back-compat with apps that previously ran on SocketsHttpHandler | ||
| // (i.e. UseNativeHttpHandler=false) and got env-var-only proxy | ||
| // behavior. Suddenly honoring the system proxy could change the | ||
| // destination of their HTTP requests; this switch is the escape | ||
| // hatch. | ||
| // * Trimming: when set to false at publish time, the linker can | ||
| // trim the AndroidPlatformProxy class, the Interop P/Invoke | ||
| // declarations, and (transitively) leave the native pal_proxy | ||
| // code referenced only by other paths. | ||
| // * Testing / debugging where pure env-var behavior is required. | ||
| [FeatureSwitchDefinition("System.Net.Http.UseAndroidSystemProxy")] | ||
|
simonrozsival marked this conversation as resolved.
|
||
| private static bool UseAndroidSystemProxy { get; } = | ||
| RuntimeSettingParser.QueryRuntimeSettingSwitch( | ||
| "System.Net.Http.UseAndroidSystemProxy", | ||
| defaultValue: true); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.