Sunshine0.0.5917
Sunshine 0.0.5917
Self-hosted game stream host for Moonlight.
Loading...
Searching...
No Matches
Go to the documentation of this file.
1
5// macros
6#pragma once
7
8// standard includes
9#include <chrono>
10#include <condition_variable>
11#include <cstddef>
12#include <memory>
13#include <mutex>
14#include <optional>
15#include <string>
16#include <string_view>
17#include <vector>
18
19// lib includes
20#include <boost/property_tree/ptree.hpp>
21#include <nlohmann/json.hpp>
22#include <Simple-Web-Server/server_https.hpp>
23
24// local includes
25#include "crypto.h"
26
30namespace nvhttp {
31
37 constexpr auto VERSION = "7.1.431.-1";
38
42 constexpr auto GFE_VERSION = "3.23.0.74";
43
47 constexpr auto PORT_HTTP = 0;
48
52 constexpr auto PORT_HTTPS = -5;
53
57 constexpr std::size_t MAX_PENDING_PAIRING_SESSIONS = 32;
58
62 constexpr std::size_t PAIRING_ID_SIZE = 32;
63
67 constexpr std::size_t MAX_PAIRING_CLIENT_NAME_SIZE = 128;
68
72 constexpr auto PAIRING_SESSION_TIMEOUT = std::chrono::minutes {5};
73
80 void start();
81
87 void setup(const std::string &pkey, const std::string &cert);
88
92 class SunshineHTTPS: public SimpleWeb::HTTPS {
93 public:
100 SunshineHTTPS(boost::asio::io_context &io_context, boost::asio::ssl::context &ctx):
101 SimpleWeb::HTTPS(io_context, ctx) {
102 }
103
104 virtual ~SunshineHTTPS() {
105 // Gracefully shutdown the TLS connection
106 SimpleWeb::error_code ec;
107 shutdown(ec);
108 }
109 };
110
121
126 std::condition_variable condition;
127 std::mutex mutex;
128 std::optional<bool> result;
129 };
130
135 struct {
136 std::string uniqueID = {};
137 std::string cert = {};
138 std::string name = {};
140
141 std::unique_ptr<crypto::aes_t> cipher_key = {};
142 std::vector<uint8_t> clienthash = {};
143
144 std::string serversecret = {};
145 std::string serverchallenge = {};
146 std::shared_ptr<pairing_completion_t> completion = std::make_shared<pairing_completion_t>();
147
148 struct {
150 std::shared_ptr<typename SimpleWeb::ServerBase<SimpleWeb::HTTP>::Response>,
151 std::shared_ptr<typename SimpleWeb::ServerBase<SunshineHTTPS>::Response>>
152 response;
153 std::string salt = {};
154 std::string id = {};
155 std::string device_name = {};
156 std::string address = {};
157 std::chrono::steady_clock::time_point expires_at = std::chrono::steady_clock::time_point::max();
159
164 bool failed = false;
165 };
166
171 std::string id;
172 std::string name;
173 std::string address;
174 };
175
184
191 bool is_valid_pairing_id(std::string_view pairing_id);
192
199 bool is_valid_pairing_pin(std::string_view pin);
200
207 bool is_valid_pairing_name(std::string_view name);
208
216 pair_session_insert_e insert_pair_session(pair_session_t sess, std::string &pairing_id);
217
233 bool evict_abandoned_pair_session(const std::string &unique_id);
234
240 void expire_pair_sessions(std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now());
241
247 std::vector<pending_pairing_t> get_pending_pairings();
248
255 bool cancel_pairing(std::string_view pairing_id);
256
261 void remove_session(const pair_session_t &sess);
262
276 void getservercert(pair_session_t &sess, boost::property_tree::ptree &tree, const std::string &pin);
277
293 void clientchallenge(pair_session_t &sess, boost::property_tree::ptree &tree, const std::string &challenge);
294
305 void serverchallengeresp(pair_session_t &sess, boost::property_tree::ptree &tree, const std::string &encrypted_response);
306
324 void clientpairingsecret(pair_session_t &sess, boost::property_tree::ptree &tree, const std::string &client_pairing_secret);
325
337 bool pin(std::string_view pairing_id, std::string pin, std::string name);
338
348 bool unpair_client(std::string_view uuid);
349
356 bool set_client_enabled(std::string_view uuid, bool enabled);
363 std::string get_cert_by_uuid(std::string_view uuid);
364
372 nlohmann::json get_all_clients();
373
380 void erase_all_clients();
381
382#ifdef SUNSHINE_TESTS
386 namespace test_support {
395 std::unique_ptr<SimpleWeb::ServerBase<SunshineHTTPS>> make_https_server(
396 const std::string &certificate_file,
397 const std::string &private_key_file,
398 std::shared_ptr<SimpleWeb::ScopeRunner> &handler_scope
399 );
400
407 void pair_http(
408 std::shared_ptr<typename SimpleWeb::ServerBase<SimpleWeb::HTTP>::Response> response,
409 std::shared_ptr<typename SimpleWeb::ServerBase<SimpleWeb::HTTP>::Request> request
410 );
411
415 void reset_client_state();
416
425 std::string add_client(const std::string &name, std::string cert, bool enabled);
426
433 bool duplicate_client(std::string_view uuid);
434
441 bool authorize_client_certificate(std::string_view cert);
442
450 bool complete_pairing(std::string_view pairing_id, bool success);
451
455 void reload_client_state();
456 } // namespace test_support
457#endif
458} // namespace nvhttp
Simple-Web-Server HTTPS backend configured for Sunshine certificate handling.
Definition nvhttp.h:92
SunshineHTTPS(boost::asio::io_context &io_context, boost::asio::ssl::context &ctx)
Construct an HTTPS connection using Sunshine's TLS context.
Definition nvhttp.h:100
Tagged storage for one of two possible value types.
Definition utility.h:750
Declarations for cryptography functions.
Contains all the functions and variables related to the nvhttp (GameStream) server.
Definition nvhttp.cpp:46
std::vector< pending_pairing_t > get_pending_pairings()
List pairing requests that are waiting for operator PIN approval.
Definition nvhttp.cpp:670
constexpr auto GFE_VERSION
The GFE version we are replicating.
Definition nvhttp.h:42
void serverchallengeresp(pair_session_t &sess, pt::ptree &tree, const std::string &encrypted_response)
Handle the server-challenge response phase of GameStream pairing.
Definition nvhttp.cpp:833
bool is_valid_pairing_id(const std::string_view pairing_id)
Validate an operator approval identifier received from the REST API.
Definition nvhttp.cpp:649
void expire_pair_sessions(const std::chrono::steady_clock::time_point now)
Remove pairing sessions whose completion deadline has passed.
Definition nvhttp.cpp:665
void clientpairingsecret(pair_session_t &sess, pt::ptree &tree, const std::string &client_pairing_secret)
Handle the client pairing-secret phase of GameStream pairing.
Definition nvhttp.cpp:869
pair_session_insert_e insert_pair_session(pair_session_t sess, std::string &pairing_id)
Insert a newly created pairing session into bounded pending storage.
Definition nvhttp.cpp:612
constexpr auto PAIRING_SESSION_TIMEOUT
Lifetime of an incomplete pairing session.
Definition nvhttp.h:72
PAIR_PHASE
Enumerates supported pAIR PHASE options.
Definition nvhttp.h:114
@ SERVERCHALLENGERESP
Sunshine is in the server challenge response phase.
Definition nvhttp.h:118
@ CLIENTCHALLENGE
Sunshine is in the client challenge phase.
Definition nvhttp.h:117
@ NONE
Sunshine is not in a pairing phase.
Definition nvhttp.h:115
@ CLIENTPAIRINGSECRET
Sunshine is in the client pairing secret phase.
Definition nvhttp.h:119
@ GETSERVERCERT
Sunshine is in the get server certificate phase.
Definition nvhttp.h:116
bool is_valid_pairing_name(const std::string_view name)
Validate an operator-supplied paired-client name received from the REST API.
Definition nvhttp.cpp:661
constexpr auto PORT_HTTPS
The HTTPS port, as a difference from the config port.
Definition nvhttp.h:52
constexpr std::size_t MAX_PAIRING_CLIENT_NAME_SIZE
Maximum number of bytes accepted for an operator-supplied paired-client name.
Definition nvhttp.h:67
constexpr std::size_t MAX_PENDING_PAIRING_SESSIONS
Maximum number of pairing sessions retained at one time.
Definition nvhttp.h:57
constexpr auto VERSION
The protocol version.
Definition nvhttp.h:37
void start()
Start the nvhttp server.
Definition nvhttp.cpp:1647
bool evict_abandoned_pair_session(const std::string &unique_id)
Evict an abandoned in-progress pairing session for a client.
Definition nvhttp.cpp:637
void setup(const std::string &pkey, const std::string &cert)
Setup the nvhttp server.
Definition nvhttp.cpp:1633
constexpr auto PORT_HTTP
The HTTP port, as a difference from the config port.
Definition nvhttp.h:47
void clientchallenge(pair_session_t &sess, pt::ptree &tree, const std::string &challenge)
Handle the client-challenge phase of GameStream pairing.
Definition nvhttp.cpp:783
pair_session_insert_e
Result of inserting a new pairing session into bounded pending storage.
Definition nvhttp.h:179
@ ALREADY_EXISTS
A session with the same client unique ID is already active.
Definition nvhttp.h:181
@ ADDED
The session was inserted successfully.
Definition nvhttp.h:180
@ FULL
The bounded session store has reached its limit.
Definition nvhttp.h:182
bool is_valid_pairing_pin(const std::string_view pin)
Validate a Moonlight pairing PIN received from the REST API.
Definition nvhttp.cpp:655
bool unpair_client(const std::string_view uuid)
Remove single client.
Definition nvhttp.cpp:1799
void complete_pairing(pair_session_t &sess, const bool success)
Publish the final result of a pairing handshake.
Definition nvhttp.cpp:579
constexpr std::size_t PAIRING_ID_SIZE
Number of hexadecimal characters in an operator approval identifier.
Definition nvhttp.h:62
void getservercert(pair_session_t &sess, pt::ptree &tree, const std::string &pin)
Return the server certificate text for pairing responses.
Definition nvhttp.cpp:752
void erase_all_clients()
Remove all paired clients.
Definition nvhttp.cpp:1792
bool pin(const std::string_view pairing_id, std::string pin, std::string name)
Apply the user supplied PIN to the explicitly selected pairing request.
Definition nvhttp.cpp:1149
bool set_client_enabled(const std::string_view uuid, bool enabled)
Enable or disable a client.
Definition nvhttp.cpp:1816
void remove_session(const pair_session_t &sess)
removes the temporary pairing session
Definition nvhttp.cpp:722
bool cancel_pairing(const std::string_view pairing_id)
Cancel a pairing request that is waiting for operator approval.
Definition nvhttp.cpp:697
nlohmann::json get_all_clients()
Get all paired clients.
Definition nvhttp.cpp:1318
std::string get_cert_by_uuid(const std::string_view uuid)
Get cert by UUID.
Definition nvhttp.cpp:1832
std::string address
Network address from which the pairing request originated.
std::string device_name
Untrusted device name reported by the pairing client.
util::Either< std::shared_ptr< typename SimpleWeb::ServerBase< SimpleWeb::HTTP >::Response >, std::shared_ptr< typename SimpleWeb::ServerBase< SunshineHTTPS >::Response > > response
Pending HTTP or HTTPS response completed after PIN approval.
std::string salt
Client-provided salt used to derive the pairing key.
std::chrono::steady_clock::time_point expires_at
Deadline for completing this pairing session.
std::string name
Client name recorded after successful pairing.
std::string cert
Client certificate bytes exchanged during pairing.
std::string uniqueID
Client-provided pairing-session identifier.
Pairing handshake state exchanged with a Moonlight client.
Definition nvhttp.h:134
std::string serverchallenge
Server challenge sent during pairing.
Definition nvhttp.h:145
struct nvhttp::pair_session_t::@305111106156267002241031277360116001064101025004 async_insert_pin
Async insert pin.
struct nvhttp::pair_session_t::@105363203101100234310146234343010345115334117372 client
Client object or client certificate data owned by this state..
bool failed
Whether protocol validation failed and the session must be removed.
Definition nvhttp.h:164
std::shared_ptr< pairing_completion_t > completion
Result shared with the REST request waiting for the handshake.
Definition nvhttp.h:146
std::vector< uint8_t > clienthash
Client certificate hash used during pairing.
Definition nvhttp.h:142
std::string serversecret
Server pairing secret.
Definition nvhttp.h:144
std::unique_ptr< crypto::aes_t > cipher_key
Cipher key.
Definition nvhttp.h:141
PAIR_PHASE last_phase
used as a security measure to prevent out of order calls
Definition nvhttp.h:163
Shared result used to return the completed pairing outcome to a PIN submitter.
Definition nvhttp.h:125
std::condition_variable condition
Wakes the REST request when pairing finishes.
Definition nvhttp.h:126
std::optional< bool > result
Final pairing result, or no value while the handshake is pending.
Definition nvhttp.h:128
std::mutex mutex
Protects the completion result.
Definition nvhttp.h:127
Operator-visible context for a pairing request awaiting PIN approval.
Definition nvhttp.h:170
std::string id
Unguessable approval identifier.
Definition nvhttp.h:171
std::string address
Network address from which the request originated.
Definition nvhttp.h:173
std::string name
Untrusted device name reported by the pairing client.
Definition nvhttp.h:172