Skip to content

Commit

Permalink
feat: add TUIC protocol (#781)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: 2022-blake3-chacha8-poly1305 <[email protected]>
Co-authored-by: arm64v8a <[email protected]>
  • Loading branch information
3 people authored Aug 10, 2023
1 parent 0325767 commit d8bf56a
Show file tree
Hide file tree
Showing 25 changed files with 1,075 additions and 598 deletions.
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,9 @@ set(PROJECT_SOURCES
ui/edit/edit_naive.cpp
ui/edit/edit_naive.ui

ui/edit/edit_hysteria.h
ui/edit/edit_hysteria.cpp
ui/edit/edit_hysteria.ui
ui/edit/edit_quic.h
ui/edit/edit_quic.cpp
ui/edit/edit_quic.ui

ui/edit/edit_custom.h
ui/edit/edit_custom.cpp
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ https://matsuridayo.github.io
* VMess
* VLESS
* Trojan
* TUIC ( sing-box )
* NaïveProxy ( Custom Core )
* Hysteria ( Custom Core or sing-box )
* Custom Outbound
Expand Down
4 changes: 3 additions & 1 deletion db/Database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,9 @@ namespace NekoGui {
} else if (type == "naive") {
bean = new NekoGui_fmt::NaiveBean();
} else if (type == "hysteria") {
bean = new NekoGui_fmt::HysteriaBean();
bean = new NekoGui_fmt::QUICBean(NekoGui_fmt::QUICBean::proxy_Hysteria);
} else if (type == "tuic") {
bean = new NekoGui_fmt::QUICBean(NekoGui_fmt::QUICBean::proxy_TUIC);
} else if (type == "custom") {
bean = new NekoGui_fmt::CustomBean();
} else {
Expand Down
6 changes: 3 additions & 3 deletions db/ProxyEntity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace NekoGui_fmt {

class NaiveBean;

class HysteriaBean;
class QUICBean;

class CustomBean;

Expand Down Expand Up @@ -65,8 +65,8 @@ namespace NekoGui {
return (NekoGui_fmt::NaiveBean *) bean.get();
};

[[nodiscard]] NekoGui_fmt::HysteriaBean *HysteriaBean() const {
return (NekoGui_fmt::HysteriaBean *) bean.get();
[[nodiscard]] NekoGui_fmt::QUICBean *QUICBean() const {
return (NekoGui_fmt::QUICBean *) bean.get();
};

[[nodiscard]] NekoGui_fmt::CustomBean *CustomBean() const {
Expand Down
41 changes: 26 additions & 15 deletions fmt/Bean2CoreObj_box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,36 +169,47 @@ namespace NekoGui_fmt {
return result;
}

CoreObjOutboundBuildResult HysteriaBean::BuildCoreObjSingBox() {
CoreObjOutboundBuildResult QUICBean::BuildCoreObjSingBox() {
CoreObjOutboundBuildResult result;

QJsonObject coreTlsObj{
{"enabled", true},
{"disable_sni", disableSni},
{"insecure", allowInsecure},
{"certificate", caText.trimmed()},
{"server_name", sni},
};
if (!alpn.trimmed().isEmpty()) coreTlsObj["alpn"] = QJsonArray{alpn};
if (!alpn.trimmed().isEmpty()) coreTlsObj["alpn"] = QList2QJsonArray(alpn.split(","));

QJsonObject coreHysteriaObj{
{"type", "hysteria"},
QJsonObject outbound{
{"server", serverAddress},
{"server_port", serverPort},
{"obfs", obfsPassword},
{"disable_mtu_discovery", disableMtuDiscovery},
{"recv_window", streamReceiveWindow},
{"recv_window_conn", connectionReceiveWindow},
{"up_mbps", uploadMbps},
{"down_mbps", downloadMbps},
{"tls", coreTlsObj},
};

if (!hopPort.trimmed().isEmpty()) coreHysteriaObj["hop_ports"] = hopPort;

if (authPayloadType == hysteria_auth_base64) coreHysteriaObj["auth"] = authPayload;
if (authPayloadType == hysteria_auth_string) coreHysteriaObj["auth_str"] = authPayload;
if (proxy_type == proxy_Hysteria) {
outbound["type"] = "hysteria";
outbound["obfs"] = obfsPassword;
outbound["disable_mtu_discovery"] = disableMtuDiscovery;
outbound["recv_window"] = streamReceiveWindow;
outbound["recv_window_conn"] = connectionReceiveWindow;
outbound["up_mbps"] = uploadMbps;
outbound["down_mbps"] = downloadMbps;

if (!hopPort.trimmed().isEmpty()) outbound["hop_ports"] = hopPort;
if (authPayloadType == hysteria_auth_base64) outbound["auth"] = authPayload;
if (authPayloadType == hysteria_auth_string) outbound["auth_str"] = authPayload;
} else if (proxy_type == proxy_TUIC) {
outbound["type"] = "tuic";
outbound["uuid"] = uuid;
outbound["password"] = password;
outbound["congestion_control"] = congestionControl;
outbound["udp_relay_mode"] = udpRelayMode;
outbound["zero_rtt_handshake"] = zeroRttHandshake;
if (!heartbeat.trimmed().isEmpty()) outbound["heartbeat"] = heartbeat;
}

result.outbound = coreHysteriaObj;
result.outbound = outbound;
return result;
}

Expand Down
160 changes: 110 additions & 50 deletions fmt/Bean2External.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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 relay;

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(","));

if (!caText.trimmed().isEmpty()) {
WriteTempFile("tuic_" + GetRandomString(10) + ".crt", caText.toUtf8());
QJsonArray certificate;
certificate.append(TempFile);
relay["certificates"] = certificate;
}

QJsonObject config;
// The most confused part of TUIC......
if (serverAddress == sni) {
relay["server"] = serverAddress + ":" + Int2String(serverPort);
} else {
relay["server"] = sni + ":" + Int2String(serverPort);
relay["ip"] = serverAddress;
}

// determine server format
auto is_direct = external_stat == 2;
auto sniGen = sni;
if (sni.isEmpty() && !IsIpAddress(serverAddress)) sniGen = serverAddress;
QJsonObject local{
{"server", "127.0.0.1:" + Int2String(socks_port)},
};

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);
QJsonObject config{
{"relay", relay},
{"local", local},
};

// listen
config["socks5"] = QJsonObject{
{"listen", "127.0.0.1:" + Int2String(socks_port)},
};
//

// misc
result.config_export = QJsonObject2QString(config, false);
WriteTempFile("tuic_" + GetRandomString(10) + ".json", result.config_export.toUtf8());
result.arguments = QStringList{"-c", TempFile};

config["retry"] = 5;
config["fast_open"] = true;
config["lazy_start"] = true;
config["obfs"] = obfsPassword;
config["up_mbps"] = uploadMbps;
config["down_mbps"] = downloadMbps;
return result;
} else { // Hysteria
ExternalBuildResult result{NekoGui::dataStore->extraCore->Get("hysteria")};

if (authPayloadType == hysteria_auth_base64) config["auth"] = authPayload;
if (authPayloadType == hysteria_auth_string) config["auth_str"] = authPayload;
QJsonObject config;

if (protocol == hysteria_protocol_facktcp) config["protocol"] = "faketcp";
if (protocol == hysteria_protocol_wechat_video) config["protocol"] = "wechat-video";
// determine server format
auto is_direct = external_stat == 2;
auto sniGen = sni;
if (sni.isEmpty() && !IsIpAddress(serverAddress)) sniGen = serverAddress;

if (!sniGen.isEmpty()) config["server_name"] = sniGen;
if (!alpn.isEmpty()) config["alpn"] = alpn;
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);

if (!caText.trimmed().isEmpty()) {
WriteTempFile("hysteria_" + GetRandomString(10) + ".crt", caText.toUtf8());
config["ca"] = TempFile;
}
// listen
config["socks5"] = QJsonObject{
{"listen", "127.0.0.1:" + Int2String(socks_port)},
};

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;
// misc

//
config["retry"] = 5;
config["fast_open"] = true;
config["lazy_start"] = true;
config["obfs"] = obfsPassword;
config["up_mbps"] = uploadMbps;
config["down_mbps"] = downloadMbps;

result.config_export = QJsonObject2QString(config, false);
WriteTempFile("hysteria_" + GetRandomString(10) + ".json", result.config_export.toUtf8());
result.arguments = QStringList{"--no-check", "-c", TempFile};
if (authPayloadType == hysteria_auth_base64) config["auth"] = authPayload;
if (authPayloadType == hysteria_auth_string) config["auth_str"] = authPayload;

return result;
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;
}
}

ExternalBuildResult CustomBean::BuildExternal(int mapping_port, int socks_port, int external_stat) {
Expand Down Expand Up @@ -206,4 +265,5 @@ namespace NekoGui_fmt {

return result;
}
} // namespace NekoGui_fmt

} // namespace NekoGui_fmt
47 changes: 26 additions & 21 deletions fmt/Bean2Link.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "QUICBean.hpp"
#include "db/ProxyEntity.hpp"
#include "fmt/includes.h"

Expand Down Expand Up @@ -127,29 +128,33 @@ namespace NekoGui_fmt {
return url.toString(QUrl::FullyEncoded);
}

QString HysteriaBean::ToShareLink() {
QString QUICBean::ToShareLink() {
QUrl url;
url.setScheme("hysteria");
url.setHost(serverAddress);
url.setPort(serverPort);
QUrlQuery q;
q.addQueryItem("upmbps", Int2String(uploadMbps));
q.addQueryItem("downmbps", Int2String(downloadMbps));
if (!obfsPassword.isEmpty()) {
q.addQueryItem("obfs", "xplus");
q.addQueryItem("obfsParam", obfsPassword);
if (proxy_type == proxy_Hysteria) {
url.setScheme("hysteria");
url.setHost(serverAddress);
url.setPort(serverPort);
QUrlQuery q;
q.addQueryItem("upmbps", Int2String(uploadMbps));
q.addQueryItem("downmbps", Int2String(downloadMbps));
if (!obfsPassword.isEmpty()) {
q.addQueryItem("obfs", "xplus");
q.addQueryItem("obfsParam", obfsPassword);
}
if (authPayloadType == hysteria_auth_string) q.addQueryItem("auth", authPayload);
if (hyProtocol == hysteria_protocol_facktcp) q.addQueryItem("protocol", "faketcp");
if (hyProtocol == hysteria_protocol_wechat_video) q.addQueryItem("protocol", "wechat-video");
if (!hopPort.trimmed().isEmpty()) q.addQueryItem("mport", hopPort);
if (allowInsecure) q.addQueryItem("insecure", "1");
if (!sni.isEmpty()) q.addQueryItem("peer", sni);
if (!alpn.isEmpty()) q.addQueryItem("alpn", alpn);
if (connectionReceiveWindow > 0) q.addQueryItem("recv_window", Int2String(connectionReceiveWindow));
if (streamReceiveWindow > 0) q.addQueryItem("recv_window_conn", Int2String(streamReceiveWindow));
if (!q.isEmpty()) url.setQuery(q);
if (!name.isEmpty()) url.setFragment(name);
} else if (proxy_type == proxy_TUIC) {
// TODO std link
}
if (authPayloadType == hysteria_auth_string) q.addQueryItem("auth", authPayload);
if (protocol == hysteria_protocol_facktcp) q.addQueryItem("protocol", "faketcp");
if (protocol == hysteria_protocol_wechat_video) q.addQueryItem("protocol", "wechat-video");
if (!hopPort.trimmed().isEmpty()) q.addQueryItem("mport", hopPort);
if (allowInsecure) q.addQueryItem("insecure", "1");
if (!sni.isEmpty()) q.addQueryItem("peer", sni);
if (!alpn.isEmpty()) q.addQueryItem("alpn", alpn);
if (connectionReceiveWindow > 0) q.addQueryItem("recv_window", Int2String(connectionReceiveWindow));
if (streamReceiveWindow > 0) q.addQueryItem("recv_window_conn", Int2String(streamReceiveWindow));
if (!q.isEmpty()) url.setQuery(q);
if (!name.isEmpty()) url.setFragment(name);
return url.toString(QUrl::FullyEncoded);
}

Expand Down
Loading

0 comments on commit d8bf56a

Please sign in to comment.