config.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // MIT License
  2. // Copyright(c) 2024 FastWeb - fwlua.com - nianhua
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files(the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and /or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions :
  9. // The above copyright notice and this permission notice shall be included in all
  10. // copies or substantial portions of the Software.
  11. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
  14. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. // SOFTWARE.
  18. // ## Additional Terms for Commercial Use
  19. // This software is licensed for personal, educational, and non - commercial use.
  20. // For commercial use or use within a company, organization, or institution, a
  21. // separate commercial license is required.To obtain a commercial license,
  22. // please contact
  23. // EMail:1585346868@qq.com
  24. // QQ:1585346868
  25. #include "config.h"
  26. #include <regex>
  27. #include "core/app.h"
  28. fastweb::config::config(fastweb::app* ptr):Interface(ptr)
  29. {
  30. }
  31. bool fastweb::config::open(const std::string& ini_filepath)
  32. {
  33. if (ylib::file::exist(ini_filepath) == false)
  34. {
  35. LOG_ERROR("not found file: " + ini_filepath);
  36. return false;
  37. }
  38. std::string temp_filepath = ylib::file::temp_filepath() + ".bak";
  39. if (ylib::file::copy(ini_filepath, temp_filepath) == false)
  40. {
  41. LOG_ERROR("Failed to copy temporary INI configuration file from '" + ini_filepath + "' to '" + temp_filepath + "'.");
  42. return false;
  43. }
  44. std::string src_content = ylib::file::read(temp_filepath);
  45. // EXE运行目录
  46. src_content = strutils::replace(src_content, "${current_dir}", strutils::replace(system::current_dir(),'\\','/'));
  47. // 配置文件目录
  48. {
  49. std::string ini_dir = ylib::file::parent_dir(ini_filepath);
  50. src_content = strutils::replace(strutils::replace(src_content, "${config_dir}", ini_dir), '\\', '/');
  51. }
  52. ylib::file::write(temp_filepath,src_content);
  53. if (m_ini.open(temp_filepath))
  54. {
  55. auto vars = extractVariableNames(ylib::file::read(temp_filepath));
  56. for (size_t i = 0; i < vars.size(); i++)
  57. {
  58. if (m_ini.read("variable", vars[i]) == "")
  59. {
  60. LOG_ERROR("Variable declaration not found or empty content: ${" + vars[i] + "}");
  61. return false;
  62. }
  63. src_content = strutils::replace(src_content, "${" + vars[i] + "}", m_ini.read("variable", vars[i]));
  64. }
  65. }
  66. ylib::file::write(temp_filepath,src_content);
  67. m_ini.close();
  68. m_ini.open(temp_filepath);
  69. cache();
  70. return true;
  71. }
  72. std::vector<std::string> fastweb::config::lua_lib_files()
  73. {
  74. std::vector<std::string> results;
  75. auto luas = ylib::file::traverse(app()->config->website.dir, "(.*\\.lua)");
  76. for_iter(iter, luas)
  77. {
  78. if (iter->second == IS_DIRECTORY)
  79. continue;
  80. std::string path = app()->config->website.dir +"/"+ strutils::replace(iter->first, '\\', '/');
  81. results.push_back(path);
  82. }
  83. // 去重
  84. std::sort(results.begin(), results.end());
  85. auto newEnd = std::unique(results.begin(), results.end());
  86. results.erase(newEnd, results.end());
  87. return results;
  88. }
  89. std::vector<std::string> fastweb::config::extractVariableNames(const std::string& text)
  90. {
  91. std::regex pattern("\\$\\{([^}]+)\\}"); // 使用捕获组提取中间的内容
  92. std::vector<std::string> results;
  93. // 使用 std::sregex_iterator 迭代所有匹配项
  94. auto begin = std::sregex_iterator(text.begin(), text.end(), pattern);
  95. auto end = std::sregex_iterator();
  96. for (std::sregex_iterator i = begin; i != end; ++i) {
  97. std::smatch match = *i;
  98. results.push_back(match[1]); // 仅添加捕获组的内容
  99. }
  100. return results;
  101. }
  102. void fastweb::config::cache()
  103. {
  104. scripts.module_dir = m_ini.read("scripts", "module_dir");
  105. scripts.lua_cache_size = ylib::stoi(m_ini.read("scripts", "lua_cache_size"));
  106. scripts.auto_update_sec = ylib::stoi(m_ini.read("scripts", "auto_update_sec"));
  107. website.dir = m_ini.read("website","dir");
  108. website.default_404 = m_ini.read("website", "default_404");
  109. website.default_index = strutils::split(m_ini.read("website", "default_index"), ',');
  110. website.session_dir = m_ini.read("website", "session_dir");
  111. website.session_timeout_sec = ylib::stoi(m_ini.read("website", "session_timeout_sec"));
  112. website.Initialization_script = m_ini.read("website", "Initialization_script");
  113. website.debug = m_ini.read("website", "debug") == "1";
  114. website.domain = strutils::split(m_ini.read("website", "domain"),',');
  115. website.direct_url_mapping = m_ini.read("website", "direct_url_mapping") == "1";
  116. log.enable = m_ini.read("log", "enable") == "1";
  117. log.dir = m_ini.read("log", "dir");
  118. log.name = m_ini.read("log", "name");
  119. log.sqlite = m_ini.read("log", "sqlite") == "1";
  120. //log.succ = m_ini.read("log", "succ") == "1";
  121. //log.info = m_ini.read("log", "info") == "1";
  122. //log.warn = m_ini.read("log", "warn") == "1";
  123. //log.error = m_ini.read("log", "error") == "1";
  124. // website 域名参数
  125. for(size_t i=0;i< website.domain.size();i++)
  126. {
  127. if (website.domain[i].empty())
  128. continue;
  129. struct config::domain dm;
  130. dm.port = ylib::stoi(m_ini.read(website.domain[i], "port"));
  131. dm.https = m_ini.read(website.domain[i],"https")=="1";
  132. dm.ssl.pem_key = ylib::file::read(m_ini.read(website.domain[i], "ssl_key"));
  133. dm.ssl.pem_cert = ylib::file::read(m_ini.read(website.domain[i], "ssl_pem"));
  134. dm.ssl.pem_ca = ylib::file::read(m_ini.read(website.domain[i], "ssl_ca"));
  135. dm.ssl.pem_password = m_ini.read(website.domain[i], "ssl_pwd");
  136. dm.ssl.type = (network::http::ssl_verify_type)ylib::stoi(m_ini.read(website.domain[i], "ssl_ver_type"));
  137. if (dm.port == 0)
  138. {
  139. LOG_ERROR("domain("+website.domain[i] + ") name unavailable, port is 0");
  140. continue;
  141. }
  142. if (dm.https)
  143. {
  144. dm.ssl.enable = true;
  145. if (dm.ssl.pem_key.empty())
  146. {
  147. LOG_ERROR("domain(" + website.domain[i] + ") ssl_key file is read as empty, please check the ssl_key");
  148. continue;
  149. }
  150. if (dm.ssl.pem_cert.empty())
  151. {
  152. LOG_ERROR("domain(" + website.domain[i] + ") ssl_pem file is read as empty, please check the ssl_pem");
  153. continue;
  154. }
  155. if (dm.ssl.type < 0 || dm.ssl.type > 3)
  156. {
  157. LOG_ERROR("domain(" + website.domain[i] + ") ssl_ver_type is not filled in correctly, it should be: 0~3");
  158. continue;
  159. }
  160. }
  161. domain.emplace(website.domain[i],dm);
  162. }
  163. }