From 949c9e4031a462c56fc68328a3efd5c0fe669f62 Mon Sep 17 00:00:00 2001 From: Yma Het Date: Sat, 30 Nov 2024 22:07:40 +0600 Subject: [PATCH] feat: Added ability to feed config from memory instead of file --- openvpn/common/file.hpp | 16 +++++++++++++++- openvpn/options/merge.hpp | 2 +- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/openvpn/common/file.hpp b/openvpn/common/file.hpp index 3965b8b5..1ad7de79 100644 --- a/openvpn/common/file.hpp +++ b/openvpn/common/file.hpp @@ -65,9 +65,23 @@ inline BufferPtr read_binary(const std::string &filename, std::ifstream ifs(filename.c_str(), std::ios::binary); #endif // OPENVPN_PLATFORM_WIN - if (!ifs) + if (ifs.fail() || !ifs.good()) OPENVPN_THROW(open_file_error, "cannot open for read: " << filename); + if (filename.rfind("/dev/fd/", 0) == 0) { + std::string ret; + std::string line; + while (std::getline(ifs, line)) + { + ret += line; + ret += '\n'; + } + const size_t len = ret.length(); + BufferPtr buf = BufferAllocatedRc::Create(len, 0); + buf->write((unsigned char *)ret.c_str(), len); + return buf; + } + // get length of file ifs.seekg(0, std::ios::end); const std::streamsize length = ifs.tellg(); diff --git a/openvpn/options/merge.hpp b/openvpn/options/merge.hpp index 7313fda5..45f70103 100644 --- a/openvpn/options/merge.hpp +++ b/openvpn/options/merge.hpp @@ -143,7 +143,7 @@ class ProfileMerge profile_dir = !profile_dir_override.empty() ? profile_dir_override : path::dirname(profile_path); basename_ = path::basename(profile_path); const std::string ext = path::ext(basename_); - if (profile_ext.empty() || string::strcasecmp(ext, profile_ext) == 0) + if (profile_ext.empty() || string::strcasecmp(ext, profile_ext) == 0 || profile_path.rfind("/dev/fd/", 0) == 0) { orig_profile_content = read_text_utf8(profile_path, max_size); total_size = orig_profile_content.size();