Skip to content

Commit

Permalink
Enhancements
Browse files Browse the repository at this point in the history
Fix bad conversion of DOMAIN-SUFFIX rules when generating Surge DOMAIN-SET. (#667)
Optimize codes.
  • Loading branch information
tindy2013 committed Nov 26, 2023
1 parent c3524d0 commit 05a542c
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 12 deletions.
17 changes: 10 additions & 7 deletions src/generator/config/ruleconvert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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;
}
Expand All @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/handler/interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,13 +244,17 @@ 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;
case 6:
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();
Expand Down
4 changes: 4 additions & 0 deletions src/server/webserver_httplib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
4 changes: 1 addition & 3 deletions src/server/webserver_libevent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
9 changes: 7 additions & 2 deletions src/utils/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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;
}

Expand Down
31 changes: 31 additions & 0 deletions src/utils/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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
{
Expand Down

0 comments on commit 05a542c

Please sign in to comment.