Sunshine0.0.5917
Sunshine 0.0.5917
Self-hosted game stream host for Moonlight.
Loading...
Searching...
No Matches
nvhttp.cpp File Reference

Definitions for the nvhttp (GameStream) server. More...

#include <algorithm>
#include <array>
#include <chrono>
#include <filesystem>
#include <format>
#include <mutex>
#include <string>
#include <utility>
#include <boost/asio/ssl/context.hpp>
#include <boost/asio/ssl/context_base.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <Simple-Web-Server/server_http.hpp>
#include "config.h"
#include "display_device.h"
#include "file_handler.h"
#include "globals.h"
#include "httpcommon.h"
#include "logging.h"
#include "network.h"
#include "nvhttp.h"
#include "platform/common.h"
#include "process.h"
#include "rtsp.h"
#include "string_hash.h"
#include "system_tray.h"
#include "utility.h"
#include "uuid.h"
#include "video.h"
Include dependency graph for nvhttp.cpp:

Classes

struct  nvhttp::client_t
 Persisted pairing data for one Moonlight client. More...
struct  nvhttp::conf_intern_t
 Internal HTTPS credential paths for the configuration server. More...
struct  nvhttp::named_cert_t
 Certificate entry associated with a client name and UUID. More...
class  nvhttp::SunshineHTTPSServer
 HTTPS server backend that adds Sunshine's client-certificate verification. More...
struct  nvhttp::tunnel< SimpleWeb::HTTP >
 Plain HTTP server wrapper used for non-TLS endpoints. More...
struct  nvhttp::tunnel< SunshineHTTPS >
 HTTPS tunnel session used for encrypted client requests. More...

Namespaces

namespace  nvhttp
 Contains all the functions and variables related to the nvhttp (GameStream) server.

Typedefs

using nvhttp::args_t = SimpleWeb::CaseInsensitiveMultimap
 Case-insensitive map used for HTTP headers and query parameters.
using nvhttp::http_server_t = SimpleWeb::Server<SimpleWeb::HTTP>
 Plain HTTP server type used for GameStream endpoints without TLS.
using nvhttp::https_server_t = SunshineHTTPSServer
 HTTPS server type used for GameStream endpoints requiring TLS.
using nvhttp::req_http_t = std::shared_ptr<typename SimpleWeb::ServerBase<SimpleWeb::HTTP>::Request>
 Shared HTTP request object received by redirect and discovery handlers.
using nvhttp::req_https_t = std::shared_ptr<typename SimpleWeb::ServerBase<SunshineHTTPS>::Request>
 Shared HTTPS request object received by GameStream handlers.
using nvhttp::resp_http_t = std::shared_ptr<typename SimpleWeb::ServerBase<SimpleWeb::HTTP>::Response>
 Shared HTTP response object passed to redirect and discovery handlers.
using nvhttp::resp_https_t = std::shared_ptr<typename SimpleWeb::ServerBase<SunshineHTTPS>::Response>
 Shared HTTPS response object passed to GameStream handlers.

Enumerations

enum class  nvhttp::op_e { nvhttp::ADD , nvhttp::REMOVE }
 Certificate operations supported by the pairing API. More...

Functions

std::string nvhttp::add_authorized_client (const std::string &name, std::string &&cert)
 Add authorized client data.
void nvhttp::appasset (resp_https_t response, req_https_t request)
 Return an application asset requested by the client.
void nvhttp::applist (resp_https_t response, req_https_t request)
 Build the GameStream application list response.
void nvhttp::cancel (resp_https_t response, req_https_t request)
 Check whether cel.
bool nvhttp::cancel_pairing (std::string_view pairing_id)
 Cancel a pairing request that is waiting for operator approval.
std::string nvhttp::canonical_certificate_pem (const std::string_view cert_pem)
 Convert a PEM certificate to Sunshine's canonical OpenSSL serialization.
std::mutex & nvhttp::client_auth_mutex ()
 Get the mutex protecting paired-client authorization state.
void nvhttp::clientchallenge (pair_session_t &sess, pt::ptree &tree, const std::string &challenge)
 Handle the client-challenge phase of GameStream pairing.
void nvhttp::clientpairingsecret (pair_session_t &sess, pt::ptree &tree, const std::string &client_pairing_secret)
 Handle the client pairing-secret phase of GameStream pairing.
void nvhttp::complete_pairing (pair_session_t &sess, const bool success)
 Publish the final result of a pairing handshake.
void nvhttp::erase_all_clients ()
 Remove all paired clients.
bool nvhttp::evict_abandoned_pair_session (const std::string &unique_id)
 Evict an abandoned in-progress pairing session for a client.
void nvhttp::expire_pair_sessions (std::chrono::steady_clock::time_point now=std::chrono::steady_clock::now())
 Remove pairing sessions whose completion deadline has passed.
void nvhttp::expire_pair_sessions_unlocked (const std::chrono::steady_clock::time_point now)
 Expire stale pairing sessions while the session mutex is held.
void nvhttp::fail_pair (pair_session_t &sess, pt::ptree &tree, const std::string status_msg)
 Return the GameStream pairing failure response.
nlohmann::json nvhttp::get_all_clients ()
 Get all paired clients.
std::string nvhttp::get_arg (const args_t &args, const char *name, const char *default_value=nullptr)
 Read a named query argument from the HTTP request map.
std::string nvhttp::get_cert_by_uuid (const std::string_view uuid)
 Get cert by UUID.
std::pair< bool, std::string > nvhttp::get_client_status (const std::string_view cert_pem)
 Check whether a paired client certificate is allowed to connect and retrieve its friendly name.
uint32_t nvhttp::get_codec_mode_flags ()
 Get codec mode flags.
std::vector< pending_pairing_t > nvhttp::get_pending_pairings ()
 List pairing requests that are waiting for operator PIN approval.
void nvhttp::getservercert (pair_session_t &sess, pt::ptree &tree, const std::string &pin)
 Return the server certificate text for pairing responses.
pair_session_insert_e nvhttp::insert_pair_session (pair_session_t sess, std::string &pairing_id)
 Insert a newly created pairing session into bounded pending storage.
bool nvhttp::is_client_enabled (const X509 *certificate)
 Check whether a certificate exactly matches an enabled paired-client record.
bool nvhttp::is_valid_pairing_id (std::string_view pairing_id)
 Validate an operator approval identifier received from the REST API.
bool nvhttp::is_valid_pairing_name (std::string_view name)
 Validate an operator-supplied paired-client name received from the REST API.
bool nvhttp::is_valid_pairing_pin (std::string_view pin)
 Validate a Moonlight pairing PIN received from the REST API.
void nvhttp::launch (bool &host_audio, resp_https_t response, req_https_t request)
 Launch the requested application for a GameStream session.
void nvhttp::load_state ()
 Load state from its backing store.
std::shared_ptr< rtsp_stream::launch_session_t > nvhttp::make_launch_session (bool host_audio, const args_t &args)
 Create launch session.
std::mutex & nvhttp::map_id_sess_mutex ()
 Get the mutex protecting pairing-session state.
template<class T>
void nvhttp::not_found (std::shared_ptr< typename SimpleWeb::ServerBase< T >::Response > response, std::shared_ptr< typename SimpleWeb::ServerBase< T >::Request > request)
 Return a GameStream HTTP not-found response.
template<class T>
void nvhttp::pair (std::shared_ptr< typename SimpleWeb::ServerBase< T >::Response > response, std::shared_ptr< typename SimpleWeb::ServerBase< T >::Request > request)
 Dispatch the top-level GameStream pairing request by phase.
bool nvhttp::pin (std::string_view pairing_id, std::string pin, std::string name)
 Apply the user supplied PIN to the explicitly selected pairing request.
template<class T>
void nvhttp::print_req (std::shared_ptr< typename SimpleWeb::ServerBase< T >::Request > request)
 Write req details to the log.
void nvhttp::rebuild_client_cert_chain ()
 Rebuild the GameStream trust stores from enabled paired-client records.
void nvhttp::remove_session (const pair_session_t &sess)
 removes the temporary pairing session
void nvhttp::resume (bool &host_audio, resp_https_t response, req_https_t request)
 Resume an existing GameStream session.
void nvhttp::save_state ()
 Persist the current state to its backing store.
void nvhttp::serverchallengeresp (pair_session_t &sess, pt::ptree &tree, const std::string &encrypted_response)
 Handle the server-challenge response phase of GameStream pairing.
template<class T>
void nvhttp::serverinfo (std::shared_ptr< typename SimpleWeb::ServerBase< T >::Response > response, std::shared_ptr< typename SimpleWeb::ServerBase< T >::Request > request)
 Build the GameStream server-info response.
bool nvhttp::set_client_enabled (std::string_view uuid, bool enabled)
 Enable or disable a client.
void nvhttp::setup (const std::string &pkey, const std::string &cert)
 Setup the nvhttp server.
void nvhttp::start ()
 Start the nvhttp server.
template<class T>
bool nvhttp::start_pairing_session (const std::shared_ptr< typename SimpleWeb::ServerBase< T >::Response > &response, const std::shared_ptr< typename SimpleWeb::ServerBase< T >::Request > &request, const args_t &args, pt::ptree &tree, const std::string &unique_id)
 Start a GameStream pairing session for a getservercert request.
bool nvhttp::unpair_client (std::string_view uuid)
 Remove single client.
const char * nvhttp::verify_client_certificate (X509 *certificate)
 Verify a client certificate against the exact enabled paired identity.
bool nvhttp::write_pairing_response (pair_session_t &sess, const pt::ptree &tree)
 Write a completed response to the client waiting for PIN approval.

Variables

crypto::cert_chain_t nvhttp::cert_chain
 Enabled paired-client certificates accepted by Sunshine's GameStream HTTPS server.
client_t nvhttp::client_root
 In-memory representation of the paired-client database.
conf_intern_t nvhttp::conf_intern
 TLS credential paths loaded from Sunshine's runtime configuration.
std::string nvhttp::last_verified_client_cert
 Last client certificate accepted by the TLS verify callback. // NOSONAR(cpp:S5421): intentionally mutable global.
std::string nvhttp::last_verified_client_name
 Friendly name of last client certificate accepted by the TLS verify callback. // NOSONAR(cpp:S5421): intentionally mutable global.
std::unordered_map< std::string, pair_session_t, util::transparent_string_hash, std::equal_to<> > nvhttp::map_id_sess
 Pairing sessions keyed by temporary unique ID.
std::atomic< uint32_t > nvhttp::session_id_counter
 Monotonic counter used to allocate GameStream session IDs.

Detailed Description

Definitions for the nvhttp (GameStream) server.