Added Prototype Lancache code/debug logging

pull/477/head
minecraftitsover90@gmail.com 2 years ago
parent 9d2446404a
commit 0b58932a30

@ -2,6 +2,7 @@ using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using SteamKit2.CDN; using SteamKit2.CDN;
@ -20,12 +21,12 @@ namespace DepotDownloader
public Client CDNClient { get; } public Client CDNClient { get; }
public Server ProxyServer { get; private set; } public Server ProxyServer { get; private set; }
private readonly ConcurrentStack<Server> activeConnectionPool = []; private readonly ConcurrentStack<Server> activeConnectionPool = new ConcurrentStack<Server>();
private readonly BlockingCollection<Server> availableServerEndpoints = []; private readonly BlockingCollection<Server> availableServerEndpoints = new BlockingCollection<Server>();
private readonly AutoResetEvent populatePoolEvent = new(true); private readonly AutoResetEvent populatePoolEvent = new AutoResetEvent(true);
private readonly Task monitorTask; private readonly Task monitorTask;
private readonly CancellationTokenSource shutdownToken = new(); private readonly CancellationTokenSource shutdownToken = new CancellationTokenSource();
public CancellationTokenSource ExhaustedToken { get; set; } public CancellationTokenSource ExhaustedToken { get; set; }
public CDNClientPool(Steam3Session steamSession, uint appId) public CDNClientPool(Steam3Session steamSession, uint appId)
@ -34,7 +35,7 @@ namespace DepotDownloader
this.appId = appId; this.appId = appId;
CDNClient = new Client(steamSession.steamClient); CDNClient = new Client(steamSession.steamClient);
monitorTask = Task.Factory.StartNew(ConnectionPoolMonitorAsync).Unwrap(); monitorTask = Task.Factory.StartNew(ConnectionPoolMonitorAsync, TaskCreationOptions.LongRunning);
} }
public void Shutdown() public void Shutdown()
@ -47,19 +48,37 @@ namespace DepotDownloader
{ {
try try
{ {
var cdnServers = await this.steamSession.steamContent.GetServersForSteamPipe(); var cdnServers = await steamSession.steamContent.GetServersForSteamPipe().ConfigureAwait(false);
if (cdnServers != null)
{
return cdnServers; return cdnServers;
} }
}
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine("Failed to retrieve content server list: {0}", ex.Message); Console.WriteLine("Failed to retrieve content server list: {0}", ex.Message);
return null;
}
} }
private async Task<IPAddress> ResolveLancacheIpAsync(string hostname)
{
try
{
var hostEntry = await Dns.GetHostEntryAsync(hostname).ConfigureAwait(false);
return hostEntry.AddressList.FirstOrDefault(ip => IsPrivateIp(ip));
}
catch (Exception ex)
{
Console.WriteLine("Failed to resolve Lancache IP: {0}", ex.Message);
return null; return null;
} }
}
private bool IsPrivateIp(IPAddress ip)
{
byte[] bytes = ip.GetAddressBytes();
return bytes[0] == 10 ||
(bytes[0] == 172 && bytes[1] >= 16 && bytes[1] <= 31) ||
(bytes[0] == 192 && bytes[1] == 168);
}
private async Task ConnectionPoolMonitorAsync() private async Task ConnectionPoolMonitorAsync()
{ {
@ -69,7 +88,6 @@ namespace DepotDownloader
{ {
populatePoolEvent.WaitOne(TimeSpan.FromSeconds(1)); populatePoolEvent.WaitOne(TimeSpan.FromSeconds(1));
// We want the Steam session so we can take the CellID from the session and pass it through to the ContentServer Directory Service
if (availableServerEndpoints.Count < ServerEndpointMinimumSize && steamSession.steamClient.IsConnected) if (availableServerEndpoints.Count < ServerEndpointMinimumSize && steamSession.steamClient.IsConnected)
{ {
var servers = await FetchBootstrapServerListAsync().ConfigureAwait(false); var servers = await FetchBootstrapServerListAsync().ConfigureAwait(false);
@ -80,29 +98,42 @@ namespace DepotDownloader
return; return;
} }
ProxyServer = servers.Where(x => x.UseAsProxy).FirstOrDefault(); ProxyServer = servers.FirstOrDefault(x => x.UseAsProxy);
var weightedCdnServers = servers foreach (var server in servers)
.Where(server =>
{ {
var isEligibleForApp = server.AllowedAppIds.Length == 0 || server.AllowedAppIds.Contains(appId); if (server.Type == "SteamCache")
return isEligibleForApp && (server.Type == "SteamCache" || server.Type == "CDN");
})
.Select(server =>
{ {
AccountSettingsStore.Instance.ContentServerPenalty.TryGetValue(server.Host, out var penalty); var lancacheIp = await ResolveLancacheIpAsync(server.Host).ConfigureAwait(false);
if (lancacheIp != null)
return (server, penalty); {
}) var lancacheServer = new Server
.OrderBy(pair => pair.penalty).ThenBy(pair => pair.server.WeightedLoad); {
Host = lancacheIp.ToString(),
Type = server.Type,
NumEntries = server.NumEntries,
WeightedLoad = server.WeightedLoad,
AllowedAppIds = server.AllowedAppIds.ToArray()
};
foreach (var (server, weight) in weightedCdnServers) // Downgrade connection to HTTP if Lancache server is found
lancacheServer.Protocol = Server.ConnectionProtocol.HTTP;
Console.WriteLine($"Found Lancache Server: {lancacheServer.Host}. Downgrading connection to HTTP.");
availableServerEndpoints.Add(lancacheServer);
}
}
else
{
var isEligibleForApp = server.AllowedAppIds.Length == 0 || server.AllowedAppIds.Contains(appId);
if (isEligibleForApp && (server.Type == "SteamCache" || server.Type == "CDN"))
{ {
for (var i = 0; i < server.NumEntries; i++) for (var i = 0; i < server.NumEntries; i++)
{ {
availableServerEndpoints.Add(server); availableServerEndpoints.Add(server);
} }
} }
}
}
didPopulate = true; didPopulate = true;
} }

@ -20,13 +20,16 @@ namespace DepotDownloader
static async Task<int> MainAsync(string[] args) static async Task<int> MainAsync(string[] args)
{ {
// Add the SteamKitLogger as a listener to DebugLog
DebugLog.AddListener(new SteamKitLogger());
if (args.Length == 0) if (args.Length == 0)
{ {
PrintUsage(); PrintUsage();
return 1; return 1;
} }
DebugLog.Enabled = false; DebugLog.Enabled = true;
AccountSettingsStore.LoadFromFile("account.config"); AccountSettingsStore.LoadFromFile("account.config");

@ -0,0 +1,14 @@
using SteamKit2;
using System;
public class SteamKitLogger : IDebugListener
{
public void WriteLine(string category, string msg)
{
Console.WriteLine("[{0}] {1}", category, msg);
}
}
public interface ILog
{
}
Loading…
Cancel
Save