Esempio n. 1
0
        private CDNClient BuildConnection(uint depotId, byte[] depotKey, CDNClient.Server serverSeed, CancellationToken token)
        {
            CDNClient.Server server = null;
            CDNClient        client = null;

            while (client == null)
            {
                // if we want to re-initialize a specific content server, try that one first
                if (serverSeed != null)
                {
                    server     = serverSeed;
                    serverSeed = null;
                }
                else
                {
                    if (availableServerEndpoints.Count < ServerEndpointMinimumSize)
                    {
                        populatePoolEvent.Set();
                    }

                    server = availableServerEndpoints.Take(token);
                }

                client = new CDNClient(steamSession.steamClient, steamSession.AppTickets[depotId]);

                string cdnAuthToken = null;

                if (server.Type == "CDN")
                {
                    steamSession.RequestCDNAuthToken(depotId, server.Host);
                    cdnAuthToken = steamSession.CDNAuthTokens[Tuple.Create(depotId, server.Host)].Token;
                }

                try
                {
                    client.Connect(server);
                    client.AuthenticateDepot(depotId, depotKey, cdnAuthToken);
                }
                catch (Exception ex)
                {
                    client = null;

                    Console.WriteLine("Failed to connect to content server {0}: {1}", server, ex.Message);

                    int penalty = 0;
                    ConfigStore.TheConfig.ContentServerPenalty.TryGetValue(server.Host, out penalty);
                    ConfigStore.TheConfig.ContentServerPenalty[server.Host] = penalty + 1;
                }
            }

            Console.WriteLine("Initialized connection to content server {0} with depot id {1}", server, depotId);

            activeClientAuthed[client] = Tuple.Create(depotId, server);
            return(client);
        }
Esempio n. 2
0
        private async Task <string> AuthenticateConnection(uint appId, uint depotId, CDNClient.Server server)
        {
            var host   = steamSession.ResolveCDNTopLevelHost(server.Host);
            var cdnKey = $"{depotId:D}:{host}";

            steamSession.RequestCDNAuthToken(appId, depotId, host, cdnKey);

            if (steamSession.CDNAuthTokens.TryGetValue(cdnKey, out var authTokenCallbackPromise))
            {
                var result = await authTokenCallbackPromise.Task;
                return(result.Token);
            }
            else
            {
                throw new Exception($"Failed to retrieve CDN token for server {server.Host} depot {depotId}");
            }
        }
Esempio n. 3
0
        private async Task <CDNClient> BuildConnectionAsync(uint appId, uint depotId, byte[] depotKey, CDNClient.Server serverSeed, CancellationToken token)
        {
            CDNClient.Server server = null;
            CDNClient        client = null;

            while (client == null)
            {
                // if we want to re-initialize a specific content server, try that one first
                if (serverSeed != null)
                {
                    server     = serverSeed;
                    serverSeed = null;
                }
                else
                {
                    if (availableServerEndpoints.Count < ServerEndpointMinimumSize)
                    {
                        populatePoolEvent.Set();
                    }

                    server = availableServerEndpoints.Take(token);
                }

                client = new CDNClient(steamSession.steamClient, steamSession.AppTickets[depotId]);

                string cdnAuthToken = null;

                try
                {
                    if (server.Type == "CDN" || server.Type == "SteamCache")
                    {
                        await steamSession.RequestCDNAuthToken(appId, depotId, server.Host);

                        var cdnKey = string.Format("{0:D}:{1}", depotId, steamSession.ResolveCDNTopLevelHost(server.Host));
                        SteamApps.CDNAuthTokenCallback authTokenCallback;

                        if (steamSession.CDNAuthTokens.TryGetValue(cdnKey, out authTokenCallback))
                        {
                            cdnAuthToken = authTokenCallback.Token;
                        }
                        else
                        {
                            throw new Exception(String.Format("Failed to retrieve CDN token for server {0} depot {1}", server.Host, depotId));
                        }
                    }

                    await client.ConnectAsync(server).ConfigureAwait(false);

                    //await client.ConnectAsync(server);
                    await client.AuthenticateDepotAsync(depotId, depotKey, cdnAuthToken).ConfigureAwait(false);

                    //await client.AuthenticateDepotAsync(depotId, depotKey, cdnAuthToken);
                }
                catch (Exception ex)
                {
                    client = null;

                    DebugLog.WriteLine("CDNClientPool", "Failed to connect to content server " + server + ": " + ex.Message);

                    int penalty = 0;
                    ConfigStore.TheConfig.ContentServerPenalty.TryGetValue(server.Host, out penalty);
                    ConfigStore.TheConfig.ContentServerPenalty[server.Host] = penalty + 1;
                }
            }

            DebugLog.WriteLine("CDNClientPool", "Initialized connection to content server " + server + " with depot id " + depotId);

            activeClientAuthed[client] = Tuple.Create(depotId, server);
            return(client);
        }
Esempio n. 4
0
        private async Task <CDNClient> BuildConnectionAsync(uint appId, uint depotId, byte[] depotKey, CDNClient.Server serverSeed, CancellationToken token)
        {
            CDNClient.Server server = null;
            CDNClient        client = null;

            while (client == null)
            {
                // if we want to re-initialize a specific content server, try that one first
                if (serverSeed != null)
                {
                    server     = serverSeed;
                    serverSeed = null;
                }
                else
                {
                    if (availableServerEndpoints.Count < ServerEndpointMinimumSize)
                    {
                        populatePoolEvent.Set();
                    }

                    server = availableServerEndpoints.Take(token);
                }

                client = new CDNClient(steamSession.steamClient, steamSession.AppTickets[depotId]);

                string cdnAuthToken = null;

                try
                {
                    if (DepotKeyStore.ContainsKey(depotId))
                    {
                        ((ConcurrentDictionary <uint, byte[]>)(typeof(CDNClient).GetField("depotKeys", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(client))).GetOrAdd(depotId, depotKey);
                        await client.ConnectAsync(server).ConfigureAwait(false);
                    }
                    else
                    {
                        if (server.Type == "CDN" || server.Type == "SteamCache")
                        {
                            steamSession.RequestCDNAuthToken(appId, depotId, server.Host);

                            var cdnKey = string.Format("{0:D}:{1}", depotId, steamSession.ResolveCDNTopLevelHost(server.Host));
                            SteamApps.CDNAuthTokenCallback authTokenCallback;

                            if (steamSession.CDNAuthTokens.TryGetValue(cdnKey, out authTokenCallback))
                            {
                                cdnAuthToken = authTokenCallback.Token;
                            }
                            else
                            {
                                throw new Exception(String.Format("Failed to retrieve CDN token for server {0} depot {1}", server.Host, depotId));
                            }
                        }
                        await client.ConnectAsync(server).ConfigureAwait(false);

                        await client.AuthenticateDepotAsync(depotId, depotKey, cdnAuthToken).ConfigureAwait(false);
                    }
                }
                catch (Exception ex)
                {
                    client = null;

                    Console.WriteLine("Failed to connect to content server {0}: {1}", server, ex.Message);

                    int penalty = 0;
                    AccountSettingsStore.Instance.ContentServerPenalty.TryGetValue(server.Host, out penalty);
                    AccountSettingsStore.Instance.ContentServerPenalty[server.Host] = penalty + 1;
                }
            }

            Console.WriteLine("Initialized connection to content server {0} with depot id {1}", server, depotId);

            activeClientAuthed[client] = Tuple.Create(depotId, server);
            return(client);
        }
        private static BlockingCollection <CDNClient> CollectCDNClientsForDepot(DepotDownloadInfo depot)
        {
            Console.WriteLine("Finding content servers...");

            var       cdnClients               = new BlockingCollection <CDNClient>();
            CDNClient initialClient            = new CDNClient(steam3.steamClient, steam3.AppTickets[depot.id]);
            List <CDNClient.Server> cdnServers = null;

            while (true)
            {
                try
                {
                    cdnServers = initialClient.FetchServerList(cellId: (uint)Config.CellID);
                    if (cdnServers != null)
                    {
                        break;
                    }
                }
                catch (WebException)
                {
                    Console.WriteLine("\nFailed to retrieve content server list.");
                    Thread.Sleep(500);
                }
            }

            if (cdnServers == null)
            {
                Console.WriteLine("\nUnable to query any content servers for depot {0} - {1}", depot.id, depot.contentName);
                return(cdnClients);
            }

            var weightedCdnServers = cdnServers.Select(x =>
            {
                int penalty = 0;
                ConfigStore.TheConfig.ContentServerPenalty.TryGetValue(x.Host, out penalty);

                return(Tuple.Create(x, penalty));
            }).OrderBy(x => x.Item2).ThenBy(x => x.Item1.WeightedLoad);

            // Grab up to the first eight server in the allegedly best-to-worst order from Steam
            Parallel.ForEach(weightedCdnServers, new ParallelOptions {
                MaxDegreeOfParallelism = 2
            }, (serverTuple, parallelLoop) =>
            {
                var server  = serverTuple.Item1;
                CDNClient c = new CDNClient(steam3.steamClient, steam3.AppTickets[depot.id]);

                try
                {
                    for (int i = 0; i < server.NumEntries; i++)
                    {
                        c.Connect(server);

                        string cdnAuthToken = null;
                        if (server.Type == "CDN")
                        {
                            steam3.RequestCDNAuthToken(depot.id, server.Host);
                            cdnAuthToken = steam3.CDNAuthTokens[Tuple.Create(depot.id, server.Host)].Token;
                        }

                        c.AuthenticateDepot(depot.id, depot.depotKey, cdnAuthToken);
                        cdnClients.Add(c);
                    }

                    if (cdnClients.Count >= Config.MaxServers)
                    {
                        parallelLoop.Stop();
                    }
                }
                catch (Exception ex)
                {
                    int penalty = 0;
                    ConfigStore.TheConfig.ContentServerPenalty.TryGetValue(server.Host, out penalty);
                    ConfigStore.TheConfig.ContentServerPenalty[server.Host] = penalty + 1;

                    Console.WriteLine("\nFailed to connect to content server {0}: {1}", server, ex.Message);
                }
            });

            if (cdnClients.Count == 0)
            {
                Console.WriteLine("\nUnable to find any content servers for depot {0} - {1}", depot.id, depot.contentName);
            }

            Config.MaxDownloads = Math.Min(Config.MaxDownloads, cdnClients.Count);

            return(cdnClients);
        }