static Uri GetProxyUri(CFProxy proxy) { string protocol; switch (proxy.ProxyType) { case CFProxyType.FTP: protocol = "ftp://"; break; case CFProxyType.HTTP: case CFProxyType.HTTPS: protocol = "http://"; break; default: return(null); } string username = proxy.Username; string password = proxy.Password; string hostname = proxy.HostName; int port = proxy.Port; string userinfo; string uri; if (username != null) { if (password != null) { userinfo = Uri.EscapeDataString(username) + ':' + Uri.EscapeDataString(password) + '@'; } else { userinfo = Uri.EscapeDataString(username) + '@'; } } else { userinfo = string.Empty; } uri = protocol + userinfo + hostname + (port != 0 ? ':' + port.ToString() : string.Empty); return(new Uri(uri, UriKind.Absolute)); }
public static CFProxy[] ExecuteProxyAutoConfigurationURL(IntPtr proxyAutoConfigURL, Uri targetURL) { CFUrl url = CFUrl.Create(targetURL.AbsoluteUri); if (url == null) { return(null); } CFProxy[] proxies = null; var runLoop = CFRunLoop.CurrentRunLoop; // Callback that will be called after executing the configuration script CFProxyAutoConfigurationResultCallback cb = delegate(IntPtr client, IntPtr proxyList, IntPtr error) { if (proxyList != IntPtr.Zero) { var array = new CFArray(proxyList, false); proxies = new CFProxy [array.Count]; for (int i = 0; i < proxies.Length; i++) { CFDictionary dict = new CFDictionary(array[i], false); proxies[i] = new CFProxy(dict); } array.Dispose(); } runLoop.Stop(); }; var clientContext = new CFStreamClientContext(); var loopSource = CFNetworkExecuteProxyAutoConfigurationURL(proxyAutoConfigURL, url.Handle, cb, ref clientContext); // Create a private mode var mode = CFString.Create("Mono.MacProxy"); runLoop.AddSource(loopSource, mode); runLoop.RunInMode(mode, double.MaxValue, false); runLoop.RemoveSource(loopSource, mode); return(proxies); }
public static CFProxy[] GetProxiesForURL (CFUrl url, CFProxySettings proxySettings) { if (url == null || url.Handle == IntPtr.Zero) throw new ArgumentNullException ("url"); if (proxySettings == null) proxySettings = GetSystemProxySettings (); CFArray array = CopyProxiesForURL (url, proxySettings.Dictionary); if (array == null) return null; CFProxy[] proxies = new CFProxy [array.Count]; for (int i = 0; i < proxies.Length; i++) { CFDictionary dict = new CFDictionary (array[i], false); proxies[i] = new CFProxy (dict); } array.Dispose (); return proxies; }
public static CFProxy[] GetProxiesForAutoConfigurationScript (IntPtr proxyAutoConfigurationScript, CFUrl targetURL) { if (proxyAutoConfigurationScript == IntPtr.Zero) throw new ArgumentNullException ("proxyAutoConfigurationScript"); if (targetURL == null) throw new ArgumentNullException ("targetURL"); CFArray array = CopyProxiesForAutoConfigurationScript (proxyAutoConfigurationScript, targetURL); if (array == null) return null; CFProxy[] proxies = new CFProxy [array.Count]; for (int i = 0; i < proxies.Length; i++) { CFDictionary dict = new CFDictionary (array[i], false); proxies[i] = new CFProxy (dict); } array.Dispose (); return proxies; }
static Uri GetProxyUri (CFProxy proxy, out NetworkCredential credentials) { string protocol; switch (proxy.ProxyType) { case CFProxyType.FTP: protocol = "ftp://"; break; case CFProxyType.HTTP: case CFProxyType.HTTPS: protocol = "http://"; break; default: credentials = null; return null; } string username = proxy.Username; string password = proxy.Password; string hostname = proxy.HostName; int port = proxy.Port; string uri; if (username != null) credentials = new NetworkCredential (username, password); else credentials = null; uri = protocol + hostname + (port != 0 ? ':' + port.ToString () : string.Empty); return new Uri (uri, UriKind.Absolute); }
static Uri GetProxyUri (CFProxy proxy) { string protocol; switch (proxy.ProxyType) { case CFProxyType.FTP: protocol = "ftp://"; break; case CFProxyType.HTTP: case CFProxyType.HTTPS: protocol = "http://"; break; default: return null; } string username = proxy.Username; string password = proxy.Password; string hostname = proxy.HostName; int port = proxy.Port; string userinfo; string uri; if (username != null) { if (password != null) userinfo = Uri.EscapeDataString (username) + ':' + Uri.EscapeDataString (password) + '@'; else userinfo = Uri.EscapeDataString (username) + '@'; } else { userinfo = string.Empty; } uri = protocol + userinfo + hostname + (port != 0 ? ':' + port.ToString () : string.Empty); return new Uri (uri, UriKind.Absolute); }
static Uri SelectProxy (CFProxy[] proxies, Uri targetUri, out NetworkCredential credentials) { if (proxies == null) { credentials = null; return targetUri; } for (int i = 0; i < proxies.Length; i++) { switch (proxies[i].ProxyType) { case CFProxyType.HTTPS: case CFProxyType.HTTP: case CFProxyType.FTP: // create a Uri based on the hostname/port/etc info return GetProxyUri (proxies[i], out credentials); case CFProxyType.SOCKS: default: // unsupported proxy type, try the next one break; case CFProxyType.None: // no proxy should be used credentials = null; return targetUri; } } credentials = null; return null; }
public static CFProxy[] ExecuteProxyAutoConfigurationURL (IntPtr proxyAutoConfigURL, Uri targetURL) { CFUrl url = CFUrl.Create (targetURL.AbsoluteUri); if (url == null) return null; CFProxy[] proxies = null; var runLoop = CFRunLoop.CurrentRunLoop; // Callback that will be called after executing the configuration script CFProxyAutoConfigurationResultCallback cb = delegate (IntPtr client, IntPtr proxyList, IntPtr error) { if (proxyList != IntPtr.Zero) { var array = new CFArray (proxyList, false); proxies = new CFProxy [array.Count]; for (int i = 0; i < proxies.Length; i++) { CFDictionary dict = new CFDictionary (array[i], false); proxies[i] = new CFProxy (dict); } array.Dispose (); } runLoop.Stop (); }; var clientContext = new CFStreamClientContext (); var loopSource = CFNetworkExecuteProxyAutoConfigurationURL (proxyAutoConfigURL, url.Handle, cb, ref clientContext); // Create a private mode var mode = CFString.Create ("Mono.MacProxy"); runLoop.AddSource (loopSource, mode); runLoop.RunInMode (mode, double.MaxValue, false); runLoop.RemoveSource (loopSource, mode); return proxies; }