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 all 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 @@ -17,9 +17,11 @@ partial class WebSocketConnection : IConnection
public EndPoint CurrentEndPoint { get; set; }
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 @@ -147,7 +147,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 @@ -193,9 +196,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 System.Net.Http;
using SteamKit2.Discovery;

Expand Down Expand Up @@ -103,11 +104,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 System.Net.Http;
using System.Net.Http.Headers;
using SteamKit2.Discovery;
Expand Down Expand Up @@ -39,7 +40,9 @@ public static SteamConfigurationState CreateDefaultState()

Universe = EUniverse.Public,

WebAPIBaseAddress = WebAPI.DefaultBaseAddress
WebAPIBaseAddress = WebAPI.DefaultBaseAddress,

WebProxy = null
};
}

Expand Down Expand Up @@ -108,6 +111,12 @@ public ISteamConfigurationBuilder WithWebAPIKey(string webApiKey)
return this;
}

public ISteamConfigurationBuilder WithWebProxy(IWebProxy webProxy)
{
state.WebProxy = webProxy;
return this;
}

static HttpClient DefaultHttpClientFactory()
{
var client = new HttpClient();
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 All @@ -20,6 +21,7 @@ struct SteamConfigurationState
public IServerListProvider ServerListProvider;
public EUniverse Universe;
public Uri WebAPIBaseAddress;
public IWebProxy WebProxy;
public string WebAPIKey;
}
}