[fix] fix thumbnail filename which stores remote id, remote host name and remote password

This commit is contained in:
dijunkun
2025-06-20 18:42:29 +08:00
parent 41c27139a0
commit 3901e26c37
5 changed files with 87 additions and 28 deletions

View File

@@ -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 {

View File

@@ -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<char*>(&cd_cache_), sizeof(CDCache));
cd_cache_file.close();
@@ -483,9 +485,9 @@ int Render::CreateConnectionPeer() {
peer_ = CreatePeer(&params_);
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_);
}

View File

@@ -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,16 +256,18 @@ 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<std::filesystem::path> Thumbnail::FindThumbnailPath(
const std::filesystem::path& directory) {

View File

@@ -38,7 +38,7 @@ class Thumbnail {
std::map<std::string, RecentConnection>& recent_connections,
int* width, int* height);
int DeleteThumbnail(const std::string& file_name);
int DeleteThumbnail(const std::string& filename_keyword);
int DeleteAllFilesInDirectory();