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); }
async private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex < 0) { return; } try { ConfigItem item = datagridConfigs.Rows[e.RowIndex].DataBoundItem as ConfigItem; if (item != null) { if (item.URL == "") { var callback = await steamWorkshop.RequestInfo(241100, item.Details.publishedfileid); var itemInfo = callback.Items.FirstOrDefault(); var ticket = await steamClient.GetHandler <SteamApps>().GetAppOwnershipTicket(241100); var decryptKey = await steamClient.GetHandler <SteamApps>().GetDepotDecryptionKey(241100, 241100); var cdn = new CDNClient(steamClient, ticket.Ticket); var servers = cdn.FetchServerList(); cdn.Connect(servers.First()); cdn.AuthenticateDepot(241100, decryptKey.DepotKey); var manifest = cdn.DownloadManifest(241100, itemInfo.ManifestID); manifest.DecryptFilenames(decryptKey.DepotKey); if (manifest.Files.First().TotalSize == 0) { MessageBox.Show("Steam Refused Download Request"); return; } var chunk = cdn.DownloadDepotChunk(241100, manifest.Files.First().Chunks.First()); if (saveFileDialog1.ShowDialog() == DialogResult.OK) { using (var wc = new WebClient()) { using (var io = saveFileDialog1.OpenFile()) { io.Write(chunk.Data, 0, chunk.Data.Length); MessageBox.Show("Download Done!"); } } } } else { if (saveFileDialog1.ShowDialog() == DialogResult.OK) { using (var wc = new WebClient()) { wc.DownloadFile(new Uri(item.URL), saveFileDialog1.FileName); MessageBox.Show("Download Done!"); } } } } } catch (TaskCanceledException) { MessageBox.Show("Timeout! This can happen if you're using anonymous account and trying to download."); } catch (Exception ex) { MessageBox.Show($"Error Downloading Config: {ex.ToString()}"); } }
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); }
private static void DownloadSteam3(int depotId, ulong depot_manifest, byte[] depotKey, string installDir) { Console.Write("Finding content servers..."); List <IPEndPoint> serverList = steam3.steamClient.GetServersOfType(EServerType.CS); List <CDNClient.ClientEndPoint> cdnServers = null; int tries = 0, counterDeferred = 0; for (int i = 0; ; i++) { IPEndPoint endpoint = serverList[i % serverList.Count]; cdnServers = CDNClient.FetchServerList(new CDNClient.ClientEndPoint(endpoint.Address.ToString(), endpoint.Port), Config.CellID); if (cdnServers == null) { counterDeferred++; } if (cdnServers != null && cdnServers.Count((ep) => { return(ep.Type == "CS"); }) > 0) { break; } if (((i + 1) % serverList.Count) == 0) { if (++tries > MAX_CONNECT_RETRIES) { Console.WriteLine("\nGiving up finding Steam3 content server."); return; } Console.Write("\nSearching for content servers... (deferred: {0})", counterDeferred); counterDeferred = 0; Thread.Sleep(1000); } } if (cdnServers == null || cdnServers.Count == 0) { Console.WriteLine("Unable to find any Steam3 content servers"); return; } Console.WriteLine(" Done!"); Console.Write("Downloading depot manifest..."); List <CDNClient.ClientEndPoint> cdnEndpoints = cdnServers.Where((ep) => { return(ep.Type == "CDN"); }).ToList(); List <CDNClient.ClientEndPoint> csEndpoints = cdnServers.Where((ep) => { return(ep.Type == "CS"); }).ToList(); CDNClient cdnClient = null; foreach (var server in csEndpoints) { CDNClient client = new CDNClient(server, steam3.AppTickets[(uint)depotId]); if (client.Connect()) { cdnClient = client; break; } } if (cdnClient == null) { Console.WriteLine("\nCould not initialize connection with CDN."); return; } DepotManifest depotManifest = cdnClient.DownloadDepotManifest(depotId, depot_manifest); if (depotManifest == null) { Console.WriteLine("\nUnable to download manifest {0} for depot {1}", depot_manifest, depotId); return; } if (!depotManifest.DecryptFilenames(depotKey)) { Console.WriteLine("\nUnable to decrypt manifest for depot {0}", depotId); return; } Console.WriteLine(" Done!"); ulong complete_download_size = 0; ulong size_downloaded = 0; depotManifest.Files.RemoveAll((x) => !TestIsFileIncluded(x.FileName)); depotManifest.Files.Sort((x, y) => { return(x.FileName.CompareTo(y.FileName)); }); foreach (var file in depotManifest.Files) { complete_download_size += file.TotalSize; } foreach (var file in depotManifest.Files) { string download_path = Path.Combine(installDir, file.FileName); if (file.Flags.HasFlag(EDepotFileFlag.Directory)) { if (!Directory.Exists(download_path)) { Directory.CreateDirectory(download_path); } continue; } string dir_path = Path.GetDirectoryName(download_path); if (!Directory.Exists(dir_path)) { Directory.CreateDirectory(dir_path); } // TODO: non-checksum validation FileInfo fi = new FileInfo(download_path); if (fi.Exists && (ulong)fi.Length == file.TotalSize) { size_downloaded += file.TotalSize; Console.WriteLine("{0,6:#00.00}% {1}", ((float)size_downloaded / (float)complete_download_size) * 100.0f, download_path); continue; } Console.Write("{0,6:#00.00}% {1}", ((float)size_downloaded / (float)complete_download_size) * 100.0f, download_path); FileStream fs = File.Create(download_path); fs.SetLength((long)file.TotalSize); foreach (var chunk in file.Chunks) { string chunkID = EncodeHexString(chunk.ChunkID); byte[] encrypted_chunk = cdnClient.DownloadDepotChunk(depotId, chunkID); byte[] chunk_data = CDNClient.ProcessChunk(encrypted_chunk, depotKey); fs.Seek((long)chunk.Offset, SeekOrigin.Begin); fs.Write(chunk_data, 0, chunk_data.Length); size_downloaded += chunk.UncompressedLength; Console.CursorLeft = 0; Console.Write("{0,6:#00.00}%", ((float)size_downloaded / (float)complete_download_size) * 100.0f); } Console.WriteLine(); } }