Skip to content

Play your Steam library's Windows games on a jailbroken PS5.

C++

Name Steam download servers and preserve their order

Leonardo Lobatoddb098881c6b
diff --git a/src/steam/backend/cdn_client.cpp b/src/steam/backend/cdn_client.cpp
index 4d95f7c..164433b 100644
--- a/src/steam/backend/cdn_client.cpp
+++ b/src/steam/backend/cdn_client.cpp
@@ -27,6 +27,48 @@ bool ValidCdnHost(const std::string& host) {
});
}
+std::string CdnServerLabel(const std::string& host) {
+ std::string lower = host;
+ for (auto& c : lower) if (c >= 'A' && c <= 'Z') c += 'a' - 'A';
+ if (lower == "steampipe.akamaized.net") return "akamai";
+ if (lower == "fastly.cdn.steampipe.steamcontent.com") return "fastly";
+ if (lower == "alibaba.cdn.steampipe.steamcontent.com") return "alibaba";
+ if (lower == "edgenext.cdn.steampipe.steamcontent.com") return "edgenext";
+ const std::string suffix = ".cdn.steampipe.steamcontent.com";
+ if (lower.size() > suffix.size() && lower.compare(lower.size() - suffix.size(), suffix.size(), suffix) == 0) {
+ const auto prefix = lower.substr(0, lower.size() - suffix.size());
+ if (prefix.compare(0, 6, "google") == 0 &&
+ std::all_of(prefix.begin() + 6, prefix.end(), [](char c) { return c >= '0' && c <= '9'; }))
+ return "google";
+ }
+ const std::string valve_suffix = ".steamcontent.com";
+ if (lower.size() > valve_suffix.size() &&
+ lower.compare(lower.size() - valve_suffix.size(), valve_suffix.size(), valve_suffix) == 0) return "valve";
+ return "cache";
+}
+
+std::string CdnServerDisplayName(const std::string& host) {
+ const auto label = CdnServerLabel(host);
+ if (label == "akamai") return "Akamai";
+ if (label == "fastly") return "Fastly";
+ if (label == "google") return "Google Cloud";
+ if (label == "alibaba") return "Alibaba Cloud";
+ if (label == "edgenext") return "EdgeNext";
+ if (label == "valve") return "Steam (Valve)";
+ return host;
+}
+
+ServerOrder PreferServer(std::vector<ContentServer>* servers, const std::string& preferred_host) {
+ if (!servers || preferred_host.empty()) return {};
+ const auto found = std::find_if(servers->begin(), servers->end(), [&](const ContentServer& server) {
+ return server.host == preferred_host;
+ });
+ if (found == servers->end()) return {};
+ const auto index = static_cast<std::size_t>(found - servers->begin());
+ std::rotate(servers->begin(), found, found + 1);
+ return {true, index};
+}
+
CdnClient::CdnClient(std::string ca_bundle) : ca_bundle_(std::move(ca_bundle)) {}
CdnClient::CdnClient(Perform perform) : perform_(std::move(perform)) {}
CdnClient::~CdnClient() = default;
@@ -91,6 +133,7 @@ CdnError CdnClient::Servers(std::uint32_t cell_id, const std::atomic<bool>& canc
const auto* vhost = server.Find("vhost");
const auto* https = server.Find("https_support");
const auto* load = server.Find("load");
+ const auto* priority = server.Find("priority_class");
if (!type || !type->is_string() || (type->text() != "SteamCache" && type->text() != "CDN")) continue;
if (!host || !host->is_string() || !ValidCdnHost(host->text())) continue;
ContentServer entry;
@@ -100,6 +143,9 @@ CdnError CdnClient::Servers(std::uint32_t cell_id, const std::atomic<bool>& canc
if (load) {
if (const auto value = load->AsUint64(); value && *value < 100000) entry.load = static_cast<int>(*value);
}
+ if (priority) {
+ if (const auto value = priority->AsUint64()) entry.priority = static_cast<int>(std::min<std::uint64_t>(*value, 9));
+ }
(entry.https ? usable : plain).push_back(std::move(entry));
}
// Plain HTTP only when the directory offers nothing better.
diff --git a/src/steam/backend/cdn_client.h b/src/steam/backend/cdn_client.h
index daf936f..3df2237 100644
--- a/src/steam/backend/cdn_client.h
+++ b/src/steam/backend/cdn_client.h
@@ -17,6 +17,7 @@ struct ContentServer {
std::string vhost; // the name used in URLs and TLS
int load = 100;
bool https = true; // false for a server the directory lists as plain HTTP only
+ int priority = 0; // directory priority_class, 0..9; does not affect Automatic order
};
struct CdnStats {
@@ -66,6 +67,17 @@ class CdnClient {
CdnStats stats_;
};
+// Fixed provider labels for logs; unknown hosts never appear in the returned label.
+std::string CdnServerLabel(const std::string& host);
+std::string CdnServerDisplayName(const std::string& host);
+
+struct ServerOrder {
+ bool preferred_found = false;
+ std::size_t preferred_index = 0; // original position of the first match
+};
+// Move the first matching host to the front, preserving every other entry's order.
+ServerOrder PreferServer(std::vector<ContentServer>* servers, const std::string& preferred_host);
+
// A host or vhost from the directory may only contain DNS name characters.
bool ValidCdnHost(const std::string& host);
diff --git a/tests/cdn_client_test.cpp b/tests/cdn_client_test.cpp
index 418274b..9d58721 100644
--- a/tests/cdn_client_test.cpp
+++ b/tests/cdn_client_test.cpp
@@ -15,6 +15,54 @@ HttpResponse Body(long status, const std::string& text) {
return response;
}
+void LabelsAndDisplayNames() {
+ struct Case { const char* host; const char* label; const char* display; };
+ for (const auto& test : std::vector<Case>{
+ {"steampipe.akamaized.net", "akamai", "Akamai"},
+ {"FASTLY.CDN.STEAMPIPE.STEAMCONTENT.COM", "fastly", "Fastly"},
+ {"google2.cdn.steampipe.steamcontent.com", "google", "Google Cloud"},
+ {"google3.cdn.steampipe.steamcontent.com", "google", "Google Cloud"},
+ {"alibaba.cdn.steampipe.steamcontent.com", "alibaba", "Alibaba Cloud"},
+ {"edgenext.cdn.steampipe.steamcontent.com", "edgenext", "EdgeNext"},
+ {"cache1.fra.steamcontent.com", "valve", "Steam (Valve)"},
+ {"isp.example", "cache", "isp.example"},
+ {"fastly.cdn.steampipe.steamcontent.com.evil.example", "cache",
+ "fastly.cdn.steampipe.steamcontent.com.evil.example"}}) {
+ assert(CdnServerLabel(test.host) == test.label);
+ assert(CdnServerDisplayName(test.host) == test.display);
+ }
+}
+
+void PreferredOrderKeepsOthersAndFirstDuplicate() {
+ std::vector<ContentServer> servers{{"a", "a"}, {"b", "first"}, {"c", "c"}, {"b", "second"}};
+ assert(!PreferServer(&servers, "").preferred_found && servers[0].host == "a");
+ assert(!PreferServer(&servers, "missing").preferred_found && servers[0].host == "a");
+ const auto order = PreferServer(&servers, "b");
+ assert(order.preferred_found && order.preferred_index == 1);
+ assert(servers[0].vhost == "first" && servers[1].host == "a" && servers[2].host == "c" && servers[3].vhost == "second");
+ assert(PreferServer(&servers, "b").preferred_index == 0);
+ servers.clear();
+ assert(!PreferServer(&servers, "a").preferred_found);
+}
+
+void PriorityIsBoundedWithoutReordering() {
+ CdnClient client([](const HttpRequest&, const std::atomic<bool>&) {
+ return Body(200, R"({"response":{"servers":[
+ {"type":"CDN","host":"a","load":0,"priority_class":8},
+ {"type":"CDN","host":"b","load":0,"priority_class":1},
+ {"type":"CDN","host":"c","load":0,"priority_class":999},
+ {"type":"CDN","host":"d","load":0,"priority_class":-1},
+ {"type":"CDN","host":"e","load":0,"priority_class":"2"},
+ {"type":"CDN","host":"f","load":0}]}})");
+ });
+ std::atomic<bool> cancel{false};
+ std::vector<ContentServer> servers;
+ assert(client.Servers(0, cancel, &servers) == CdnError::kNone);
+ assert(servers.size() == 6 && servers[0].host == "a" && servers[1].host == "b");
+ assert(servers[0].priority == 8 && servers[1].priority == 1 && servers[2].priority == 9);
+ assert(servers[3].priority == 0 && servers[4].priority == 0 && servers[5].priority == 0);
+}
+
void TotalsIncludeFailedRequests() {
int calls = 0;
CdnClient client([&](const HttpRequest&, const std::atomic<bool>&) {
@@ -134,6 +182,9 @@ void PlainHttpServerUrls() {
} // namespace
int main() {
+ LabelsAndDisplayNames();
+ PreferredOrderKeepsOthersAndFirstDuplicate();
+ PriorityIsBoundedWithoutReordering();
TotalsIncludeFailedRequests();
ServersFilterAndSortByLoad();
PlainHttpServersWhenNoHttpsIsListed();