Skip to content

Commit 8a6e7f9

Browse files
authored
Correctly format Hysteria2 server_ports for sing-box (#876)
1 parent 6d312fe commit 8a6e7f9

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/generator/config/subexport.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <numeric>
44
#include <cmath>
55
#include <climits>
6+
#include <cctype>
67

78
#include "config/regmatch.h"
89
#include "generator/config/subexport.h"
@@ -2258,6 +2259,25 @@ static rapidjson::Value stringArrayToJsonArray(const std::string &array, const s
22582259
return result;
22592260
}
22602261

2262+
static rapidjson::Value buildSingBoxHysteria2ServerPorts(const std::string &ports, rapidjson::MemoryPoolAllocator<> &allocator)
2263+
{
2264+
rapidjson::Value result(rapidjson::kArrayType);
2265+
string_array port_list = split(ports, ",");
2266+
for (const auto &raw_port : port_list)
2267+
{
2268+
std::string port_entry = trim(raw_port);
2269+
if (port_entry.empty())
2270+
continue;
2271+
2272+
const bool is_single_port = std::all_of(port_entry.begin(), port_entry.end(), [](unsigned char ch) { return std::isdigit(ch); });
2273+
if (is_single_port)
2274+
port_entry = port_entry + ":" + port_entry;
2275+
2276+
result.PushBack(rapidjson::Value(port_entry.c_str(), allocator), allocator);
2277+
}
2278+
return result;
2279+
}
2280+
22612281
void proxyToSingBox(std::vector<Proxy> &nodes, rapidjson::Document &json, std::vector<RulesetContent> &ruleset_content_array, const ProxyGroupConfigs &extra_proxy_group, extra_settings &ext) {
22622282
using namespace rapidjson_ext;
22632283
rapidjson::Document::AllocatorType &allocator = json.GetAllocator();
@@ -2424,7 +2444,7 @@ void proxyToSingBox(std::vector<Proxy> &nodes, rapidjson::Document &json, std::v
24242444
{
24252445
addSingBoxCommonMembers(proxy, x, "hysteria2", allocator);
24262446
if (!x.Ports.empty())
2427-
proxy.AddMember("server_ports", stringArrayToJsonArray(x.Ports, ",", allocator), allocator);
2447+
proxy.AddMember("server_ports", buildSingBoxHysteria2ServerPorts(x.Ports, allocator), allocator);
24282448
if (!x.Up.empty())
24292449
proxy.AddMember("up_mbps", x.UpSpeed, allocator);
24302450
if (!x.Down.empty())

0 commit comments

Comments
 (0)