Example #1
0
        public static bool Set(WebProxy wProxy)
        {
            var bReturn = false;
            var info    = new INTERNET_PROXY_INFO();
            var sPrx    = string.Empty;

            if (wProxy != null)
            {
                sPrx = string.Format("{0}:{1}", wProxy.Address.DnsSafeHost, wProxy.Address.Port);
                info.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
            }
            else
            {
                info.dwAccessType = INTERNET_OPEN_TYPE_DIRECT;
            }
            info.lpszProxy       = Marshal.StringToHGlobalAnsi(sPrx);
            info.lpszProxyBypass = Marshal.StringToHGlobalAnsi("rado.ra-host.com");

            var intptrStruct = Marshal.AllocCoTaskMem(Marshal.SizeOf(info));

            Marshal.StructureToPtr(info, intptrStruct, true);
            bReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY, intptrStruct, Marshal.SizeOf(info));
            if (!bReturn)
            {
                //    Tools.SendErrorToBD(MessageType.FatalError, MethodBase.GetCurrentMethod().Name, string.Format("{0}\r\n", GetLastError()));
            }

            bReturn = InternetSetOption(IntPtr.Zero, (int)MyOptions.INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);
            if (!bReturn)
            {
                //   Tools.SendErrorToBD(MessageType.FatalError, MethodBase.GetCurrentMethod().Name, string.Format("{0}\r\n", GetLastError()));
            }
            Marshal.FreeCoTaskMem(intptrStruct);
            return(bReturn);
        }
Example #2
0
        private static unsafe bool SetSystemProxy(string strProxy)
        {
            const int INTERNET_OPTION_PROXY     = 38;
            const int INTERNET_OPEN_TYPE_PROXY  = 3;
            const int INTERNET_OPEN_TYPE_DIRECT = 1;

            int   length = strProxy?.Length ?? 0;
            byte *proxy  = stackalloc byte[length + 1];

            if (length > 0)
                fixed(char *ptrProxy = strProxy)
                for (int i = 0; i < length; i++)
                {
                    proxy[i] = (byte)ptrProxy[i];
                }
            proxy[length] = 0;
            byte *bypass = stackalloc byte[] { (byte)'l', (byte)'o', (byte)'c', (byte)'a', (byte)'l', 0 };

            var struct_IPI = new INTERNET_PROXY_INFO
            {
                dwAccessType = length == 0 ? INTERNET_OPEN_TYPE_DIRECT : INTERNET_OPEN_TYPE_PROXY,
                proxy        = proxy,
                proxyBypass  = length == 0 ? null : bypass
            };

            return(InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY, &struct_IPI, sizeof(INTERNET_PROXY_INFO)));
        }

        #endregion
    }
Example #3
0
        public void SetProxyServer(string strProxy)
        {
            const int INTERNET_OPTION_PROXY     = 38;
            const int INTERNET_OPEN_TYPE_PROXY  = 3;
            const int INTERNET_OPEN_TYPE_DIRECT = 1;

            INTERNET_PROXY_INFO ProxyInfo = new INTERNET_PROXY_INFO();

            // Filling in structure
            if (strProxy == null)
            {
                ProxyInfo.dwAccessType = INTERNET_OPEN_TYPE_DIRECT;
            }
            else
            {
                strProxy = Regex.Replace(strProxy, "( |^0|(?<=[.])0{1,2})|(?<=:)0{1,4}", ""); //На всякий случай обрабатываю IP убираю 0 в начале каждого сегмента
                ProxyInfo.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
                ProxyInfo.proxy        = Marshal.StringToHGlobalAnsi(strProxy);
                ProxyInfo.proxyBypass  = Marshal.StringToHGlobalAnsi("local");
            }
            // Allocating memory
            IntPtr ProxyInfoPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(ProxyInfo));

            // Converting structure to IntPtr
            Marshal.StructureToPtr(ProxyInfo, ProxyInfoPtr, true);
            bool iReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY, ProxyInfoPtr, Marshal.SizeOf(ProxyInfo));
        }
Example #4
0
 public static void SetProxyInProcess(string sProxy, string sBypassList)
 {
     INTERNET_PROXY_INFO structure = new INTERNET_PROXY_INFO {
         dwAccessType = 3,
         lpszProxy = sProxy,
         lpszProxyBypass = sBypassList
     };
     uint dwLen = (uint) Marshal.SizeOf(structure);
     UrlMkSetSessionOptionProxy(0x26, structure, dwLen, 0);
 }
Example #5
0
        public static void SetProxyDisabledForProcess()
        {
            INTERNET_PROXY_INFO structure = new INTERNET_PROXY_INFO();

            structure.dwAccessType = 1;
            structure.lpszProxy    = (string)(structure.lpszProxyBypass = null);
            uint dwLen = (uint)Marshal.SizeOf(structure);

            UrlMkSetSessionOptionProxy(0x26, structure, dwLen, 0);
        }
Example #6
0
 public static void SetProxyDisabledForProcess()
 {
     INTERNET_PROXY_INFO internet_proxy_info;
     internet_proxy_info = new INTERNET_PROXY_INFO {
         dwAccessType = 1,
         lpszProxy = null,
         lpszProxyBypass = null
     };
     uint dwLen = (uint) Marshal.SizeOf(internet_proxy_info);
     UrlMkSetSessionOptionProxy(0x26, internet_proxy_info, dwLen, 0);
 }
Example #7
0
        public static void SetProxyInProcess(string sProxy, string sBypassList)
        {
            INTERNET_PROXY_INFO structure = new INTERNET_PROXY_INFO();

            structure.dwAccessType    = 3;
            structure.lpszProxy       = sProxy;
            structure.lpszProxyBypass = sBypassList;
            uint dwLen = (uint)Marshal.SizeOf(structure);

            UrlMkSetSessionOptionProxy(0x26, structure, dwLen, 0);
        }
Example #8
0
        /// <summary>
        /// urlmon.dllでプロセス内プロキシ設定を適用。
        /// </summary>
        /// <param name="proxy">プロキシサーバー</param>
        /// <param name="proxyBypass">バイパスリスト</param>
        public static void SetProxyInProcess(string proxy, string proxyBypass)
        {
            var proxyInfo = new INTERNET_PROXY_INFO {
                dwAccessType    = INTERNET_OPEN_TYPE.INTERNET_OPEN_TYPE_PROXY,
                lpszProxy       = proxy,
                lpszProxyBypass = proxyBypass,
            };
            var dwBufferLength = (uint)Marshal.SizeOf(proxyInfo);

            NativeMethods.UrlMkSetSessionOption(INTERNET_OPTION.INTERNET_OPTION_PROXY, proxyInfo, dwBufferLength, 0U);
        }
Example #9
0
        private void ClearProxy()
        {
            INTERNET_PROXY_INFO structure = new INTERNET_PROXY_INFO();

            try
            {
                structure.dwAccessType = 1;
                IntPtr ptr = Marshal.AllocCoTaskMem(Marshal.SizeOf(structure));
                Marshal.StructureToPtr(structure, ptr, true);
                InternetSetOption(IntPtr.Zero, 0x26, ptr, Marshal.SizeOf(structure));
            }
            catch (Exception)
            {
            }
        }
        public static INTERNET_PROXY_INFO GetProxy()
        {
            INTERNET_PROXY_INFO proxyInfo = new INTERNET_PROXY_INFO();

            int       bufferLength           = 0;
            IntPtr    buffer                 = IntPtr.Zero;
            Exception proxyModifierException = null;

            InternetQueryOption(IntPtr.Zero, INTERNET_OPTION_PROXY, IntPtr.Zero,
                                ref bufferLength);
            try
            {
                buffer = Marshal.AllocHGlobal(bufferLength);
                if (InternetQueryOption(IntPtr.Zero, INTERNET_OPTION_PROXY, buffer,
                                        ref bufferLength))
                {
                    // Getting the proxy details.
                    proxyInfo = (INTERNET_PROXY_INFO)
                                // Converting structure to IntPtr.
                                Marshal.PtrToStructure(buffer, typeof(INTERNET_PROXY_INFO));
                }
                else
                {
                    throw proxyModifierException = new Win32Exception();
                }
            }
            catch (Exception e)
            {
                proxyModifierException = e;
            }
            finally
            {
                if (buffer != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(buffer);
                }
            }

            if (proxyModifierException != null)
            {
                throw proxyModifierException;
            }

            return(proxyInfo);
        }
Example #11
0
        public static void SetProxyInProcess(string proxy, string proxyBypass)
        {
            IsCustomProxyEverEnabledInThisSession = true;

            var proxyInfo = new INTERNET_PROXY_INFO
            {
                dwAccessType    = INTERNET_OPEN_TYPE.INTERNET_OPEN_TYPE_PROXY,
                lpszProxy       = proxy,
                lpszProxyBypass = proxyBypass,
            };
            var dwBufferLength = (uint)Marshal.SizeOf(proxyInfo);
            var result         = UrlMkSetSessionOption(INTERNET_OPTION.INTERNET_OPTION_PROXY, proxyInfo, dwBufferLength, 0U);

            if (result != 0)
            {
                var errorCode = Marshal.GetLastWin32Error();
                logger.Warn($"UrlMkSetSessionOption failed with error code {errorCode}.");
            }
        }
Example #12
0
 public static bool Set(WebProxy wProxy)
 {
     try
     {
         bool bReturn             = false;
         INTERNET_PROXY_INFO info = new INTERNET_PROXY_INFO();
         string sPrx = "";
         if (wProxy != null)
         {
             sPrx = wProxy.Address.DnsSafeHost + ":" + wProxy.Address.Port;
             info.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
         }
         else
         {
             info.dwAccessType = INTERNET_OPEN_TYPE_DIRECT;
         }
         info.lpszProxy       = Marshal.StringToHGlobalAnsi(sPrx);
         info.lpszProxyBypass = Marshal.StringToHGlobalAnsi("rado.ra-host.com");
         // Allocating memory
         IntPtr intptrStruct = Marshal.AllocCoTaskMem(Marshal.SizeOf(info));
         // Converting structure to IntPtr
         Marshal.StructureToPtr(info, intptrStruct, true);
         bReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY, intptrStruct, Marshal.SizeOf(info));
         if (!bReturn)
         {
             Console.WriteLine(GetLastError());
         }
         //InternetSetOption(IntPtr.Zero, 75, intptrStruct, Marshal.SizeOf(struct_IPI));
         bReturn = InternetSetOption(IntPtr.Zero, (int)MyOptions.INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);
         if (!bReturn)
         {
             Console.WriteLine(GetLastError());
         }
         Marshal.FreeCoTaskMem(intptrStruct);
         return(bReturn);
     }
     catch (Exception)
     {
         throw;
     }
 }
Example #13
0
 public static bool Set(WebProxy wProxy)
 {
     try
     {
         bool bReturn = false;
         INTERNET_PROXY_INFO info = new INTERNET_PROXY_INFO();
         string sPrx = "";
         if (wProxy != null)
         {
             sPrx = wProxy.Address.DnsSafeHost + ":" + wProxy.Address.Port;
             info.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
         }
         else
         {
             info.dwAccessType = INTERNET_OPEN_TYPE_DIRECT;
         }
         info.lpszProxy = Marshal.StringToHGlobalAnsi(sPrx);
         info.lpszProxyBypass = Marshal.StringToHGlobalAnsi("rado.ra-host.com");
         // Allocating memory
         IntPtr intptrStruct = Marshal.AllocCoTaskMem(Marshal.SizeOf(info));
         // Converting structure to IntPtr
         Marshal.StructureToPtr(info, intptrStruct, true);
         bReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY, intptrStruct, Marshal.SizeOf(info));
         if (!bReturn)
         {
             Console.WriteLine(GetLastError());
         }
         //InternetSetOption(IntPtr.Zero, 75, intptrStruct, Marshal.SizeOf(struct_IPI));
         bReturn = InternetSetOption(IntPtr.Zero, (int)MyOptions.INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);
         if (!bReturn)
         {
             Console.WriteLine(GetLastError());
         }
         Marshal.FreeCoTaskMem(intptrStruct);
         return bReturn;
     }
     catch (Exception)
     {
         throw;
     }
 }
Example #14
0
        /// <summary>
        /// 设置浏览器代理, 地址为空字符串直接访问
        /// </summary>
        /// <param name="ProxyAddress"></param>
        /// <param name="BypassList"></param>
        static public void SetSessionProxy(string ProxyAddress, string BypassList)
        {
            uint dwAccessType = 0x3;

            if (string.IsNullOrEmpty(ProxyAddress))
            {
                dwAccessType = 0x1;
            }
            var proxyInfo = new INTERNET_PROXY_INFO
            {
                dwAccessType    = dwAccessType,
                lpszProxy       = ProxyAddress,
                lpszProxyBypass = BypassList
            };
            int        structSize = Marshal.SizeOf(proxyInfo);
            const uint SetProxy   = 0x26;

            if (UrlMkSetSessionOption(SetProxy, proxyInfo, (uint)structSize, 0) != 0)
            {
                throw new Win32Exception();
            }
        }
Example #15
0
        private void SetProxyServer(string proxy)
        {
            //Create structure
            INTERNET_PROXY_INFO proxyInfo = new INTERNET_PROXY_INFO();

            if (proxy == null)
            {
                proxyInfo.dwAccessType = INTERNET_OPEN_TYPE_DIRECT;
            }
            else
            {
                proxyInfo.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
                proxyInfo.proxy        = Marshal.StringToHGlobalAnsi(proxy);
                proxyInfo.proxyBypass  = Marshal.StringToHGlobalAnsi("local");
            }

            // Allocate memory
            IntPtr proxyInfoPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(proxyInfo));

            // Convert structure to IntPtr
            Marshal.StructureToPtr(proxyInfo, proxyInfoPtr, true);
            bool returnValue = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY,
                                                 proxyInfoPtr, Marshal.SizeOf(proxyInfo));
        }
 private static extern int UrlMkSetSessionOption(uint dwOption, INTERNET_PROXY_INFO structNewProxy, uint dwLen, uint dwZero);
Example #17
0
        private static void RefreshProxySettings(string strProxy)
        {
            const int INTERNET_OPTION_PROXY = 38;

            // const int INTERNET_OPEN_TYPE_PRECONFIG = 0;  // IE setting
            const int INTERNET_OPEN_TYPE_DIRECT = 1; // Direct
            const int INTERNET_OPEN_TYPE_PROXY  = 3; // Custom

            INTERNET_PROXY_INFO ipi = default(INTERNET_PROXY_INFO);

            // Filling in structure
            if (!string.IsNullOrEmpty(strProxy))
            {
                ipi.DwAccessType = INTERNET_OPEN_TYPE_PROXY;
                ipi.Proxy        = Marshal.StringToHGlobalAnsi(strProxy);
                ipi.ProxyBypass  = Marshal.StringToHGlobalAnsi("local");
            }
            else if (strProxy == null)
            {
                // IE Default
                IWebProxy p = WebRequest.GetSystemWebProxy();
                if (p.IsBypassed(new Uri("http://www.google.com/")))
                {
                    ipi.DwAccessType = INTERNET_OPEN_TYPE_DIRECT;
                    ipi.Proxy        = IntPtr.Zero;
                    ipi.ProxyBypass  = IntPtr.Zero;
                }
                else
                {
                    ipi.DwAccessType = INTERNET_OPEN_TYPE_PROXY;
                    ipi.Proxy        = Marshal.StringToHGlobalAnsi(p.GetProxy(new Uri("http://www.google.com/")).Authority);
                    ipi.ProxyBypass  = Marshal.StringToHGlobalAnsi("local");
                }
            }
            else
            {
                ipi.DwAccessType = INTERNET_OPEN_TYPE_DIRECT;
                ipi.Proxy        = IntPtr.Zero;
                ipi.ProxyBypass  = IntPtr.Zero;
            }

            try
            {
                // Allocating memory
                IntPtr pIpi = Marshal.AllocCoTaskMem(Marshal.SizeOf(ipi));
                if (pIpi.Equals(IntPtr.Zero))
                {
                    return;
                }

                try
                {
                    // Converting structure to IntPtr
                    Marshal.StructureToPtr(ipi, pIpi, true);
                    bool ret = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY, pIpi, Marshal.SizeOf(ipi));
                    if (!ret)
                    {
                        Debug.WriteLine("Proxy set failed.");
                    }
                }
                finally
                {
                    Marshal.FreeCoTaskMem(pIpi);
                }
            }
            finally
            {
                if (ipi.Proxy != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(ipi.Proxy);
                }

                if (ipi.ProxyBypass != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(ipi.ProxyBypass);
                }
            }
        }
Example #18
0
 private static extern int UrlMkSetSessionOptionProxy(uint dwOption, INTERNET_PROXY_INFO structNewProxy, uint dwLen, uint dwZero);
Example #19
0
        private void btnGetProxy_Click(object sender, EventArgs e)
        {
            int    bufferLength = 0;
            IntPtr buffer       = IntPtr.Zero;

            InternetQueryOption(IntPtr.Zero, INTERNET_OPTION_PROXY, IntPtr.Zero,
                                ref bufferLength);
            try
            {
                buffer = Marshal.AllocHGlobal(bufferLength);
                if (InternetQueryOption(IntPtr.Zero, INTERNET_OPTION_PROXY, buffer,
                                        ref bufferLength))
                {
                    INTERNET_PROXY_INFO proxyInfo = (INTERNET_PROXY_INFO)
                                                    // Converting structure to IntPtr.
                                                    Marshal.PtrToStructure(buffer, typeof(INTERNET_PROXY_INFO));
                    // Getting the proxy details.
                    switch (proxyInfo.dwAccessType.ToString())
                    {
                    case "INTERNET_OPEN_TYPE_PRECONFIG":
                        cmbAccessType.SelectedIndex = 0;
                        break;

                    case "INTERNET_OPEN_TYPE_DIRECT":
                        cmbAccessType.SelectedIndex = 1;
                        break;

                    case "INTERNET_OPEN_TYPE_PROXY":
                        cmbAccessType.SelectedIndex = 2;
                        break;

                    default:
                        break;
                    }
                    tbProxyServer.Text = proxyInfo.lpszProxy;
                    tbProxyByPass.Text = proxyInfo.lpszProxyBypass;
                    if (Registry.GetValue(RegistryKeyPath, "ProxyEnable", "").
                        ToString() == "1")
                    {
                        cmbProxyStatusInfo.SelectedIndex = 1;
                    }
                    else
                    {
                        cmbProxyStatusInfo.SelectedIndex = 0;
                    }
                    MessageBox.Show("Internet Explorer proxy settings has been retrieved.",
                                    "Proxy Modifier", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    throw new Win32Exception();
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "Proxy Modifier", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
            finally
            {
                if (buffer != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(buffer);
                }
            }
        }
        private void SetSessionProxy(string ProxyAddress, string BypassList)
        {
            var proxyInfo = new INTERNET_PROXY_INFO
            {
                dwAccessType = 0x3,
                lpszProxy = ProxyAddress,
                lpszProxyBypass = BypassList
            };
            uint structSize = (uint)Marshal.SizeOf(proxyInfo);
            const uint SetProxy = 0x26;

            if (UrlMkSetSessionOption(SetProxy, proxyInfo, structSize, 0) != 0)
                throw new Win32Exception();
        }
Example #21
0
 internal static extern int UrlMkSetSessionOption(
     INTERNET_OPTION dwOption,
     INTERNET_PROXY_INFO pBuffer,
     uint dwBufferLength,
     uint dwReserved);