Enhancements

Fix bad conversion of DOMAIN-SUFFIX rules when generating Surge DOMAIN-SET. (#667)
Optimize codes.
This commit is contained in:
Tindy X committed 2023-11-27 00:09:25 +08:00
1 parent c3524d04d7
commit 05a542c4f1
6 files changed
+57 -12

No files matched your search

+10 -7
View File
@@ -458,12 +458,13 @@ void rulesetToSurge(INIReader &base_rule, std::vector<RulesetContent> &ruleset_c
}
}
static rapidjson::Value transformRuleToSingBox(const std::string& rule, const std::string &group, rapidjson::MemoryPoolAllocator<>& allocator)
static rapidjson::Value transformRuleToSingBox(std::vector<std::string_view> &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<std::string_view> &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<RulesetContent
auto dns_object = buildObject(allocator, "protocol", "dns", "outbound", "dns-out");
rules.PushBack(dns_object, allocator);
std::vector<std::string_view> 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::vector<RulesetContent
final = rule_group;
continue;
}
rules.PushBack(transformRuleToSingBox(strLine, rule_group, allocator), allocator);
rules.PushBack(transformRuleToSingBox(temp, strLine, rule_group, allocator), allocator);
total_rules++;
continue;
}
@@ -574,7 +577,7 @@ void rulesetToSingBox(rapidjson::Document &base_rule, std::vector<RulesetContent
strLine.erase(strLine.find("//"));
strLine = trimWhitespace(strLine);
}
appendSingBoxRule(rule, strLine, allocator);
appendSingBoxRule(temp, rule, strLine, allocator);
}
if (rule.ObjectEmpty()) continue;
rule.AddMember("outbound", rapidjson::Value(rule_group.c_str(), allocator), allocator);
+4
View File
@@ -244,6 +244,8 @@ std::string getRuleset(RESPONSE_CALLBACK_ARGS)
continue;
if(filterLine())
continue;
if(strLine[posb - 2] == 'X')
output_content += '.';
output_content += strLine.substr(posb, pose);
output_content += '\n';
continue;
@@ -251,6 +253,8 @@ std::string getRuleset(RESPONSE_CALLBACK_ARGS)
if(!std::any_of(ClashRuleTypes.begin(), ClashRuleTypes.end(), [&strLine](const std::string& type){return startsWith(strLine, type);}))
continue;
output_content += " - ";
default:
break;
}
lineSize = strLine.size();
+4
View File
@@ -116,6 +116,10 @@ int WebServer::start_web_server_multi(listener_args *args)
res.set_header("Access-Control-Allow-Origin", "*");
res.set_header("Access-Control-Allow-Headers", "Content-Type,Authorization");
}
else
{
res.status = 404;
}
});
server.set_pre_routing_handler([&](const httplib::Request &req, httplib::Response &res)
{
+1 -3
View File
@@ -385,8 +385,6 @@ int WebServer::start_web_server_multi(listener_args *args)
std::string listen_address = args->listen_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;
+7 -2
View File
@@ -51,9 +51,8 @@ std::vector<std::string> split(const std::string &s, const std::string &separato
return result;
}
std::vector<std::string_view> split(std::string_view s, char separator)
void split(std::vector<std::string_view> &result, std::string_view s, char separator)
{
std::vector<std::string_view> result;
string_size i = 0;
while (i != s.size())
@@ -87,6 +86,12 @@ std::vector<std::string_view> split(std::string_view s, char separator)
i = j;
}
}
}
std::vector<std::string_view> split(std::string_view s, char separator)
{
std::vector<std::string_view> result;
split(result, s, separator);
return result;
}
+31
View File
@@ -16,6 +16,7 @@ using string_pair_array = std::vector<std::pair<std::string, std::string>>;
std::vector<std::string> split(const std::string &s, const std::string &separator);
std::vector<std::string_view> split(std::string_view s, char separator);
void split(std::vector<std::string_view> &result, std::string_view s, char separator);
std::string join(const string_array &arr, const std::string &delimiter);
template <typename InputIt>
@@ -97,6 +98,36 @@ template <typename T, typename U> static inline T to_number(const U &value, T de
int to_int(const std::string &str, int def_value = 0);
template <typename Type>
concept StringConstructible = requires(Type a) {
{ std::string(a) } -> std::same_as<std::string>;
};
template <typename Container, typename Element>
concept Insertable = requires(Container a, Element b) {
{ a.insert(b) } -> std::same_as<typename Container::iterator>;
};
template<typename Container, typename KeyType, typename ValueType>
requires Insertable<Container, std::pair<std::string, ValueType>>
void fillMap(Container& map, KeyType&& key, ValueType&& value) {
map.insert({std::string(std::forward<KeyType>(key)), std::forward<ValueType>(value)});
}
template<typename Container, typename KeyType, typename ValueType, typename... Args>
requires Insertable<Container, std::pair<std::string, ValueType>>
void fillMap(Container& map, KeyType&& key, ValueType&& value, Args&&... args) {
map.insert({std::string(std::forward<KeyType>(key)), std::forward<ValueType>(value)});
fillMap(map, std::forward<Args>(args)...);
}
template<typename KeyType, typename ValueType, typename... Args>
std::multimap<std::string, ValueType> multiMapOf(KeyType&& key, ValueType&& value, Args&&... args) {
std::multimap<std::string, ValueType> result;
fillMap(result, std::forward<KeyType>(key), std::forward<ValueType>(value), std::forward<Args>(args)...);
return result;
}
#ifndef HAVE_TO_STRING
namespace std
{