mirror of
https://github.com/MatsuriDayo/nekoray.git
synced 2025-12-17 20:44:38 +03:00
feat: add TUIC protocol (#781)
--------- Co-authored-by: 2022-blake3-chacha8-poly1305 <139959885+xchacha20-ietf-poly1305@users.noreply.github.com> Co-authored-by: arm64v8a <48624112+arm64v8a@users.noreply.github.com>
This commit is contained in:
@@ -35,29 +35,41 @@ namespace NekoGui_fmt {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int HysteriaBean::NeedExternal(bool isFirstProfile) {
|
||||
int QUICBean::NeedExternal(bool isFirstProfile) {
|
||||
auto hysteriaCore = [=] {
|
||||
if (isFirstProfile) {
|
||||
if (NekoGui::dataStore->spmode_vpn && protocol != hysteria_protocol_facktcp && hopPort.trimmed().isEmpty()) {
|
||||
if (NekoGui::dataStore->spmode_vpn && hyProtocol != hysteria_protocol_facktcp && hopPort.trimmed().isEmpty()) {
|
||||
return 1;
|
||||
}
|
||||
return 2;
|
||||
} else {
|
||||
if (protocol == hysteria_protocol_facktcp || !hopPort.trimmed().isEmpty()) {
|
||||
if (hyProtocol == hysteria_protocol_facktcp || !hopPort.trimmed().isEmpty()) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
};
|
||||
|
||||
auto tuicCore = [=] {
|
||||
if (isFirstProfile) {
|
||||
if (NekoGui::dataStore->spmode_vpn) {
|
||||
return 1;
|
||||
}
|
||||
return 2;
|
||||
}
|
||||
return 1;
|
||||
};
|
||||
|
||||
if (IS_NEKO_BOX) {
|
||||
if (protocol == hysteria_protocol_udp) {
|
||||
if (proxy_type == proxy_TUIC || hyProtocol == hysteria_protocol_udp) {
|
||||
// sing-box support
|
||||
return 0;
|
||||
} else {
|
||||
// hysteria core support
|
||||
return hysteriaCore();
|
||||
}
|
||||
} else if (proxy_type == proxy_TUIC) {
|
||||
return tuicCore();
|
||||
} else {
|
||||
return hysteriaCore();
|
||||
}
|
||||
@@ -104,65 +116,112 @@ namespace NekoGui_fmt {
|
||||
return result;
|
||||
}
|
||||
|
||||
ExternalBuildResult HysteriaBean::BuildExternal(int mapping_port, int socks_port, int external_stat) {
|
||||
ExternalBuildResult result{NekoGui::dataStore->extraCore->Get("hysteria")};
|
||||
ExternalBuildResult QUICBean::BuildExternal(int mapping_port, int socks_port, int external_stat) {
|
||||
if (proxy_type == proxy_TUIC) {
|
||||
ExternalBuildResult result{NekoGui::dataStore->extraCore->Get("tuic")};
|
||||
|
||||
QJsonObject config;
|
||||
QJsonObject relay;
|
||||
|
||||
// determine server format
|
||||
auto is_direct = external_stat == 2;
|
||||
auto sniGen = sni;
|
||||
if (sni.isEmpty() && !IsIpAddress(serverAddress)) sniGen = serverAddress;
|
||||
relay["uuid"] = uuid;
|
||||
relay["password"] = password;
|
||||
relay["udp_relay_mode"] = udpRelayMode;
|
||||
relay["congestion_control"] = congestionControl;
|
||||
relay["zero_rtt_handshake"] = zeroRttHandshake;
|
||||
relay["disable_sni"] = disableSni;
|
||||
if (!heartbeat.trimmed().isEmpty()) relay["heartbeat"] = heartbeat;
|
||||
if (!alpn.trimmed().isEmpty()) relay["alpn"] = QList2QJsonArray(alpn.split(","));
|
||||
|
||||
auto server = serverAddress;
|
||||
if (!hopPort.trimmed().isEmpty()) {
|
||||
server = WrapIPV6Host(server) + ":" + hopPort;
|
||||
} else {
|
||||
server = WrapIPV6Host(server) + ":" + Int2String(serverPort);
|
||||
if (!caText.trimmed().isEmpty()) {
|
||||
WriteTempFile("tuic_" + GetRandomString(10) + ".crt", caText.toUtf8());
|
||||
QJsonArray certificate;
|
||||
certificate.append(TempFile);
|
||||
relay["certificates"] = certificate;
|
||||
}
|
||||
|
||||
// The most confused part of TUIC......
|
||||
if (serverAddress == sni) {
|
||||
relay["server"] = serverAddress + ":" + Int2String(serverPort);
|
||||
} else {
|
||||
relay["server"] = sni + ":" + Int2String(serverPort);
|
||||
relay["ip"] = serverAddress;
|
||||
}
|
||||
|
||||
QJsonObject local{
|
||||
{"server", "127.0.0.1:" + Int2String(socks_port)},
|
||||
};
|
||||
|
||||
QJsonObject config{
|
||||
{"relay", relay},
|
||||
{"local", local},
|
||||
};
|
||||
|
||||
//
|
||||
|
||||
result.config_export = QJsonObject2QString(config, false);
|
||||
WriteTempFile("tuic_" + GetRandomString(10) + ".json", result.config_export.toUtf8());
|
||||
result.arguments = QStringList{"-c", TempFile};
|
||||
|
||||
return result;
|
||||
} else { // Hysteria
|
||||
ExternalBuildResult result{NekoGui::dataStore->extraCore->Get("hysteria")};
|
||||
|
||||
QJsonObject config;
|
||||
|
||||
// determine server format
|
||||
auto is_direct = external_stat == 2;
|
||||
auto sniGen = sni;
|
||||
if (sni.isEmpty() && !IsIpAddress(serverAddress)) sniGen = serverAddress;
|
||||
|
||||
auto server = serverAddress;
|
||||
if (!hopPort.trimmed().isEmpty()) {
|
||||
server = WrapIPV6Host(server) + ":" + hopPort;
|
||||
} else {
|
||||
server = WrapIPV6Host(server) + ":" + Int2String(serverPort);
|
||||
}
|
||||
config["server"] = is_direct ? server : "127.0.0.1:" + Int2String(mapping_port);
|
||||
|
||||
// listen
|
||||
config["socks5"] = QJsonObject{
|
||||
{"listen", "127.0.0.1:" + Int2String(socks_port)},
|
||||
};
|
||||
|
||||
// misc
|
||||
|
||||
config["retry"] = 5;
|
||||
config["fast_open"] = true;
|
||||
config["lazy_start"] = true;
|
||||
config["obfs"] = obfsPassword;
|
||||
config["up_mbps"] = uploadMbps;
|
||||
config["down_mbps"] = downloadMbps;
|
||||
|
||||
if (authPayloadType == hysteria_auth_base64) config["auth"] = authPayload;
|
||||
if (authPayloadType == hysteria_auth_string) config["auth_str"] = authPayload;
|
||||
|
||||
if (hyProtocol == hysteria_protocol_facktcp) config["protocol"] = "faketcp";
|
||||
if (hyProtocol == hysteria_protocol_wechat_video) config["protocol"] = "wechat-video";
|
||||
|
||||
if (!sniGen.isEmpty()) config["server_name"] = sniGen;
|
||||
if (!alpn.isEmpty()) config["alpn"] = alpn;
|
||||
|
||||
if (!caText.trimmed().isEmpty()) {
|
||||
WriteTempFile("hysteria_" + GetRandomString(10) + ".crt", caText.toUtf8());
|
||||
config["ca"] = TempFile;
|
||||
}
|
||||
|
||||
if (allowInsecure) config["insecure"] = true;
|
||||
if (streamReceiveWindow > 0) config["recv_window_conn"] = streamReceiveWindow;
|
||||
if (connectionReceiveWindow > 0) config["recv_window"] = connectionReceiveWindow;
|
||||
if (disableMtuDiscovery) config["disable_mtu_discovery"] = true;
|
||||
config["hop_interval"] = hopInterval;
|
||||
|
||||
//
|
||||
|
||||
result.config_export = QJsonObject2QString(config, false);
|
||||
WriteTempFile("hysteria_" + GetRandomString(10) + ".json", result.config_export.toUtf8());
|
||||
result.arguments = QStringList{"--no-check", "-c", TempFile};
|
||||
|
||||
return result;
|
||||
}
|
||||
config["server"] = is_direct ? server : "127.0.0.1:" + Int2String(mapping_port);
|
||||
|
||||
// listen
|
||||
config["socks5"] = QJsonObject{
|
||||
{"listen", "127.0.0.1:" + Int2String(socks_port)},
|
||||
};
|
||||
|
||||
// misc
|
||||
|
||||
config["retry"] = 5;
|
||||
config["fast_open"] = true;
|
||||
config["lazy_start"] = true;
|
||||
config["obfs"] = obfsPassword;
|
||||
config["up_mbps"] = uploadMbps;
|
||||
config["down_mbps"] = downloadMbps;
|
||||
|
||||
if (authPayloadType == hysteria_auth_base64) config["auth"] = authPayload;
|
||||
if (authPayloadType == hysteria_auth_string) config["auth_str"] = authPayload;
|
||||
|
||||
if (protocol == hysteria_protocol_facktcp) config["protocol"] = "faketcp";
|
||||
if (protocol == hysteria_protocol_wechat_video) config["protocol"] = "wechat-video";
|
||||
|
||||
if (!sniGen.isEmpty()) config["server_name"] = sniGen;
|
||||
if (!alpn.isEmpty()) config["alpn"] = alpn;
|
||||
|
||||
if (!caText.trimmed().isEmpty()) {
|
||||
WriteTempFile("hysteria_" + GetRandomString(10) + ".crt", caText.toUtf8());
|
||||
config["ca"] = TempFile;
|
||||
}
|
||||
|
||||
if (allowInsecure) config["insecure"] = true;
|
||||
if (streamReceiveWindow > 0) config["recv_window_conn"] = streamReceiveWindow;
|
||||
if (connectionReceiveWindow > 0) config["recv_window"] = connectionReceiveWindow;
|
||||
if (disableMtuDiscovery) config["disable_mtu_discovery"] = true;
|
||||
config["hop_interval"] = hopInterval;
|
||||
|
||||
//
|
||||
|
||||
result.config_export = QJsonObject2QString(config, false);
|
||||
WriteTempFile("hysteria_" + GetRandomString(10) + ".json", result.config_export.toUtf8());
|
||||
result.arguments = QStringList{"--no-check", "-c", TempFile};
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
ExternalBuildResult CustomBean::BuildExternal(int mapping_port, int socks_port, int external_stat) {
|
||||
@@ -206,4 +265,5 @@ namespace NekoGui_fmt {
|
||||
|
||||
return result;
|
||||
}
|
||||
} // namespace NekoGui_fmt
|
||||
|
||||
} // namespace NekoGui_fmt
|
||||
|
||||
Reference in New Issue
Block a user