forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Read system proxy information on macOS (dotnet/corefx#36177)
* Read system proxy information on macOS * Replace CFRunLoopRemoveSource with CFRunLoopSourceInvalidate to mimic recommended flow from Chromium / WebKit Commit migrated from dotnet/corefx@2aa2518
- Loading branch information
1 parent
ff946bf
commit 19d90b8
Showing
11 changed files
with
455 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
43 changes: 43 additions & 0 deletions
43
src/libraries/Common/src/Interop/OSX/Interop.CoreFoundation.CFDictionary.cs
This file contains 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,43 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
using Microsoft.Win32.SafeHandles; | ||
|
||
internal static partial class Interop | ||
{ | ||
internal static partial class CoreFoundation | ||
{ | ||
[DllImport(Libraries.CoreFoundationLibrary)] | ||
internal static extern IntPtr CFDictionaryGetValue(SafeCFDictionaryHandle handle, IntPtr key); | ||
} | ||
} | ||
|
||
namespace Microsoft.Win32.SafeHandles | ||
{ | ||
internal sealed class SafeCFDictionaryHandle : SafeHandle | ||
{ | ||
private SafeCFDictionaryHandle() | ||
: base(IntPtr.Zero, ownsHandle: true) | ||
{ | ||
} | ||
|
||
internal SafeCFDictionaryHandle(IntPtr handle, bool ownsHandle) | ||
: base(handle, ownsHandle) | ||
{ | ||
} | ||
|
||
protected override bool ReleaseHandle() | ||
{ | ||
Interop.CoreFoundation.CFRelease(handle); | ||
SetHandle(IntPtr.Zero); | ||
return true; | ||
} | ||
|
||
public override bool IsInvalid => handle == IntPtr.Zero; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/libraries/Common/src/Interop/OSX/Interop.CoreFoundation.CFNumber.cs
This file contains 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,23 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
using Microsoft.Win32.SafeHandles; | ||
|
||
internal static partial class Interop | ||
{ | ||
internal static partial class CoreFoundation | ||
{ | ||
internal enum CFNumberType | ||
{ | ||
kCFNumberIntType = 9, | ||
} | ||
|
||
[DllImport(Libraries.CoreFoundationLibrary)] | ||
private static extern int CFNumberGetValue(IntPtr handle, CFNumberType type, out int value); | ||
} | ||
} |
145 changes: 145 additions & 0 deletions
145
src/libraries/Common/src/Interop/OSX/Interop.CoreFoundation.CFProxy.cs
This file contains 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,145 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
using Microsoft.Win32.SafeHandles; | ||
|
||
using CFRunLoopSourceRef = System.IntPtr; | ||
|
||
internal static partial class Interop | ||
{ | ||
internal static partial class CoreFoundation | ||
{ | ||
[DllImport(Libraries.CFNetworkLibrary)] | ||
internal static extern SafeCFDictionaryHandle CFNetworkCopySystemProxySettings(); | ||
|
||
[DllImport(Libraries.CFNetworkLibrary)] | ||
internal static extern SafeCFArrayHandle CFNetworkCopyProxiesForURL(SafeCreateHandle url, SafeCFDictionaryHandle proxySettings); | ||
|
||
internal delegate void CFProxyAutoConfigurationResultCallback(IntPtr client, IntPtr proxyList, IntPtr error); | ||
|
||
[DllImport(Libraries.CFNetworkLibrary)] | ||
internal static extern CFRunLoopSourceRef CFNetworkExecuteProxyAutoConfigurationURL( | ||
IntPtr proxyAutoConfigURL, | ||
SafeCreateHandle targetURL, | ||
CFProxyAutoConfigurationResultCallback cb, | ||
ref CFStreamClientContext clientContext); | ||
|
||
[DllImport(Libraries.CFNetworkLibrary)] | ||
internal static extern CFRunLoopSourceRef CFNetworkExecuteProxyAutoConfigurationScript( | ||
IntPtr proxyAutoConfigurationScript, | ||
SafeCreateHandle targetURL, | ||
CFProxyAutoConfigurationResultCallback cb, | ||
ref CFStreamClientContext clientContext); | ||
|
||
[StructLayout(LayoutKind.Sequential)] | ||
internal struct CFStreamClientContext | ||
{ | ||
public IntPtr Version; | ||
public IntPtr Info; | ||
public IntPtr Retain; | ||
public IntPtr Release; | ||
public IntPtr CopyDescription; | ||
} | ||
|
||
internal class CFProxy | ||
{ | ||
private SafeCFDictionaryHandle _dictionary; | ||
|
||
internal static readonly string kCFProxyTypeAutoConfigurationURL; | ||
internal static readonly string kCFProxyTypeAutoConfigurationJavaScript; | ||
internal static readonly string kCFProxyTypeFTP; | ||
internal static readonly string kCFProxyTypeHTTP; | ||
internal static readonly string kCFProxyTypeHTTPS; | ||
internal static readonly string kCFProxyTypeSOCKS; | ||
|
||
private static readonly IntPtr kCFProxyAutoConfigurationJavaScriptKey; | ||
private static readonly IntPtr kCFProxyAutoConfigurationURLKey; | ||
private static readonly IntPtr kCFProxyHostNameKey; | ||
private static readonly IntPtr kCFProxyPasswordKey; | ||
private static readonly IntPtr kCFProxyPortNumberKey; | ||
private static readonly IntPtr kCFProxyTypeKey; | ||
private static readonly IntPtr kCFProxyUsernameKey; | ||
|
||
static CFProxy() | ||
{ | ||
IntPtr lib = NativeLibrary.Load(Interop.Libraries.CFNetworkLibrary); | ||
if (lib != IntPtr.Zero) | ||
{ | ||
kCFProxyTypeAutoConfigurationURL = LoadCFStringSymbol(lib, "kCFProxyTypeAutoConfigurationURL"); | ||
kCFProxyTypeAutoConfigurationJavaScript = LoadCFStringSymbol(lib, "kCFProxyTypeAutoConfigurationJavaScript"); | ||
kCFProxyTypeFTP = LoadCFStringSymbol(lib, "kCFProxyTypeFTP"); | ||
kCFProxyTypeHTTP = LoadCFStringSymbol(lib, "kCFProxyTypeHTTP"); | ||
kCFProxyTypeHTTPS = LoadCFStringSymbol(lib, "kCFProxyTypeHTTPS"); | ||
kCFProxyTypeSOCKS = LoadCFStringSymbol(lib, "kCFProxyTypeSOCKS"); | ||
|
||
kCFProxyAutoConfigurationJavaScriptKey = LoadSymbol(lib, "kCFProxyAutoConfigurationJavaScriptKey"); | ||
kCFProxyAutoConfigurationURLKey = LoadSymbol(lib, "kCFProxyAutoConfigurationURLKey"); | ||
kCFProxyHostNameKey = LoadSymbol(lib, "kCFProxyHostNameKey"); | ||
kCFProxyPasswordKey = LoadSymbol(lib, "kCFProxyPasswordKey"); | ||
kCFProxyPortNumberKey = LoadSymbol(lib, "kCFProxyPortNumberKey"); | ||
kCFProxyTypeKey = LoadSymbol(lib, "kCFProxyTypeKey"); | ||
kCFProxyUsernameKey = LoadSymbol(lib, "kCFProxyUsernameKey"); | ||
} | ||
} | ||
|
||
public CFProxy(SafeCFDictionaryHandle dictionary) | ||
{ | ||
_dictionary = dictionary; | ||
} | ||
|
||
private static IntPtr LoadSymbol(IntPtr lib, string name) | ||
{ | ||
IntPtr indirect = NativeLibrary.GetExport(lib, name); | ||
return indirect == IntPtr.Zero ? IntPtr.Zero : Marshal.ReadIntPtr(indirect); | ||
} | ||
|
||
private static string LoadCFStringSymbol(IntPtr lib, string name) | ||
{ | ||
using (SafeCFStringHandle cfString = new SafeCFStringHandle(LoadSymbol(lib, name), false)) | ||
{ | ||
Debug.Assert(!cfString.IsInvalid); | ||
return Interop.CoreFoundation.CFStringToString(cfString); | ||
} | ||
} | ||
|
||
private string GetString(IntPtr key) | ||
{ | ||
IntPtr dictValue = CFDictionaryGetValue(_dictionary, key); | ||
if (dictValue != IntPtr.Zero) | ||
{ | ||
using (SafeCFStringHandle handle = new SafeCFStringHandle(dictValue, false)) | ||
{ | ||
return CFStringToString(handle); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public string ProxyType => GetString(kCFProxyTypeKey); | ||
public string HostName => GetString(kCFProxyHostNameKey); | ||
public string Username => GetString(kCFProxyUsernameKey); | ||
public string Password => GetString(kCFProxyPasswordKey); | ||
|
||
public int PortNumber | ||
{ | ||
get | ||
{ | ||
IntPtr dictValue = CFDictionaryGetValue(_dictionary, kCFProxyPortNumberKey); | ||
if (dictValue != IntPtr.Zero && CFNumberGetValue(dictValue, CFNumberType.kCFNumberIntType, out int value) > 0) | ||
{ | ||
return value; | ||
} | ||
return -1; | ||
} | ||
} | ||
|
||
public IntPtr AutoConfigurationURL => CFDictionaryGetValue(_dictionary, kCFProxyAutoConfigurationURLKey); | ||
public IntPtr AutoConfigurationJavaScript => CFDictionaryGetValue(_dictionary, kCFProxyAutoConfigurationJavaScriptKey); | ||
} | ||
} | ||
} |
This file contains 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
30 changes: 30 additions & 0 deletions
30
src/libraries/Common/src/Interop/OSX/Interop.CoreFoundation.CFUrl.cs
This file contains 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,30 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
using Microsoft.Win32.SafeHandles; | ||
|
||
internal static partial class Interop | ||
{ | ||
internal static partial class CoreFoundation | ||
{ | ||
[DllImport(Libraries.CoreFoundationLibrary)] | ||
private static extern SafeCreateHandle CFURLCreateWithString( | ||
IntPtr allocator, | ||
SafeCreateHandle str, | ||
IntPtr baseUrl); | ||
|
||
internal static SafeCreateHandle CFURLCreateWithString(string url) | ||
{ | ||
Debug.Assert(url != null); | ||
using (SafeCreateHandle stringHandle = CFStringCreateWithCString(url)) | ||
{ | ||
return CFURLCreateWithString(IntPtr.Zero, stringHandle, IntPtr.Zero); | ||
} | ||
} | ||
} | ||
} |
This file contains 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 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 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.