mirror of
https://github.com/asdlokj1qpi233/subconverter.git
synced 2026-11-05 03:02:06 +00:00
Enhancements
Fix bad conversion of DOMAIN-SUFFIX rules when generating Surge DOMAIN-SET. (#667) Optimize codes.
This commit is contained in:
6 files changed
+57
-12
No files matched your search
@@ -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);
|
if (args.size() < 2) return rapidjson::Value(rapidjson::kObjectType);
|
||||||
auto type = toLower(std::string(args[0]));
|
auto type = toLower(std::string(args[0]));
|
||||||
auto value = toLower(args[1]);
|
auto value = toLower(std::string(args[1]));
|
||||||
// std::string_view option;
|
// std::string_view option;
|
||||||
// if (args.size() >= 3) option = args[2];
|
// if (args.size() >= 3) option = args[2];
|
||||||
|
|
||||||
@@ -483,10 +484,11 @@ static rapidjson::Value transformRuleToSingBox(const std::string& rule, const st
|
|||||||
return rule_obj;
|
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;
|
using namespace rapidjson_ext;
|
||||||
auto args = split(rule, ',');
|
args.clear();
|
||||||
|
split(args, rule, ',');
|
||||||
if (args.size() < 2) return;
|
if (args.size() < 2) return;
|
||||||
auto type = args[0];
|
auto type = args[0];
|
||||||
// std::string_view option;
|
// 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");
|
auto dns_object = buildObject(allocator, "protocol", "dns", "outbound", "dns-out");
|
||||||
rules.PushBack(dns_object, allocator);
|
rules.PushBack(dns_object, allocator);
|
||||||
|
|
||||||
|
std::vector<std::string_view> temp(4);
|
||||||
for(RulesetContent &x : ruleset_content_array)
|
for(RulesetContent &x : ruleset_content_array)
|
||||||
{
|
{
|
||||||
if(global.maxAllowedRules && total_rules > global.maxAllowedRules)
|
if(global.maxAllowedRules && total_rules > global.maxAllowedRules)
|
||||||
@@ -548,7 +551,7 @@ void rulesetToSingBox(rapidjson::Document &base_rule, std::vector<RulesetContent
|
|||||||
final = rule_group;
|
final = rule_group;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
rules.PushBack(transformRuleToSingBox(strLine, rule_group, allocator), allocator);
|
rules.PushBack(transformRuleToSingBox(temp, strLine, rule_group, allocator), allocator);
|
||||||
total_rules++;
|
total_rules++;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -574,7 +577,7 @@ void rulesetToSingBox(rapidjson::Document &base_rule, std::vector<RulesetContent
|
|||||||
strLine.erase(strLine.find("//"));
|
strLine.erase(strLine.find("//"));
|
||||||
strLine = trimWhitespace(strLine);
|
strLine = trimWhitespace(strLine);
|
||||||
}
|
}
|
||||||
appendSingBoxRule(rule, strLine, allocator);
|
appendSingBoxRule(temp, rule, strLine, allocator);
|
||||||
}
|
}
|
||||||
if (rule.ObjectEmpty()) continue;
|
if (rule.ObjectEmpty()) continue;
|
||||||
rule.AddMember("outbound", rapidjson::Value(rule_group.c_str(), allocator), allocator);
|
rule.AddMember("outbound", rapidjson::Value(rule_group.c_str(), allocator), allocator);
|
||||||
|
|||||||
@@ -244,6 +244,8 @@ std::string getRuleset(RESPONSE_CALLBACK_ARGS)
|
|||||||
continue;
|
continue;
|
||||||
if(filterLine())
|
if(filterLine())
|
||||||
continue;
|
continue;
|
||||||
|
if(strLine[posb - 2] == 'X')
|
||||||
|
output_content += '.';
|
||||||
output_content += strLine.substr(posb, pose);
|
output_content += strLine.substr(posb, pose);
|
||||||
output_content += '\n';
|
output_content += '\n';
|
||||||
continue;
|
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);}))
|
if(!std::any_of(ClashRuleTypes.begin(), ClashRuleTypes.end(), [&strLine](const std::string& type){return startsWith(strLine, type);}))
|
||||||
continue;
|
continue;
|
||||||
output_content += " - ";
|
output_content += " - ";
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
lineSize = strLine.size();
|
lineSize = strLine.size();
|
||||||
|
|||||||
@@ -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-Origin", "*");
|
||||||
res.set_header("Access-Control-Allow-Headers", "Content-Type,Authorization");
|
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)
|
server.set_pre_routing_handler([&](const httplib::Request &req, httplib::Response &res)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -385,8 +385,6 @@ int WebServer::start_web_server_multi(listener_args *args)
|
|||||||
std::string listen_address = args->listen_address;
|
std::string listen_address = args->listen_address;
|
||||||
int port = args->port, nthreads = args->max_workers, max_conn = args->max_conn;
|
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);
|
int nfd = httpserver_bindsocket(listen_address, port, max_conn);
|
||||||
if (nfd < 0)
|
if (nfd < 0)
|
||||||
return -1;
|
return -1;
|
||||||
@@ -405,7 +403,7 @@ int WebServer::start_web_server_multi(listener_args *args)
|
|||||||
return -1;
|
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_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);
|
evhttp_set_timeout(httpd, 30);
|
||||||
if (pthread_create(&ths[i], nullptr, httpserver_dispatch, base[i]) != 0)
|
if (pthread_create(&ths[i], nullptr, httpserver_dispatch, base[i]) != 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|||||||
@@ -51,9 +51,8 @@ std::vector<std::string> split(const std::string &s, const std::string &separato
|
|||||||
return result;
|
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;
|
string_size i = 0;
|
||||||
|
|
||||||
while (i != s.size())
|
while (i != s.size())
|
||||||
@@ -87,6 +86,12 @@ std::vector<std::string_view> split(std::string_view s, char separator)
|
|||||||
i = j;
|
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;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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> split(const std::string &s, const std::string &separator);
|
||||||
std::vector<std::string_view> split(std::string_view s, char 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);
|
std::string join(const string_array &arr, const std::string &delimiter);
|
||||||
|
|
||||||
template <typename InputIt>
|
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);
|
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
|
#ifndef HAVE_TO_STRING
|
||||||
namespace std
|
namespace std
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in new issue
Block a user