Skip to content

Commit

Permalink
Add support for exporting node list in sing-box format
Browse files Browse the repository at this point in the history
  • Loading branch information
tindy2013 committed Nov 20, 2023
1 parent 7968d54 commit c3524d0
Showing 1 changed file with 33 additions and 10 deletions.
43 changes: 33 additions & 10 deletions src/generator/config/subexport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit c3524d0

Please sign in to comment.