From 05a542c4f1730f4c84526b0c95d3cca388fd276e Mon Sep 17 00:00:00 2001 From: Tindy X <49061470+tindy2013@users.noreply.github.com> Date: Mon, 27 Nov 2023 00:09:25 +0800 Subject: [PATCH] Enhancements Fix bad conversion of DOMAIN-SUFFIX rules when generating Surge DOMAIN-SET. (#667) Optimize codes. --- src/generator/config/ruleconvert.cpp | 17 ++++++++------- src/handler/interfaces.cpp | 4 ++++ src/server/webserver_httplib.cpp | 4 ++++ src/server/webserver_libevent.cpp | 4 +--- src/utils/string.cpp | 9 ++++++-- src/utils/string.h | 31 ++++++++++++++++++++++++++++ 6 files changed, 57 insertions(+), 12 deletions(-) diff --git a/src/generator/config/ruleconvert.cpp b/src/generator/config/ruleconvert.cpp index 37935c4ea..ae992d958 100644 --- a/src/generator/config/ruleconvert.cpp +++ b/src/generator/config/ruleconvert.cpp @@ -458,12 +458,13 @@ void rulesetToSurge(INIReader &base_rule, std::vector &ruleset_c } } -static rapidjson::Value transformRuleToSingBox(const std::string& rule, const std::string &group, rapidjson::MemoryPoolAllocator<>& allocator) +static rapidjson::Value transformRuleToSingBox(std::vector &args, const std::string& rule, const std::string &group, rapidjson::MemoryPoolAllocator<>& allocator) { - auto args = split(rule, ","); + args.clear(); + split(args, rule, ','); if (args.size() < 2) return rapidjson::Value(rapidjson::kObjectType); auto type = toLower(std::string(args[0])); - auto value = toLower(args[1]); + auto value = toLower(std::string(args[1])); // std::string_view option; // if (args.size() >= 3) option = args[2]; @@ -483,10 +484,11 @@ static rapidjson::Value transformRuleToSingBox(const std::string& rule, const st return rule_obj; } -static void appendSingBoxRule(rapidjson::Value &rules, const std::string& rule, rapidjson::MemoryPoolAllocator<>& allocator) +static void appendSingBoxRule(std::vector &args, rapidjson::Value &rules, const std::string& rule, rapidjson::MemoryPoolAllocator<>& allocator) { using namespace rapidjson_ext; - auto args = split(rule, ','); + args.clear(); + split(args, rule, ','); if (args.size() < 2) return; auto type = args[0]; // std::string_view option; @@ -529,6 +531,7 @@ void rulesetToSingBox(rapidjson::Document &base_rule, std::vector temp(4); for(RulesetContent &x : ruleset_content_array) { if(global.maxAllowedRules && total_rules > global.maxAllowedRules) @@ -548,7 +551,7 @@ void rulesetToSingBox(rapidjson::Document &base_rule, std::vectorlisten_address; int port = args->port, nthreads = args->max_workers, max_conn = args->max_conn; - auto call_on_request = [&](evhttp_request *req, void *args) { on_request(req, args); }; - int nfd = httpserver_bindsocket(listen_address, port, max_conn); if (nfd < 0) return -1; @@ -405,7 +403,7 @@ int WebServer::start_web_server_multi(listener_args *args) return -1; evhttp_set_allowed_methods(httpd, EVHTTP_REQ_GET | EVHTTP_REQ_POST | EVHTTP_REQ_OPTIONS | EVHTTP_REQ_PUT | EVHTTP_REQ_PATCH | EVHTTP_REQ_DELETE | EVHTTP_REQ_HEAD); - evhttp_set_gencb(httpd, wrap(call_on_request), this); + evhttp_set_gencb(httpd, on_request, this); evhttp_set_timeout(httpd, 30); if (pthread_create(&ths[i], nullptr, httpserver_dispatch, base[i]) != 0) return -1; diff --git a/src/utils/string.cpp b/src/utils/string.cpp index 86b1ca881..f4751e9a4 100644 --- a/src/utils/string.cpp +++ b/src/utils/string.cpp @@ -51,9 +51,8 @@ std::vector split(const std::string &s, const std::string &separato return result; } -std::vector split(std::string_view s, char separator) +void split(std::vector &result, std::string_view s, char separator) { - std::vector result; string_size i = 0; while (i != s.size()) @@ -87,6 +86,12 @@ std::vector split(std::string_view s, char separator) i = j; } } +} + +std::vector split(std::string_view s, char separator) +{ + std::vector result; + split(result, s, separator); return result; } diff --git a/src/utils/string.h b/src/utils/string.h index 9d1571fa2..931c11459 100644 --- a/src/utils/string.h +++ b/src/utils/string.h @@ -16,6 +16,7 @@ using string_pair_array = std::vector>; std::vector split(const std::string &s, const std::string &separator); std::vector split(std::string_view s, char separator); +void split(std::vector &result, std::string_view s, char separator); std::string join(const string_array &arr, const std::string &delimiter); template @@ -97,6 +98,36 @@ template static inline T to_number(const U &value, T de int to_int(const std::string &str, int def_value = 0); +template +concept StringConstructible = requires(Type a) { + { std::string(a) } -> std::same_as; +}; + +template +concept Insertable = requires(Container a, Element b) { + { a.insert(b) } -> std::same_as; +}; + +template +requires Insertable> +void fillMap(Container& map, KeyType&& key, ValueType&& value) { + map.insert({std::string(std::forward(key)), std::forward(value)}); +} + +template +requires Insertable> +void fillMap(Container& map, KeyType&& key, ValueType&& value, Args&&... args) { + map.insert({std::string(std::forward(key)), std::forward(value)}); + fillMap(map, std::forward(args)...); +} + +template +std::multimap multiMapOf(KeyType&& key, ValueType&& value, Args&&... args) { + std::multimap result; + fillMap(result, std::forward(key), std::forward(value), std::forward(args)...); + return result; +} + #ifndef HAVE_TO_STRING namespace std {