Skip to content

Feat(hysteria2): support hysteria2 port hopping for clash/singbox results #817

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/generator/config/subexport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,8 @@ void proxyToClash(std::vector<Proxy> &nodes, YAML::Node &yamlnode, const ProxyGr
break;
case ProxyType::Hysteria2:
singleproxy["type"] = "hysteria2";
if (!x.Ports.empty())
singleproxy["ports"] = x.Ports;
if (!x.Up.empty())
singleproxy["up"] = x.UpSpeed;
if (!x.Down.empty())
Expand All @@ -552,6 +554,8 @@ void proxyToClash(std::vector<Proxy> &nodes, YAML::Node &yamlnode, const ProxyGr
singleproxy["ca-str"] = x.CaStr;
if (x.CWND)
singleproxy["cwnd"] = x.CWND;
if (x.HopInterval)
singleproxy["hop-interval"] = x.HopInterval;
break;
default:
continue;
Expand Down Expand Up @@ -2395,6 +2399,8 @@ void proxyToSingBox(std::vector<Proxy> &nodes, rapidjson::Document &json, std::v
case ProxyType::Hysteria2:
{
addSingBoxCommonMembers(proxy, x, "hysteria2", allocator);
if (!x.Ports.empty())
proxy.AddMember("server_ports", stringArrayToJsonArray(x.Ports, ",", allocator), allocator);
if (!x.Up.empty())
proxy.AddMember("up_mbps", x.UpSpeed, allocator);
if (!x.Down.empty())
Expand All @@ -2409,7 +2415,8 @@ void proxyToSingBox(std::vector<Proxy> &nodes, rapidjson::Document &json, std::v
}
if (!x.Password.empty())
proxy.AddMember("password", rapidjson::StringRef(x.Password.c_str()), allocator);

if (x.HopInterval)
proxy.AddMember("hop_interval", rapidjson::Value(formatSingBoxInterval(x.HopInterval).c_str(), allocator), allocator);
rapidjson::Value tls(rapidjson::kObjectType);
tls.AddMember("enabled", true, allocator);
if (!scv.is_undef())
Expand Down
37 changes: 31 additions & 6 deletions src/parser/subparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ void hysteriaConstruct(
const std::string &alpn,
tribool tfo,
tribool scv,
const std::string &underlying_proxy = ""
const std::string &underlying_proxy
) {
commonConstruct(node, ProxyType::Hysteria, group, remarks, server, port, tribool(), tfo, scv, tribool(), underlying_proxy);
node.Ports = ports;
Expand Down Expand Up @@ -208,10 +208,33 @@ void hysteriaConstruct(
}
}

void hysteria2Construct(Proxy &node, const std::string &group, const std::string &remarks, const std::string &server, const std::string &port,const std::string &up, const std::string &down, const std::string &password, const std::string &obfs, const std::string &obfs_password, const std::string &sni, const std::string &fingerprint, const std::string &alpn, const std::string &ca, const std::string &ca_str, const std::string &cwnd, tribool tfo, tribool scv, const std::string &underlying_proxy) {
void hysteria2Construct(
Proxy &node,
const std::string &group,
const std::string &remarks,
const std::string &server,
const std::string &port,
const std::string &ports,
const std::string &up,
const std::string &down,
const std::string &password,
const std::string &obfs,
const std::string &obfs_password,
const std::string &sni,
const std::string &fingerprint,
const std::string &alpn,
const std::string &ca,
const std::string &caStr,
const std::string &cwnd,
const std::string &hop_interval,
tribool tfo,
tribool scv,
const std::string &underlying_proxy
) {
commonConstruct(node, ProxyType::Hysteria2, group, remarks, server, port, tribool(), tfo, scv, tribool(), underlying_proxy);
node.UpSpeed = to_int(up);
node.DownSpeed = to_int(down);
node.Ports = ports;
node.Password = password;
node.OBFS = obfs;
node.OBFSParam = obfs_password;
Expand All @@ -222,9 +245,9 @@ void hysteria2Construct(Proxy &node, const std::string &group, const std::string
node.Alpn = StringArray {alpn};
}
node.Ca = ca;
node.CaStr = ca_str;
node.CaStr = caStr;
node.CWND = to_int(cwnd);

node.HopInterval = to_int(hop_interval);
}

void explodeVmess(std::string vmess, Proxy &node)
Expand Down Expand Up @@ -1324,6 +1347,7 @@ void explodeClash(Node yamlnode, std::vector<Proxy> &nodes)
break;
case "hysteria2"_hash:
group = HYSTERIA2_DEFAULT_GROUP;
singleproxy["ports"] >>= ports;
singleproxy["up"] >>= up;
singleproxy["down"] >>= down;
singleproxy["password"] >>= password;
Expand All @@ -1340,8 +1364,9 @@ void explodeClash(Node yamlnode, std::vector<Proxy> &nodes)
singleproxy["ca"] >>= ca;
singleproxy["ca-str"] >>= ca_str;
singleproxy["cwnd"] >>= cwnd;
singleproxy["hop-interval"] >>= hop_interval;

hysteria2Construct(node, group, ps, server, port, up, down, password, obfs, obfs_password, sni, fingerprint, alpn, ca, ca_str, cwnd, tfo, scv, underlying_proxy);
hysteria2Construct(node, group, ps, server, port, ports, up, down, password, obfs, obfs_password, sni, fingerprint, ca, ca_str, cwnd, alpn, hop_interval, tfo, scv, underlying_proxy);
break;

default:
Expand Down Expand Up @@ -1526,7 +1551,7 @@ void explodeStdHysteria2(std::string hysteria2, Proxy &node) {
if (remarks.empty())
remarks = add + ":" + port;

hysteria2Construct(node, HYSTERIA2_DEFAULT_GROUP, remarks, add, port, up, down, password, obfs, obfs_password, sni, fingerprint, "", "", "", "", tribool(), scv, "");
hysteria2Construct(node, HYSTERIA2_DEFAULT_GROUP, remarks, add, port, port, up, down, password, obfs, obfs_password, sni, fingerprint, "", "", "", "", "", tribool(), scv, "");
return;
}

Expand Down
26 changes: 24 additions & 2 deletions src/parser/subparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,35 @@ void hysteriaConstruct(
const std::string &recv_window,
const std::string &disable_mtu_discovery,
const std::string &hop_interval,
const string_array &alpn,
const std::string &alpn,
tribool tfo,
tribool scv,
const std::string &underlying_proxy = ""
);

void hysteria2Construct(Proxy &node, const std::string &group, const std::string &remarks, const std::string &server, const std::string &port,const std::string &up, const std::string &down, const std::string &password, const std::string &obfs, const std::string &obfs_password, const std::string &sni, const std::string &fingerprint, const string_array &alpn, const std::string &ca, const std::string &caStr, const std::string &cwnd, tribool tfo, tribool scv, const std::string &underlying_proxy = "");
void hysteria2Construct(
Proxy &node,
const std::string &group,
const std::string &remarks,
const std::string &server,
const std::string &port,
const std::string &ports,
const std::string &up,
const std::string &down,
const std::string &password,
const std::string &obfs,
const std::string &obfs_password,
const std::string &sni,
const std::string &fingerprint,
const std::string &alpn,
const std::string &ca,
const std::string &caStr,
const std::string &cwnd,
const std::string &hop_interval,
tribool tfo,
tribool scv,
const std::string &underlying_proxy = ""
);

void explodeVmess(std::string vmess, Proxy &node);
void explodeSSR(std::string ssr, Proxy &node);
Expand Down