Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proxy support for WebSocket connections #717

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions SteamKit2/SteamKit2/Networking/Steam3/WebSocketConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ partial class WebSocketConnection : IConnection

public ProtocolTypes ProtocolTypes => ProtocolTypes.WebSocket;

public void Connect(EndPoint endPoint, int timeout = 5000)
public void Connect(EndPoint endPoint, int timeout = 5000) => Connect(endPoint, null, timeout);

public void Connect(EndPoint endPoint, IWebProxy proxy, int timeout = 5000)
{
var newContext = new WebSocketContext(this, endPoint);
var newContext = new WebSocketContext(this, endPoint, proxy);
var oldContext = Interlocked.Exchange(ref currentContext, newContext);
if (oldContext != null)
{
Expand Down
6 changes: 5 additions & 1 deletion SteamKit2/SteamKit2/Networking/Steam3/WebSocketContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ partial class WebSocketConnection : IConnection
{
class WebSocketContext : IDisposable
{
public WebSocketContext(WebSocketConnection connection, EndPoint endPoint)
public WebSocketContext(WebSocketConnection connection, EndPoint endPoint, IWebProxy proxy = null)
{
this.connection = connection;
EndPoint = endPoint;

cts = new CancellationTokenSource();
socket = new ClientWebSocket();
if (proxy != null) {
socket.Options.Proxy = proxy;
}

hostAndPort = GetHostAndPort(endPoint);
}

Expand Down
18 changes: 15 additions & 3 deletions SteamKit2/SteamKit2/Steam/CMClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ public CMClient( SteamConfiguration configuration )
/// The <see cref="IPEndPoint"/> of the CM server to connect to.
/// If <c>null</c>, SteamKit will randomly select a CM server from its internal list.
/// </param>
public void Connect( ServerRecord cmServer = null )
/// <param name="proxy">
/// The proxy server that should be used to connect to CM server (only WebSocketConnection is supported).
/// </param>
public void Connect( ServerRecord cmServer = null, WebProxy proxy = null )
{
lock ( connectionLock )
{
Expand Down Expand Up @@ -179,9 +182,18 @@ public void Connect( ServerRecord cmServer = null )
connection.NetMsgReceived += NetMsgReceived;
connection.Connected += Connected;
connection.Disconnected += Disconnected;
connection.Connect( record.EndPoint, ( int )ConnectionTimeout.TotalMilliseconds );

IWebProxy webProxy = proxy ?? Configuration.WebProxy;
if ( ( webProxy != null ) && connection is WebSocketConnection socketConnection)
{
socketConnection.Connect(record.EndPoint, webProxy, (int) ConnectionTimeout.TotalMilliseconds);
}
else
{
connection.Connect(record.EndPoint, (int) ConnectionTimeout.TotalMilliseconds);
}
}, TaskContinuationOptions.ExecuteSynchronously ).ContinueWith( t =>
{
{
if ( t.IsFaulted )
{
DebugLog.WriteLine( nameof( CMClient ), "Unhandled exception when attempting to connect to Steam: {0}", t.Exception );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@


using System;
using System.Net;
using SteamKit2.Discovery;

namespace SteamKit2
Expand Down Expand Up @@ -89,11 +90,16 @@ internal static SteamConfiguration CreateDefault()
public Uri WebAPIBaseAddress => state.WebAPIBaseAddress;

/// <summary>
/// An API key to be used for authorized requests.
/// An API key to be used for authorized requests.
/// Keys can be obtained from https://steamcommunity.com/dev or the Steamworks Partner site.
/// </summary>
public string WebAPIKey => state.WebAPIKey;

/// <summary>
/// A proxy server to use for WebSocket requests.
/// </summary>
public IWebProxy WebProxy => state.WebProxy;

/// <summary>
/// The server list used for this configuration.
/// If this configuration is used by multiple <see cref="SteamClient"/> instances, they all share the server list.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@


using System;
using System.Net;
using SteamKit2.Discovery;

namespace SteamKit2
Expand Down Expand Up @@ -35,7 +36,9 @@ public static SteamConfigurationState CreateDefaultState()

Universe = EUniverse.Public,

WebAPIBaseAddress = WebAPI.DefaultBaseAddress
WebAPIBaseAddress = WebAPI.DefaultBaseAddress,

WebProxy = null
};
}

Expand Down Expand Up @@ -97,5 +100,11 @@ public ISteamConfigurationBuilder WithWebAPIKey(string webApiKey)
state.WebAPIKey = webApiKey ?? throw new ArgumentNullException(nameof(webApiKey));
return this;
}

public ISteamConfigurationBuilder WithWebProxy(IWebProxy webProxy)
{
state.WebProxy = webProxy;
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@


using System;
using System.Net;
using SteamKit2.Discovery;

namespace SteamKit2
Expand All @@ -19,6 +20,7 @@ struct SteamConfigurationState
public IServerListProvider ServerListProvider;
public EUniverse Universe;
public Uri WebAPIBaseAddress;
public IWebProxy WebProxy;
public string WebAPIKey;
}
}