-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathentry_handler.cpp
More file actions
281 lines (233 loc) · 6.76 KB
/
Copy pathentry_handler.cpp
File metadata and controls
281 lines (233 loc) · 6.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/**
* @file entry_handler.cpp
* @brief Definitions for entry handling functions.
*/
// standard includes
#include <csignal>
#include <iostream>
#include <thread>
// local includes
#include "config.h"
#include "confighttp.h"
#include "entry_handler.h"
#include "globals.h"
#include "httpcommon.h"
#include "logging.h"
#include "network.h"
#include "platform/common.h"
extern "C" {
#ifdef _WIN32
#include <iphlpapi.h>
#endif
}
using namespace std::literals;
void launch_ui() {
std::string url = "https://localhost:" + std::to_string(net::map_port(confighttp::PORT_HTTPS));
platf::open_url(url);
}
void launch_ui_with_path(std::string path) {
std::string url = "https://localhost:" + std::to_string(net::map_port(confighttp::PORT_HTTPS)) + path;
platf::open_url(url);
}
namespace args {
int creds(const char *name, int argc, char *argv[]) {
if (argc < 2 || argv[0] == "help"sv || argv[1] == "help"sv) {
help(name);
}
http::save_user_creds(config::sunshine.credentials_file, argv[0], argv[1]);
return 0;
}
int help(const char *name) {
logging::print_help(name);
return 0;
}
int version() {
// version was already logged at startup
return 0;
}
#ifdef _WIN32
int restore_nvprefs_undo() {
if (nvprefs_instance.load()) {
nvprefs_instance.restore_from_and_delete_undo_file_if_exists();
nvprefs_instance.unload();
}
return 0;
}
#endif
} // namespace args
namespace lifetime {
char **argv;
std::atomic_int desired_exit_code;
void exit_sunshine(int exit_code, bool async) {
// Store the exit code of the first exit_sunshine() call
int zero = 0;
desired_exit_code.compare_exchange_strong(zero, exit_code);
// Raise SIGINT to start termination
std::raise(SIGINT);
// Termination will happen asynchronously, but the caller may
// have wanted synchronous behavior.
while (!async) {
std::this_thread::sleep_for(1s);
}
}
void debug_trap() {
#ifdef _WIN32
DebugBreak();
#else
std::raise(SIGTRAP);
#endif
}
char **get_argv() {
return argv;
}
} // namespace lifetime
void log_publisher_data() {
BOOST_LOG(info) << "Package Publisher: "sv << SUNSHINE_PUBLISHER_NAME;
BOOST_LOG(info) << "Publisher Website: "sv << SUNSHINE_PUBLISHER_WEBSITE;
BOOST_LOG(info) << "Get support: "sv << SUNSHINE_PUBLISHER_ISSUE_URL;
}
#ifdef _WIN32
bool is_gamestream_enabled() {
DWORD enabled;
DWORD size = sizeof(enabled);
return RegGetValueW(
HKEY_LOCAL_MACHINE,
L"SOFTWARE\\NVIDIA Corporation\\NvStream",
L"EnableStreaming",
RRF_RT_REG_DWORD,
nullptr,
&enabled,
&size
) == ERROR_SUCCESS &&
enabled != 0;
}
namespace service_ctrl {
class service_controller {
public:
/**
* @brief Constructor for service_controller class.
* @param service_desired_access SERVICE_* desired access flags.
*/
service_controller(DWORD service_desired_access) {
scm_handle = OpenSCManagerA(nullptr, nullptr, SC_MANAGER_CONNECT);
if (!scm_handle) {
auto winerr = GetLastError();
BOOST_LOG(error) << "OpenSCManager() failed: "sv << winerr;
return;
}
service_handle = OpenServiceA(scm_handle, "SunshineService", service_desired_access);
if (!service_handle) {
auto winerr = GetLastError();
BOOST_LOG(error) << "OpenService() failed: "sv << winerr;
return;
}
}
~service_controller() {
if (service_handle) {
CloseServiceHandle(service_handle);
}
if (scm_handle) {
CloseServiceHandle(scm_handle);
}
}
/**
* @brief Asynchronously starts the Sunshine service.
*/
bool start_service() {
if (!service_handle) {
return false;
}
if (!StartServiceA(service_handle, 0, nullptr)) {
auto winerr = GetLastError();
if (winerr != ERROR_SERVICE_ALREADY_RUNNING) {
BOOST_LOG(error) << "StartService() failed: "sv << winerr;
return false;
}
}
return true;
}
/**
* @brief Query the service status.
* @param status The SERVICE_STATUS struct to populate.
*/
bool query_service_status(SERVICE_STATUS &status) {
if (!service_handle) {
return false;
}
if (!QueryServiceStatus(service_handle, &status)) {
auto winerr = GetLastError();
BOOST_LOG(error) << "QueryServiceStatus() failed: "sv << winerr;
return false;
}
return true;
}
private:
SC_HANDLE scm_handle = NULL;
SC_HANDLE service_handle = NULL;
};
bool is_service_running() {
service_controller sc {SERVICE_QUERY_STATUS};
SERVICE_STATUS status;
if (!sc.query_service_status(status)) {
return false;
}
return status.dwCurrentState == SERVICE_RUNNING;
}
bool start_service() {
service_controller sc {SERVICE_QUERY_STATUS | SERVICE_START};
std::cout << "Starting Sunshine..."sv;
// This operation is asynchronous, so we must wait for it to complete
if (!sc.start_service()) {
return false;
}
SERVICE_STATUS status;
do {
Sleep(1000);
std::cout << '.';
} while (sc.query_service_status(status) && status.dwCurrentState == SERVICE_START_PENDING);
if (status.dwCurrentState != SERVICE_RUNNING) {
BOOST_LOG(error) << SERVICE_NAME " failed to start: "sv << status.dwWin32ExitCode;
return false;
}
std::cout << std::endl;
return true;
}
bool wait_for_ui_ready() {
std::cout << "Waiting for Web UI to be ready...";
// Wait up to 30 seconds for the web UI to start
for (int i = 0; i < 30; i++) {
PMIB_TCPTABLE tcp_table = nullptr;
ULONG table_size = 0;
ULONG err;
auto fg = util::fail_guard([&tcp_table]() {
free(tcp_table);
});
do {
// Query all open TCP sockets to look for our web UI port
err = GetTcpTable(tcp_table, &table_size, false);
if (err == ERROR_INSUFFICIENT_BUFFER) {
free(tcp_table);
tcp_table = (PMIB_TCPTABLE) malloc(table_size);
}
} while (err == ERROR_INSUFFICIENT_BUFFER);
if (err != NO_ERROR) {
BOOST_LOG(error) << "Failed to query TCP table: "sv << err;
return false;
}
uint16_t port_nbo = htons(net::map_port(confighttp::PORT_HTTPS));
for (DWORD i = 0; i < tcp_table->dwNumEntries; i++) {
auto &entry = tcp_table->table[i];
// Look for our port in the listening state
if (entry.dwLocalPort == port_nbo && entry.dwState == MIB_TCP_STATE_LISTEN) {
std::cout << std::endl;
return true;
}
}
Sleep(1000);
std::cout << '.';
}
std::cout << "timed out"sv << std::endl;
return false;
}
} // namespace service_ctrl
#endif