From 39863c597ecdc851e2e7fad1421f5fc79f57d95c Mon Sep 17 00:00:00 2001 From: dijunkun Date: Tue, 3 Sep 2024 15:50:38 +0800 Subject: [PATCH] [feat] allow user to set customized password --- src/localization/localization.h | 6 +- .../connection_status_window.cpp | 10 +- src/single_window/local_peer_window.cpp | 140 +++++++++++++++--- src/single_window/remote_peer_window.cpp | 2 +- src/single_window/render.cpp | 12 +- src/single_window/render.h | 6 +- src/single_window/render_callback_func.cpp | 6 +- thirdparty/projectx | 2 +- 8 files changed, 144 insertions(+), 40 deletions(-) diff --git a/src/localization/localization.h b/src/localization/localization.h index f9983f5..8fd44fc 100644 --- a/src/localization/localization.h +++ b/src/localization/localization.h @@ -51,8 +51,10 @@ static std::vector enable_turn = {u8"启用中继服务:", static std::vector ok = {u8"确认", "OK"}; static std::vector cancel = {u8"取消", "Cancel"}; -static std::vector input_password = {u8"请输入密码", - "Please input password"}; +static std::vector new_password = {u8"请输入新密码:", + "Please input new password:"}; +static std::vector input_password = {u8"请输入密码:", + "Please input password:"}; static std::vector validate_password = {u8"验证密码中...", "Validate password ..."}; static std::vector reinput_password = { diff --git a/src/single_window/connection_status_window.cpp b/src/single_window/connection_status_window.cpp index af26293..815428c 100644 --- a/src/single_window/connection_status_window.cpp +++ b/src/single_window/connection_status_window.cpp @@ -86,10 +86,10 @@ int Render::ConnectionStatusWindow() { ImGui::SetKeyboardFocusHere(); focus_on_input_widget_ = false; } - bool enter_pressed = - ImGui::InputText("##password", (char *)remote_password_.c_str(), 7, - ImGuiInputTextFlags_CharsNoBlank | - ImGuiInputTextFlags_EnterReturnsTrue); + bool enter_pressed = ImGui::InputText( + "##password", remote_password_, IM_ARRAYSIZE(remote_password_), + ImGuiInputTextFlags_CharsNoBlank | + ImGuiInputTextFlags_EnterReturnsTrue); ImGui::PopStyleVar(); @@ -109,7 +109,7 @@ int Render::ConnectionStatusWindow() { if (ImGui::Button( localization::cancel[localization_language_index_].c_str())) { - remote_password_ = ""; + memset(remote_password_, 0, sizeof(remote_password_)); show_connection_status_window_ = false; focus_on_input_widget_ = true; } diff --git a/src/single_window/local_peer_window.cpp b/src/single_window/local_peer_window.cpp index ada454f..e7f14a7 100644 --- a/src/single_window/local_peer_window.cpp +++ b/src/single_window/local_peer_window.cpp @@ -48,17 +48,16 @@ int Render::LocalWindow() { ImGui::SetWindowFontScale(1.0f); ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, 1.0f); - std::string client_id_display = ""; - for (int i = 0; i < sizeof(client_id_); i++) { - client_id_display += client_id_[i]; + char client_id_display[12] = ""; + for (int i = 0, j = 0; i < sizeof(client_id_) + 2; i++, j++) { + client_id_display[j] = client_id_[i]; if (i == 2 || i == 5) { - client_id_display += " "; + client_id_display[++j] = ' '; } } ImGui::InputText( - "##local_id", (char *)client_id_display.c_str(), - sizeof(client_id_display), + "##local_id", client_id_display, IM_ARRAYSIZE(client_id_display), ImGuiInputTextFlags_CharsUppercase | ImGuiInputTextFlags_ReadOnly); ImGui::PopStyleVar(); @@ -142,8 +141,9 @@ int Render::LocalWindow() { random_password_ += a[distribution(generator)]; } password_inited_ = true; - if (random_password_ != password_saved_) { - password_saved_ = random_password_; + if (0 != strcmp(random_password_.c_str(), password_saved_)) { + strncpy(password_saved_, random_password_.c_str(), + sizeof(password_saved_)); LOG_INFO("Generate new password and save into cache file"); SaveSettingsIntoCacheFile(); } @@ -154,10 +154,12 @@ int Render::LocalWindow() { ImGui::InputTextWithHint( "##server_pwd", localization::max_password_len[localization_language_index_].c_str(), - (char *)password_saved_.c_str(), password_saved_.length() + 1, - show_password_ ? ImGuiInputTextFlags_CharsNoBlank - : ImGuiInputTextFlags_CharsNoBlank | - ImGuiInputTextFlags_Password); + password_saved_, IM_ARRAYSIZE(password_saved_), + show_password_ + ? ImGuiInputTextFlags_CharsNoBlank | ImGuiInputTextFlags_ReadOnly + : ImGuiInputTextFlags_CharsNoBlank | + ImGuiInputTextFlags_Password | + ImGuiInputTextFlags_ReadOnly); ImGui::PopStyleVar(); ImGui::SameLine(); @@ -166,20 +168,27 @@ int Render::LocalWindow() { ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0, 0, 0, 0)); ImGui::SetWindowFontScale(0.5f); - if (ImGui::Button(show_password_ ? ICON_FA_EYE : ICON_FA_EYE_SLASH, - ImVec2(35, 38))) { + auto l_x = ImGui::GetCursorScreenPos().x; + auto l_y = ImGui::GetCursorScreenPos().y; + if (ImGui::Button(ICON_FA_EYE, ImVec2(22, 38))) { show_password_ = !show_password_; } - ImGui::PopStyleColor(3); + + if (!show_password_) { + ImDrawList *draw_list = ImGui::GetWindowDrawList(); + draw_list->AddLine(ImVec2(l_x + 3.0f, l_y + 12.5f), + ImVec2(l_x + 20.3f, l_y + 26.5f), + IM_COL32(239, 240, 242, 255), 2.0f); + draw_list->AddLine(ImVec2(l_x + 3.0f, l_y + 11.0f), + ImVec2(l_x + 20.3f, l_y + 25.0f), + IM_COL32(0, 0, 0, 255), 1.5f); + } ImGui::SameLine(); - ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0)); - ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0, 0, 0, 0)); - ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0, 0, 0, 0)); if (ImGui::Button( regenerate_password_ ? ICON_FA_SPINNER : ICON_FA_ARROWS_ROTATE, - ImVec2(35, 38))) { + ImVec2(22, 38))) { regenerate_password_ = true; password_inited_ = false; regenerate_password_start_time_ = ImGui::GetTime(); @@ -190,11 +199,104 @@ int Render::LocalWindow() { regenerate_password_ = false; } + ImGui::SameLine(); + + if (ImGui::Button(ICON_FA_PEN, ImVec2(22, 38))) { + show_reset_password_window_ = true; + } ImGui::SetWindowFontScale(1.0f); ImGui::PopStyleColor(3); + + if (show_reset_password_window_) { + const ImGuiViewport *viewport = ImGui::GetMainViewport(); + + ImGui::SetNextWindowPos( + ImVec2((viewport->WorkSize.x - viewport->WorkPos.x - + connection_status_window_width_) / + 2, + (viewport->WorkSize.y - viewport->WorkPos.y - + connection_status_window_height_) / + 2)); + + ImGui::SetNextWindowSize(ImVec2(connection_status_window_width_, + connection_status_window_height_)); + + ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 3.0f); + + ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(1.0, 1.0, 1.0, 1.0)); + ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 1.0f); + ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 5.0f); + + ImGui::Begin("ResetPasswordWindow", nullptr, + ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse | + ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar | + ImGuiWindowFlags_NoSavedSettings); + ImGui::PopStyleVar(2); + ImGui::PopStyleColor(); + + auto window_width = ImGui::GetWindowSize().x; + auto window_height = ImGui::GetWindowSize().y; + std::string text = + localization::new_password[localization_language_index_]; + auto text_width = ImGui::CalcTextSize(text.c_str()).x; + ImGui::SetWindowFontScale(0.5f); + ImGui::SetCursorPosX((window_width - text_width / 2) * 0.5f); + ImGui::SetCursorPosY(window_height * 0.2f); + ImGui::Text("%s", text.c_str()); + + ImGui::SetCursorPosX((window_width - IPUT_WINDOW_WIDTH / 2) * 0.5f); + ImGui::SetCursorPosY(window_height * 0.4f); + ImGui::SetNextItemWidth(IPUT_WINDOW_WIDTH / 2); + + ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, 1.0f); + + if (focus_on_input_widget_) { + ImGui::SetKeyboardFocusHere(); + focus_on_input_widget_ = false; + } + + bool enter_pressed = ImGui::InputText( + "##new_password", new_password_, IM_ARRAYSIZE(new_password_), + ImGuiInputTextFlags_CharsNoBlank | + ImGuiInputTextFlags_EnterReturnsTrue); + + ImGui::PopStyleVar(); + + ImGui::SetCursorPosX(window_width * 0.315f); + ImGui::SetCursorPosY(window_height * 0.75f); + + // OK + if (enter_pressed || + ImGui::Button( + localization::ok[localization_language_index_].c_str())) { + show_reset_password_window_ = false; + LOG_INFO("Generate new password and save into cache file"); + strncpy(password_saved_, new_password_, sizeof(password_saved_)); + memset(new_password_, 0, sizeof(new_password_)); + SaveSettingsIntoCacheFile(); + LeaveConnection(peer_, client_id_); + is_create_connection_ = false; + focus_on_input_widget_ = true; + } + + ImGui::SameLine(); + + if (ImGui::Button( + localization::cancel[localization_language_index_].c_str())) { + show_reset_password_window_ = false; + focus_on_input_widget_ = true; + memset(new_password_, 0, sizeof(new_password_)); + } + + ImGui::SetWindowFontScale(1.0f); + + ImGui::End(); + ImGui::PopStyleVar(); + } } ImGui::EndChild(); + ImGui::PopStyleVar(); } ImGui::EndChild(); diff --git a/src/single_window/remote_peer_window.cpp b/src/single_window/remote_peer_window.cpp index c3617b7..0208d82 100644 --- a/src/single_window/remote_peer_window.cpp +++ b/src/single_window/remote_peer_window.cpp @@ -91,7 +91,7 @@ int Render::RemoteWindow() { } } ret = JoinConnection(peer_reserved_ ? peer_reserved_ : peer_, - remote_id_.c_str(), remote_password_.c_str()); + remote_id_.c_str(), remote_password_); if (0 == ret) { is_client_mode_ = true; rejoin_ = false; diff --git a/src/single_window/render.cpp b/src/single_window/render.cpp index 66b514c..0c9728a 100644 --- a/src/single_window/render.cpp +++ b/src/single_window/render.cpp @@ -77,8 +77,7 @@ int Render::SaveSettingsIntoCacheFile() { memset(&cd_cache_.client_id, 0, sizeof(cd_cache_.client_id)); strncpy(cd_cache_.client_id, client_id_, sizeof(client_id_)); memset(&cd_cache_.password, 0, sizeof(cd_cache_.password)); - strncpy(cd_cache_.password, password_saved_.c_str(), - password_saved_.length()); + strncpy(cd_cache_.password, password_saved_, sizeof(password_saved_)); memcpy(&cd_cache_.language, &language_button_value_, sizeof(language_button_value_)); memcpy(&cd_cache_.video_quality, &video_quality_button_value_, @@ -107,7 +106,7 @@ int Render::LoadSettingsFromCacheFile() { cd_cache_file_ = fopen("cache.cd", "r+"); if (!cd_cache_file_) { LOG_INFO("Init cache file by using default settings"); - password_saved_ = ""; + memset(password_saved_, 0, sizeof(password_saved_)); language_button_value_ = 0; video_quality_button_value_ = 0; video_encode_format_button_value_ = 1; @@ -129,8 +128,8 @@ int Render::LoadSettingsFromCacheFile() { memset(&client_id_, 0, sizeof(client_id_)); strncpy(client_id_, cd_cache_.client_id, sizeof(client_id_)); - password_saved_ = cd_cache_.password; - if (!password_saved_.empty() && 6 == password_saved_.length()) { + strncpy(password_saved_, cd_cache_.password, sizeof(password_saved_)); + if (0 != strcmp(password_saved_, "") && 7 == sizeof(password_saved_)) { password_inited_ = true; } language_button_value_ = cd_cache_.language; @@ -442,8 +441,7 @@ int Render::Run() { "Failed" != connection_status_str_) { LOG_INFO("Connected with signal server, create p2p connection"); is_create_connection_ = - CreateConnection(peer_, client_id_, password_saved_.c_str()) ? false - : true; + CreateConnection(peer_, client_id_, password_saved_) ? false : true; } if (!inited_ || diff --git a/src/single_window/render.h b/src/single_window/render.h index f4f53b2..0386990 100644 --- a/src/single_window/render.h +++ b/src/single_window/render.h @@ -122,7 +122,8 @@ class Render { char input_password_tmp_[7] = ""; char input_password_[7] = ""; std::string random_password_ = ""; - std::string remote_password_ = ""; + char remote_password_[7] = ""; + char new_password_[7] = ""; char remote_id_display_[12] = ""; std::string remote_id_ = ""; char client_password_[20] = ""; @@ -202,6 +203,7 @@ class Render { bool regenerate_password_ = false; bool show_about_window_ = false; bool show_connection_status_window_ = false; + bool show_reset_password_window_ = false; bool focus_on_input_widget_ = true; bool window_maximized_ = false; bool streaming_ = false; @@ -253,7 +255,7 @@ class Render { private: char client_id_[10] = ""; - std::string password_saved_ = ""; + char password_saved_[7] = ""; int language_button_value_ = 0; int video_quality_button_value_ = 0; int video_encode_format_button_value_ = 0; diff --git a/src/single_window/render_callback_func.cpp b/src/single_window/render_callback_func.cpp index 10f7531..525fdd9 100644 --- a/src/single_window/render_callback_func.cpp +++ b/src/single_window/render_callback_func.cpp @@ -183,15 +183,15 @@ void Render::OnConnectionStatusCb(ConnectionStatus status, void *user_data) { render->show_connection_status_window_ = true; if (ConnectionStatus::Connecting == status) { render->connection_status_str_ = "Connecting"; + } else if (ConnectionStatus::Gathering == status) { + render->connection_status_str_ = "Gathering"; } else if (ConnectionStatus::Connected == status) { render->connection_status_str_ = "Connected"; render->connection_established_ = true; - if (render->peer_reserved_ || !render->is_client_mode_) { render->start_screen_capture_ = true; render->start_mouse_control_ = true; } - } else if (ConnectionStatus::Disconnected == status) { render->connection_status_str_ = "Disconnected"; render->password_validating_time_ = 0; @@ -212,7 +212,7 @@ void Render::OnConnectionStatusCb(ConnectionStatus status, void *user_data) { } render->exit_video_window_ = false; if (!render->rejoin_) { - render->remote_password_.clear(); + memset(render->remote_password_, 0, sizeof(render->remote_password_)); } if (render->dst_buffer_) { memset(render->dst_buffer_, 0, 1280 * 720 * 3); diff --git a/thirdparty/projectx b/thirdparty/projectx index fd39292..d78dc45 160000 --- a/thirdparty/projectx +++ b/thirdparty/projectx @@ -1 +1 @@ -Subproject commit fd392922d782557acf5b4f4dc3787e25a43ccb22 +Subproject commit d78dc4585f5524601baf844f3963fa358be5b433