Sunshine0.0.5917
Sunshine 0.0.5917
Self-hosted game stream host for Moonlight.
Loading...
Searching...
No Matches
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 std::string &error_message = "Not Found");
45 void bad_request(const resp_https_t &response, 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);
73 bool check_app_index(const resp_https_t &response, int index);
82 void getPage(const resp_https_t &response, const req_https_t &request, bool require_auth = true, bool redirect_if_username = false);
89 void getFallbackPage(const resp_https_t &response, const req_https_t &request);
90 void getAsset(const resp_https_t &response, const req_https_t &request);
91 void browseDirectory(const resp_https_t &response, const req_https_t &request);
92 void getLocale(const resp_https_t &response, const req_https_t &request);
93 void getCSRFToken(const resp_https_t &response, const req_https_t &request);
94
101 void getPendingPairings(const resp_https_t &response, const req_https_t &request);
102
109 void cancelPairing(const resp_https_t &response, const req_https_t &request);
110
117 void savePin(const resp_https_t &response, const req_https_t &request);
118
128 bool is_driver_version_development(std::string_view version);
129
141 bool is_driver_version_supported(std::string_view version, std::string_view minimum_version);
142
151 nlohmann::json build_driver_status(bool installed, const std::string &version, std::string_view minimum_version);
152
159 nlohmann::json build_virtualhid_license_status(const lvh::LicenseResult &result);
160
166 nlohmann::json get_virtualhid_driver_status();
167
173 nlohmann::json get_vigembus_driver_status();
174
175 void getVirtualInputStatus(const resp_https_t &response, const req_https_t &request);
176
183 void getPermissions(const resp_https_t &response, const req_https_t &request);
184
191 void requestPermission(const resp_https_t &response, const req_https_t &request);
192
193 void getVirtualInputLicense(const resp_https_t &response, const req_https_t &request);
194
195 void updateVirtualInputLicense(const resp_https_t &response, const req_https_t &request);
196
197 void resetPortalToken(const resp_https_t &response, const req_https_t &request);
198
199#ifdef SUNSHINE_TESTS
205 void set_permission_statuses_for_testing(nlohmann::json permissions);
206
210 void reset_permission_statuses_for_testing();
211
212 using virtual_input_license_status_provider_t = std::function<lvh::LicenseResult()>;
213 using portal_token_path_provider_t = std::function<std::filesystem::path()>;
214
220 void set_virtual_input_license_status_provider_for_testing(virtual_input_license_status_provider_t status_provider);
221
225 void reset_virtual_input_license_status_provider_for_testing();
226
232 void set_portal_token_path_provider_for_testing(portal_token_path_provider_t path_provider);
233
237 void reset_portal_token_path_provider_for_testing();
238
244 void clear_sensitive_string_for_testing(std::string &value);
245#endif
246
247 // Browse helper functions (also exposed for unit testing)
254 bool is_browsable_executable(const std::filesystem::directory_entry &entry, const std::filesystem::file_status &status);
255
262 nlohmann::json build_browse_entries(const std::filesystem::path &dir_path, const std::string &type_str);
263
264#ifdef _WIN32
269 nlohmann::json get_windows_drives();
270#endif
271} // namespace confighttp
272
273// mime types map
277const std::map<std::string, std::string, std::less<>> mime_types = {
278 {"css", "text/css"},
279 {"gif", "image/gif"},
280 {"htm", "text/html"},
281 {"html", "text/html"},
282 {"ico", "image/x-icon"},
283 {"jpeg", "image/jpeg"},
284 {"jpg", "image/jpeg"},
285 {"js", "application/javascript"},
286 {"json", "application/json"},
287 {"png", "image/png"},
288 {"svg", "image/svg+xml"},
289 {"ttf", "font/ttf"},
290 {"txt", "text/plain"},
291 {"woff2", "font/woff2"},
292 {"xml", "text/xml"},
293};
std::string generate_csrf_token(const std::string &client_id)
Generate a new CSRF token for a client.
Definition confighttp.cpp:789
void requestPermission(const resp_https_t &response, const req_https_t &request)
Start a native permission request or open its settings pane.
Definition confighttp.cpp:2061
void getCSRFToken(const resp_https_t &response, const req_https_t &request)
Get a CSRF token for the authenticated user.
Definition confighttp.cpp:1063
bool check_app_index(const resp_https_t &response, int index)
Validates the application index and sends an error response if invalid.
Definition confighttp.cpp:912
nlohmann::json get_vigembus_driver_status()
Build ViGEmBus fallback driver version and installation status.
Definition confighttp.cpp:2143
void getPage(const resp_https_t &response, const req_https_t &request, const bool require_auth, const bool redirect_if_username)
Serve the Web UI single-page application entry document.
Definition confighttp.cpp:928
void getVirtualInputLicense(const resp_https_t &response, const req_https_t &request)
Get the current libvirtualhid machine license status.
Definition confighttp.cpp:2195
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:1806
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:2271
void send_unauthorized(const resp_https_t &response, const req_https_t &request)
Send a 401 Unauthorized response.
Definition confighttp.cpp:605
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:2313
void print_req(const req_https_t &request)
Log the request details.
Definition confighttp.cpp:570
std::shared_ptr< SimpleWeb::ServerBase< SimpleWeb::HTTPS >::Response > resp_https_t
Shared HTTPS response object passed to configuration handlers.
Definition confighttp.cpp:84
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:850
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:1964
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:1504
std::string get_client_id(const req_https_t &request)
Get a unique client identifier for CSRF token management.
Definition confighttp.cpp:769
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:313
void send_response(const resp_https_t &response, const nlohmann::json &output_tree)
Send a response.
Definition confighttp.cpp:592
nlohmann::json build_virtualhid_license_status(const lvh::LicenseResult &result)
Convert a libvirtualhid license result into a Web UI response.
Definition confighttp.cpp:353
void not_found(const resp_https_t &response, const std::string &error_message)
Send a 404 Not Found response.
Definition confighttp.cpp:698
void getVirtualInputStatus(const resp_https_t &response, const req_https_t &request)
Get virtual input driver version and installation status.
Definition confighttp.cpp:2174
std::shared_ptr< SimpleWeb::ServerBase< SimpleWeb::HTTPS >::Request > req_https_t
Shared HTTPS request object received by configuration handlers.
Definition confighttp.cpp:88
void getAsset(const resp_https_t &response, const req_https_t &request)
Get an asset.
Definition confighttp.cpp:1018
nlohmann::json get_virtualhid_driver_status()
Build Virtual HID Broker version and installation status.
Definition confighttp.cpp:2096
nlohmann::json get_windows_drives()
Builds a JSON array of available Windows drive letters.
Definition confighttp.cpp:2289
void updateVirtualInputLicense(const resp_https_t &response, const req_https_t &request)
Activate, validate, or deactivate the libvirtualhid machine license.
Definition confighttp.cpp:2211
bool is_driver_version_development(std::string_view version)
Check whether a detected driver version identifies a development build.
Definition confighttp.cpp:285
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:738
void bad_request(const resp_https_t &response, const std::string &error_message)
Send a 400 Bad Request response.
Definition confighttp.cpp:719
bool authenticate(const resp_https_t &response, const req_https_t &request)
Authenticate the user.
Definition confighttp.cpp:649
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:1887
void getFallbackPage(const resp_https_t &response, const req_https_t &request)
Serve the SPA entry for browser routes and preserve 404 responses for server-owned route prefixes.
Definition confighttp.cpp:952
SimpleWeb::Server< SimpleWeb::HTTPS > https_server_t
HTTPS server type used for Sunshine's configuration UI.
Definition confighttp.cpp:75
void start()
Start the HTTPS configuration server.
Definition confighttp.cpp:2455
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:290
void getPermissions(const resp_https_t &response, const req_https_t &request)
Return permission status for the current host platform.
Definition confighttp.cpp:2020
void browseDirectory(const resp_https_t &response, const req_https_t &request)
Browse the server filesystem.
Definition confighttp.cpp:2376
void send_redirect(const resp_https_t &response, const req_https_t &request, const char *path)
Send a redirect response.
Definition confighttp.cpp:632
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:1836
const std::map< std::string, std::string, std::less<> > mime_types
File-extension to MIME-type mapping used when serving the Web UI.
Definition confighttp.h:277
constexpr auto PORT_HTTPS
GameStream port offset for port https.
Definition confighttp.h:29
Declarations for thread-safe data structures.