diff --git a/CefSharp.Core/Cef.h b/CefSharp.Core/Cef.h index c8c4cbe07..0b9b371bd 100644 --- a/CefSharp.Core/Cef.h +++ b/CefSharp.Core/Cef.h @@ -182,6 +182,16 @@ namespace CefSharp DependencyChecker::AssertAllDependenciesPresent(cefSettings->Locale, cefSettings->LocalesDirPath, cefSettings->ResourcesDirPath, cefSettings->PackLoadingDisabled, cefSettings->BrowserSubprocessPath); } + if (CefSharpSettings::Proxy != nullptr && !cefSettings->CommandLineArgsDisabled) + { + cefSettings->CefCommandLineArgs->Add("proxy-server", CefSharpSettings::Proxy->IP + ":" + CefSharpSettings::Proxy->Port); + + if (!String::IsNullOrEmpty(CefSharpSettings::Proxy->BypassList)) + { + cefSettings->CefCommandLineArgs->Add("proxy-bypass-list", CefSharpSettings::Proxy->BypassList); + } + } + UIThreadTaskFactory = gcnew TaskFactory(gcnew CefTaskScheduler(TID_UI)); IOThreadTaskFactory = gcnew TaskFactory(gcnew CefTaskScheduler(TID_IO)); FileThreadTaskFactory = gcnew TaskFactory(gcnew CefTaskScheduler(TID_FILE)); diff --git a/CefSharp.Core/Internals/ClientAdapter.cpp b/CefSharp.Core/Internals/ClientAdapter.cpp index 9679ddfc7..16241c5a3 100644 --- a/CefSharp.Core/Internals/ClientAdapter.cpp +++ b/CefSharp.Core/Internals/ClientAdapter.cpp @@ -732,6 +732,12 @@ namespace CefSharp bool ClientAdapter::GetAuthCredentials(CefRefPtr browser, CefRefPtr frame, bool isProxy, const CefString& host, int port, const CefString& realm, const CefString& scheme, CefRefPtr callback) { + if (isProxy && CefSharpSettings::Proxy != nullptr && CefSharpSettings::Proxy->IP == StringUtils::ToClr(host) && CefSharpSettings::Proxy->HasUsernameAndPassword()) + { + callback->Continue(StringUtils::ToNative(CefSharpSettings::Proxy->Username), StringUtils::ToNative(CefSharpSettings::Proxy->Password)); + return true; + } + auto handler = _browserControl->RequestHandler; if (handler == nullptr) diff --git a/CefSharp/CefSharp.csproj b/CefSharp/CefSharp.csproj index ac8afc60c..40aae9912 100644 --- a/CefSharp/CefSharp.csproj +++ b/CefSharp/CefSharp.csproj @@ -199,6 +199,7 @@ + diff --git a/CefSharp/CefSharpSettings.cs b/CefSharp/CefSharpSettings.cs index 34c3957be..7ede8752a 100644 --- a/CefSharp/CefSharpSettings.cs +++ b/CefSharp/CefSharpSettings.cs @@ -29,7 +29,7 @@ namespace CefSharp /// /// Change the Close timeout for the WCF channel used by the sync JSB binding. - /// The default value is currently 2 seconds. Chaning this to + /// The default value is currently 2 seconds. Changing this to /// will result on Abort() being called on the WCF Channel Host /// public static TimeSpan WcfTimeout { get; set; } @@ -41,5 +41,16 @@ namespace CefSharp /// the event handlers are hooked in the static constructor for the ChromiumWebBrowser class /// public static bool ShutdownOnExit { get; set; } + + /// + /// The proxy options that will be used for all connections + /// + /// If set before the call to Cef.Initialize, command line arguments will be set for you + /// If a username and password is provided and the IPs match authentication is done automatically + /// + /// NOTE: GetAuthCredentials won't be called for a proxy server that matches the IP + /// NOTE: It isn't possble to change the proxy after the call to Cef.Initialize + /// + public static ProxyOptions Proxy { get; set; } } } diff --git a/CefSharp/ProxyOptions.cs b/CefSharp/ProxyOptions.cs new file mode 100644 index 000000000..59621ab40 --- /dev/null +++ b/CefSharp/ProxyOptions.cs @@ -0,0 +1,57 @@ +// Copyright © 2010-2017 The CefSharp Authors. All rights reserved. +// +// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. + +namespace CefSharp +{ + public class ProxyOptions + { + /// + /// The IP address for the proxy + /// + public string IP { get; private set; } + + /// + /// The port for the proxy + /// + public string Port { get; private set; } + + /// + /// The username for authentication + /// + public string Username { get; set; } + + /// + /// The password for authentication + /// + public string Password { get; set; } + + /// + /// The list of domains that shouldn't be affected by the proxy, Format: example.com;example2.com + /// + public string BypassList { get; private set; } + + /// + /// Checks if username and password is set + /// + /// Returns true if both username and password is set, otherwise false + public bool HasUsernameAndPassword() + { + return !string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password); + } + + /// The IP address for the proxy + /// The port for the proxy + /// The username required for authentication + /// The password required for authentication + /// The list of domains that shouldn't be affected by the proxy, Format: example.com;example2.com + public ProxyOptions(string ip, string port, string username = "", string password = "", string bypassList = "") + { + IP = ip; + Port = port; + Username = username; + Password = password; + BypassList = bypassList; + } + } +}