Skip to content

Commit

Permalink
Include protocol and vhost for proper HTTPS support in CDNClient.
Browse files Browse the repository at this point in the history
  • Loading branch information
azuisleet committed Dec 1, 2017
1 parent 517d052 commit 49e857b
Showing 1 changed file with 40 additions and 5 deletions.
45 changes: 40 additions & 5 deletions SteamKit2/SteamKit2/Steam/CDNClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,33 @@ public sealed class CDNClient : IDisposable
/// </summary>
public sealed class Server
{
/// The protocol used to connect to this server
/// </summary>
public enum ConnectionProtocol
{
/// <summary>
/// Server does not advertise HTTPS support, connect over HTTP
/// </summary>
HTTP = 0,
/// <summary>
/// Server advertises it supports HTTPS, connection made over HTTPS
/// </summary>
HTTPS = 1
}

/// <summary>
/// Gets the supported connection protocol of the server.
/// </summary>
public ConnectionProtocol Protocol { get; internal set; }
/// <summary>
/// Gets the hostname of the server.
/// </summary>
public string Host { get; internal set; }
/// <summary>
/// Gets the virtual hostname of the server.
/// </summary>
public string VHost { get; internal set; }
/// <summary>
/// Gets the port of the server.
/// </summary>
public int Port { get; internal set; }
Expand Down Expand Up @@ -70,7 +92,9 @@ public static implicit operator Server( IPEndPoint endPoint )
{
return new Server
{
Protocol = endPoint.Port == 443 ? ConnectionProtocol.HTTPS : ConnectionProtocol.HTTP,
Host = endPoint.Address.ToString(),
VHost = endPoint.Address.ToString(),
Port = endPoint.Port,
};
}
Expand All @@ -86,7 +110,9 @@ public static implicit operator Server( DnsEndPoint endPoint )
{
return new Server
{
Protocol = endPoint.Port == 443 ? ConnectionProtocol.HTTPS : ConnectionProtocol.HTTP,
Host = endPoint.Host,
VHost = endPoint.Host,
Port = endPoint.Port,
};
}
Expand Down Expand Up @@ -273,6 +299,7 @@ public async Task<IList<Server>> FetchServerListAsync( IPEndPoint csServer = nul
{
string type = server[ "type" ].AsString();
string host = server[ "host" ].AsString();
string vhost = server[ "vhost" ].AsString();

string[] hostSplits = host.Split( ':' );

Expand All @@ -291,17 +318,22 @@ public async Task<IList<Server>> FetchServerListAsync( IPEndPoint csServer = nul
int weightedLoad = server[ "weightedload" ].AsInteger();
int entries = server[ "NumEntriesInClientList" ].AsInteger( 1 );
int useTokenAuth = server[ "usetokenauth" ].AsInteger();
string httpsSupport = server[ "https_support" ].AsString();

// If usetokenauth is specified, we can treat this server as a CDN and request tokens
if ( useTokenAuth > 0 )
{
type = "CDN";
}

Server.ConnectionProtocol protocol = ( httpsSupport == "optional" || httpsSupport == "mandatory" ) ? Server.ConnectionProtocol.HTTPS : Server.ConnectionProtocol.HTTP;

serverList.Add( new Server
{
Protocol = protocol,
Host = host,
Port = port,
VHost = vhost,
Port = protocol == Server.ConnectionProtocol.HTTPS ? 443 : port,

Type = type,

Expand Down Expand Up @@ -437,7 +469,9 @@ public async Task<DepotManifest> DownloadManifestAsync( uint depotId, ulong mani
{
var server = new Server
{
Protocol = Server.ConnectionProtocol.HTTP,
Host = host,
VHost = host,
Port = 80
};

Expand Down Expand Up @@ -528,7 +562,9 @@ public async Task<DepotChunk> DownloadDepotChunkAsync( uint depotId, DepotManife

var server = new Server
{
Protocol = Server.ConnectionProtocol.HTTP,
Host = host,
VHost = host,
Port = 80
};

Expand All @@ -545,7 +581,8 @@ public void Dispose()

string BuildCommand( Server server, string command, string args, string authtoken = null )
{
return string.Format( "http://{0}:{1}/{2}/{3}{4}", server.Host, server.Port, command, args, authtoken ?? "" );
string protocol = server.Protocol == Server.ConnectionProtocol.HTTP ? "http" : "https";
return string.Format( "{0}://{1}:{2}/{3}/{4}{5}", protocol, server.VHost, server.Port, command, args, authtoken ?? "" );
}

async Task<byte[]> DoRawCommandAsync( Server server, HttpMethod method, string command, string data = null, bool doAuth = false, string args = "", string authtoken = null )
Expand Down Expand Up @@ -584,15 +621,13 @@ async Task<byte[]> DoRawCommandAsync( Server server, HttpMethod method, string c
request.Content.Headers.ContentType = new MediaTypeHeaderValue( "application/x-www-form-urlencoded" );
}


HttpResponseMessage response;
using ( var cts = new CancellationTokenSource() )
{
cts.CancelAfter( RequestTimeout );

try
{
response = await httpClient.SendAsync( request, cts.Token ).ConfigureAwait( false );
var response = await httpClient.SendAsync( request, cts.Token ).ConfigureAwait( false );

var responseData = await response.Content.ReadAsByteArrayAsync().ConfigureAwait( false );
return responseData;
Expand Down

0 comments on commit 49e857b

Please sign in to comment.