Internet属性 - 读取配置脚本路径

时间:2018-05-09 15:22:31

标签: proxy cefsharp pac

我想阅读"使用自动配置脚本"地址栏。

我需要像这样设置CefSharp的代理

settings.CefCommandLineArgs.Add("proxy-pac-url","proxy address");

我尝试过不同的WebRequest.GetSystemWebProxy变种,并在wininet.dll中调用函数InternetQueryOption。

来自Github上的CefSharp仓库的代码

 public static class ProxyConfig
{
    [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern bool InternetQueryOption(IntPtr hInternet, uint dwOption, IntPtr lpBuffer, ref int lpdwBufferLength);

    private const uint InternetOptionProxy = 38;

    public static InternetProxyInfo GetProxyInformation()
    {
        var bufferLength = 0;
        InternetQueryOption(IntPtr.Zero, InternetOptionProxy, IntPtr.Zero, ref bufferLength);
        var buffer = IntPtr.Zero;

        try
        {
            buffer = Marshal.AllocHGlobal(bufferLength);

            if (InternetQueryOption(IntPtr.Zero, InternetOptionProxy, buffer, ref bufferLength))
            {
                var ipi = (InternetProxyInfo)Marshal.PtrToStructure(buffer, typeof(InternetProxyInfo));
                return ipi;
            }
            {
                throw new Win32Exception();
            }
        }
        finally
        {
            if (buffer != IntPtr.Zero)
            {
                Marshal.FreeHGlobal(buffer);
            }
        }
    }
}

如果在Windows设置中有代理,则此代码有效,易于使用Fiddler进行测试。

我可以从注册表中读取值,但感觉就像是黑客

  RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", false);
  var autoConfigUrl = registry?.GetValue("AutoConfigURL");

必须有一个正确的"这样做的方法?

当前测试代码:

if (settingsViewModel.UseProxy)
        {
            // https://securelink.be/blog/windows-proxy-settings-explained/
            RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", false);
            var autoConfigUrl = registry?.GetValue("AutoConfigURL").ToString();
            var proxyEnable = registry?.GetValue("ProxyEnable").ToString();
            if (!string.IsNullOrEmpty(autoConfigUrl) && !string.IsNullOrEmpty(proxyEnable) && proxyEnable == "1")
            {
                settings.CefCommandLineArgs.Add("proxy-pac-url", autoConfigUrl);
            }
            else
            {
                var proxy = ProxyConfig.GetProxyInformation();
                switch (proxy.AccessType)
                {
                    case InternetOpenType.Direct:
                        {
                            //Don't use a proxy server, always make direct connections.
                            settings.CefCommandLineArgs.Add("no-proxy-server", "1");
                            break;
                        }
                    case InternetOpenType.Proxy:
                        {
                            settings.CefCommandLineArgs.Add("proxy-server", proxy.ProxyAddress.Replace(' ', ';'));
                            break;
                        }
                    case InternetOpenType.PreConfig:
                        {
                            settings.CefCommandLineArgs.Add("proxy-auto-detect", "1");
                            break;
                        }
                }
            }
        }

enter image description here

0 个答案:

没有答案