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); }