From f8d4ae5b8480640a8f9693fbf060ac0e1c7be830 Mon Sep 17 00:00:00 2001 From: loki Date: Wed, 4 Dec 2019 19:05:03 +0100 Subject: [PATCH] insert pin through an http request --- assets/sunshine.service | 13 ++++ nvhttp.cpp | 149 +++++++++++++++++++++++----------------- 2 files changed, 99 insertions(+), 63 deletions(-) create mode 100644 assets/sunshine.service diff --git a/assets/sunshine.service b/assets/sunshine.service new file mode 100644 index 00000000..b5645c70 --- /dev/null +++ b/assets/sunshine.service @@ -0,0 +1,13 @@ +[Unit] +Description=Remote desktop service (VNC) + +[Service] +WorkingDirectory=/home/%u +Environment="DISPLAY=:0" +Type=simple +# wait for Xorg started by ${USER} +ExecStartPre=/bin/sh -c 'while ! pgrep Xorg; do sleep 2; done' +ExecStart=/home/%u/Github/sunshine/cmake-build-release/sunshine + +[Install] +WantedBy=default.target diff --git a/nvhttp.cpp b/nvhttp.cpp index 2cf80473..6db5b1a0 100644 --- a/nvhttp.cpp +++ b/nvhttp.cpp @@ -53,6 +53,14 @@ struct pair_session_t { std::string serversecret; std::string serverchallenge; + + struct { + util::Either< + std::shared_ptr::Response>, + std::shared_ptr::Response> + > response; + std::string salt; + } async_insert_pin; }; // uniqueID, session @@ -66,14 +74,6 @@ enum class op_e { REMOVE }; -std::string get_pin() { - std::cout << "Please insert PIN: "; - std::string pin; - std::getline(std::cin, pin); - - return pin; -} - void save_devices() { pt::ptree root; @@ -127,10 +127,8 @@ void update_id_client(client_t &client, op_e op) { save_devices(); } -void getservercert(pair_session_t &sess, pt::ptree &tree, const args_t &args) { - auto salt = util::from_hex>(args.at("salt"s), true); - - auto pin = get_pin(); +void getservercert(pair_session_t &sess, pt::ptree &tree, const std::string &pin) { + auto salt = util::from_hex>(sess.async_insert_pin.salt, true); auto key = crypto::gen_aes_key(*salt, pin); sess.cipher_key = std::make_unique(key); @@ -243,46 +241,6 @@ void clientpairingsecret(pair_session_t &sess, pt::ptree &tree, const args_t &ar tree.put("root..status_code", 200); } -pt::ptree pair_xml(args_t &&args) { - auto uniqID { std::move(args.at("uniqueid"s)) }; - auto sess_it = map_id_sess.find(uniqID); - - pt::ptree tree; - - args_t::const_iterator it; - if(it = args.find("phrase"); it != std::end(args)) { - if(it->second == "getservercert"sv) { - pair_session_t sess; - - sess.client.uniqueID = std::move(uniqID); - sess.client.cert = util::from_hex_vec(args.at("clientcert"s), true); - - std::cout << sess.client.cert; - - auto ptr = map_id_sess.emplace(sess.client.uniqueID, std::move(sess)).first; - getservercert(ptr->second, tree, args); - } - else if(it->second == "pairchallenge"sv) { - tree.put("root.paired", 1); - tree.put("root..status_code", 200); - } - } - else if(it = args.find("clientchallenge"); it != std::end(args)) { - clientchallenge(sess_it->second, tree, args); - } - else if(it = args.find("serverchallengeresp"); it != std::end(args)) { - serverchallengeresp(sess_it->second, tree, args); - } - else if(it = args.find("clientpairingsecret"); it != std::end(args)) { - clientpairingsecret(sess_it->second, tree, args); - } - else { - tree.put("root..status_code", 404); - } - - return tree; -} - template struct tunnel; @@ -335,7 +293,44 @@ template void pair(std::shared_ptr::Response> response, std::shared_ptr::Request> request) { print_req(request); - auto tree = pair_xml(request->parse_query_string()); + auto args = request->parse_query_string(); + auto uniqID { std::move(args.at("uniqueid"s)) }; + auto sess_it = map_id_sess.find(uniqID); + + pt::ptree tree; + + args_t::const_iterator it; + if(it = args.find("phrase"); it != std::end(args)) { + if(it->second == "getservercert"sv) { + pair_session_t sess; + + sess.client.uniqueID = std::move(uniqID); + sess.client.cert = util::from_hex_vec(args.at("clientcert"s), true); + + std::cout << sess.client.cert; + auto ptr = map_id_sess.emplace(sess.client.uniqueID, std::move(sess)).first; + + ptr->second.async_insert_pin.salt = std::move(args.at("salt"s)); + ptr->second.async_insert_pin.response = std::move(response); + return; + } + else if(it->second == "pairchallenge"sv) { + tree.put("root.paired", 1); + tree.put("root..status_code", 200); + } + } + else if(it = args.find("clientchallenge"); it != std::end(args)) { + clientchallenge(sess_it->second, tree, args); + } + else if(it = args.find("serverchallengeresp"); it != std::end(args)) { + serverchallengeresp(sess_it->second, tree, args); + } + else if(it = args.find("clientpairingsecret"); it != std::end(args)) { + clientpairingsecret(sess_it->second, tree, args); + } + else { + tree.put("root..status_code", 404); + } std::ostringstream data; @@ -343,6 +338,32 @@ void pair(std::shared_ptr::Response> response, response->write(data.str()); } +template +void pin(std::shared_ptr::Response> response, std::shared_ptr::Request> request) { + print_req(request); + + pt::ptree tree; + + auto &sess = std::begin(map_id_sess)->second; + getservercert(sess, tree, request->path_match[1]); + + // response to the request for pin + std::ostringstream data; + pt::write_xml(data, tree); + + auto &async_response = sess.async_insert_pin.response; + if(async_response.left()) { + async_response.left()->write(data.str()); + } + else { + async_response.right()->write(data.str()); + } + + async_response = std::decay_t(); + // response to the current request + response->write(""s); +} + template void serverinfo(std::shared_ptr::Response> response, std::shared_ptr::Request> request) { print_req(request); @@ -477,22 +498,24 @@ void start() { http_server_t http_server; https_server.default_resource = not_found; - https_server.resource["^/serverinfo"]["GET"] = serverinfo; - https_server.resource["^/pair"]["GET"] = pair; - https_server.resource["^/applist"]["GET"] = applist; - https_server.resource["^/appasset"]["GET"] = appasset; - https_server.resource["^/launch"]["GET"] = launch; + https_server.resource["^/serverinfo$"]["GET"] = serverinfo; + https_server.resource["^/pair$"]["GET"] = pair; + https_server.resource["^/applist$"]["GET"] = applist; + https_server.resource["^/appasset$"]["GET"] = appasset; + https_server.resource["^/launch$"]["GET"] = launch; + https_server.resource["^/pin/([0-9]+)$"]["GET"] = pin; https_server.config.reuse_address = true; https_server.config.address = "0.0.0.0"s; https_server.config.port = PORT_HTTPS; http_server.default_resource = not_found; - http_server.resource["^/serverinfo"]["GET"] = serverinfo; - http_server.resource["^/pair"]["GET"] = pair; - http_server.resource["^/applist"]["GET"] = applist; - http_server.resource["^/appasset"]["GET"] = appasset; - http_server.resource["^/launch"]["GET"] = launch; + http_server.resource["^/serverinfo$"]["GET"] = serverinfo; + http_server.resource["^/pair$"]["GET"] = pair; + http_server.resource["^/applist$"]["GET"] = applist; + http_server.resource["^/appasset$"]["GET"] = appasset; + http_server.resource["^/launch$"]["GET"] = launch; + http_server.resource["^/pin/([0-9]+)$"]["GET"] = pin; http_server.config.reuse_address = true; http_server.config.address = "0.0.0.0"s;