Skip to content

Commit

Permalink
Expanded proxy support, made it easier to set proxy settings (cefshar…
Browse files Browse the repository at this point in the history
…p#2007)

- Added a ProxyOptions class
- Added a CefSharpSettings.Proxy property
- Made ClientAdapter::GetAuthCredentials take care of the authentication for the proxy
  • Loading branch information
merceyz authored Apr 12, 2017
1 parent a683ead commit 8df4ec5
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 1 deletion.
10 changes: 10 additions & 0 deletions CefSharp.Core/Cef.h
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
6 changes: 6 additions & 0 deletions CefSharp.Core/Internals/ClientAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,12 @@ namespace CefSharp
bool ClientAdapter::GetAuthCredentials(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, bool isProxy,
const CefString& host, int port, const CefString& realm, const CefString& scheme, CefRefPtr<CefAuthCallback> 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)
Expand Down
1 change: 1 addition & 0 deletions CefSharp/CefSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@
<Compile Include="PdfPrintSettings.cs" />
<Compile Include="ICookieManager.cs" />
<Compile Include="IMenuModel.cs" />
<Compile Include="ProxyOptions.cs" />
<Compile Include="DefaultResourceHandlerFactoryItem.cs" />
<Compile Include="Structs\ViewRect.cs" />
<Compile Include="ISslInfo.cs" />
Expand Down
13 changes: 12 additions & 1 deletion CefSharp/CefSharpSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ static CefSharpSettings()

/// <summary>
/// Change the Close timeout for the WCF channel used by the sync JSB binding.
/// The default value is currently 2 seconds. Chaning this to <see cref="TimeSpan.Zero"/>
/// The default value is currently 2 seconds. Changing this to <see cref="TimeSpan.Zero"/>
/// will result on Abort() being called on the WCF Channel Host
/// </summary>
public static TimeSpan WcfTimeout { get; set; }
Expand All @@ -41,5 +41,16 @@ static CefSharpSettings()
/// the event handlers are hooked in the static constructor for the ChromiumWebBrowser class
/// </summary>
public static bool ShutdownOnExit { get; set; }

/// <summary>
/// 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
/// </summary>
public static ProxyOptions Proxy { get; set; }
}
}
57 changes: 57 additions & 0 deletions CefSharp/ProxyOptions.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// The IP address for the proxy
/// </summary>
public string IP { get; private set; }

/// <summary>
/// The port for the proxy
/// </summary>
public string Port { get; private set; }

/// <summary>
/// The username for authentication
/// </summary>
public string Username { get; set; }

/// <summary>
/// The password for authentication
/// </summary>
public string Password { get; set; }

/// <summary>
/// The list of domains that shouldn't be affected by the proxy, Format: example.com;example2.com
/// </summary>
public string BypassList { get; private set; }

/// <summary>
/// Checks if username and password is set
/// </summary>
/// <returns>Returns true if both username and password is set, otherwise false</returns>
public bool HasUsernameAndPassword()
{
return !string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password);
}

/// <param name="ip">The IP address for the proxy</param>
/// <param name="port">The port for the proxy</param>
/// <param name="username">The username required for authentication</param>
/// <param name="password">The password required for authentication</param>
/// <param name="bypassList">The list of domains that shouldn't be affected by the proxy, Format: example.com;example2.com</param>
public ProxyOptions(string ip, string port, string username = "", string password = "", string bypassList = "")
{
IP = ip;
Port = port;
Username = username;
Password = password;
BypassList = bypassList;
}
}
}

0 comments on commit 8df4ec5

Please sign in to comment.