Hermes 0.7.0
Linux game streaming host
Loading...
Searching...
No Matches
httpcommon.cpp
Go to the documentation of this file.
1
5#define BOOST_BIND_GLOBAL_PLACEHOLDERS
6
7// standard includes
8#include <filesystem>
9#include <fstream>
10#include <utility>
11
12// lib includes
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>
18#include <curl/curl.h>
19#include <Simple-Web-Server/server_http.hpp>
20#include <Simple-Web-Server/server_https.hpp>
21
22// local includes
23#include "config.h"
24#include "crypto.h"
25#include "file_handler.h"
26#include "httpcommon.h"
27#include "logging.h"
28#include "network.h"
29#include "nvhttp.h"
30#include "platform/common.h"
31#include "process.h"
32#include "rtsp.h"
33#include "utility.h"
34
35namespace http {
36 using namespace std::literals;
37 namespace fs = std::filesystem;
38 namespace pt = boost::property_tree;
39
40 int reload_user_creds(const std::string &file);
41 bool user_creds_exist(const std::string &file);
42
43 std::string unique_id;
44 uuid_util::uuid_t uuid;
45 net::net_e origin_web_ui_allowed;
46
47 int init() {
48 bool clean_slate = config::sunshine.flags[config::flag::FRESH_STATE];
49 origin_web_ui_allowed = net::from_enum_string(config::nvhttp.origin_web_ui_allowed);
50
51 if (clean_slate) {
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();
57 }
58
59 if ((!fs::exists(config::nvhttp.pkey) || !fs::exists(config::nvhttp.cert)) &&
60 create_creds(config::nvhttp.pkey, config::nvhttp.cert)) {
61 return -1;
62 }
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)) {
66 return -1;
67 }
68 return 0;
69 }
70
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;
73
74 if (fs::exists(file)) {
75 try {
76 std::ifstream in(file);
77 in >> outputTree;
78 } catch (std::exception &e) {
79 // Setting credentials is the way out of a broken state file, so it must
80 // not be blocked by the state file being broken — that locks the user
81 // out of the exact recovery path they were told to use. Move the
82 // unreadable file aside and start from an empty tree. Paired clients
83 // live in this file too, so it is preserved rather than overwritten:
84 // that copy is the only chance of getting them back.
85 std::error_code ec;
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();
89 if (ec) {
90 BOOST_LOG(error) << "Couldn't move it aside either ("sv << ec.message()
91 << "); the new credentials will overwrite it."sv;
92 } else {
93 BOOST_LOG(warning) << "Moved it to "sv << salvaged
94 << " and started a fresh one. Paired clients are in that copy."sv;
95 }
96 outputTree = nlohmann::json::object();
97 }
98 }
99
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();
104 try {
105 std::ofstream out(file);
106 out << outputTree.dump(4); // Pretty-print with an indent of 4 spaces.
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();
109 return -1;
110 }
111
112 BOOST_LOG(info) << "New credentials have been created"sv;
113 return 0;
114 }
115
116 bool user_creds_exist(const std::string &file) {
117 if (!fs::exists(file)) {
118 return false;
119 }
120
121 pt::ptree inputTree;
122 try {
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();
129 }
130
131 return false;
132 }
133
134 int reload_user_creds(const std::string &file) {
135 pt::ptree inputTree;
136 try {
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();
143 return -1;
144 }
145 return 0;
146 }
147
148 int create_creds(const std::string &pkey, const std::string &cert) {
149 fs::path pkey_path = pkey;
150 fs::path cert_path = cert;
151
152 auto creds = crypto::gen_creds("Sunshine Gamestream Host"sv, 2048);
153
154 auto pkey_dir = pkey_path;
155 auto cert_dir = cert_path;
156 pkey_dir.remove_filename();
157 cert_dir.remove_filename();
158
159 std::error_code err_code {};
160 fs::create_directories(pkey_dir, err_code);
161 if (err_code) {
162 BOOST_LOG(error) << "Couldn't create directory ["sv << pkey_dir << "] :"sv << err_code.message();
163 return -1;
164 }
165
166 fs::create_directories(cert_dir, err_code);
167 if (err_code) {
168 BOOST_LOG(error) << "Couldn't create directory ["sv << cert_dir << "] :"sv << err_code.message();
169 return -1;
170 }
171
172 if (file_handler::write_file(pkey.c_str(), creds.pkey)) {
173 BOOST_LOG(error) << "Couldn't open ["sv << config::nvhttp.pkey << ']';
174 return -1;
175 }
176
177 if (file_handler::write_file(cert.c_str(), creds.x509)) {
178 BOOST_LOG(error) << "Couldn't open ["sv << config::nvhttp.cert << ']';
179 return -1;
180 }
181
182 fs::permissions(pkey_path, fs::perms::owner_read | fs::perms::owner_write, fs::perm_options::replace, err_code);
183
184 if (err_code) {
185 BOOST_LOG(error) << "Couldn't change permissions of ["sv << config::nvhttp.pkey << "] :"sv << err_code.message();
186 return -1;
187 }
188
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);
190
191 if (err_code) {
192 BOOST_LOG(error) << "Couldn't change permissions of ["sv << config::nvhttp.cert << "] :"sv << err_code.message();
193 return -1;
194 }
195
196 return 0;
197 }
198
199 bool download_file(const std::string &url, const std::string &file, long ssl_version) {
200 // sonar complains about weak ssl and tls versions; however sonar cannot detect the fix
201 CURL *curl = curl_easy_init(); // NOSONAR
202 if (!curl) {
203 BOOST_LOG(error) << "Couldn't create CURL instance";
204 return false;
205 }
206
207 if (std::string file_dir = file_handler::get_parent_directory(file); !file_handler::make_directory(file_dir)) {
208 BOOST_LOG(error) << "Couldn't create directory ["sv << file_dir << ']';
209 curl_easy_cleanup(curl);
210 return false;
211 }
212
213 FILE *fp = fopen(file.c_str(), "wb");
214 if (!fp) {
215 BOOST_LOG(error) << "Couldn't open ["sv << file << ']';
216 curl_easy_cleanup(curl);
217 return false;
218 }
219
220 curl_easy_setopt(curl, CURLOPT_SSLVERSION, ssl_version); // NOSONAR
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 << ']';
228 }
229 curl_easy_cleanup(curl);
230 fclose(fp);
231 return result == CURLE_OK;
232 }
233
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);
237 curl_free(string);
238 return result;
239 }
240
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()));
244 char *host;
245 if (curl_url_get(curlu, CURLUPART_HOST, &host, 0) != CURLUE_OK) {
246 curl_url_cleanup(curlu);
247 return "";
248 }
249 std::string result(host);
250 curl_free(host);
251 curl_url_cleanup(curlu);
252 return result;
253 }
254} // namespace http
Declarations for common platform specific utilities.
Declarations for the configuration of Sunshine.
@ FRESH_STATE
Do not load or save state.
Definition config.h:209
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.
net_e
Definition network.h:39
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.