From c03d3bb9856f0b1e0dc8b87576a3edb4f67a3aa0 Mon Sep 17 00:00:00 2001 From: pigeatgarlic Date: Thu, 22 Feb 2024 16:42:28 +0700 Subject: [PATCH] parse username password from arg --- .gitignore | 1 + src/config.cpp | 139 +++---------------------------------------------- src/main.cpp | 7 ++- src/rtsp.cpp | 6 +-- 4 files changed, 17 insertions(+), 136 deletions(-) diff --git a/.gitignore b/.gitignore index c3a04d96..874d39cf 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,7 @@ cmake-build* .idea # npm +build** node_modules/ package-lock.json diff --git a/src/config.cpp b/src/config.cpp index 7807566b..f455ad89 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -1107,149 +1108,23 @@ namespace config { int parse(int argc, char *argv[]) { std::unordered_map cmd_vars; -#ifdef _WIN32 - bool shortcut_launch = false; - bool service_admin_launch = false; -#endif for (auto x = 1; x < argc; ++x) { auto line = argv[x]; - if (line == "--help"sv) { - print_help(*argv); - return 1; - } -#ifdef _WIN32 - else if (line == "--shortcut"sv) { - shortcut_launch = true; - } - else if (line == "--shortcut-admin"sv) { - service_admin_launch = true; - } -#endif - else if (*line == '-') { + if (*line == '-') { if (*(line + 1) == '-') { - sunshine.cmd.name = line + 2; - sunshine.cmd.argc = argc - x - 1; - sunshine.cmd.argv = argv + x + 1; + char* command = line + 2; - break; - } - if (apply_flags(line + 1)) { - print_help(*argv); - return -1; - } - } - else { - auto line_end = line + strlen(line); + if(strcmp(command, "username") == 0) + sunshine.username = std::string((char*)argv[x + 1]); + else if(strcmp(command, "password") == 0) + sunshine.password = std::string((char*)argv[x + 1]); - auto pos = std::find(line, line_end, '='); - if (pos == line_end) { - sunshine.config_file = line; - } - else { - TUPLE_EL(var, 1, parse_option(line, line_end)); - if (!var) { - print_help(*argv); - return -1; - } - - TUPLE_EL_REF(name, 0, *var); - - auto it = cmd_vars.find(name); - if (it != std::end(cmd_vars)) { - cmd_vars.erase(it); - } - - cmd_vars.emplace(std::move(*var)); } } } - bool config_loaded = false; - try { - // Create appdata folder if it does not exist - if (!boost::filesystem::exists(platf::appdata().string())) { - boost::filesystem::create_directories(platf::appdata().string()); - } - - // Create empty config file if it does not exist - if (!fs::exists(sunshine.config_file)) { - std::ofstream { sunshine.config_file }; - } - - // Read config file - auto vars = parse_config(read_file(sunshine.config_file.c_str())); - - for (auto &[name, value] : cmd_vars) { - vars.insert_or_assign(std::move(name), std::move(value)); - } - - // Apply the config. Note: This will try to create any paths - // referenced in the config, so we may receive exceptions if - // the path is incorrect or inaccessible. - apply_config(std::move(vars)); - config_loaded = true; - } - catch (const std::filesystem::filesystem_error &err) { - BOOST_LOG(fatal) << "Failed to apply config: "sv << err.what(); - } - catch (const boost::filesystem::filesystem_error &err) { - BOOST_LOG(fatal) << "Failed to apply config: "sv << err.what(); - } - - if (!config_loaded) { -#ifdef _WIN32 - BOOST_LOG(fatal) << "To relaunch Sunshine successfully, use the shortcut in the Start Menu. Do not run Sunshine.exe manually."sv; - std::this_thread::sleep_for(10s); -#endif - return -1; - } - -#ifdef _WIN32 - // We have to wait until the config is loaded to handle these launches, - // because we need to have the correct base port loaded in our config. - if (service_admin_launch) { - // This is a relaunch as admin to start the service - service_ctrl::start_service(); - - // Always return 1 to ensure Sunshine doesn't start normally - return 1; - } - else if (shortcut_launch) { - if (!service_ctrl::is_service_running()) { - // If the service isn't running, relaunch ourselves as admin to start it - WCHAR executable[MAX_PATH]; - GetModuleFileNameW(NULL, executable, ARRAYSIZE(executable)); - - SHELLEXECUTEINFOW shell_exec_info {}; - shell_exec_info.cbSize = sizeof(shell_exec_info); - shell_exec_info.fMask = SEE_MASK_NOASYNC | SEE_MASK_NO_CONSOLE | SEE_MASK_NOCLOSEPROCESS; - shell_exec_info.lpVerb = L"runas"; - shell_exec_info.lpFile = executable; - shell_exec_info.lpParameters = L"--shortcut-admin"; - shell_exec_info.nShow = SW_NORMAL; - if (!ShellExecuteExW(&shell_exec_info)) { - auto winerr = GetLastError(); - std::cout << "Error: ShellExecuteEx() failed:"sv << winerr << std::endl; - return 1; - } - - // Wait for the elevated process to finish starting the service - WaitForSingleObject(shell_exec_info.hProcess, INFINITE); - CloseHandle(shell_exec_info.hProcess); - - // Wait for the UI to be ready for connections - service_ctrl::wait_for_ui_ready(); - } - - // Launch the web UI - launch_ui(); - - // Always return 1 to ensure Sunshine doesn't start normally - return 1; - } -#endif return 0; } diff --git a/src/main.cpp b/src/main.cpp index d9ad4118..ed249ac7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -741,8 +741,13 @@ main(int argc, char *argv[]) { } + if(config::parse(argc,argv)) { + BOOST_LOG(error) << "Video failed to parse arguments"sv; + return -1; + } - + BOOST_LOG(info) << "Starting sunshine with username: "sv << config::sunshine.username; + BOOST_LOG(info) << "Starting sunshine with password: "sv << config::sunshine.password; rtsp_stream::rtpThread(); diff --git a/src/rtsp.cpp b/src/rtsp.cpp index aded3533..22b3cf44 100644 --- a/src/rtsp.cpp +++ b/src/rtsp.cpp @@ -699,9 +699,9 @@ namespace rtsp_stream { return; } - auto a = util::from_hex("0", true); - auto b = util::from_hex("0", true); - auto session = stream::session::alloc(config, a,b); + auto gcm = util::from_hex(config::sunshine.username, true); + auto iv = util::from_hex(config::sunshine.password, true); + auto session = stream::session::alloc(config, gcm,iv); auto slot = server->accept(session); if (!slot) {