From 885a63b83778fbcf85765d10ec94ea06b9ca72da Mon Sep 17 00:00:00 2001 From: Tindy X <49061470+tindy2013@users.noreply.github.com> Date: Tue, 14 Nov 2023 16:42:22 +0800 Subject: [PATCH] Enhancements Fix a typo which cause the server to hang. Add option to enable reload pref config on request. Optimize codes. --- base/pref.example.ini | 3 +++ base/pref.example.toml | 3 +++ base/pref.example.yml | 1 + src/generator/config/subexport.cpp | 26 +++++++++++++------------- src/handler/interfaces.cpp | 2 +- src/handler/settings.cpp | 5 ++++- src/handler/settings.h | 1 + 7 files changed, 26 insertions(+), 15 deletions(-) diff --git a/base/pref.example.ini b/base/pref.example.ini index 6029e18a4..fe1688fa8 100644 --- a/base/pref.example.ini +++ b/base/pref.example.ini @@ -73,6 +73,9 @@ proxy_subscription=NONE ;Append a proxy type string ([SS] [SSR] [VMess]) to node remark. append_proxy_type=false +;When requesting /sub, reload this config file first. +reload_conf_on_request=false + [userinfo] ;Rules to extract stream data from node ;Format: full_match_regex|new_format_regex diff --git a/base/pref.example.toml b/base/pref.example.toml index 9a486d969..b78a007e5 100644 --- a/base/pref.example.toml +++ b/base/pref.example.toml @@ -79,6 +79,9 @@ proxy_subscription = "NONE" # Append a proxy type string ([SS] [SSR] [VMess]) to node remark. append_proxy_type = false +# When requesting /sub, reload this config file first. +reload_conf_on_request = false + [[userinfo.stream_rule]] # Rules to extract stream data from node # Format: full_match_regex|new_format_regex diff --git a/base/pref.example.yml b/base/pref.example.yml index b7df31297..f532994cc 100644 --- a/base/pref.example.yml +++ b/base/pref.example.yml @@ -24,6 +24,7 @@ common: proxy_ruleset: SYSTEM proxy_subscription: NONE append_proxy_type: false + reload_conf_on_request: false userinfo: stream_rule: diff --git a/src/generator/config/subexport.cpp b/src/generator/config/subexport.cpp index 8dcb27bc8..941eabc51 100644 --- a/src/generator/config/subexport.cpp +++ b/src/generator/config/subexport.cpp @@ -172,7 +172,7 @@ void processRemark(std::string &remark, const string_array &remarks_list, bool p } std::string tempRemark = remark; int cnt = 2; - while(std::find(remarks_list.cbegin(), remarks_list.cend(), tempRemark) != remarks_list.cbegin()) + while(std::find(remarks_list.cbegin(), remarks_list.cend(), tempRemark) != remarks_list.cend()) { tempRemark = remark + " " + std::to_string(cnt); cnt++; @@ -2078,7 +2078,7 @@ static rapidjson::Value buildSingBoxTransport(const Proxy& proxy, rapidjson::Mem return transport; } -void addSingBoxCommonMembers(rapidjson::Value &proxy, const Proxy &x, const rapidjson::GenericStringRef &type, rapidjson::MemoryPoolAllocator<> &allocator) +static void addSingBoxCommonMembers(rapidjson::Value &proxy, const Proxy &x, const rapidjson::GenericStringRef &type, rapidjson::MemoryPoolAllocator<> &allocator) { proxy.AddMember("type", type, allocator); proxy.AddMember("tag", rapidjson::StringRef(x.Remark.c_str()), allocator); @@ -2086,6 +2086,15 @@ void addSingBoxCommonMembers(rapidjson::Value &proxy, const Proxy &x, const rapi proxy.AddMember("server_port", x.Port, allocator); } +static rapidjson::Value stringArrayToJsonArray(const std::string &array, const std::string &delimiter, rapidjson::MemoryPoolAllocator<> &allocator) +{ + rapidjson::Value result(rapidjson::kArrayType); + string_array vArray = split(array, delimiter); + for (const auto &x : vArray) + result.PushBack(rapidjson::Value(trim(x).c_str(), allocator), allocator); + return result; +} + void proxyToSingBox(std::vector &nodes, rapidjson::Document &json, std::vector &ruleset_content_array, const ProxyGroupConfigs &extra_proxy_group, extra_settings &ext) { using namespace rapidjson_ext; rapidjson::Document::AllocatorType &allocator = json.GetAllocator(); @@ -2181,22 +2190,13 @@ void proxyToSingBox(std::vector &nodes, rapidjson::Document &json, std::v if (!x.AllowedIPs.empty()) { - auto allowed = split(x.AllowedIPs, ","); - rapidjson::Value allowed_ips(rapidjson::kArrayType); - for (const auto &ip: allowed) { - allowed_ips.PushBack(rapidjson::Value(trim(ip).c_str(), allocator), allocator); - } + auto allowed_ips = stringArrayToJsonArray(x.AllowedIPs, ",", allocator); peer.AddMember("allowed_ips", allowed_ips, allocator); } if (!x.ClientId.empty()) { - auto client_id = split(x.ClientId, ","); - rapidjson::Value reserved(rapidjson::kArrayType); - for (const auto &id : client_id) - { - reserved.PushBack(to_int(trim(id)), allocator); - } + auto reserved = stringArrayToJsonArray(x.ClientId, ",", allocator); peer.AddMember("reserved", reserved, allocator); } diff --git a/src/handler/interfaces.cpp b/src/handler/interfaces.cpp index faced4f90..44e7c1773 100644 --- a/src/handler/interfaces.cpp +++ b/src/handler/interfaces.cpp @@ -323,7 +323,7 @@ std::string subconverter(RESPONSE_CALLBACK_ARGS) return "Invalid target!"; } //check if we need to read configuration - if((!global.APIMode || global.CFWChildProcess) && !global.generatorMode) + if(global.reloadConfOnRequest && (!global.APIMode || global.CFWChildProcess) && !global.generatorMode) readConf(); /// string values diff --git a/src/handler/settings.cpp b/src/handler/settings.cpp index 51f4437b7..678da3368 100644 --- a/src/handler/settings.cpp +++ b/src/handler/settings.cpp @@ -338,6 +338,7 @@ void readYAMLConf(YAML::Node &node) section["proxy_config"] >> global.proxyConfig; section["proxy_ruleset"] >> global.proxyRuleset; section["proxy_subscription"] >> global.proxySubscription; + section["reload_conf_on_request"] >> global.reloadConfOnRequest; if(node["userinfo"].IsDefined()) { @@ -612,7 +613,8 @@ void readTOMLConf(toml::value &root) "proxy_config", global.proxyConfig, "proxy_ruleset", global.proxyRuleset, "proxy_subscription", global.proxySubscription, - "append_proxy_type", global.appendType + "append_proxy_type", global.appendType, + "reload_conf_on_request", global.reloadConfOnRequest ); if(filter) @@ -854,6 +856,7 @@ void readConf() ini.get_if_exist("proxy_config", global.proxyConfig); ini.get_if_exist("proxy_ruleset", global.proxyRuleset); ini.get_if_exist("proxy_subscription", global.proxySubscription); + ini.get_bool_if_exist("reload_conf_on_request", global.reloadConfOnRequest); if(ini.section_exist("surge_external_proxy")) { diff --git a/src/handler/settings.h b/src/handler/settings.h index d2d6932aa..9705145a5 100644 --- a/src/handler/settings.h +++ b/src/handler/settings.h @@ -42,6 +42,7 @@ struct Settings std::string generateProfiles; //preferences + bool reloadConfOnRequest = false; RegexMatchConfigs renames, emojis; bool addEmoji = false, removeEmoji = false, appendType = false, filterDeprecated = true; tribool UDPFlag, TFOFlag, skipCertVerify, TLS13Flag, enableInsert;