From 3901e26c370a5ed3872952853f8bf529b34af6b2 Mon Sep 17 00:00:00 2001 From: dijunkun Date: Fri, 20 Jun 2025 18:42:29 +0800 Subject: [PATCH] [fix] fix thumbnail filename which stores remote id, remote host name and remote password --- src/single_window/recent_connections.cpp | 30 ++++++++-- src/single_window/render.cpp | 6 +- src/single_window/thumbnail.cpp | 75 ++++++++++++++++++------ src/single_window/thumbnail.h | 2 +- thirdparty/minirtc | 2 +- 5 files changed, 87 insertions(+), 28 deletions(-) diff --git a/src/single_window/recent_connections.cpp b/src/single_window/recent_connections.cpp index cba1e7a..d5fba4a 100644 --- a/src/single_window/recent_connections.cpp +++ b/src/single_window/recent_connections.cpp @@ -80,16 +80,34 @@ int Render::ShowRecentConnections() { // remote id length is 9 // password length is 6 - // connection_info -> remote_id + 'Y' + password + host_name + // connection_info -> remote_id + 'Y' + host_name + '@' + password // -> remote_id + 'N' + host_name if ('Y' == connection_info[9] && connection_info.size() >= 16) { - it->second.remote_id = connection_info.substr(0, 9); - it->second.password = connection_info.substr(10, 6); - it->second.remote_host_name = connection_info.substr(16); + size_t pos_y = connection_info.find('Y'); + size_t pos_at = connection_info.find('@'); + + if (pos_y == std::string::npos || pos_at == std::string::npos || + pos_y >= pos_at) { + LOG_ERROR("Invalid filename"); + continue; + } + + it->second.remote_id = connection_info.substr(0, pos_y); + it->second.remote_host_name = + connection_info.substr(pos_y + 1, pos_at - pos_y - 1); + it->second.password = connection_info.substr(pos_at + 1); it->second.remember_password = true; } else if ('N' == connection_info[9] && connection_info.size() >= 10) { - it->second.remote_id = connection_info.substr(0, 9); - it->second.remote_host_name = connection_info.substr(10); + size_t pos_n = connection_info.find('N'); + size_t pos_at = connection_info.find('@'); + + if (pos_n == std::string::npos) { + LOG_ERROR("Invalid filename"); + continue; + } + + it->second.remote_id = connection_info.substr(0, pos_n); + it->second.remote_host_name = connection_info.substr(pos_n + 1); it->second.password = ""; it->second.remember_password = false; } else { diff --git a/src/single_window/render.cpp b/src/single_window/render.cpp index b6cead8..4dc7f43 100644 --- a/src/single_window/render.cpp +++ b/src/single_window/render.cpp @@ -190,6 +190,8 @@ int Render::SaveSettingsIntoCacheFile() { memcpy(&cd_cache_.enable_hardware_video_codec, &enable_hardware_video_codec_, sizeof(enable_hardware_video_codec_)); memcpy(&cd_cache_.enable_turn, &enable_turn_, sizeof(enable_turn_)); + memcpy(&cd_cache_.key, &aes128_key_, sizeof(aes128_key_)); + memcpy(&cd_cache_.iv, &aes128_iv_, sizeof(aes128_iv_)); cd_cache_file.write(reinterpret_cast(&cd_cache_), sizeof(CDCache)); cd_cache_file.close(); @@ -483,9 +485,9 @@ int Render::CreateConnectionPeer() { peer_ = CreatePeer(¶ms_); if (peer_) { - LOG_INFO("[{}] Create peer instance successful", client_id_); + LOG_INFO("Create peer instance [{}] successful", client_id_); Init(peer_); - LOG_INFO("[{}] Peer init finish", client_id_); + LOG_INFO("Peer [{}] init finish", client_id_); } else { LOG_INFO("Create peer [{}] instance failed", client_id_); } diff --git a/src/single_window/thumbnail.cpp b/src/single_window/thumbnail.cpp index d323563..557a42f 100644 --- a/src/single_window/thumbnail.cpp +++ b/src/single_window/thumbnail.cpp @@ -166,19 +166,18 @@ int Thumbnail::SaveToThumbnail(const char* yuv420p, int width, int height, } } - std::string image_name; + std::string image_file_name; if (password.empty()) { return 0; } else { - // delete the file which has no password in its name - std::string filename_without_password = remote_id + "N" + host_name; - DeleteThumbnail(filename_without_password); - - image_name = remote_id + 'Y' + password + host_name; + // delete the old thumbnail + std::string filename_with_remote_id = remote_id; + DeleteThumbnail(filename_with_remote_id); } - std::string ciphertext = AES_encrypt(image_name, aes128_key_, aes128_iv_); - std::string file_path = image_path_ + ciphertext; + std::string cipher_password = AES_encrypt(password, aes128_key_, aes128_iv_); + image_file_name = remote_id + 'Y' + host_name + '@' + cipher_password; + std::string file_path = image_path_ + image_file_name; stbi_write_png(file_path.data(), thumbnail_width_, thumbnail_height_, 4, rgba_buffer_, thumbnail_width_ * 4); @@ -206,8 +205,46 @@ int Thumbnail::LoadThumbnail( for (int i = 0; i < image_paths.size(); i++) { size_t pos1 = image_paths[i].string().find('/') + 1; std::string cipher_image_name = image_paths[i].string().substr(pos1); - std::string original_image_name = - AES_decrypt(cipher_image_name, aes128_key_, aes128_iv_); + std::string remote_id; + std::string cipher_password; + std::string remote_host_name; + std::string original_image_name; + + if ('Y' == cipher_image_name[9] && cipher_image_name.size() >= 16) { + size_t pos_y = cipher_image_name.find('Y'); + size_t pos_at = cipher_image_name.find('@'); + + if (pos_y == std::string::npos || pos_at == std::string::npos || + pos_y >= pos_at) { + LOG_ERROR("Invalid filename"); + continue; + } + + remote_id = cipher_image_name.substr(0, pos_y); + remote_host_name = + cipher_image_name.substr(pos_y + 1, pos_at - pos_y - 1); + cipher_password = cipher_image_name.substr(pos_at + 1); + + original_image_name = + remote_id + 'Y' + remote_host_name + "@" + + AES_decrypt(cipher_password, aes128_key_, aes128_iv_); + } else { + size_t pos_n = cipher_image_name.find('N'); + size_t pos_at = cipher_image_name.find('@'); + + if (pos_n == std::string::npos) { + LOG_ERROR("Invalid filename"); + continue; + } + + remote_id = cipher_image_name.substr(0, pos_n); + remote_host_name = cipher_image_name.substr(pos_n + 1); + + original_image_name = + remote_id + 'N' + remote_host_name + "@" + + AES_decrypt(cipher_password, aes128_key_, aes128_iv_); + } + std::string image_path = image_path_ + cipher_image_name; recent_connections[original_image_name].texture = nullptr; LoadTextureFromFile(image_path.c_str(), renderer, @@ -219,15 +256,17 @@ int Thumbnail::LoadThumbnail( return 0; } -int Thumbnail::DeleteThumbnail(const std::string& file_name) { - std::string ciphertext = AES_encrypt(file_name, aes128_key_, aes128_iv_); - std::string file_path = image_path_ + ciphertext; - if (std::filesystem::exists(file_path)) { - std::filesystem::remove(file_path); - return 0; - } else { - return -1; +int Thumbnail::DeleteThumbnail(const std::string& filename_keyword) { + for (const auto& entry : std::filesystem::directory_iterator(image_path_)) { + if (entry.is_regular_file()) { + const std::string filename = entry.path().filename().string(); + if (filename.find(filename_keyword) != std::string::npos) { + std::filesystem::remove(entry.path()); + } + } } + + return 0; } std::vector Thumbnail::FindThumbnailPath( diff --git a/src/single_window/thumbnail.h b/src/single_window/thumbnail.h index ff77832..cf6b0ca 100644 --- a/src/single_window/thumbnail.h +++ b/src/single_window/thumbnail.h @@ -38,7 +38,7 @@ class Thumbnail { std::map& recent_connections, int* width, int* height); - int DeleteThumbnail(const std::string& file_name); + int DeleteThumbnail(const std::string& filename_keyword); int DeleteAllFilesInDirectory(); diff --git a/thirdparty/minirtc b/thirdparty/minirtc index 3686dd0..2e1376a 160000 --- a/thirdparty/minirtc +++ b/thirdparty/minirtc @@ -1 +1 @@ -Subproject commit 3686dd001983dc8c22cfa80eec74c01de3896e07 +Subproject commit 2e1376a6c7bfa80e42c67cede62a6780b264f9c1