Skip to content

Commit

Permalink
Making suggested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tpill90 committed Sep 6, 2024
1 parent 70c1fbb commit 442f608
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 39 deletions.
15 changes: 3 additions & 12 deletions SteamKit2/SteamKit2/Steam/CDN/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ public sealed class Client : IDisposable
/// </summary>
public static TimeSpan ResponseBodyTimeout { get; set; } = TimeSpan.FromSeconds( 60 );

//TODO comment
private static readonly SemaphoreSlim _lancacheDetectionLock = new SemaphoreSlim( 1, 1 );
private static bool CheckedForLancacheServer { get; set; }
private static bool LancacheDetected { get; set; }

Expand Down Expand Up @@ -232,20 +230,13 @@ public async Task<int> DownloadDepotChunkAsync( uint depotId, DepotManifest.Chun
}
}

await _lancacheDetectionLock.WaitAsync();
try
{
if ( !CheckedForLancacheServer )
{
LancacheDetected = await LancacheDetector.DetectLancacheServerAsync(httpClient);
}
}
finally
if ( !CheckedForLancacheServer )
{
LancacheDetected = LancacheDetector.DetectLancacheServer();
CheckedForLancacheServer = true;
_lancacheDetectionLock.Release();
}


var chunkID = Utils.EncodeHexString( chunk.ChunkID );
var url = $"depot/{depotId}/chunk/{chunkID}";

Expand Down
32 changes: 5 additions & 27 deletions SteamKit2/SteamKit2/Steam/CDN/LancacheDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
* file 'license.txt', which is part of this source code package.
*/

using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Sockets;
using System.Threading.Tasks;

namespace SteamKit2.Steam.CDN
{
Expand All @@ -22,38 +19,19 @@ public static class LancacheDetector
{
private static string TriggerDomain = "lancache.steamcontent.com";

public static async Task<bool> DetectLancacheServerAsync(HttpClient httpClient)
public static bool DetectLancacheServer()

Check failure on line 22 in SteamKit2/SteamKit2/Steam/CDN/LancacheDetector.cs

View workflow job for this annotation

GitHub Actions / Create NetHookAnalyzer2 Release

Missing XML comment for publicly visible type or member 'LancacheDetector.DetectLancacheServer()'

Check failure on line 22 in SteamKit2/SteamKit2/Steam/CDN/LancacheDetector.cs

View workflow job for this annotation

GitHub Actions / SteamKit2 on windows-latest - Debug (SDK 8.0.x)

Missing XML comment for publicly visible type or member 'LancacheDetector.DetectLancacheServer()'

Check failure on line 22 in SteamKit2/SteamKit2/Steam/CDN/LancacheDetector.cs

View workflow job for this annotation

GitHub Actions / SteamKit2 on windows-latest - Debug (SDK 8.0.x)

Missing XML comment for publicly visible type or member 'LancacheDetector.DetectLancacheServer()'

Check failure on line 22 in SteamKit2/SteamKit2/Steam/CDN/LancacheDetector.cs

View workflow job for this annotation

GitHub Actions / Create NuGet Package

Missing XML comment for publicly visible type or member 'LancacheDetector.DetectLancacheServer()'

Check failure on line 22 in SteamKit2/SteamKit2/Steam/CDN/LancacheDetector.cs

View workflow job for this annotation

GitHub Actions / SteamKit2 on windows-latest - Release (SDK 8.0.x)

Missing XML comment for publicly visible type or member 'LancacheDetector.DetectLancacheServer()'

Check failure on line 22 in SteamKit2/SteamKit2/Steam/CDN/LancacheDetector.cs

View workflow job for this annotation

GitHub Actions / SteamKit2 on windows-latest - Release (SDK 8.0.x)

Missing XML comment for publicly visible type or member 'LancacheDetector.DetectLancacheServer()'

Check failure on line 22 in SteamKit2/SteamKit2/Steam/CDN/LancacheDetector.cs

View workflow job for this annotation

GitHub Actions / SteamKit2 on macos-latest - Debug (SDK 8.0.x)

Missing XML comment for publicly visible type or member 'LancacheDetector.DetectLancacheServer()'

Check failure on line 22 in SteamKit2/SteamKit2/Steam/CDN/LancacheDetector.cs

View workflow job for this annotation

GitHub Actions / SteamKit2 on macos-latest - Debug (SDK 8.0.x)

Missing XML comment for publicly visible type or member 'LancacheDetector.DetectLancacheServer()'

Check failure on line 22 in SteamKit2/SteamKit2/Steam/CDN/LancacheDetector.cs

View workflow job for this annotation

GitHub Actions / SteamKit2 on macos-latest - Release (SDK 8.0.x)

Missing XML comment for publicly visible type or member 'LancacheDetector.DetectLancacheServer()'

Check failure on line 22 in SteamKit2/SteamKit2/Steam/CDN/LancacheDetector.cs

View workflow job for this annotation

GitHub Actions / SteamKit2 on macos-latest - Release (SDK 8.0.x)

Missing XML comment for publicly visible type or member 'LancacheDetector.DetectLancacheServer()'

Check failure on line 22 in SteamKit2/SteamKit2/Steam/CDN/LancacheDetector.cs

View workflow job for this annotation

GitHub Actions / SteamKit2 on macos-14 - Debug (SDK 8.0.x)

Missing XML comment for publicly visible type or member 'LancacheDetector.DetectLancacheServer()'

Check failure on line 22 in SteamKit2/SteamKit2/Steam/CDN/LancacheDetector.cs

View workflow job for this annotation

GitHub Actions / SteamKit2 on macos-14 - Debug (SDK 8.0.x)

Missing XML comment for publicly visible type or member 'LancacheDetector.DetectLancacheServer()'

Check failure on line 22 in SteamKit2/SteamKit2/Steam/CDN/LancacheDetector.cs

View workflow job for this annotation

GitHub Actions / SteamKit2 on macos-14 - Release (SDK 8.0.x)

Missing XML comment for publicly visible type or member 'LancacheDetector.DetectLancacheServer()'

Check failure on line 22 in SteamKit2/SteamKit2/Steam/CDN/LancacheDetector.cs

View workflow job for this annotation

GitHub Actions / SteamKit2 on macos-14 - Release (SDK 8.0.x)

Missing XML comment for publicly visible type or member 'LancacheDetector.DetectLancacheServer()'

Check failure on line 22 in SteamKit2/SteamKit2/Steam/CDN/LancacheDetector.cs

View workflow job for this annotation

GitHub Actions / SteamKit2 on ubuntu-latest - Debug (SDK 8.0.x)

Missing XML comment for publicly visible type or member 'LancacheDetector.DetectLancacheServer()'

Check failure on line 22 in SteamKit2/SteamKit2/Steam/CDN/LancacheDetector.cs

View workflow job for this annotation

GitHub Actions / SteamKit2 on ubuntu-latest - Debug (SDK 8.0.x)

Missing XML comment for publicly visible type or member 'LancacheDetector.DetectLancacheServer()'

Check failure on line 22 in SteamKit2/SteamKit2/Steam/CDN/LancacheDetector.cs

View workflow job for this annotation

GitHub Actions / SteamKit2 on ubuntu-latest - Release (SDK 8.0.x)

Missing XML comment for publicly visible type or member 'LancacheDetector.DetectLancacheServer()'

Check failure on line 22 in SteamKit2/SteamKit2/Steam/CDN/LancacheDetector.cs

View workflow job for this annotation

GitHub Actions / SteamKit2 on ubuntu-latest - Release (SDK 8.0.x)

Missing XML comment for publicly visible type or member 'LancacheDetector.DetectLancacheServer()'
{
// Gets a list of ipv4 addresses, Lancache cannot use ipv6 currently
var ipAddresses = (await Dns.GetHostAddressesAsync( TriggerDomain ) )
var ipAddresses = Dns.GetHostAddresses( TriggerDomain )
.Where(e => e.AddressFamily == AddressFamily.InterNetwork)
.ToArray();

// If there are no private IPs, then there can't be a Lancache instance. Lancache's IP must resolve to an RFC 1918 address
if (!ipAddresses.Any(e => e.IsPrivateAddress()))
if (ipAddresses.Any(e => IsPrivateAddress( e ) ))
{
return false;
return true;
}

// DNS hostnames can possibly resolve to more than one IP address (one-to-many), so we must check each one for a Lancache server
foreach (var ip in ipAddresses)
{
try
{
// If the IP resolves to a private subnet, then we want to query the Lancache server to see if it is actually there.
// Requests that are served from the cache will have an additional header.
var response = await httpClient.GetAsync(new Uri($"http://{ip}/lancache-heartbeat"));
if (response.Headers.Contains("X-LanCache-Processed-By"))
{
Console.WriteLine($"Enabling local content cache at '{ip}' from lookup of lancache.steamcontent.com.");
return true;
}
}
catch (Exception e) when (e is HttpRequestException | e is TaskCanceledException)
{
// Target machine refused connection errors are to be expected if there is no Lancache at that IP address.
}
}
return false;
}

Expand All @@ -62,7 +40,7 @@ public static async Task<bool> DetectLancacheServerAsync(HttpClient httpClient)
/// </summary>
/// <param name="toTest">The IP address that will be tested</param>
/// <returns>Returns true if the IP is a private address, false if it isn't private</returns>
private static bool IsPrivateAddress( this IPAddress toTest )
private static bool IsPrivateAddress( IPAddress toTest )
{
if ( IPAddress.IsLoopback( toTest ) )
{
Expand Down

0 comments on commit 442f608

Please sign in to comment.