diff --git a/SteamKit2/SteamKit2/Steam/CDN/Client.cs b/SteamKit2/SteamKit2/Steam/CDN/Client.cs index d3a0c23fa..4ecd8981b 100644 --- a/SteamKit2/SteamKit2/Steam/CDN/Client.cs +++ b/SteamKit2/SteamKit2/Steam/CDN/Client.cs @@ -30,8 +30,6 @@ public sealed class Client : IDisposable /// 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; } @@ -232,20 +230,13 @@ public async Task 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}"; diff --git a/SteamKit2/SteamKit2/Steam/CDN/LancacheDetector.cs b/SteamKit2/SteamKit2/Steam/CDN/LancacheDetector.cs index 62ce73ea7..2f4bcb400 100644 --- a/SteamKit2/SteamKit2/Steam/CDN/LancacheDetector.cs +++ b/SteamKit2/SteamKit2/Steam/CDN/LancacheDetector.cs @@ -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 { @@ -22,38 +19,19 @@ public static class LancacheDetector { private static string TriggerDomain = "lancache.steamcontent.com"; - public static async Task DetectLancacheServerAsync(HttpClient httpClient) + public static bool 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; } @@ -62,7 +40,7 @@ public static async Task DetectLancacheServerAsync(HttpClient httpClient) /// /// The IP address that will be tested /// Returns true if the IP is a private address, false if it isn't private - private static bool IsPrivateAddress( this IPAddress toTest ) + private static bool IsPrivateAddress( IPAddress toTest ) { if ( IPAddress.IsLoopback( toTest ) ) {