5#define BOOST_BIND_GLOBAL_PLACEHOLDERS
13#include <boost/asio/ssl/context.hpp>
14#include <boost/asio/ssl/context_base.hpp>
15#include <boost/property_tree/json_parser.hpp>
16#include <boost/property_tree/ptree.hpp>
17#include <boost/property_tree/xml_parser.hpp>
19#include <Simple-Web-Server/server_http.hpp>
20#include <Simple-Web-Server/server_https.hpp>
36 using namespace std::literals;
37 namespace fs = std::filesystem;
38 namespace pt = boost::property_tree;
40 int reload_user_creds(
const std::string &file);
41 bool user_creds_exist(
const std::string &file);
43 std::string unique_id;
44 uuid_util::uuid_t uuid;
49 origin_web_ui_allowed = net::from_enum_string(config::nvhttp.origin_web_ui_allowed);
52 uuid = uuid_util::uuid_t::generate();
53 unique_id = uuid.string();
54 auto dir = std::filesystem::temp_directory_path() /
"Sunshine"sv;
55 config::nvhttp.cert = (dir / (
"cert-"s + unique_id)).
string();
56 config::nvhttp.pkey = (dir / (
"pkey-"s + unique_id)).
string();
59 if ((!fs::exists(config::nvhttp.pkey) || !fs::exists(config::nvhttp.cert)) &&
60 create_creds(config::nvhttp.pkey, config::nvhttp.cert)) {
63 if (!user_creds_exist(config::sunshine.credentials_file)) {
64 BOOST_LOG(info) <<
"Open the Web UI to set your new username and password and getting started";
65 }
else if (reload_user_creds(config::sunshine.credentials_file)) {
71 int save_user_creds(
const std::string &file,
const std::string &username,
const std::string &password,
bool run_our_mouth) {
72 nlohmann::json outputTree;
74 if (fs::exists(file)) {
76 std::ifstream in(file);
78 }
catch (std::exception &e) {
86 const std::string salvaged = file +
".unreadable";
87 fs::rename(file, salvaged, ec);
88 BOOST_LOG(error) <<
"Couldn't read the state file: "sv << e.what();
90 BOOST_LOG(error) <<
"Couldn't move it aside either ("sv << ec.message()
91 <<
"); the new credentials will overwrite it."sv;
93 BOOST_LOG(warning) <<
"Moved it to "sv << salvaged
94 <<
" and started a fresh one. Paired clients are in that copy."sv;
96 outputTree = nlohmann::json::object();
100 auto salt = crypto::rand_alphabet(16);
101 outputTree[
"username"] = username;
102 outputTree[
"salt"] = salt;
103 outputTree[
"password"] = util::hex(crypto::hash(password + salt)).to_string();
105 std::ofstream out(file);
106 out << outputTree.dump(4);
107 }
catch (std::exception &e) {
108 BOOST_LOG(error) <<
"error writing to the credentials file, perhaps try this again as an administrator? Details: "sv << e.what();
112 BOOST_LOG(info) <<
"New credentials have been created"sv;
116 bool user_creds_exist(
const std::string &file) {
117 if (!fs::exists(file)) {
123 pt::read_json(file, inputTree);
124 return inputTree.find(
"username") != inputTree.not_found() &&
125 inputTree.find(
"password") != inputTree.not_found() &&
126 inputTree.find(
"salt") != inputTree.not_found();
127 }
catch (std::exception &e) {
128 BOOST_LOG(error) <<
"validating user credentials: "sv << e.what();
134 int reload_user_creds(
const std::string &file) {
137 pt::read_json(file, inputTree);
138 config::sunshine.username = inputTree.get<std::string>(
"username");
139 config::sunshine.password = inputTree.get<std::string>(
"password");
140 config::sunshine.salt = inputTree.get<std::string>(
"salt");
141 }
catch (std::exception &e) {
142 BOOST_LOG(error) <<
"loading user credentials: "sv << e.what();
148 int create_creds(
const std::string &pkey,
const std::string &cert) {
149 fs::path pkey_path = pkey;
150 fs::path cert_path = cert;
152 auto creds = crypto::gen_creds(
"Sunshine Gamestream Host"sv, 2048);
154 auto pkey_dir = pkey_path;
155 auto cert_dir = cert_path;
156 pkey_dir.remove_filename();
157 cert_dir.remove_filename();
159 std::error_code err_code {};
160 fs::create_directories(pkey_dir, err_code);
162 BOOST_LOG(error) <<
"Couldn't create directory ["sv << pkey_dir <<
"] :"sv << err_code.message();
166 fs::create_directories(cert_dir, err_code);
168 BOOST_LOG(error) <<
"Couldn't create directory ["sv << cert_dir <<
"] :"sv << err_code.message();
173 BOOST_LOG(error) <<
"Couldn't open ["sv << config::nvhttp.pkey <<
']';
178 BOOST_LOG(error) <<
"Couldn't open ["sv << config::nvhttp.cert <<
']';
182 fs::permissions(pkey_path, fs::perms::owner_read | fs::perms::owner_write, fs::perm_options::replace, err_code);
185 BOOST_LOG(error) <<
"Couldn't change permissions of ["sv << config::nvhttp.pkey <<
"] :"sv << err_code.message();
189 fs::permissions(cert_path, fs::perms::owner_read | fs::perms::group_read | fs::perms::others_read | fs::perms::owner_write, fs::perm_options::replace, err_code);
192 BOOST_LOG(error) <<
"Couldn't change permissions of ["sv << config::nvhttp.cert <<
"] :"sv << err_code.message();
199 bool download_file(
const std::string &url,
const std::string &file,
long ssl_version) {
201 CURL *curl = curl_easy_init();
203 BOOST_LOG(error) <<
"Couldn't create CURL instance";
208 BOOST_LOG(error) <<
"Couldn't create directory ["sv << file_dir <<
']';
209 curl_easy_cleanup(curl);
213 FILE *fp = fopen(file.c_str(),
"wb");
215 BOOST_LOG(error) <<
"Couldn't open ["sv << file <<
']';
216 curl_easy_cleanup(curl);
220 curl_easy_setopt(curl, CURLOPT_SSLVERSION, ssl_version);
221 curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
222 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
223 curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
224 curl_easy_setopt(curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
225 CURLcode result = curl_easy_perform(curl);
226 if (result != CURLE_OK) {
227 BOOST_LOG(error) <<
"Couldn't download ["sv << url <<
", code:" << result <<
']';
229 curl_easy_cleanup(curl);
231 return result == CURLE_OK;
234 std::string url_escape(
const std::string &url) {
235 char *
string = curl_easy_escape(
nullptr, url.c_str(),
static_cast<int>(url.length()));
236 std::string result(
string);
241 std::string url_get_host(
const std::string &url) {
242 CURLU *curlu = curl_url();
243 curl_url_set(curlu, CURLUPART_URL, url.c_str(),
static_cast<unsigned int>(url.length()));
245 if (curl_url_get(curlu, CURLUPART_HOST, &host, 0) != CURLUE_OK) {
246 curl_url_cleanup(curlu);
249 std::string result(host);
251 curl_url_cleanup(curlu);
Declarations for common platform specific utilities.
Declarations for the configuration of Sunshine.
@ FRESH_STATE
Do not load or save state.
Declarations for cryptography functions.
Declarations for file handling functions.
Declarations for common HTTP.
Declarations for logging related functions.
int creds(const char *name, int argc, char *argv[])
Reset the user credentials.
std::string get_parent_directory(const std::string &path)
Get the parent directory of a file or directory.
int write_file(const char *path, const std::string_view &contents)
Writes a file.
bool make_directory(const std::string &path)
Make a directory.
Declarations for networking related functions.
Declarations for the nvhttp (GameStream) server.
Declarations for the startup and shutdown of the apps started by a streaming Session.
Declarations for RTSP streaming.
Declarations for utility functions.