Sunshine latest
Self-hosted game stream host for Moonlight.
confighttp.h
Go to the documentation of this file.
1
5#pragma once
6
7// standard includes
8#include <filesystem>
9#include <functional>
10#include <memory>
11#include <string>
12#include <string_view>
13
14// lib includes
15#include <libvirtualhid/license.hpp>
16#include <nlohmann/json.hpp>
17#include <Simple-Web-Server/server_https.hpp>
18
19// local includes
20#include "thread_safe.h"
21
26#define WEB_DIR SUNSHINE_ASSETS_DIR "/web/"
27
28namespace confighttp {
29 constexpr auto PORT_HTTPS = 1;
30
31 // Type aliases for HTTPS server components
32 using https_server_t = SimpleWeb::Server<SimpleWeb::HTTPS>;
33 using resp_https_t = std::shared_ptr<typename SimpleWeb::ServerBase<SimpleWeb::HTTPS>::Response>;
34 using req_https_t = std::shared_ptr<typename SimpleWeb::ServerBase<SimpleWeb::HTTPS>::Request>;
35
36 // Main server start function
37 void start();
38
39 void print_req(const req_https_t &request);
40 void send_response(const resp_https_t &response, const nlohmann::json &output_tree);
41 void send_unauthorized(const resp_https_t &response, const req_https_t &request);
42 void send_redirect(const resp_https_t &response, const req_https_t &request, const char *path);
43 bool authenticate(const resp_https_t &response, const req_https_t &request);
44 void not_found(const resp_https_t &response, const req_https_t &request, const std::string &error_message = "Not Found");
45 void bad_request(const resp_https_t &response, const req_https_t &request, const std::string &error_message = "Bad Request");
54 bool check_content_type(const resp_https_t &response, const req_https_t &request, const std::string_view &contentType);
55 std::string generate_csrf_token(const std::string &client_id);
64 bool validate_csrf_token(const resp_https_t &response, const req_https_t &request, const std::string &client_id);
65 std::string get_client_id(const req_https_t &request);
74 bool check_app_index(const resp_https_t &response, const req_https_t &request, int index);
75 void getPage(const resp_https_t &response, const req_https_t &request, const char *html_file, bool require_auth = true, bool redirect_if_username = false);
76 void getAsset(const resp_https_t &response, const req_https_t &request);
77 void browseDirectory(const resp_https_t &response, const req_https_t &request);
78 void getLocale(const resp_https_t &response, const req_https_t &request);
79 void getCSRFToken(const resp_https_t &response, const req_https_t &request);
80
87 void getPendingPairings(const resp_https_t &response, const req_https_t &request);
88
95 void cancelPairing(const resp_https_t &response, const req_https_t &request);
96
103 void savePin(const resp_https_t &response, const req_https_t &request);
104
114 bool is_driver_version_development(std::string_view version);
115
127 bool is_driver_version_supported(std::string_view version, std::string_view minimum_version);
128
137 nlohmann::json build_driver_status(bool installed, const std::string &version, std::string_view minimum_version);
138
145 nlohmann::json build_virtualhid_license_status(const lvh::LicenseResult &result);
146
152 nlohmann::json get_virtualhid_driver_status();
153
159 nlohmann::json get_vigembus_driver_status();
160
161 void getVirtualInputStatus(const resp_https_t &response, const req_https_t &request);
162
163 void getVirtualInputLicense(const resp_https_t &response, const req_https_t &request);
164
165 void updateVirtualInputLicense(const resp_https_t &response, const req_https_t &request);
166
167 void resetPortalToken(const resp_https_t &response, const req_https_t &request);
168
169#ifdef SUNSHINE_TESTS
170 using virtual_input_license_status_provider_t = std::function<lvh::LicenseResult()>;
171 using portal_token_path_provider_t = std::function<std::filesystem::path()>;
172
178 void set_virtual_input_license_status_provider_for_testing(virtual_input_license_status_provider_t status_provider);
179
183 void reset_virtual_input_license_status_provider_for_testing();
184
190 void set_portal_token_path_provider_for_testing(portal_token_path_provider_t path_provider);
191
195 void reset_portal_token_path_provider_for_testing();
196
202 void clear_sensitive_string_for_testing(std::string &value);
203#endif
204
205 // Browse helper functions (also exposed for unit testing)
212 bool is_browsable_executable(const std::filesystem::directory_entry &entry, const std::filesystem::file_status &status);
213
220 nlohmann::json build_browse_entries(const std::filesystem::path &dir_path, const std::string &type_str);
221
222#ifdef _WIN32
227 nlohmann::json get_windows_drives();
228#endif
229} // namespace confighttp
230
231// mime types map
235const std::map<std::string, std::string> mime_types = {
236 {"css", "text/css"},
237 {"gif", "image/gif"},
238 {"htm", "text/html"},
239 {"html", "text/html"},
240 {"ico", "image/x-icon"},
241 {"jpeg", "image/jpeg"},
242 {"jpg", "image/jpeg"},
243 {"js", "application/javascript"},
244 {"json", "application/json"},
245 {"png", "image/png"},
246 {"svg", "image/svg+xml"},
247 {"ttf", "font/ttf"},
248 {"txt", "text/plain"},
249 {"woff2", "font/woff2"},
250 {"xml", "text/xml"},
251};
std::string generate_csrf_token(const std::string &client_id)
Generate a new CSRF token for a client.
Definition confighttp.cpp:735
void not_found(const resp_https_t &response, const req_https_t &request, const std::string &error_message)
Send a 404 Not Found response.
Definition confighttp.cpp:644
void getCSRFToken(const resp_https_t &response, const req_https_t &request)
Get a CSRF token for the authenticated user.
Definition confighttp.cpp:1003
nlohmann::json get_vigembus_driver_status()
Build ViGEmBus fallback driver version and installation status.
Definition confighttp.cpp:2001
void getVirtualInputLicense(const resp_https_t &response, const req_https_t &request)
Get the current libvirtualhid machine license status.
Definition confighttp.cpp:2053
void getPendingPairings(const resp_https_t &response, const req_https_t &request)
List client pairing requests that are waiting for PIN approval.
Definition confighttp.cpp:1746
bool check_app_index(const resp_https_t &response, const req_https_t &request, int index)
Validates the application index and sends an error response if invalid.
Definition confighttp.cpp:858
bool is_browsable_executable(const fs::directory_entry &entry, const fs::file_status &status)
Checks whether a directory entry qualifies as an executable file.
Definition confighttp.cpp:2129
void bad_request(const resp_https_t &response, const req_https_t &request, const std::string &error_message)
Send a 400 Bad Request response.
Definition confighttp.cpp:665
void send_unauthorized(const resp_https_t &response, const req_https_t &request)
Send a 401 Unauthorized response.
Definition confighttp.cpp:551
nlohmann::json build_browse_entries(const fs::path &dir_path, const std::string &type_str)
Lists, filters, and sorts the entries of a directory for the browse API.
Definition confighttp.cpp:2171
void print_req(const req_https_t &request)
Log the request details.
Definition confighttp.cpp:516
std::shared_ptr< SimpleWeb::ServerBase< SimpleWeb::HTTPS >::Response > resp_https_t
Shared HTTPS response object passed to configuration handlers.
Definition confighttp.cpp:74
bool validate_csrf_token(const resp_https_t &response, const req_https_t &request, const std::string &client_id)
Validate CSRF token.
Definition confighttp.cpp:796
void resetPortalToken(const resp_https_t &response, const req_https_t &request)
Authenticate a Web UI request and delete the saved XDG Portal restore token.
Definition confighttp.cpp:1904
void getLocale(const resp_https_t &response, const req_https_t &request)
Get the locale setting. This endpoint does not require authentication.
Definition confighttp.cpp:1444
std::string get_client_id(const req_https_t &request)
Get a unique client identifier for CSRF token management.
Definition confighttp.cpp:715
nlohmann::json build_driver_status(bool installed, const std::string &version, std::string_view minimum_version)
Build a standard driver status response.
Definition confighttp.cpp:295
void send_response(const resp_https_t &response, const nlohmann::json &output_tree)
Send a response.
Definition confighttp.cpp:538
nlohmann::json build_virtualhid_license_status(const lvh::LicenseResult &result)
Convert a libvirtualhid license result into a Web UI response.
Definition confighttp.cpp:335
void getVirtualInputStatus(const resp_https_t &response, const req_https_t &request)
Get virtual input driver version and installation status.
Definition confighttp.cpp:2032
void getPage(const resp_https_t &response, const req_https_t &request, const char *html_file, const bool require_auth, const bool redirect_if_username)
Get an HTML page.
Definition confighttp.cpp:882
std::shared_ptr< SimpleWeb::ServerBase< SimpleWeb::HTTPS >::Request > req_https_t
Shared HTTPS request object received by configuration handlers.
Definition confighttp.cpp:78
void getAsset(const resp_https_t &response, const req_https_t &request)
Get an asset.
Definition confighttp.cpp:958
nlohmann::json get_virtualhid_driver_status()
Build libvirtualhid driver version and installation status.
Definition confighttp.cpp:1960
nlohmann::json get_windows_drives()
Builds a JSON array of available Windows drive letters.
Definition confighttp.cpp:2147
void updateVirtualInputLicense(const resp_https_t &response, const req_https_t &request)
Activate, validate, or deactivate the libvirtualhid machine license.
Definition confighttp.cpp:2069
bool is_driver_version_development(std::string_view version)
Check whether a detected driver version identifies a development build.
Definition confighttp.cpp:267
bool check_content_type(const resp_https_t &response, const req_https_t &request, const std::string_view &contentType)
Validate the request content type and send a bad request when mismatched.
Definition confighttp.cpp:684
bool authenticate(const resp_https_t &response, const req_https_t &request)
Authenticate the user.
Definition confighttp.cpp:595
void savePin(const resp_https_t &response, const req_https_t &request)
Submit a PIN and return whether the selected client completes pairing.
Definition confighttp.cpp:1827
SimpleWeb::Server< SimpleWeb::HTTPS > https_server_t
HTTPS server type used for Sunshine's configuration UI.
Definition confighttp.cpp:65
void start()
Start the HTTPS configuration server.
Definition confighttp.cpp:2313
bool is_driver_version_supported(std::string_view version, std::string_view minimum_version)
Check whether a detected driver version satisfies a minimum version.
Definition confighttp.cpp:272
void browseDirectory(const resp_https_t &response, const req_https_t &request)
Browse the server filesystem.
Definition confighttp.cpp:2234
void send_redirect(const resp_https_t &response, const req_https_t &request, const char *path)
Send a redirect response.
Definition confighttp.cpp:578
void cancelPairing(const resp_https_t &response, const req_https_t &request)
Cancel a client pairing request that is waiting for PIN approval. The body for the delete request sho...
Definition confighttp.cpp:1776
const std::map< std::string, std::string > mime_types
File-extension to MIME-type mapping used when serving the Web UI.
Definition confighttp.h:235
constexpr auto PORT_HTTPS
GameStream port offset for port https.
Definition confighttp.h:29
Declarations for thread-safe data structures.