Ejemplo n.º 1
0
 static WinINet()
 {
     try
     {
         initialSetting = Query();
     }
     catch (DllNotFoundException)
     {
         operational = false;
         // Not on windows
         logger.Info("You are not running on Windows platform, system proxy will disable");
     }
     catch (Win32Exception we)
     {
         if (we.NativeErrorCode == 12178)
         {
             logger.Warn("WPAD service is not running, system proxy will disable");
             // WPAD not running
         }
         else
         {
             throw;
         }
     }
     finally
     {
         if (initialSetting == null)
         {
             initialSetting = new();
         }
     }
 }
Ejemplo n.º 2
0
        public static void Set(WinINetSetting setting)
        {
            List <InternetPerConnectionOption> options = new List <InternetPerConnectionOption>
            {
                GetOption(InternetPerConnectionOptionEnum.Flags, setting.Flags),
                GetOption(InternetPerConnectionOptionEnum.ProxyServer, setting.ProxyServer),
                GetOption(InternetPerConnectionOptionEnum.ProxyBypass, setting.ProxyBypass),
                GetOption(InternetPerConnectionOptionEnum.AutoConfigUrl, setting.AutoConfigUrl),
            };

            Exec(options);
        }
Ejemplo n.º 3
0
        public static WinINetSetting Query()
        {
            if (!operational)
            {
                return(new WinINetSetting());
            }

            List <InternetPerConnectionOption> options = new List <InternetPerConnectionOption>
            {
                new InternetPerConnectionOption {
                    dwOption = (int)InternetPerConnectionOptionEnum.FlagsUI
                },
                new InternetPerConnectionOption {
                    dwOption = (int)InternetPerConnectionOptionEnum.ProxyServer
                },
                new InternetPerConnectionOption {
                    dwOption = (int)InternetPerConnectionOptionEnum.ProxyBypass
                },
                new InternetPerConnectionOption {
                    dwOption = (int)InternetPerConnectionOptionEnum.AutoConfigUrl
                },
            };

            (IntPtr unmanagedList, int listSize) = PrepareOptionList(options, null);
            bool ok = InternetQueryOption(IntPtr.Zero, (int)InternetOptions.PerConnectionOption, unmanagedList, ref listSize);

            if (!ok)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            WinINetSetting proxy = new WinINetSetting();

            InternetPerConnectionOptionList ret = Marshal.PtrToStructure <InternetPerConnectionOptionList>(unmanagedList);
            IntPtr p       = ret.pOptions;
            int    nOption = ret.OptionCount;
            List <InternetPerConnectionOption> outOptions = new List <InternetPerConnectionOption>();

            for (int i = 0; i < nOption; i++)
            {
                InternetPerConnectionOption o = Marshal.PtrToStructure <InternetPerConnectionOption>(p);
                outOptions.Add(o);
                p += Marshal.SizeOf(o);
            }

            foreach (InternetPerConnectionOption o in outOptions)
            {
                switch ((InternetPerConnectionOptionEnum)o.dwOption)
                {
                case InternetPerConnectionOptionEnum.FlagsUI:
                case InternetPerConnectionOptionEnum.Flags:
                    proxy.Flags = (InternetPerConnectionFlags)o.Value.dwValue;
                    break;

                case InternetPerConnectionOptionEnum.AutoConfigUrl:
                    proxy.AutoConfigUrl = Marshal.PtrToStringAuto(o.Value.pszValue);
                    break;

                case InternetPerConnectionOptionEnum.ProxyBypass:
                    proxy.ProxyBypass = Marshal.PtrToStringAuto(o.Value.pszValue);
                    break;

                case InternetPerConnectionOptionEnum.ProxyServer:
                    proxy.ProxyServer = Marshal.PtrToStringAuto(o.Value.pszValue);
                    break;

                default:
                    break;
                }
            }
            return(proxy);
        }