Add support for exporting node list in sing-box format

This commit is contained in:
Tindy X
2023-11-20 20:48:29 +08:00
parent 7968d54476
commit c3524d04d7

View File

@@ -2110,12 +2110,15 @@ void proxyToSingBox(std::vector<Proxy> &nodes, rapidjson::Document &json, std::v
std::vector<Proxy> nodelist;
string_array remarks_list;
auto direct = buildObject(allocator, "type", "direct", "tag", "DIRECT");
outbounds.PushBack(direct, allocator);
auto reject = buildObject(allocator, "type", "block", "tag", "REJECT");
outbounds.PushBack(reject, allocator);
auto dns = buildObject(allocator, "type", "dns", "tag", "dns-out");
outbounds.PushBack(dns, allocator);
if (!ext.nodelist)
{
auto direct = buildObject(allocator, "type", "direct", "tag", "DIRECT");
outbounds.PushBack(direct, allocator);
auto reject = buildObject(allocator, "type", "block", "tag", "REJECT");
outbounds.PushBack(reject, allocator);
auto dns = buildObject(allocator, "type", "dns", "tag", "dns-out");
outbounds.PushBack(dns, allocator);
}
for (Proxy &x : nodes)
{
@@ -2256,6 +2259,13 @@ void proxyToSingBox(std::vector<Proxy> &nodes, rapidjson::Document &json, std::v
remarks_list.emplace_back(x.Remark);
outbounds.PushBack(proxy, allocator);
}
if (ext.nodelist)
{
json | AddMemberOrReplace("outbounds", outbounds, allocator);
return;
}
for (const ProxyGroupConfig &x: extra_proxy_group)
{
string_array filtered_nodelist;
@@ -2326,14 +2336,27 @@ std::string proxyToSingBox(std::vector<Proxy> &nodes, const std::string &base_co
{
using namespace rapidjson_ext;
rapidjson::Document json;
json.Parse(base_conf.data());
if(json.HasParseError())
if (!ext.nodelist)
{
writeLog(0, "sing-box base loader failed with error: " + std::string(rapidjson::GetParseError_En(json.GetParseError())), LOG_LEVEL_ERROR);
return "";
json.Parse(base_conf.data());
if (json.HasParseError())
{
writeLog(0, "sing-box base loader failed with error: " +
std::string(rapidjson::GetParseError_En(json.GetParseError())), LOG_LEVEL_ERROR);
return "";
}
}
else
{
json.SetObject();
}
proxyToSingBox(nodes, json, ruleset_content_array, extra_proxy_group, ext);
if(ext.nodelist || !ext.enable_rule_generator)
return json | SerializeObject();
rulesetToSingBox(json, ruleset_content_array, ext.overwrite_original_rules);
return json | SerializeObject();