Xbox Cloud Gaming app for PS5 Homebrew
C
TLS: Mbed TLS's own locking for concurrent requests; a failed GET is retried
Rafael Abud Ferreira6695e71ecb8e
diff --git a/cmake/deps/mbedtls_user_config.h b/cmake/deps/mbedtls_user_config.hindex f3d7f26..562cded 100644--- a/cmake/deps/mbedtls_user_config.h+++ b/cmake/deps/mbedtls_user_config.h@@ -3,3 +3,10 @@/* Appended to deps/mbedtls's default config (MBEDTLS_USER_CONFIG_FILE).* WebRTC needs the DTLS-SRTP extension (use_srtp), off by default. */#define MBEDTLS_SSL_DTLS_SRTP++/* The app makes HTTPS requests from several threads at once (lists, prices,+ * images) while WebRTC runs DTLS: the shared random generator and the PSA+ * crypto state (TLS 1.3) need Mbed TLS's own locking, or handshakes fail+ * with "This is a bug in the library" (-0x006E, CORRUPTION_DETECTED). */+#define MBEDTLS_THREADING_C+#define MBEDTLS_THREADING_PTHREADdiff --git a/src/app/cli_main.cpp b/src/app/cli_main.cppindex 471ef78..119321f 100644--- a/src/app/cli_main.cpp+++ b/src/app/cli_main.cpp@@ -36,6 +36,9 @@#include "stb_image_write.h"#include <atomic>+#include <thread>+#include <mutex>+#include <map>#include <cstdio>#include <cstdlib>#include <cstring>@@ -502,6 +505,37 @@ int main(int argc, char** argv) {platform::init();if (!net::initTls(platform::caBundlePath())) return 1;+ if (cmd == "tls-stress") {+ // Diagnostics: many HTTPS requests at once (public URLs, no account),+ // as the app makes them at start; prints how many failed and why.+ int threads = argi < argc ? std::atoi(argv[argi]) : 16, rounds = argi + 1 < argc ? std::atoi(argv[argi + 1]) : 4;+ const char* urls[] = {"https://catalog.gamepass.com/sigls/v2?id=af206485-e87d-4624-9007-cb7f6d0cc42e&market=US&language=en-us",+ "https://www.xbox.com/en-US/play", "https://store-images.s-microsoft.com/image/apps.28754.13851675759085158.74991af7-70f4-4aea-ad50-57bb8bc448c3.552869ac-6877-4457-90ec-ee9ce0701c50?w=240&h=240",+ "https://displaycatalog.mp.microsoft.com/v7.0/products?bigIds=9NBLGGH4R315&market=US&languages=en-us"};+ std::atomic<int> ok{0}, failed{0};+ std::mutex errorsMutex;+ std::map<std::string, int> errors;+ std::vector<std::thread> pool;+ for (int t = 0; t < threads; ++t)+ pool.emplace_back([&, t] {+ for (int r = 0; r < rounds; ++r) {+ net::Request req;+ req.url = urls[(t + r) % 4];+ auto resp = net::perform(req);+ if (resp.status > 0) {+ ++ok;+ } else {+ ++failed;+ std::lock_guard<std::mutex> lock(errorsMutex);+ ++errors[resp.error.substr(resp.error.find(':') == std::string::npos ? 0 : resp.error.find(':') + 2, 60)];+ }+ }+ });+ for (auto& th : pool) th.join();+ std::printf("tls-stress: %d threads x %d: %d answered, %d failed\n", threads, rounds, ok.load(), failed.load());+ for (const auto& [e, n] : errors) std::printf(" %3d x %s\n", n, e.c_str());+ return failed ? 1 : 0;+ }auth::AuthManager am(platform::dataDir() + "/account.json");int rc;diff --git a/src/net/http.cpp b/src/net/http.cppindex b138f38..dfb74b8 100644--- a/src/net/http.cpp+++ b/src/net/http.cpp@@ -193,7 +193,9 @@ void shutdownTls() {g.ready = false;}-Response perform(const Request& req) {+namespace {++Response performOnce(const Request& req) {Response resp;Url url;if (!Url::parse(req.url, url)) {@@ -335,6 +337,24 @@ Response perform(const Request& req) {return finish({});}+} // namespace++Response perform(const Request& req) {+ Response resp = performOnce(req);+ // A GET that got no answer at all (the connection or the handshake+ // failed, or timed out) is asked once more: they are seldom and+ // passing, and a GET can be repeated safely.+ if (resp.status == 0 && (req.method.empty() || req.method == "GET") && resp.error.rfind("bad URL", 0) != 0 &&+ resp.error.rfind("only https", 0) != 0 && resp.error != "TLS not initialised") {+ Url url;+ Url::parse(req.url, url);+ XC_LOGW("%s: %s; asking again", url.host.c_str(), resp.error.c_str());+ platform::sleepMs(300);+ resp = performOnce(req);+ }+ return resp;+}+std::string urlEncode(const std::string& s) {static const char* hex = "0123456789ABCDEF";std::string out;