49 namespace fs = std::filesystem;
50 namespace pt = boost::property_tree;
52 using p_named_cert_t = crypto::p_named_cert_t;
55 bool experimental_isolated_sessions_enabled() {
56 return config::video.virtual_display_backend ==
"hermes_kms" &&
57 config::video.hermes_kms_isolated_sessions;
61 std::vector<p_named_cert_t> named_devices;
79 const char *source_name(proc::scale_factor_source_e source) {
81 case proc::scale_factor_source_e::app_forced:
83 case proc::scale_factor_source_e::client:
85 case proc::scale_factor_source_e::app:
87 case proc::scale_factor_source_e::host_default:
90 return "host_default";
95 const auto apps = proc::proc.get_apps();
96 const auto app = std::find_if(apps.begin(), apps.end(), [&](
const proc::ctx_t &candidate) {
97 return candidate.id == app_id;
99 if (app == apps.end()) {
104 probe.virtual_display = virtual_display;
106 probe.scale_factor = scale_factor;
110 bool display_effective = display.use;
111 std::string display_source = source_name(display.source);
114 config::video.isolation_method) != proc::session_route_e::shared) {
115 display_effective =
true;
116 display_source =
"isolated_session";
121 {
"virtual_display", {
122 {
"requested", virtual_display},
123 {
"effective", display_effective},
124 {
"source", display_source},
127 {
"requested", scale_factor},
128 {
"effective", scale.percent},
129 {
"source", source_name(scale.source)},
134 struct pair_session_t;
137 static std::string one_time_pin;
138 static std::string otp_passphrase;
139 static std::string otp_device_name;
140 static std::chrono::time_point<std::chrono::steady_clock> otp_creation_time;
142 class SunshineHTTPSServer:
public SimpleWeb::ServerBase<SunshineHTTPS> {
144 SunshineHTTPSServer(
const std::string &certification_file,
const std::string &private_key_file):
145 ServerBase<SunshineHTTPS>::ServerBase(443),
146 context(boost::asio::ssl::context::tls_server) {
148 context.set_options(boost::asio::ssl::context::no_tlsv1);
149 context.set_options(boost::asio::ssl::context::no_tlsv1_1);
150 context.use_certificate_chain_file(certification_file);
151 context.use_private_key_file(private_key_file, boost::asio::ssl::context::pem);
154 std::function<bool(std::shared_ptr<Request>, SSL*)> verify;
155 std::function<void(std::shared_ptr<Response>, std::shared_ptr<Request>)> on_verify_failed;
158 boost::asio::ssl::context context;
160 void after_bind()
override {
162 context.set_verify_mode(boost::asio::ssl::verify_peer | boost::asio::ssl::verify_fail_if_no_peer_cert | boost::asio::ssl::verify_client_once);
163 context.set_verify_callback([](
int verified, boost::asio::ssl::verify_context &ctx) {
171 void accept()
override {
172 auto connection = create_connection(*io_service, context);
174 acceptor->async_accept(connection->socket->lowest_layer(), [
this, connection](
const SimpleWeb::error_code &ec) {
175 auto lock = connection->handler_runner->continue_lock();
180 if (ec != SimpleWeb::error::operation_aborted) {
184 auto session = std::make_shared<Session>(config.max_request_streambuf_size, connection);
187 boost::asio::ip::tcp::no_delay option(
true);
188 SimpleWeb::error_code ec;
189 session->connection->socket->lowest_layer().set_option(option, ec);
191 session->connection->set_timeout(config.timeout_request);
192 session->connection->socket->async_handshake(boost::asio::ssl::stream_base::server, [
this, session](
const SimpleWeb::error_code &ec) {
193 session->connection->cancel_timeout();
194 auto lock = session->connection->handler_runner->continue_lock();
199 if (verify && !verify(session->request, session->connection->socket->native_handle())) {
200 this->write(session, on_verify_failed);
204 }
else if (this->on_error) {
205 this->on_error(session->request, ec);
208 }
else if (this->on_error) {
209 this->on_error(session->request, ec);
215 using https_server_t = SunshineHTTPSServer;
216 using http_server_t = SimpleWeb::Server<SimpleWeb::HTTP>;
219 std::string servercert;
224 std::unordered_map<std::string, pair_session_t> map_id_sess;
227 std::recursive_mutex pair_session_mutex;
229 std::atomic<uint32_t> session_id_counter;
232 return session_id_counter.load();
254 std::unordered_map<std::string, std::string> pending_pairs;
256 void set_pending_pair(
const std::string &unique_id,
const std::string &name) {
258 pending_pairs[unique_id] = name;
261 void clear_pending_pair(
const std::string &unique_id) {
262 std::lock_guard lock {pending_pair_mutex};
263 pending_pairs.erase(unique_id);
266 void clear_pending_pairs() {
268 pending_pairs.clear();
273 std::chrono::steady_clock::time_point expires_at;
275 static std::mutex pending_hestia_session_prepares_mutex;
276 static std::unordered_map<std::string, pending_hestia_session_prepare_t> pending_hestia_session_prepares;
278 using resp_https_t = std::shared_ptr<typename SimpleWeb::ServerBase<SunshineHTTPS>::Response>;
279 using req_https_t = std::shared_ptr<typename SimpleWeb::ServerBase<SunshineHTTPS>::Request>;
280 using resp_http_t = std::shared_ptr<typename SimpleWeb::ServerBase<SimpleWeb::HTTP>::Response>;
281 using req_http_t = std::shared_ptr<typename SimpleWeb::ServerBase<SimpleWeb::HTTP>::Request>;
288 std::string get_arg(
const args_t &
args,
const char *name,
const char *default_value) {
289 auto it =
args.find(name);
290 if (it == std::end(
args)) {
291 if (default_value !=
nullptr) {
292 return std::string(default_value);
295 throw std::out_of_range(name);
301 cmd_list_t extract_command_entries(
const nlohmann::json& j,
const std::string& key) {
305 if (j.contains(key)) {
308 for (
const auto& item : j.at(key)) {
311 std::string cmd = item.at(
"cmd").get<std::string>();
312 bool elevated = util::get_non_string_json_value<bool>(item,
"elevated",
false);
315 commands.push_back({cmd, elevated});
316 }
catch (
const std::exception& e) {
317 BOOST_LOG(warning) <<
"Error parsing command entry: " << e.what();
320 }
catch (
const std::exception &e) {
321 BOOST_LOG(warning) <<
"Error retrieving key \"" << key <<
"\": " << e.what();
324 BOOST_LOG(debug) <<
"Key \"" << key <<
"\" not found in the JSON.";
331 nlohmann::json root = nlohmann::json::object();
333 if (fs::exists(config::nvhttp.file_state)) {
335 std::ifstream in(config::nvhttp.file_state);
337 }
catch (std::exception &e) {
338 BOOST_LOG(error) <<
"Couldn't read "sv << config::nvhttp.file_state <<
": "sv << e.what();
347 root[
"root"] = nlohmann::json::object();
348 root[
"root"][
"uniqueid"] = http::unique_id;
350 client_t &client = client_root;
351 nlohmann::json named_cert_nodes = nlohmann::json::array();
353 std::unordered_set<std::string> unique_certs;
354 std::unordered_map<std::string, int> name_counts;
356 for (
auto &named_cert_p : client.named_devices) {
358 if (unique_certs.insert(named_cert_p->cert).second) {
359 nlohmann::json named_cert_node = nlohmann::json::object();
360 std::string base_name = named_cert_p->name;
362 size_t pos = base_name.find(
" (");
363 if (pos != std::string::npos) {
364 base_name = base_name.substr(0, pos);
366 int count = name_counts[base_name]++;
367 std::string final_name = base_name;
369 final_name +=
" (" + std::to_string(count + 1) +
")";
371 named_cert_node[
"name"] = final_name;
372 named_cert_node[
"cert"] = named_cert_p->cert;
373 named_cert_node[
"uuid"] = named_cert_p->uuid;
374 named_cert_node[
"display_mode"] = named_cert_p->display_mode;
375 named_cert_node[
"perm"] =
static_cast<uint32_t
>(named_cert_p->perm);
376 named_cert_node[
"enable_legacy_ordering"] = named_cert_p->enable_legacy_ordering;
377 named_cert_node[
"allow_client_commands"] = named_cert_p->allow_client_commands;
378 named_cert_node[
"always_use_virtual_display"] = named_cert_p->always_use_virtual_display;
381 if (!named_cert_p->do_cmds.empty()) {
382 nlohmann::json do_cmds_node = nlohmann::json::array();
383 for (
const auto &cmd : named_cert_p->do_cmds) {
384 do_cmds_node.push_back(crypto::command_entry_t::serialize(cmd));
386 named_cert_node[
"do"] = do_cmds_node;
390 if (!named_cert_p->undo_cmds.empty()) {
391 nlohmann::json undo_cmds_node = nlohmann::json::array();
392 for (
const auto &cmd : named_cert_p->undo_cmds) {
393 undo_cmds_node.push_back(crypto::command_entry_t::serialize(cmd));
395 named_cert_node[
"undo"] = undo_cmds_node;
398 named_cert_nodes.push_back(named_cert_node);
402 root[
"root"][
"named_devices"] = named_cert_nodes;
405 std::ofstream out(config::nvhttp.file_state);
407 }
catch (std::exception &e) {
408 BOOST_LOG(error) <<
"Couldn't write "sv << config::nvhttp.file_state <<
": "sv << e.what();
414 if (!fs::exists(config::nvhttp.file_state)) {
415 BOOST_LOG(info) <<
"File "sv << config::nvhttp.file_state <<
" doesn't exist"sv;
416 http::unique_id = uuid_util::uuid_t::generate().string();
422 std::ifstream in(config::nvhttp.file_state);
424 }
catch (std::exception &e) {
425 BOOST_LOG(error) <<
"Couldn't read "sv << config::nvhttp.file_state <<
": "sv << e.what();
433 std::string uid = (tree.contains(
"root") && tree[
"root"].contains(
"uniqueid")) ?
434 tree[
"root"][
"uniqueid"].get<std::string>() :
437 http::uuid = uuid_util::uuid_t::parse(uid);
438 http::unique_id = uid;
439 }
catch (
const std::invalid_argument &) {
440 BOOST_LOG(warning) <<
"State file has an invalid uniqueid; regenerating"sv;
441 http::uuid = uuid_util::uuid_t::generate();
442 http::unique_id = http::uuid.string();
445 nlohmann::json root = tree[
"root"];
449 if (root.contains(
"devices")) {
450 for (
auto &device_node : root[
"devices"]) {
452 if (device_node.contains(
"certs")) {
453 for (
auto &el : device_node[
"certs"]) {
454 auto named_cert_p = std::make_shared<crypto::named_cert_t>();
455 named_cert_p->name =
"";
456 named_cert_p->cert = el.get<std::string>();
457 named_cert_p->uuid = uuid_util::uuid_t::generate().string();
458 named_cert_p->display_mode =
"";
459 named_cert_p->perm = PERM::_all;
460 named_cert_p->enable_legacy_ordering =
true;
461 named_cert_p->allow_client_commands =
true;
462 named_cert_p->always_use_virtual_display =
false;
463 client.named_devices.emplace_back(named_cert_p);
470 if (root.contains(
"named_devices")) {
471 for (
auto &el : root[
"named_devices"]) {
472 auto named_cert_p = std::make_shared<crypto::named_cert_t>();
473 named_cert_p->name = el.value(
"name",
"");
474 named_cert_p->cert = el.value(
"cert",
"");
475 named_cert_p->uuid = el.value(
"uuid",
"");
476 named_cert_p->display_mode = el.value(
"display_mode",
"");
477 named_cert_p->perm = (
PERM)(util::get_non_string_json_value<uint32_t>(el,
"perm", (uint32_t)PERM::_all)) & PERM::_all;
478 named_cert_p->enable_legacy_ordering = el.value(
"enable_legacy_ordering",
true);
479 named_cert_p->allow_client_commands = el.value(
"allow_client_commands",
true);
480 named_cert_p->always_use_virtual_display = el.value(
"always_use_virtual_display",
false);
482 named_cert_p->do_cmds = extract_command_entries(el,
"do");
483 named_cert_p->undo_cmds = extract_command_entries(el,
"undo");
484 client.named_devices.emplace_back(named_cert_p);
490 for (
auto &named_cert : client.named_devices) {
491 cert_chain.add(named_cert);
494 client_root = client;
497 void add_authorized_client(
const p_named_cert_t& named_cert_p) {
498 client_t &client = client_root;
499 client.named_devices.push_back(named_cert_p);
501#if defined HERMES_TRAY && HERMES_TRAY >= 1
502 system_tray::update_tray_paired(named_cert_p->name);
511 std::shared_ptr<rtsp_stream::launch_session_t> make_launch_session(
bool host_audio,
bool input_only,
const args_t &
args,
const crypto::named_cert_t* named_cert_p) {
512 auto launch_session = std::make_shared<rtsp_stream::launch_session_t>();
514 launch_session->id = ++session_id_counter;
517 if (named_cert_p->uuid != http::unique_id) {
518 auto rikey = util::from_hex_vec(get_arg(
args,
"rikey"),
true);
519 std::copy(rikey.cbegin(), rikey.cend(), std::back_inserter(launch_session->gcm_key));
521 launch_session->host_audio = host_audio;
524 auto corever = util::from_view(get_arg(
args,
"corever",
"0"));
527 launch_session->gcm_key,
false
529 launch_session->rtsp_iv_counter = 0;
531 launch_session->rtsp_url_scheme = launch_session->rtsp_cipher ?
"rtspenc://"s :
"rtsp://"s;
534 unsigned char raw_payload[8];
535 RAND_bytes(raw_payload,
sizeof(raw_payload));
536 launch_session->av_ping_payload = util::hex_vec(raw_payload);
537 RAND_bytes((
unsigned char *) &launch_session->control_connect_data,
sizeof(launch_session->control_connect_data));
539 launch_session->iv.resize(16);
540 uint32_t prepend_iv = util::endian::big<uint32_t>(util::from_view(get_arg(
args,
"rikeyid")));
541 auto prepend_iv_p = (uint8_t *) &prepend_iv;
542 std::copy(prepend_iv_p, prepend_iv_p +
sizeof(prepend_iv), std::begin(launch_session->iv));
545 std::stringstream mode;
546 if (named_cert_p->display_mode.empty()) {
547 auto mode_str = get_arg(
args,
"mode", config::video.fallback_mode.c_str());
548 mode = std::stringstream(mode_str);
549 BOOST_LOG(info) <<
"Display mode for client ["sv << named_cert_p->name <<
"] requested to ["sv << mode_str <<
']';
551 mode = std::stringstream(named_cert_p->display_mode);
552 BOOST_LOG(info) <<
"Display mode for client ["sv << named_cert_p->name <<
"] overriden to ["sv << named_cert_p->display_mode <<
']';
558 while (std::getline(mode, segment,
'x')) {
560 launch_session->width = atoi(segment.c_str());
563 launch_session->height = atoi(segment.c_str());
566 auto fps = atof(segment.c_str());
570 launch_session->fps = (int)fps;
578 launch_session->width = 1920;
579 launch_session->height = 1080;
580 launch_session->fps = 60000;
583 launch_session->device_name = named_cert_p->name.empty() ?
"HermesDisplay"s : named_cert_p->name;
584 launch_session->unique_id = named_cert_p->uuid;
585 launch_session->perm = named_cert_p->perm;
586 launch_session->enable_sops = util::from_view(get_arg(
args,
"sops",
"0"));
587 launch_session->surround_info = util::from_view(get_arg(
args,
"surroundAudioInfo",
"196610"));
588 launch_session->surround_params = (get_arg(
args,
"surroundParams",
""));
589 launch_session->gcmap = util::from_view(get_arg(
args,
"gcmap",
"0"));
590 launch_session->enable_hdr = util::from_view(get_arg(
args,
"hdrMode",
"0"));
591 launch_session->virtual_display = util::from_view(get_arg(
args,
"virtualDisplay",
"0")) || named_cert_p->always_use_virtual_display;
592 launch_session->scale_factor = util::from_view(get_arg(
args,
"scaleFactor",
"100"));
593 if (
args.find(
"scaleFactor") !=
args.end()) {
594 launch_session->client_scale_factor = launch_session->scale_factor;
596 launch_session->launch_mode =
"normal";
598 launch_session->client_do_cmds = named_cert_p->do_cmds;
599 launch_session->client_undo_cmds = named_cert_p->undo_cmds;
601 launch_session->input_only = input_only;
603 return launch_session;
607 std::lock_guard lock {pair_session_mutex};
610 const std::string unique_id = sess.client.uniqueID;
611 clear_pending_pair(unique_id);
612 map_id_sess.erase(unique_id);
616 std::lock_guard lock {pair_session_mutex};
617 auto it = map_id_sess.find(unique_id);
618 if (it == map_id_sess.end()) {
621 const std::string
id = unique_id;
623 auto &response = it->second.async_insert_pin.response;
624 const std::string cancelled =
"<root status_code=\"409\" status_message=\"Pairing cancelled\"><paired>0</paired></root>";
625 if (response.has_left() && response.left()) {
626 response.left()->close_connection_after_response =
true;
627 response.left()->write(cancelled);
628 }
else if (response.has_right() && response.right()) {
629 response.right()->close_connection_after_response =
true;
630 response.right()->write(cancelled);
632 clear_pending_pair(
id);
633 map_id_sess.erase(it);
638 std::lock_guard lock {pair_session_mutex};
639 const std::string
id = session.client.uniqueID;
641 return map_id_sess.emplace(
id, std::move(session)).first->second;
644 void fail_pair(pair_session_t &sess, pt::ptree &tree,
const std::string status_msg) {
645 tree.put(
"root.paired", 0);
646 tree.put(
"root.<xmlattr>.status_code", 400);
647 tree.put(
"root.<xmlattr>.status_message", status_msg);
648 remove_session(sess);
649 BOOST_LOG(warning) <<
"Pair attempt failed due to " << status_msg;
652 void getservercert(pair_session_t &sess, pt::ptree &tree,
const std::string &pin) {
653 if (sess.last_phase != PAIR_PHASE::NONE) {
654 fail_pair(sess, tree,
"Out of order call to getservercert");
657 sess.last_phase = PAIR_PHASE::GETSERVERCERT;
659 if (sess.async_insert_pin.salt.size() < 32) {
660 fail_pair(sess, tree,
"Salt too short");
664 std::string_view salt_view {sess.async_insert_pin.salt.data(), 32};
666 auto salt = util::from_hex<std::array<uint8_t, 16>>(salt_view,
true);
668 auto key = crypto::gen_aes_key(salt, pin);
669 sess.cipher_key = std::make_unique<crypto::aes_t>(key);
671 tree.put(
"root.paired", 1);
672 tree.put(
"root.plaincert", util::hex_vec(conf_intern.servercert,
true));
673 tree.put(
"root.<xmlattr>.status_code", 200);
676 void clientchallenge(pair_session_t &sess, pt::ptree &tree,
const std::string &challenge) {
677 if (sess.last_phase != PAIR_PHASE::GETSERVERCERT) {
678 fail_pair(sess, tree,
"Out of order call to clientchallenge");
681 sess.last_phase = PAIR_PHASE::CLIENTCHALLENGE;
683 if (!sess.cipher_key) {
684 fail_pair(sess, tree,
"Cipher key not set");
689 std::vector<uint8_t> decrypted;
690 cipher.decrypt(challenge, decrypted);
692 auto x509 = crypto::x509(conf_intern.servercert);
693 auto sign = crypto::signature(x509);
694 auto serversecret = crypto::rand(16);
696 decrypted.insert(std::end(decrypted), std::begin(sign), std::end(sign));
697 decrypted.insert(std::end(decrypted), std::begin(serversecret), std::end(serversecret));
700 auto serverchallenge = crypto::rand(16);
702 std::string plaintext;
703 plaintext.reserve(
hash.size() + serverchallenge.size());
705 plaintext.insert(std::end(plaintext), std::begin(hash), std::end(hash));
706 plaintext.insert(std::end(plaintext), std::begin(serverchallenge), std::end(serverchallenge));
708 std::vector<uint8_t> encrypted;
709 cipher.encrypt(plaintext, encrypted);
711 sess.serversecret = std::move(serversecret);
712 sess.serverchallenge = std::move(serverchallenge);
714 tree.put(
"root.paired", 1);
715 tree.put(
"root.challengeresponse", util::hex_vec(encrypted,
true));
716 tree.put(
"root.<xmlattr>.status_code", 200);
719 void serverchallengeresp(pair_session_t &sess, pt::ptree &tree,
const std::string &encrypted_response) {
720 if (sess.last_phase != PAIR_PHASE::CLIENTCHALLENGE) {
721 fail_pair(sess, tree,
"Out of order call to serverchallengeresp");
724 sess.last_phase = PAIR_PHASE::SERVERCHALLENGERESP;
726 if (!sess.cipher_key || sess.serversecret.empty()) {
727 fail_pair(sess, tree,
"Cipher key or serversecret not set");
731 std::vector<uint8_t> decrypted;
734 cipher.decrypt(encrypted_response, decrypted);
736 sess.clienthash = std::move(decrypted);
738 auto serversecret = sess.serversecret;
739 auto sign = crypto::sign256(crypto::pkey(conf_intern.pkey), serversecret);
741 serversecret.insert(std::end(serversecret), std::begin(sign), std::end(sign));
743 tree.put(
"root.pairingsecret", util::hex_vec(serversecret,
true));
744 tree.put(
"root.paired", 1);
745 tree.put(
"root.<xmlattr>.status_code", 200);
748 void clientpairingsecret(pair_session_t &sess, pt::ptree &tree,
const std::string &client_pairing_secret) {
749 if (sess.last_phase != PAIR_PHASE::SERVERCHALLENGERESP) {
750 fail_pair(sess, tree,
"Out of order call to clientpairingsecret");
753 sess.last_phase = PAIR_PHASE::CLIENTPAIRINGSECRET;
755 auto &client = sess.client;
757 if (client_pairing_secret.size() <= 16) {
758 fail_pair(sess, tree,
"Client pairing secret too short");
762 std::string_view secret {client_pairing_secret.data(), 16};
763 std::string_view sign {client_pairing_secret.data() + secret.size(), client_pairing_secret.size() - secret.size()};
765 auto x509 = crypto::x509(client.cert);
767 fail_pair(sess, tree,
"Invalid client certificate");
770 auto x509_sign = crypto::signature(x509);
773 data.reserve(sess.serverchallenge.size() + x509_sign.size() + secret.size());
775 data.insert(std::end(data), std::begin(sess.serverchallenge), std::end(sess.serverchallenge));
776 data.insert(std::end(data), std::begin(x509_sign), std::end(x509_sign));
777 data.insert(std::end(data), std::begin(secret), std::end(secret));
782 bool same_hash =
hash.size() == sess.clienthash.size() && std::equal(
hash.begin(),
hash.end(), sess.clienthash.begin());
783 auto verify = crypto::verify256(crypto::x509(client.cert), secret, sign);
784 if (same_hash && verify) {
785 tree.put(
"root.paired", 1);
787 auto named_cert_p = std::make_shared<crypto::named_cert_t>();
788 named_cert_p->name = client.name;
789 for (
char& c : named_cert_p->name) {
790 if (c ==
'(') c =
'[';
791 else if (c ==
')') c =
']';
793 named_cert_p->cert = std::move(client.cert);
794 named_cert_p->uuid = uuid_util::uuid_t::generate().string();
796 if (client_root.named_devices.empty()) {
797 named_cert_p->perm = PERM::_all;
799 named_cert_p->perm = PERM::_default;
802 named_cert_p->enable_legacy_ordering =
true;
803 named_cert_p->allow_client_commands =
true;
804 named_cert_p->always_use_virtual_display =
false;
808 add_authorized_client(named_cert_p);
810 tree.put(
"root.paired", 0);
811 BOOST_LOG(warning) <<
"Pair attempt failed due to same_hash: " << same_hash <<
", verify: " << verify;
815 tree.put(
"root.<xmlattr>.status_code", 200);
823 static auto constexpr to_string =
"HTTPS"sv;
828 static auto constexpr to_string =
"NONE"sv;
836 void print_req(std::shared_ptr<
typename SimpleWeb::ServerBase<T>::Request> request) {
837 BOOST_LOG(debug) <<
"TUNNEL :: "sv << tunnel<T>::to_string;
839 BOOST_LOG(debug) <<
"METHOD :: "sv << request->method;
840 BOOST_LOG(debug) <<
"DESTINATION :: "sv << request->path;
842 for (
auto &[name, val] : request->header) {
843 BOOST_LOG(debug) << name <<
" -- " << val;
846 BOOST_LOG(debug) <<
" [--] "sv;
848 for (
auto &[name, val] : request->parse_query_string()) {
849 BOOST_LOG(debug) << name <<
" -- " << val;
852 BOOST_LOG(debug) <<
" [--] "sv;
856 void not_found(std::shared_ptr<
typename SimpleWeb::ServerBase<T>::Response> response, std::shared_ptr<
typename SimpleWeb::ServerBase<T>::Request> request) {
857 print_req<T>(request);
860 tree.put(
"root.<xmlattr>.status_code", 404);
862 std::ostringstream data;
864 pt::write_xml(data, tree);
865 response->write(SimpleWeb::StatusCode::client_error_not_found, data.str());
866 response->close_connection_after_response =
true;
870 void pair(std::shared_ptr<
typename SimpleWeb::ServerBase<T>::Response> response, std::shared_ptr<
typename SimpleWeb::ServerBase<T>::Request> request) {
871 std::lock_guard lock {pair_session_mutex};
875 auto fg = util::fail_guard([&]() {
876 std::ostringstream data;
878 pt::write_xml(data, tree);
879 response->write(data.str());
880 response->close_connection_after_response =
true;
883 if (!config::sunshine.enable_pairing) {
884 tree.put(
"root.<xmlattr>.status_code", 403);
885 tree.put(
"root.<xmlattr>.status_message",
"Pairing is disabled for this instance");
890 auto args = request->parse_query_string();
891 if (
args.find(
"uniqueid"s) == std::end(
args)) {
892 tree.put(
"root.<xmlattr>.status_code", 400);
893 tree.put(
"root.<xmlattr>.status_message",
"Missing uniqueid parameter");
898 auto uniqID {get_arg(
args,
"uniqueid")};
900 args_t::const_iterator it;
901 if (it =
args.find(
"phrase"); it != std::end(
args)) {
902 if (it->second ==
"getservercert"sv) {
905 auto deviceName { get_arg(
args,
"devicename") };
907 if (deviceName ==
"roth"sv) {
908 deviceName =
"Legacy Moonlight Client";
911 sess.client.uniqueID = std::move(uniqID);
912 sess.client.name = std::move(deviceName);
913 sess.client.cert = util::from_hex_vec(get_arg(
args,
"clientcert"),
true);
917 new_session.async_insert_pin.salt = std::move(get_arg(
args,
"salt"));
919 auto it =
args.find(
"otpauth");
920 if (it != std::end(
args)) {
921 if (one_time_pin.empty() || (std::chrono::steady_clock::now() - otp_creation_time > OTP_EXPIRE_DURATION)) {
922 one_time_pin.clear();
923 otp_passphrase.clear();
924 otp_device_name.clear();
925 tree.put(
"root.<xmlattr>.status_code", 503);
926 tree.put(
"root.<xmlattr>.status_message",
"OTP auth not available.");
928 auto hash = util::hex(
crypto::hash(one_time_pin + new_session.async_insert_pin.salt + otp_passphrase),
true);
930 if (
hash.to_string_view() == it->second) {
932 if (!otp_device_name.empty()) {
933 new_session.client.name = std::move(otp_device_name);
936 getservercert(new_session, tree, one_time_pin);
938 one_time_pin.clear();
939 otp_passphrase.clear();
940 otp_device_name.clear();
946 getservercert(new_session, tree, crypto::rand(16));
953 std::cout <<
"Please insert pin: "sv;
954 std::getline(std::cin, pin);
956 getservercert(new_session, tree, pin);
958#if defined HERMES_TRAY && HERMES_TRAY >= 1
961 set_pending_pair(new_session.client.uniqueID, new_session.client.name);
962 new_session.async_insert_pin.response = std::move(response);
967 }
else if (it->second ==
"pairchallenge"sv) {
968 tree.put(
"root.paired", 1);
969 tree.put(
"root.<xmlattr>.status_code", 200);
974 auto sess_it = map_id_sess.find(uniqID);
975 if (sess_it == std::end(map_id_sess)) {
976 tree.put(
"root.<xmlattr>.status_code", 400);
977 tree.put(
"root.<xmlattr>.status_message",
"Invalid uniqueid");
982 if (it =
args.find(
"clientchallenge"); it != std::end(
args)) {
983 auto challenge = util::from_hex_vec(it->second,
true);
984 clientchallenge(sess_it->second, tree, challenge);
985 }
else if (it =
args.find(
"serverchallengeresp"); it != std::end(
args)) {
986 auto encrypted_response = util::from_hex_vec(it->second,
true);
987 serverchallengeresp(sess_it->second, tree, encrypted_response);
988 }
else if (it =
args.find(
"clientpairingsecret"); it != std::end(
args)) {
989 auto pairingsecret = util::from_hex_vec(it->second,
true);
990 clientpairingsecret(sess_it->second, tree, pairingsecret);
992 tree.put(
"root.<xmlattr>.status_code", 404);
993 tree.put(
"root.<xmlattr>.status_message",
"Invalid pairing request");
997 void cancel_pairing_request(std::shared_ptr<SimpleWeb::ServerBase<SimpleWeb::HTTP>::Response> response,
998 std::shared_ptr<SimpleWeb::ServerBase<SimpleWeb::HTTP>::Request> request) {
999 auto args = request->parse_query_string();
1001 auto id =
args.find(
"uniqueid");
1002 if (
id ==
args.end() || id->second.empty()) {
1003 tree.put(
"root.<xmlattr>.status_code", 400);
1004 tree.put(
"root.<xmlattr>.status_message",
"Missing uniqueid parameter");
1009 tree.put(
"root.<xmlattr>.status_code", 200);
1010 tree.put(
"root.paired", 0);
1012 std::ostringstream data;
1013 pt::write_xml(data, tree);
1014 response->write(data.str());
1015 response->close_connection_after_response =
true;
1020 if (pending_pairs.empty()) {
1021 return std::nullopt;
1023 return pending_pairs.begin()->second;
1026 bool pin(std::string
pin, std::string name) {
1027 std::lock_guard lock {pair_session_mutex};
1029 if (map_id_sess.empty()) {
1034 if (
pin.size() != 4) {
1035 tree.put(
"root.paired", 0);
1036 tree.put(
"root.<xmlattr>.status_code", 400);
1038 "root.<xmlattr>.status_message",
1039 std::format(
"Pin must be 4 digits, {} provided",
pin.size())
1045 if (!std::all_of(
pin.begin(),
pin.end(), ::isdigit)) {
1046 tree.put(
"root.paired", 0);
1047 tree.put(
"root.<xmlattr>.status_code", 400);
1048 tree.put(
"root.<xmlattr>.status_message",
"Pin must be numeric");
1054 auto pending = std::find_if(map_id_sess.begin(), map_id_sess.end(), [](
const auto &entry) {
1055 const auto &session = entry.second;
1056 const auto &response = session.async_insert_pin.response;
1057 return session.last_phase == PAIR_PHASE::NONE &&
1058 ((response.has_left() && response.left()) || (response.has_right() && response.right()));
1060 if (pending == map_id_sess.end()) {
1063 auto &sess = pending->second;
1064 clear_pending_pair(sess.client.uniqueID);
1065 if (!name.empty()) {
1066 sess.client.name = name;
1070 auto async_response = std::move(sess.async_insert_pin.response);
1071 sess.async_insert_pin.response = std::decay_t<
decltype(async_response.left())>();
1072 getservercert(sess, tree,
pin);
1075 std::ostringstream data;
1076 pt::write_xml(data, tree);
1078 if (async_response.has_left() && async_response.left()) {
1079 async_response.left()->write(data.str());
1080 }
else if (async_response.has_right() && async_response.right()) {
1081 async_response.right()->write(data.str());
1091 void serverinfo(std::shared_ptr<
typename SimpleWeb::ServerBase<T>::Response> response, std::shared_ptr<
typename SimpleWeb::ServerBase<T>::Request> request) {
1092 print_req<T>(request);
1094 int pair_status = 0;
1095 if constexpr (std::is_same_v<SunshineHTTPS, T>) {
1096 auto args = request->parse_query_string();
1097 auto clientID =
args.find(
"uniqueid"s);
1099 if (clientID != std::end(
args)) {
1104 auto local_endpoint = request->local_endpoint();
1108 tree.put(
"root.<xmlattr>.status_code", 200);
1109 tree.put(
"root.hostname", config::nvhttp.sunshine_name);
1111 tree.put(
"root.appversion", VERSION);
1112 tree.put(
"root.GfeVersion", GFE_VERSION);
1113 tree.put(
"root.uniqueid", http::unique_id);
1116 tree.put(
"root.MaxLumaPixelsHEVC", video::active_hevc_mode > 1 ?
"1869449984" :
"0");
1120 if constexpr (std::is_same_v<SunshineHTTPS, T>) {
1123 auto named_cert_p = get_verified_cert(request);
1124 if (!!(named_cert_p->perm & PERM::server_cmd)) {
1125 pt::ptree& root_node = tree.get_child(
"root");
1127 if (config::sunshine.server_cmds.size() > 0) {
1129 for (
const auto& cmd : config::sunshine.server_cmds) {
1131 cmd_node.put_value(cmd.cmd_name);
1132 root_node.push_back(std::make_pair(
"ServerCommand", cmd_node));
1136 BOOST_LOG(debug) <<
"Permission Get ServerCommand denied for [" << named_cert_p->name <<
"] (" << (uint32_t)named_cert_p->perm <<
")";
1139 tree.put(
"root.Permission", std::to_string((uint32_t)named_cert_p->perm));
1141 tree.put(
"root.VirtualDisplayCapable",
true);
1142 if (!!(named_cert_p->perm & PERM::_all_actions)) {
1145 tree.put(
"root.VirtualDisplayDriverReady",
true);
1148 tree.put(
"root.mac",
"00:00:00:00:00:00");
1149 tree.put(
"root.Permission",
"0");
1161 if (local_endpoint.address().is_v6() && !local_endpoint.address().to_v6().is_v4_mapped()) {
1162 tree.put(
"root.LocalIP",
"127.0.0.1");
1167 uint32_t codec_mode_flags = SCM_H264;
1168 if (video::last_encoder_probe_supported_yuv444_for_codec[0]) {
1169 codec_mode_flags |= SCM_H264_HIGH8_444;
1171 if (video::active_hevc_mode >= 2) {
1172 codec_mode_flags |= SCM_HEVC;
1173 if (video::last_encoder_probe_supported_yuv444_for_codec[1]) {
1174 codec_mode_flags |= SCM_HEVC_REXT8_444;
1177 if (video::active_hevc_mode >= 3) {
1178 codec_mode_flags |= SCM_HEVC_MAIN10;
1179 if (video::last_encoder_probe_supported_yuv444_for_codec[1]) {
1180 codec_mode_flags |= SCM_HEVC_REXT10_444;
1183 if (video::active_av1_mode >= 2) {
1184 codec_mode_flags |= SCM_AV1_MAIN8;
1185 if (video::last_encoder_probe_supported_yuv444_for_codec[2]) {
1186 codec_mode_flags |= SCM_AV1_HIGH8_444;
1189 if (video::active_av1_mode >= 3) {
1190 codec_mode_flags |= SCM_AV1_MAIN10;
1191 if (video::last_encoder_probe_supported_yuv444_for_codec[2]) {
1192 codec_mode_flags |= SCM_AV1_HIGH10_444;
1195 if (config::video.pyrowave && video::pyrowave_available()) {
1196 codec_mode_flags |= video::PYROWAVE_SERVER_CODEC_MODE;
1198 tree.put(
"root.ServerCodecModeSupport", codec_mode_flags);
1200 tree.put(
"root.PairStatus", pair_status);
1202 if constexpr (std::is_same_v<SunshineHTTPS, T>) {
1203 auto named_cert_p = get_verified_cert(request);
1206 proc::proc.running_for_client(named_cert_p->uuid) :
1208 int current_appid = isolated_appid > 0 ?
1210 proc::proc.running();
1212 if (config::input.enable_input_only_mode && current_appid != proc::input_only_app_id) {
1215 tree.put(
"root.currentgame", current_appid);
1217 "root.currentgameuuid",
1218 isolated_appid > 0 ?
1219 proc::proc.running_app_uuid_for_client(named_cert_p->uuid) :
1220 proc::proc.get_running_app_uuid()
1222 tree.put(
"root.state", current_appid > 0 ?
"SUNSHINE_SERVER_BUSY" :
"SUNSHINE_SERVER_FREE");
1224 tree.put(
"root.currentgame", 0);
1225 tree.put(
"root.currentgameuuid",
"");
1226 tree.put(
"root.state",
"SUNSHINE_SERVER_FREE");
1229 std::ostringstream data;
1231 pt::write_xml(data, tree);
1232 response->write(data.str());
1233 response->close_connection_after_response =
true;
1237 nlohmann::json named_cert_nodes = nlohmann::json::array();
1239 std::list<std::string> connected_uuids = rtsp_stream::get_all_session_uuids();
1241 for (
auto &named_cert : client.named_devices) {
1242 nlohmann::json named_cert_node;
1243 named_cert_node[
"name"] = named_cert->name;
1244 named_cert_node[
"uuid"] = named_cert->uuid;
1245 named_cert_node[
"display_mode"] = named_cert->display_mode;
1246 named_cert_node[
"perm"] =
static_cast<uint32_t
>(named_cert->perm);
1247 named_cert_node[
"enable_legacy_ordering"] = named_cert->enable_legacy_ordering;
1248 named_cert_node[
"allow_client_commands"] = named_cert->allow_client_commands;
1249 named_cert_node[
"always_use_virtual_display"] = named_cert->always_use_virtual_display;
1252 if (!named_cert->do_cmds.empty()) {
1253 nlohmann::json do_cmds_node = nlohmann::json::array();
1254 for (
const auto &cmd : named_cert->do_cmds) {
1255 do_cmds_node.push_back(crypto::command_entry_t::serialize(cmd));
1257 named_cert_node[
"do"] = do_cmds_node;
1261 if (!named_cert->undo_cmds.empty()) {
1262 nlohmann::json undo_cmds_node = nlohmann::json::array();
1263 for (
const auto &cmd : named_cert->undo_cmds) {
1264 undo_cmds_node.push_back(crypto::command_entry_t::serialize(cmd));
1266 named_cert_node[
"undo"] = undo_cmds_node;
1270 bool connected =
false;
1271 if (connected_uuids.empty()) {
1274 for (
auto it = connected_uuids.begin(); it != connected_uuids.end(); ++it) {
1275 if (*it == named_cert->uuid) {
1277 connected_uuids.erase(it);
1282 named_cert_node[
"connected"] = connected;
1284 named_cert_nodes.push_back(named_cert_node);
1287 return named_cert_nodes;
1290 void applist(resp_https_t response, req_https_t request) {
1291 print_req<SunshineHTTPS>(request);
1295 auto g = util::fail_guard([&]() {
1296 std::ostringstream data;
1298 pt::write_xml(data, tree);
1299 response->write(data.str());
1300 response->close_connection_after_response =
true;
1303 auto &apps = tree.add_child(
"root", pt::ptree {});
1305 apps.put(
"<xmlattr>.status_code", 200);
1307 auto named_cert_p = get_verified_cert(request);
1308 if (!!(named_cert_p->perm & PERM::_all_actions)) {
1309 const auto isolated_appid = experimental_isolated_sessions_enabled() ?
1310 proc::proc.running_for_client(named_cert_p->uuid) :
1312 auto current_appid = isolated_appid > 0 ?
1314 proc::proc.running();
1315 auto should_hide_inactive_apps = config::input.enable_input_only_mode && current_appid > 0 && current_appid != proc::input_only_app_id;
1317 auto app_list = proc::proc.get_apps();
1319 bool enable_legacy_ordering = config::sunshine.legacy_ordering && named_cert_p->enable_legacy_ordering;
1321 if (enable_legacy_ordering) {
1322 bits = zwpad::pad_width_for_count(app_list.size());
1325 for (
size_t i = 0; i < app_list.size(); i++) {
1326 auto&
app = app_list[i];
1327 auto appid = util::from_view(
app.id);
1328 if (should_hide_inactive_apps) {
1330 appid != current_appid
1331 && appid != proc::input_only_app_id
1332 && appid != proc::terminate_app_id
1337 if (appid == proc::terminate_app_id) {
1342 std::string app_name;
1343 if (enable_legacy_ordering) {
1344 app_name = zwpad::pad_for_ordering(
app.name, bits, i);
1346 app_name =
app.name;
1351 app_node.put(
"IsHdrSupported"s, video::active_hevc_mode == 3 ? 1 : 0);
1352 app_node.put(
"AppTitle"s, app_name);
1353 app_node.put(
"UUID",
app.uuid);
1354 app_node.put(
"IDX",
app.idx);
1355 app_node.put(
"ID",
app.id);
1357 apps.push_back(std::make_pair(
"App", std::move(app_node)));
1360 BOOST_LOG(debug) <<
"Permission ListApp denied for [" << named_cert_p->name <<
"] (" << (uint32_t)named_cert_p->perm <<
")";
1364 app_node.put(
"IsHdrSupported"s, 0);
1365 app_node.put(
"AppTitle"s,
"Permission Denied");
1366 app_node.put(
"UUID",
"");
1367 app_node.put(
"IDX",
"0");
1368 app_node.put(
"ID",
"114514");
1370 apps.push_back(std::make_pair(
"App", std::move(app_node)));
1377 void launch(
bool &host_audio, resp_https_t response, req_https_t request) {
1378 print_req<SunshineHTTPS>(request);
1381 auto g = util::fail_guard([&]() {
1382 std::ostringstream data;
1384 pt::write_xml(data, tree);
1385 response->write(data.str());
1386 response->close_connection_after_response =
true;
1389 auto args = request->parse_query_string();
1391 auto appid_str = get_arg(
args,
"appid",
"0");
1392 auto appuuid_str = get_arg(
args,
"appuuid",
"");
1393 auto appid = util::from_view(appid_str);
1394 auto named_cert_p = get_verified_cert(request);
1397 const auto host_current_appid = proc::proc.running();
1398 const auto host_current_app_uuid = proc::proc.get_running_app_uuid();
1400 proc::proc.running_for_client(named_cert_p->uuid) :
1403 proc::proc.running_app_uuid_for_client(named_cert_p->uuid) :
1406 bool is_input_only =
1407 config::input.enable_input_only_mode &&
1408 (appid == proc::input_only_app_id || appuuid_str == REMOTE_INPUT_UUID);
1411 if (!is_input_only && (appid > 0 || !appuuid_str.empty())) {
1412 const auto &apps = proc::proc.get_apps();
1413 const auto app_iter = std::find_if(
1416 [&appid_str, &appuuid_str](
const auto &app) {
1417 return app.id == appid_str || app.uuid == appuuid_str;
1420 if (app_iter != apps.end()) {
1421 requested_app = &*app_iter;
1425 auto perm = PERM::launch;
1427 BOOST_LOG(verbose) <<
"Launching app [" << appid_str <<
"] with UUID [" << appuuid_str <<
"]";
1433 bool permission_isolated =
false;
1434 if (requested_app) {
1435 permission_isolated =
1440 config::video.isolation_method
1441 ) != proc::session_route_e::shared;
1444 const auto permission_current_appid =
1445 permission_isolated ? isolated_current_appid : host_current_appid;
1446 const auto &permission_current_app_uuid =
1447 permission_isolated ? isolated_current_app_uuid : host_current_app_uuid;
1450 !permission_isolated
1451 && permission_current_appid > 0
1452 && (appuuid_str != TERMINATE_APP_UUID || appid != proc::terminate_app_id)
1455 || appid == permission_current_appid
1456 || (!appuuid_str.empty() && appuuid_str == permission_current_app_uuid)
1459 perm = PERM::_allow_view;
1462 if (!(named_cert_p->perm & perm)) {
1463 BOOST_LOG(debug) <<
"Permission LaunchApp denied for [" << named_cert_p->name <<
"] (" << (uint32_t)named_cert_p->perm <<
")";
1465 tree.put(
"root.resume", 0);
1466 tree.put(
"root.<xmlattr>.status_code", 403);
1467 tree.put(
"root.<xmlattr>.status_message",
"Permission denied");
1472 args.find(
"rikey"s) == std::end(
args) ||
1473 args.find(
"rikeyid"s) == std::end(
args) ||
1474 args.find(
"localAudioPlayMode"s) == std::end(
args) ||
1475 (
args.find(
"appid"s) == std::end(
args) &&
args.find(
"appuuid"s) == std::end(
args))
1477 tree.put(
"root.resume", 0);
1478 tree.put(
"root.<xmlattr>.status_code", 400);
1479 tree.put(
"root.<xmlattr>.status_message",
"Missing a required launch parameter");
1484 if (!is_input_only) {
1487 (config::input.enable_input_only_mode && appid == proc::terminate_app_id)
1488 || appuuid_str == TERMINATE_APP_UUID
1490 const auto stream_route =
1491 rtsp_stream::session_is_isolated(named_cert_p->uuid);
1492 const bool terminate_isolated =
1493 stream_route.value_or(
1494 isolated_sessions &&
1495 proc::proc.isolated_client_present(named_cert_p->uuid)
1499 BOOST_LOG(info) <<
"Client " << named_cert_p->name <<
" launched Terminate" << (terminate_isolated ?
" for its isolated session." :
".");
1500 if (terminate_isolated) {
1502 proc::proc.terminate_isolated_client(named_cert_p->uuid);
1504 proc::proc.terminate();
1507 tree.put(
"root.resume", 0);
1508 tree.put(
"root.<xmlattr>.status_code", 410);
1509 tree.put(
"root.<xmlattr>.status_message",
"App terminated.");
1516 host_audio = util::from_view(get_arg(
args,
"localAudioPlayMode"));
1517 auto launch_session = make_launch_session(host_audio, is_input_only,
args, named_cert_p);
1518 if (!is_input_only) {
1520 launch_session->width = hestia_prepare->width;
1521 launch_session->height = hestia_prepare->height;
1522 launch_session->fps = hestia_prepare->fps * 1000;
1523 launch_session->enable_hdr = hestia_prepare->hdr;
1524 launch_session->virtual_display = hestia_prepare->virtual_display;
1525 launch_session->client_virtual_display = hestia_prepare->virtual_display;
1526 launch_session->scale_factor = hestia_prepare->scale_factor;
1527 launch_session->client_scale_factor =
static_cast<int>(hestia_prepare->scale_factor);
1528 launch_session->launch_mode = hestia_prepare->launch_mode;
1529 BOOST_LOG(info) <<
"[HestiaAPI] applying prepared session for client " << named_cert_p->uuid
1530 <<
": " << launch_session->width <<
'x' << launch_session->height
1531 <<
'@' << hestia_prepare->fps <<
"Hz virtual_display=" << launch_session->virtual_display
1532 <<
" launch_mode=" << launch_session->launch_mode;
1536 auto session_route = proc::session_route_e::shared;
1537 if (requested_app) {
1541 config::video.headless_mode
1543 if (launch_session->client_virtual_display && decision.use != *launch_session->client_virtual_display) {
1544 BOOST_LOG(info) <<
"[Settings] Virtual display " << (decision.use ?
"on" :
"off") <<
" for ["
1545 << requested_app->name <<
"]: the client asked for "
1546 << (*launch_session->client_virtual_display ?
"on" :
"off") <<
", but "
1549 BOOST_LOG(info) <<
"[Settings] Virtual display " << (decision.use ?
"on" :
"off") <<
" for ["
1550 << requested_app->name <<
"]: " <<
proc::describe(decision.source) <<
'.';
1552 launch_session->virtual_display = decision.use;
1553 launch_session->virtual_display_decided =
true;
1555 if (launch_session->client_scale_factor && scale.percent != *launch_session->client_scale_factor) {
1556 BOOST_LOG(info) <<
"[Settings] Resolution scale " << scale.percent <<
"% for [" << requested_app->name
1557 <<
"]: the client asked for " << *launch_session->client_scale_factor <<
"%, but "
1560 BOOST_LOG(info) <<
"[Settings] Resolution scale " << scale.percent <<
"% for [" << requested_app->name
1563 const bool virtual_display_requested = decision.use;
1567 virtual_display_requested,
1568 config::video.isolation_method
1572 const bool isolated_launch =
1573 session_route != proc::session_route_e::shared;
1575 const auto current_appid =
1576 isolated_launch ? isolated_current_appid : host_current_appid;
1577 const auto ¤t_app_uuid =
1578 isolated_launch ? isolated_current_app_uuid : host_current_app_uuid;
1583 && host_current_appid > 0
1584 && host_current_appid != proc::input_only_app_id
1586 (appid > 0 && appid != host_current_appid)
1587 || (!appuuid_str.empty() && appuuid_str != host_current_app_uuid)
1590 tree.put(
"root.resume", 0);
1591 tree.put(
"root.<xmlattr>.status_code", 400);
1592 tree.put(
"root.<xmlattr>.status_message",
"An app is already running on this host");
1597 if (!launch_session->rtsp_cipher && encryption_mode == config::ENCRYPTION_MODE_MANDATORY) {
1598 BOOST_LOG(error) <<
"Rejecting client that cannot comply with mandatory encryption requirement"sv;
1600 tree.put(
"root.<xmlattr>.status_code", 403);
1601 tree.put(
"root.<xmlattr>.status_message",
"Encryption is mandatory for this host but unsupported by the client");
1602 tree.put(
"root.gamesession", 0);
1607 bool no_active_sessions = rtsp_stream::shared_session_count() == 0;
1610 bool started_here =
false;
1612 if (is_input_only) {
1613 BOOST_LOG(info) <<
"Launching input only session..."sv;
1615 launch_session->client_do_cmds.clear();
1616 launch_session->client_undo_cmds.clear();
1620 if (no_active_sessions && !proc::proc.virtual_display) {
1622 if (current_appid == 0) {
1623 proc::proc.launch_input_only();
1626 }
else if (appid > 0 || !appuuid_str.empty()) {
1627 if (isolated_launch) {
1628 const auto &apps = proc::proc.get_apps();
1629 const auto app_iter = std::find_if(
1632 [&appid_str, &appuuid_str](
const auto &app) {
1633 return app.id == appid_str || app.uuid == appuuid_str;
1636 if (app_iter == apps.end()) {
1637 BOOST_LOG(error) <<
"Couldn't find app with ID [" << appid_str
1638 <<
"] or UUID [" << appuuid_str <<
']';
1639 tree.put(
"root.<xmlattr>.status_code", 404);
1640 tree.put(
"root.<xmlattr>.status_message",
"Cannot find requested application");
1641 tree.put(
"root.gamesession", 0);
1644 if (!app_iter->allow_client_commands) {
1645 launch_session->client_do_cmds.clear();
1646 launch_session->client_undo_cmds.clear();
1649 const auto err = proc::proc.execute_isolated(*app_iter, launch_session);
1651 tree.put(
"root.<xmlattr>.status_code", err);
1652 if (!launch_session->launch_error_message.empty()) {
1653 tree.put(
"root.<xmlattr>.status_message", launch_session->launch_error_message);
1656 "root.<xmlattr>.status_message",
1658 "This client already owns an isolated session." :
1660 "Failed to start the isolated compositor/capture session." :
1661 "Failed to start the specified application"
1664 tree.put(
"root.gamesession", 0);
1667 started_here =
true;
1668 }
else if (appid == current_appid || (!appuuid_str.empty() && appuuid_str == current_app_uuid)) {
1671 BOOST_LOG(debug) <<
"Resuming app [" << proc::proc.get_last_run_app_name() <<
"] from launch app path...";
1673 if (!proc::proc.allow_client_commands || !named_cert_p->allow_client_commands) {
1674 launch_session->client_do_cmds.clear();
1675 launch_session->client_undo_cmds.clear();
1678 if (current_appid == proc::input_only_app_id) {
1679 launch_session->input_only =
true;
1682 if (no_active_sessions && !proc::proc.virtual_display) {
1685 tree.put(
"root.resume", 0);
1686 tree.put(
"root.<xmlattr>.status_code", 503);
1687 tree.put(
"root.<xmlattr>.status_message",
"Failed to initialize video capture/encoding. Is a display connected and turned on?");
1693 const auto& apps = proc::proc.get_apps();
1694 auto app_iter = std::find_if(apps.begin(), apps.end(), [&appid_str, &appuuid_str](
const auto _app) {
1695 return _app.id == appid_str || _app.uuid == appuuid_str;
1698 if (app_iter == apps.end()) {
1699 BOOST_LOG(error) <<
"Couldn't find app with ID ["sv << appid_str <<
"] or UUID ["sv << appuuid_str <<
']';
1700 tree.put(
"root.<xmlattr>.status_code", 404);
1701 tree.put(
"root.<xmlattr>.status_message",
"Cannot find requested application");
1702 tree.put(
"root.gamesession", 0);
1706 if (!app_iter->allow_client_commands) {
1707 launch_session->client_do_cmds.clear();
1708 launch_session->client_undo_cmds.clear();
1711 auto err = proc::proc.execute(*app_iter, launch_session);
1713 tree.put(
"root.<xmlattr>.status_code", err);
1714 if (!launch_session->launch_error_message.empty()) {
1715 tree.put(
"root.<xmlattr>.status_message", launch_session->launch_error_message);
1718 "root.<xmlattr>.status_message",
1720 ?
"Failed to initialize video capture/encoding. Is a display connected and turned on?"
1721 :
"Failed to start the specified application");
1723 tree.put(
"root.gamesession", 0);
1727 started_here =
true;
1730 tree.put(
"root.<xmlattr>.status_code", 403);
1731 tree.put(
"root.<xmlattr>.status_message",
"How did you get here?");
1732 tree.put(
"root.gamesession", 0);
1735 if (!is_input_only &&
1737 config::video.virtual_display_backend ==
"hermes_kms" &&
1738 config::video.hermes_kms_multi_output &&
1739 (proc::proc.virtual_display || launch_session->virtual_display || config::video.headless_mode) &&
1740 !launch_session->session_scoped_virtual_display) {
1741 if (
const int result = proc::proc.prepare_session_virtual_display(launch_session); result != 0) {
1742 tree.put(
"root.<xmlattr>.status_code", result);
1743 tree.put(
"root.<xmlattr>.status_message",
"No independent Hermes-KMS output is available for this client.");
1744 tree.put(
"root.gamesession", 0);
1753 stream.display_name = launch_session->display_name;
1754 stream.kwin_wayland_socket = launch_session->kwin_wayland_socket;
1755 stream.kwin_pipewire_socket = launch_session->kwin_pipewire_socket;
1758 stream.dynamicRange = launch_session->enable_hdr ? 1 : 0;
1759 stream.framerate = launch_session->fps / 1000;
1760 std::string failure;
1762 if (config::video.ignore_encoder_probe_failure) {
1764 BOOST_LOG(warning) <<
"Capture readiness: " << failure <<
"; continuing due to user configuration."sv;
1766 BOOST_LOG(error) <<
"Capture readiness: " << failure;
1770 if (isolated_launch) {
1771 proc::proc.terminate_isolated(
1772 launch_session->isolated_runtime_owner_id ? launch_session->isolated_runtime_owner_id : launch_session->id
1775 proc::proc.terminate();
1780 if (!isolated_launch && launch_session->session_virtual_display_cleanup_pending) {
1782 launch_session->session_virtual_display_cleanup_pending =
false;
1783 launch_session->session_scoped_virtual_display =
false;
1784 launch_session->display_name.clear();
1786 tree.put(
"root.<xmlattr>.status_code", 503);
1787 tree.put(
"root.<xmlattr>.status_message", failure +
".");
1788 tree.put(
"root.gamesession", 0);
1792 }
else if (!is_input_only) {
1799 tree.put(
"root.<xmlattr>.status_code", 503);
1800 tree.put(
"root.<xmlattr>.status_message",
"Another client is still completing its RTSP handshake. Please retry.");
1801 tree.put(
"root.gamesession", 0);
1805 tree.put(
"root.<xmlattr>.status_code", 200);
1810 launch_session->rtsp_url_scheme,
1812 static_cast<int>(
net::map_port(rtsp_stream::RTSP_SETUP_PORT))
1815 tree.put(
"root.gamesession", 1);
1818 void resume(
bool &host_audio, resp_https_t response, req_https_t request) {
1819 print_req<SunshineHTTPS>(request);
1822 auto g = util::fail_guard([&]() {
1823 std::ostringstream data;
1825 pt::write_xml(data, tree);
1826 response->write(data.str());
1827 response->close_connection_after_response =
true;
1830 auto named_cert_p = get_verified_cert(request);
1831 const bool isolated_sessions_enabled = experimental_isolated_sessions_enabled();
1832 if (!(named_cert_p->perm & PERM::_allow_view)) {
1833 BOOST_LOG(debug) <<
"Permission ViewApp denied for [" << named_cert_p->name <<
"] (" << (uint32_t)named_cert_p->perm <<
")";
1835 tree.put(
"root.resume", 0);
1836 tree.put(
"root.<xmlattr>.status_code", 403);
1837 tree.put(
"root.<xmlattr>.status_message",
"Permission denied");
1842 const auto isolated_current_appid = isolated_sessions_enabled ?
1843 proc::proc.running_for_client(named_cert_p->uuid) :
1845 const bool isolated_resume = isolated_current_appid > 0;
1846 const auto current_appid = isolated_resume ?
1847 isolated_current_appid :
1848 proc::proc.running();
1850 if (isolated_resume && rtsp_stream::find_session(named_cert_p->uuid)) {
1851 tree.put(
"root.resume", 0);
1852 tree.put(
"root.<xmlattr>.status_code", 409);
1853 tree.put(
"root.<xmlattr>.status_message",
"This client already has an active stream");
1856 if (current_appid == 0) {
1857 tree.put(
"root.resume", 0);
1858 tree.put(
"root.<xmlattr>.status_code", 503);
1859 tree.put(
"root.<xmlattr>.status_message",
"No running app to resume");
1864 auto args = request->parse_query_string();
1866 args.find(
"rikey"s) == std::end(
args) ||
1867 args.find(
"rikeyid"s) == std::end(
args)
1869 tree.put(
"root.resume", 0);
1870 tree.put(
"root.<xmlattr>.status_code", 400);
1871 tree.put(
"root.<xmlattr>.status_message",
"Missing a required resume parameter");
1880 const bool no_active_shared_sessions {rtsp_stream::shared_session_count() == 0};
1881 if (no_active_sessions &&
args.find(
"localAudioPlayMode"s) != std::end(
args)) {
1882 host_audio = util::from_view(get_arg(
args,
"localAudioPlayMode"));
1884 auto launch_session = make_launch_session(host_audio,
false,
args, named_cert_p);
1886 if ((!isolated_resume && !proc::proc.allow_client_commands) ||
1887 !named_cert_p->allow_client_commands) {
1888 launch_session->client_do_cmds.clear();
1889 launch_session->client_undo_cmds.clear();
1892 if (isolated_resume && !proc::proc.prepare_isolated_resume(launch_session)) {
1893 tree.put(
"root.resume", 0);
1894 tree.put(
"root.<xmlattr>.status_code", 503);
1895 tree.put(
"root.<xmlattr>.status_message", launch_session->launch_error_message.empty() ?
"No isolated session is available to resume" : launch_session->launch_error_message);
1899 if (config::input.enable_input_only_mode && current_appid == proc::input_only_app_id) {
1900 launch_session->input_only =
true;
1903 if (!isolated_resume && no_active_shared_sessions && !proc::proc.virtual_display) {
1914 tree.put(
"root.resume", 0);
1915 tree.put(
"root.<xmlattr>.status_code", 503);
1916 tree.put(
"root.<xmlattr>.status_message",
"Failed to initialize video capture/encoding. Is a display connected and turned on?");
1921 else if (!isolated_resume && no_active_shared_sessions && proc::proc.virtual_display && !proc::proc.display_name.empty()) {
1926 int target_fps = launch_session->fps ? launch_session->fps : 60000;
1927 if (target_fps < 1000) {
1930 if (launch_session->width > 0 && launch_session->height > 0) {
1939 proc::proc.display_name.c_str(),
1940 launch_session->width,
1941 launch_session->height,
1945 if (change_result != 0) {
1948 BOOST_LOG(warning) <<
"Virtual display did not reach "sv
1949 << launch_session->width <<
'x' << launch_session->height
1950 <<
"; streaming the display's active mode instead."sv;
1955 tree.put(
"root.resume", 0);
1956 tree.put(
"root.<xmlattr>.status_code", 503);
1957 tree.put(
"root.<xmlattr>.status_message",
"The virtual display could not apply the requested HDR/SDR mode.");
1962 tree.put(
"root.resume", 0);
1963 tree.put(
"root.<xmlattr>.status_code", 503);
1964 tree.put(
"root.<xmlattr>.status_message",
"Failed to initialize video capture/encoding. Is a display connected and turned on?");
1971 if (!launch_session->rtsp_cipher && encryption_mode == config::ENCRYPTION_MODE_MANDATORY) {
1972 BOOST_LOG(error) <<
"Rejecting client that cannot comply with mandatory encryption requirement"sv;
1974 tree.put(
"root.<xmlattr>.status_code", 403);
1975 tree.put(
"root.<xmlattr>.status_message",
"Encryption is mandatory for this host but unsupported by the client");
1976 tree.put(
"root.gamesession", 0);
1981 if (config::video.virtual_display_backend ==
"hermes_kms" &&
1983 config::video.hermes_kms_multi_output &&
1984 (proc::proc.virtual_display || launch_session->virtual_display || config::video.headless_mode)) {
1985 if (
const int result = proc::proc.prepare_session_virtual_display(launch_session); result != 0) {
1986 tree.put(
"root.resume", 0);
1987 tree.put(
"root.<xmlattr>.status_code", result);
1988 tree.put(
"root.<xmlattr>.status_message",
"No independent Hermes-KMS output is available for this client.");
1994 tree.put(
"root.resume", 0);
1995 tree.put(
"root.<xmlattr>.status_code", 503);
1996 tree.put(
"root.<xmlattr>.status_message",
"Another client is still completing its RTSP handshake. Please retry.");
2000 tree.put(
"root.<xmlattr>.status_code", 200);
2005 launch_session->rtsp_url_scheme,
2007 static_cast<int>(
net::map_port(rtsp_stream::RTSP_SETUP_PORT))
2010 tree.put(
"root.resume", 1);
2012#if defined HERMES_TRAY && HERMES_TRAY >= 1
2013 system_tray::update_tray_client_connected(named_cert_p->name);
2017 void cancel(resp_https_t response, req_https_t request) {
2018 print_req<SunshineHTTPS>(request);
2021 auto g = util::fail_guard([&]() {
2022 std::ostringstream data;
2024 pt::write_xml(data, tree);
2025 response->write(data.str());
2026 response->close_connection_after_response =
true;
2029 auto named_cert_p = get_verified_cert(request);
2030 if (!(named_cert_p->perm & PERM::launch)) {
2031 BOOST_LOG(debug) <<
"Permission CancelApp denied for [" << named_cert_p->name <<
"] (" << (uint32_t)named_cert_p->perm <<
")";
2033 tree.put(
"root.resume", 0);
2034 tree.put(
"root.<xmlattr>.status_code", 403);
2035 tree.put(
"root.<xmlattr>.status_message",
"Permission denied");
2040 tree.put(
"root.cancel", 1);
2041 tree.put(
"root.<xmlattr>.status_code", 200);
2043 const auto stream_route =
2044 rtsp_stream::session_is_isolated(named_cert_p->uuid);
2045 const bool cancel_isolated =
2046 stream_route.value_or(
2047 experimental_isolated_sessions_enabled() &&
2048 proc::proc.isolated_client_present(named_cert_p->uuid)
2052 BOOST_LOG(info) <<
"Client " << named_cert_p->name <<
" quit its app" << (cancel_isolated ?
" and isolated session." :
".");
2053 if (cancel_isolated) {
2055 proc::proc.terminate_isolated_client(named_cert_p->uuid);
2058 if (proc::proc.running() > 0) {
2059 proc::proc.terminate();
2066 void appasset(resp_https_t response, req_https_t request) {
2067 print_req<SunshineHTTPS>(request);
2069 auto fg = util::fail_guard([&]() {
2070 response->write(SimpleWeb::StatusCode::server_error_internal_server_error);
2071 response->close_connection_after_response =
true;
2074 auto named_cert_p = get_verified_cert(request);
2076 if (!(named_cert_p->perm & PERM::_all_actions)) {
2077 BOOST_LOG(debug) <<
"Permission Get AppAsset denied for [" << named_cert_p->name <<
"] (" << (uint32_t)named_cert_p->perm <<
")";
2080 response->write(SimpleWeb::StatusCode::client_error_unauthorized);
2081 response->close_connection_after_response =
true;
2085 auto args = request->parse_query_string();
2086 auto app_image = proc::proc.get_app_image(util::from_view(get_arg(
args,
"appid")));
2090 std::ifstream in(app_image, std::ios::binary);
2091 SimpleWeb::CaseInsensitiveMultimap headers;
2092 headers.emplace(
"Content-Type",
"image/png");
2093 response->write(SimpleWeb::StatusCode::success_ok, in, headers);
2094 response->close_connection_after_response =
true;
2097 void getClipboard(resp_https_t response, req_https_t request) {
2098 print_req<SunshineHTTPS>(request);
2100 auto named_cert_p = get_verified_cert(request);
2103 !(named_cert_p->perm & PERM::_allow_view)
2104 || !(named_cert_p->perm & PERM::clipboard_read)
2106 BOOST_LOG(debug) <<
"Permission Read Clipboard denied for [" << named_cert_p->name <<
"] (" << (uint32_t)named_cert_p->perm <<
")";
2108 response->write(SimpleWeb::StatusCode::client_error_unauthorized);
2109 response->close_connection_after_response =
true;
2113 auto args = request->parse_query_string();
2114 auto clipboard_type = get_arg(
args,
"type");
2115 if (clipboard_type !=
"text"sv) {
2116 BOOST_LOG(debug) <<
"Clipboard type [" << clipboard_type <<
"] is not supported!";
2118 response->write(SimpleWeb::StatusCode::client_error_bad_request);
2119 response->close_connection_after_response =
true;
2123 std::list<std::string> connected_uuids = rtsp_stream::get_all_session_uuids();
2125 bool found = !connected_uuids.empty();
2128 found = (std::find(connected_uuids.begin(), connected_uuids.end(), named_cert_p->uuid) != connected_uuids.end());
2132 BOOST_LOG(debug) <<
"Client ["<< named_cert_p->name <<
"] trying to get clipboard is not connected to a stream";
2134 response->write(SimpleWeb::StatusCode::client_error_forbidden);
2135 response->close_connection_after_response =
true;
2139 std::string content = platf::get_clipboard();
2140 response->write(content);
2145 setClipboard(resp_https_t response, req_https_t request) {
2146 print_req<SunshineHTTPS>(request);
2148 auto named_cert_p = get_verified_cert(request);
2151 !(named_cert_p->perm & PERM::_allow_view)
2152 || !(named_cert_p->perm & PERM::clipboard_set)
2154 BOOST_LOG(debug) <<
"Permission Write Clipboard denied for [" << named_cert_p->name <<
"] (" << (uint32_t)named_cert_p->perm <<
")";
2156 response->write(SimpleWeb::StatusCode::client_error_unauthorized);
2157 response->close_connection_after_response =
true;
2161 auto args = request->parse_query_string();
2162 auto clipboard_type = get_arg(
args,
"type");
2163 if (clipboard_type !=
"text"sv) {
2164 BOOST_LOG(debug) <<
"Clipboard type [" << clipboard_type <<
"] is not supported!";
2166 response->write(SimpleWeb::StatusCode::client_error_bad_request);
2167 response->close_connection_after_response =
true;
2171 std::list<std::string> connected_uuids = rtsp_stream::get_all_session_uuids();
2173 bool found = !connected_uuids.empty();
2176 found = (std::find(connected_uuids.begin(), connected_uuids.end(), named_cert_p->uuid) != connected_uuids.end());
2180 BOOST_LOG(debug) <<
"Client ["<< named_cert_p->name <<
"] trying to set clipboard is not connected to a stream";
2182 response->write(SimpleWeb::StatusCode::client_error_forbidden);
2183 response->close_connection_after_response =
true;
2187 std::string content = request->content.string();
2189 bool success = platf::set_clipboard(content);
2192 BOOST_LOG(debug) <<
"Setting clipboard failed!";
2194 response->write(SimpleWeb::StatusCode::server_error_internal_server_error);
2195 response->close_connection_after_response =
true;
2202 void setup(
const std::string &pkey,
const std::string &cert) {
2203 conf_intern.pkey = pkey;
2204 conf_intern.servercert = cert;
2208 return certificate !=
nullptr && cert_chain.verify(certificate, named_cert_out) ==
nullptr;
2212 std::lock_guard lock(pending_hestia_session_prepares_mutex);
2213 pending_hestia_session_prepares[client_uuid] = {
2214 .prepare = std::move(prepare),
2215 .expires_at = std::chrono::steady_clock::now() + 60s,
2220 std::lock_guard lock(pending_hestia_session_prepares_mutex);
2221 const auto it = pending_hestia_session_prepares.find(client_uuid);
2222 if (it == pending_hestia_session_prepares.end()) {
2223 return std::nullopt;
2226 if (it->second.expires_at < std::chrono::steady_clock::now()) {
2227 pending_hestia_session_prepares.erase(it);
2228 return std::nullopt;
2231 auto prepare = std::move(it->second.prepare);
2232 pending_hestia_session_prepares.erase(it);
2237 std::lock_guard lock(pending_hestia_session_prepares_mutex);
2238 pending_hestia_session_prepares.erase(client_uuid);
2242 auto shutdown_event =
mail::man->event<
bool>(mail::shutdown);
2262 https_server_t https_server {config::nvhttp.cert, config::nvhttp.pkey};
2263 http_server_t http_server;
2266 https_server.verify = [](req_https_t req, SSL *ssl) {
2267 crypto::x509_t x509 {
2268#if OPENSSL_VERSION_MAJOR >= 3
2269 SSL_get1_peer_certificate(ssl)
2271 SSL_get_peer_certificate(ssl)
2275 BOOST_LOG(info) <<
"unknown -- denied"sv;
2279 bool verified =
false;
2280 p_named_cert_t named_cert_p;
2282 auto fg = util::fail_guard([&]() {
2283 char subject_name[256];
2285 X509_NAME_oneline(X509_get_subject_name(x509.get()), subject_name,
sizeof(subject_name));
2288 BOOST_LOG(debug) << subject_name <<
" -- "sv <<
"verified, device name: "sv << named_cert_p->name;
2290 BOOST_LOG(debug) << subject_name <<
" -- "sv <<
"denied"sv;
2295 auto err_str = cert_chain.verify(x509.get(), named_cert_p);
2297 BOOST_LOG(warning) <<
"SSL Verification error :: "sv << err_str;
2302 req->userp = named_cert_p;
2307 https_server.on_verify_failed = [](resp_https_t resp, req_https_t req) {
2309 auto g = util::fail_guard([&]() {
2310 std::ostringstream data;
2312 pt::write_xml(data, tree);
2313 resp->write(data.str());
2314 resp->close_connection_after_response =
true;
2317 tree.put(
"root.<xmlattr>.status_code"s, 401);
2318 tree.put(
"root.<xmlattr>.query"s, req->path);
2319 tree.put(
"root.<xmlattr>.status_message"s,
"The client is not authorized. Certificate verification failed."s);
2322 https_server.default_resource[
"GET"] = not_found<SunshineHTTPS>;
2323 https_server.resource[
"^/serverinfo$"][
"GET"] = serverinfo<SunshineHTTPS>;
2324 https_server.resource[
"^/pair$"][
"GET"] = pair<SunshineHTTPS>;
2325 https_server.resource[
"^/applist$"][
"GET"] = applist;
2326 https_server.resource[
"^/appasset$"][
"GET"] = appasset;
2327 https_server.resource[
"^/launch$"][
"GET"] = [&host_audio](
auto resp,
auto req) {
2328 launch(host_audio, resp, req);
2330 https_server.resource[
"^/resume$"][
"GET"] = [&host_audio](
auto resp,
auto req) {
2331 resume(host_audio, resp, req);
2333 https_server.resource[
"^/cancel$"][
"GET"] = cancel;
2334 https_server.resource[
"^/actions/clipboard$"][
"GET"] = getClipboard;
2335 https_server.resource[
"^/actions/clipboard$"][
"POST"] = setClipboard;
2337 https_server.config.reuse_address =
true;
2339 https_server.config.port = port_https;
2341 http_server.default_resource[
"GET"] = not_found<SimpleWeb::HTTP>;
2342 http_server.resource[
"^/serverinfo$"][
"GET"] = serverinfo<SimpleWeb::HTTP>;
2343 http_server.resource[
"^/pair$"][
"GET"] = pair<SimpleWeb::HTTP>;
2344 http_server.resource[
"^/unpair$"][
"GET"] = cancel_pairing_request;
2346 http_server.config.reuse_address =
true;
2348 http_server.config.port = port_http;
2350 auto accept_and_run = [&](
auto *http_server) {
2352 http_server->start();
2353 }
catch (boost::system::system_error &err) {
2355 if (shutdown_event->peek()) {
2359 BOOST_LOG(fatal) <<
"Couldn't start http server on ports ["sv << port_http <<
", "sv << port_https <<
"]: "sv << err.what();
2360 BOOST_LOG(fatal) <<
"Another host is most likely already bound to them. Hermes, Apollo and Sunshine all use these ports, so only one of them can run at a time."sv;
2361 shutdown_event->raise(
true);
2365 std::thread ssl {accept_and_run, &https_server};
2366 std::thread tcp {accept_and_run, &http_server};
2369 shutdown_event->view();
2372 std::lock_guard lock {pair_session_mutex};
2373 clear_pending_pairs();
2374 map_id_sess.clear();
2377 https_server.stop();
2384 std::string request_otp(
const std::string& passphrase,
const std::string& deviceName) {
2385 if (passphrase.size() < 4) {
2389 one_time_pin = crypto::rand_alphabet(4,
"0123456789"sv);
2390 otp_passphrase = passphrase;
2391 otp_device_name = deviceName;
2392 otp_creation_time = std::chrono::steady_clock::now();
2394 return one_time_pin;
2400 client_root = client;
2408 stream::session::graceful_stop(session);
2415 auto session = rtsp_stream::find_session(uuid);
2424 stream::session::update_device_info(session, name, newPerm);
2428 auto session = rtsp_stream::find_session(uuid);
2437 const std::string& uuid,
2438 const std::string& name,
2439 const std::string& display_mode,
2440 const cmd_list_t& do_cmds,
2441 const cmd_list_t& undo_cmds,
2443 const bool enable_legacy_ordering,
2444 const bool allow_client_commands,
2445 const bool always_use_virtual_display
2450 auto it = client.named_devices.begin();
2451 for (; it != client.named_devices.end(); ++it) {
2452 auto named_cert_p = *it;
2453 if (named_cert_p->uuid == uuid) {
2454 named_cert_p->name = name;
2455 named_cert_p->display_mode = display_mode;
2456 named_cert_p->perm = newPerm;
2457 named_cert_p->do_cmds = do_cmds;
2458 named_cert_p->undo_cmds = undo_cmds;
2459 named_cert_p->enable_legacy_ordering = enable_legacy_ordering;
2460 named_cert_p->allow_client_commands = allow_client_commands;
2461 named_cert_p->always_use_virtual_display = always_use_virtual_display;
2471 bool removed =
false;
2473 for (
auto it = client.named_devices.begin(); it != client.named_devices.end();) {
2474 if ((*it)->uuid == uuid) {
2475 it = client.named_devices.erase(it);
2486 auto session = rtsp_stream::find_session(uuid);
2491 if (client.named_devices.empty()) {
2492 proc::proc.terminate();