Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find out the browser's proxy settings?

I am writing a command-line tool for Windows that uses libcurl to download files from the internet.

Obviously, the downloading doesn't work when the user is behind a proxy server, because the proxy needs to be configured. I want to keep my tool as simple as possible however, and not have to burden the user with having to configure the proxy. My tool doesn't even have a config file, so the user would otherwise have to pass in the proxy settings on every command, or set an environment variable or somesuch -- way too much hassle.

So I thought, everyone's browser will usually already be set up properly, proxy configured and everything. This will be true for even the most basic user because otherwise "their internet wouldn't work".

So I figure that I can find out whether to use a proxy by looking at IE's proxy settings.

How do I go about this? More specifically:

  • Is there one set of "proxy settings" in Windows, used by all browsers (probably IE's), or would I have to write different routines for IE, Firefox, Opera, etc?
  • I know that I can probably read the values directly out of the appropriate registry locations if they are configured manually, but does this also work with "automatically detect proxy server?" Do I even have to bother with that option, or is it (almost) never used?

Before people start suggesting alternatives: I'm using C, so I'm limited to the Win32 API, and I really really want to keep using C and libcurl.

like image 257
rix0rrr Avatar asked Oct 14 '08 19:10

rix0rrr


People also ask

How do I change proxy settings in Chrome?

Step1: To set proxy in Google Chrome Go to Option (Top-Right Side) > Click on Under the Hood Tab > Click on Change Proxy Settings and you can change Proxy from there.


2 Answers

The function you're looking for is WinHttpGetIEProxyConfigForCurrentUser(), which is documented at http://msdn.microsoft.com/en-us/library/aa384096(VS.85).aspx. This function is used by Firefox and Opera to get their proxy settings by default, although you can override them per-browser. Don't do that, though. The right thing to do (which is what everybody else does) is to just get the IE settings and assume that they're correct, since they almost always are.

Here's a sample of the relevant logic, which you should adapt for your needs:

if( WinHttpGetIEProxyConfigForCurrentUser( &ieProxyConfig ) ) {     if( ieProxyConfig.fAutoDetect )     {         fAutoProxy = TRUE;     }      if( ieProxyConfig.lpszAutoConfigUrl != NULL )     {         fAutoProxy = TRUE;         autoProxyOptions.lpszAutoConfigUrl = ieProxyConfig.lpszAutoConfigUrl;     } } else {     // use autoproxy     fAutoProxy = TRUE; }  if( fAutoProxy ) {     if ( autoProxyOptions.lpszAutoConfigUrl != NULL )     {         autoProxyOptions.dwFlags = WINHTTP_AUTOPROXY_CONFIG_URL;     }     else     {         autoProxyOptions.dwFlags = WINHTTP_AUTOPROXY_AUTO_DETECT;         autoProxyOptions.dwAutoDetectFlags = WINHTTP_AUTO_DETECT_TYPE_DHCP | WINHTTP_AUTO_DETECT_TYPE_DNS_A;     }      // basic flags you almost always want     autoProxyOptions.fAutoLogonIfChallenged = TRUE;      // here we reset fAutoProxy in case an auto-proxy isn't actually     // configured for this url     fAutoProxy = WinHttpGetProxyForUrl( hiOpen, pwszUrl, &autoProxyOptions, &autoProxyInfo ); }  if ( fAutoProxy ) {     // set proxy options for libcurl based on autoProxyInfo } else {     if( ieProxyConfig.lpszProxy != NULL )     {         // IE has an explicit proxy. set proxy options for libcurl here         // based on ieProxyConfig         //         // note that sometimes IE gives just a single or double colon         // for proxy or bypass list, which means "no proxy"     }     else     {         // there is no auto proxy and no manually configured proxy     } } 
like image 83
JSBձոգչ Avatar answered Oct 08 '22 13:10

JSBձոգչ


Here is a complete code sample how to call WinHttpGetIEProxyConfigForCurrentUser method from winhttp.dll library in C#

[TestClass] public class UnitTest1 {     [StructLayout(LayoutKind.Sequential)]     public struct WinhttpCurrentUserIeProxyConfig     {         [MarshalAs(UnmanagedType.Bool)]         public bool AutoDetect;         [MarshalAs(UnmanagedType.LPWStr)]         public string AutoConfigUrl;         [MarshalAs(UnmanagedType.LPWStr)]         public string Proxy;         [MarshalAs(UnmanagedType.LPWStr)]         public string ProxyBypass;      }      [DllImport("winhttp.dll", SetLastError = true)]     static extern bool WinHttpGetIEProxyConfigForCurrentUser(ref WinhttpCurrentUserIeProxyConfig pProxyConfig);      [TestMethod]     public void TestMethod1()     {         var config = new WinhttpCurrentUserIeProxyConfig();          WinHttpGetIEProxyConfigForCurrentUser(ref config);          Console.WriteLine(config.Proxy);         Console.WriteLine(config.AutoConfigUrl);         Console.WriteLine(config.AutoDetect);         Console.WriteLine(config.ProxyBypass);     } } 
like image 40
Maksym Kozlenko Avatar answered Oct 08 '22 11:10

Maksym Kozlenko