From 583e4e752d56a3d45ab1228c9996f95461ebd22a Mon Sep 17 00:00:00 2001 From: faumaray Date: Wed, 24 Jun 2026 21:37:46 +0300 Subject: [PATCH] Ensure all outbound calls honor the proxy (fix YouTube API bypass) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two gaps let network calls escape the configured proxy: 1. NetworkClient::executeRequest read proxy_ without holding mutex_, while setProxy()/initialize() write it from other threads (init, config reload). A torn read of optional (it holds std::string host/user/pass) could yield an empty host -> enabled()==false -> a silent DIRECT connection. yt-dlp/ffmpeg snapshot their proxy under their own mutexes, so only the HTTP path (YouTube Data API, URL shortener) was affected — matching the observed "YouTube API without proxy". Snapshot proxy_ under the lock. 2. Core::reloadConfig updated only the config copy + command manager; it never re-applied the proxy to the network stack, so editing the proxy and running !reloadconfig left every client on the old/no proxy. Add MusicManager::applyNetworkConfig() to push the proxy to the NetworkClient, the yt-dlp subprocess, and the ffmpeg AudioDecoder at once, and call it from both reloadConfig and updateConfig (which previously touched only the HTTP client). reloadConfig now also syncs the shared ConfigManager. Co-Authored-By: Claude Opus 4.8 --- include/echoion/core/MusicManager.hpp | 7 +++++++ src/Core.cpp | 20 ++++++++++++++++-- src/MusicManager.cpp | 30 +++++++++++++++++++++++++++ src/NetworkClient.cpp | 15 ++++++++++++-- 4 files changed, 68 insertions(+), 4 deletions(-) diff --git a/include/echoion/core/MusicManager.hpp b/include/echoion/core/MusicManager.hpp index ce0c62d..559ec70 100644 --- a/include/echoion/core/MusicManager.hpp +++ b/include/echoion/core/MusicManager.hpp @@ -51,6 +51,13 @@ namespace Echoion::Core configManager_ = cfg; } + // Re-apply network settings (proxy) to every outbound client: + // the HTTP NetworkClient (YouTube Data API, URL shortener), the + // yt-dlp subprocess, and the ffmpeg AudioDecoder. Call after a + // config reload so proxy changes actually take effect everywhere + // instead of only living in the config copy. + void applyNetworkConfig(const Api::NetworkConfig& network); + [[nodiscard]] Api::Result play(const std::string& query, uint64_t clientId); [[nodiscard]] Api::Result pause(); diff --git a/src/Core.cpp b/src/Core.cpp index 0dd45ce..edba864 100644 --- a/src/Core.cpp +++ b/src/Core.cpp @@ -830,8 +830,11 @@ namespace Echoion::Core commandManager_->setServerConfig(config); } - // Re-apply the network proxy from config (idempotent store). - if (networkClient_) { + // Re-apply network settings to every outbound client (NetworkClient, + // yt-dlp, ffmpeg), not just the HTTP client. + if (musicManager_) { + musicManager_->applyNetworkConfig(config.network); + } else if (networkClient_) { networkClient_->setProxy(config.network.proxy); } @@ -898,12 +901,25 @@ namespace Echoion::Core // Update local config config_ = *configResult; + // Keep the shared ConfigManager in sync so components that read through + // it (cookies, API key, etc.) see the reloaded values. + configManager_.updateConfig(config_, false); + // Update components with new config if (commandManager_) { commandManager_->setPrefix(config_.bot.commandPrefix); commandManager_->setServerConfig(config_); } + // Re-apply network settings so a proxy edit actually reaches every + // outbound client (HTTP NetworkClient, yt-dlp, ffmpeg) — not just the + // config copy. Without this the YouTube API kept using the old/no proxy. + if (musicManager_) { + musicManager_->applyNetworkConfig(config_.network); + } else if (networkClient_) { + networkClient_->setProxy(config_.network.proxy); + } + Void::Log::info("Core", "{}", "Configuration reloaded from disk"); return {}; } diff --git a/src/MusicManager.cpp b/src/MusicManager.cpp index 04b28e9..80cb64e 100644 --- a/src/MusicManager.cpp +++ b/src/MusicManager.cpp @@ -128,6 +128,36 @@ namespace Echoion::Core return {}; } + void MusicManager::applyNetworkConfig(const Api::NetworkConfig& network) + { + // Push the (possibly changed) proxy to every outbound client. Passing + // the optional straight through also clears the proxy everywhere when + // it is disabled/removed in the config. + const auto& proxy = network.proxy; + const bool enabled = proxy.has_value() && proxy->enabled(); + + if (networkClient_) { + networkClient_->setProxy(proxy); + } + if (youTubeClient_) { + youTubeClient_->setProxyConfig(proxy); + } + if (audioDecoder_) { + audioDecoder_->setProxyConfig(proxy); + } + + if (enabled) { + Void::Log::info("MusicManager", "{}", std::format( + "Network config re-applied: {} proxy {}:{}", + (proxy->kind == Void::Net::ProxyConfig::Kind::Socks5) ? "SOCKS5" + : "HTTP", + proxy->host, proxy->port)); + } else { + Void::Log::info("MusicManager", "{}", + "Network config re-applied: proxy disabled"); + } + } + void MusicManager::shutdown() { isPlaying_ = false; diff --git a/src/NetworkClient.cpp b/src/NetworkClient.cpp index 4591694..e691af9 100644 --- a/src/NetworkClient.cpp +++ b/src/NetworkClient.cpp @@ -13,7 +13,10 @@ namespace Echoion::Core Api::Result NetworkClient::initialize( const std::optional& proxy) { - proxy_ = proxy; + { + std::lock_guard lock(mutex_); + proxy_ = proxy; + } initialized_ = true; Void::Log::info("NetworkClient", "{}", std::format("NetworkClient initialized (proxy: {})", proxy.has_value() ? "enabled" : "disabled")); @@ -26,7 +29,15 @@ namespace Echoion::Core NetworkClient::executeRequest(const Void::Net::Request& req) { Void::Net::HttpClient client; - const auto active = proxy_; + // Snapshot the proxy under the lock. setProxy()/initialize() can run on + // other threads (config reload, init), and a torn read of the + // optional (it holds std::string host/user/pass) could + // yield an empty host -> enabled()==false -> a silent direct connection. + std::optional active; + { + std::lock_guard lock(mutex_); + active = proxy_; + } if (active && active->enabled()) { client.set_proxy(*active); }