#pragma once #include "echoion/web/HttpSecurity.hpp" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace Echoion::Web { namespace asio = boost::asio; namespace beast = boost::beast; namespace http = beast::http; // Serves the static browser UI over HTTP and tunnels GET /rpc upgrades to // the local RPC server so only the web port ever needs to be exposed. It // has no handle on the music bot itself. class HttpServer { public: struct Config { uint16_t port = 8080; std::string bindAddress = "127.0.0.1"; std::string webRoot = "./web"; // Explicit RPC URL served to the browser; when empty the UI // derives ws://:. std::string rpcUrl; // External VoidWatch deployment used by the temporary // /watch redirect and exposed to the browser in // /config.json. std::string watchUrl; uint16_t rpcPort = 8765; // Server-wide switch for the Now Playing video preview, // surfaced to the browser via /config.json. bool nowPlayingVideo = true; std::vector trustedProxyCidrs; }; HttpServer(); ~HttpServer(); bool start(const Config& config); void stop(); [[nodiscard]] bool isRunning() const { return running_; } using TrustedProxyUpdate = std::expected; [[nodiscard]] static TrustedProxyUpdate validateTrustedProxyCidrs( const std::vector& cidrs); [[nodiscard]] TrustedProxyUpdate setTrustedProxyCidrs( const std::vector& cidrs); // A user's avatar image, served at GET /avatar/. Installed // by Core (which owns the TeamSpeak plugin handle); best-effort — // nullopt yields a 404 and the UI falls back to initials. It may be // installed before or after start() and survives restarts. struct AvatarImage { std::string contentType; std::string bytes; }; using AvatarProvider = std::function( const std::string& uid)>; void setAvatarProvider(AvatarProvider provider) { std::lock_guard lock(avatarProviderMutex_); avatarProvider_ = std::move(provider); } struct DashboardSnapshot { std::string body; std::string etag; }; using DashboardProvider = std::function; void setDashboardProvider(DashboardProvider provider) { std::lock_guard lock(dashboardProviderMutex_); dashboardProvider_ = std::move(provider); } enum class ShortLinkStatus { found, notFound, gone, rateLimited, unavailable, }; struct ShortLinkResolution { ShortLinkStatus status = ShortLinkStatus::notFound; std::string location; }; using ShortLinkResolver = std::function; void setShortLinkResolver(ShortLinkResolver resolver) { std::lock_guard lock(shortLinkResolverMutex_); shortLinkResolver_ = std::move(resolver); } private: void acceptLoop(); void doAccept(); void onAccept(asio::ip::tcp::socket socket); void handleConnection(std::shared_ptr socket); // Tunnel a browser WebSocket upgrade at /rpc to the local RPC // server as raw TCP: the original upgrade request is replayed // upstream and bytes are pumped in both directions, so the real // handshake/auth all happen against the RPC server itself. void bridgeRpc(std::shared_ptr browser, http::request& req, beast::flat_buffer& residual); // Serves one request; returns true when the connection should be // kept open for the next request (HTTP keep-alive). bool serveHttp(asio::ip::tcp::socket& socket, const http::request& req, std::string_view clientAddress); [[nodiscard]] std::string clientAddress( const asio::ip::tcp::socket& socket, const http::request& req) const; [[nodiscard]] bool allowShortLinkRequest(std::string_view peer); Config config_; asio::io_context ioContext_; std::optional acceptor_; std::thread serverThread_; std::atomic running_{false}; // Per-connection handlers run on a worker pool; ~ThreadPool drains // and joins them on stop. static constexpr std::size_t POOL_SIZE = 32; std::unique_ptr pool_; std::atomic nextConnId_{1}; // Live /rpc bridges (browser, upstream); shut down on stop() so // the blocking pump loops unblock and the pool can join. std::unordered_map< uint64_t, std::pair, std::shared_ptr>> bridges_; std::mutex bridgesMutex_; // Called from pool threads; guarded so Core can (re)install it at // any time. AvatarProvider avatarProvider_; std::mutex avatarProviderMutex_; DashboardProvider dashboardProvider_; std::mutex dashboardProviderMutex_; ShortLinkResolver shortLinkResolver_; std::mutex shortLinkResolverMutex_; mutable std::shared_mutex trustedProxyMutex_; std::vector trustedProxyNetworks_; Security::ShortLinkRateLimiter shortLinkRateLimiter_; }; } // namespace Echoion::Web