Skip to content

Commit

Permalink
Support hysteria2
Browse files Browse the repository at this point in the history
  • Loading branch information
arm64v8a committed Sep 6, 2023
1 parent 9efde1a commit 29c6185
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 98 deletions.
2 changes: 1 addition & 1 deletion fmt/Bean2CoreObj_box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ namespace NekoGui_fmt {
if (authPayloadType == hysteria_auth_string) outbound["auth_str"] = authPayload;
} else if (proxy_type == proxy_Hysteria2) {
outbound["type"] = "hysteria2";
outbound["password"] = authPayload;
outbound["password"] = password;
outbound["up_mbps"] = uploadMbps;
outbound["down_mbps"] = downloadMbps;
if (!obfsPassword.isEmpty()) {
Expand Down
6 changes: 1 addition & 5 deletions fmt/Bean2External.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,7 @@ namespace NekoGui_fmt {
{"listen", "127.0.0.1:" + Int2String(socks_port)},
{"disableUDP", false},
};
if (username.isEmpty()) {
config["auth"] = authPayload;
} else {
config["auth"] = username + ":" + authPayload;
}
config["auth"] = password;

QJsonObject bandwidth;
if (uploadMbps > 0) bandwidth["up"] = Int2String(uploadMbps) + " mbps";
Expand Down
20 changes: 20 additions & 0 deletions fmt/Bean2Link.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,26 @@ namespace NekoGui_fmt {
if (disableSni) q.addQueryItem("disable_sni", "1");
if (!q.isEmpty()) url.setQuery(q);
if (!name.isEmpty()) url.setFragment(name);
} else if (proxy_type == proxy_Hysteria2) {
url.setScheme("hy2");
url.setHost(serverAddress);
url.setPort(serverPort);
if (password.contains(":")) {
url.setUserName(SubStrBefore(password, ":"));
url.setPassword(SubStrAfter(password, ":"));
} else {
url.setUserName(password);
}
QUrlQuery q;
if (!obfsPassword.isEmpty()) {
q.addQueryItem("obfs", "salamander");
q.addQueryItem("obfs-password", obfsPassword);
}
// if (!hopPort.trimmed().isEmpty()) q.addQueryItem("mport", hopPort);
if (allowInsecure) q.addQueryItem("insecure", "1");
if (!sni.isEmpty()) q.addQueryItem("sni", sni);
if (!q.isEmpty()) url.setQuery(q);
if (!name.isEmpty()) url.setFragment(name);
}
return url.toString(QUrl::FullyEncoded);
}
Expand Down
18 changes: 16 additions & 2 deletions fmt/Link2Bean.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,9 @@ namespace NekoGui_fmt {
name = url.fragment();
serverAddress = url.host();
serverPort = url.port();
serverAddress = url.host(); // default sni
hopPort = query.queryItemValue("mport");
obfsPassword = query.queryItemValue("obfsParam");
allowInsecure = query.queryItemValue("insecure") == "1";
allowInsecure = QStringList{"1", "true"}.contains(query.queryItemValue("insecure"));
uploadMbps = query.queryItemValue("upmbps").toInt();
downloadMbps = query.queryItemValue("downmbps").toInt();

Expand Down Expand Up @@ -275,6 +274,21 @@ namespace NekoGui_fmt {
udpRelayMode = query.queryItemValue("udp_relay_mode");
allowInsecure = query.queryItemValue("allow_insecure") == "1";
disableSni = query.queryItemValue("disable_sni") == "1";
} else if (QStringList{"hy2", "hysteria2"}.contains(url.scheme())) {
name = url.fragment();
serverAddress = url.host();
serverPort = url.port();
// hopPort = query.queryItemValue("mport");
obfsPassword = query.queryItemValue("obfs-password");
allowInsecure = QStringList{"1", "true"}.contains(query.queryItemValue("insecure"));

if (url.password().isEmpty()) {
password = url.userName();
} else {
password = url.userName() + ":" + url.password();
}

sni = query.queryItemValue("sni");
}

return true;
Expand Down
27 changes: 18 additions & 9 deletions fmt/QUICBean.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace NekoGui_fmt {

bool forceExternal = false;

// Hysteria
// Hysteria 1

static constexpr int hysteria_protocol_udp = 0;
static constexpr int hysteria_protocol_facktcp = 1;
Expand All @@ -23,8 +23,10 @@ namespace NekoGui_fmt {
static constexpr int hysteria_auth_string = 1;
static constexpr int hysteria_auth_base64 = 2;
int authPayloadType = 0;

QString authPayload = "";

// Hysteria 1&2

QString obfsPassword = "";

int uploadMbps = 100;
Expand All @@ -37,19 +39,19 @@ namespace NekoGui_fmt {
int hopInterval = 10;
QString hopPort = "";

// Hysteria 2 (Something same as hy1)
QString username = "";

// TUIC

QString uuid = "";
QString password = "";
QString congestionControl = "bbr";
QString udpRelayMode = "native";
bool zeroRttHandshake = false;
QString heartbeat = "10s";
bool uos = false;

// HY2&TUIC

QString password = "";

// TLS

bool allowInsecure = false;
Expand All @@ -76,9 +78,8 @@ namespace NekoGui_fmt {
} else { // hy2
uploadMbps = 0;
downloadMbps = 0;
_add(new configItem("username", &username, itemType::string));
_add(new configItem("password", &password, itemType::string));
}

} else if (proxy_type == proxy_TUIC) {
_add(new configItem("uuid", &uuid, itemType::string));
_add(new configItem("password", &password, itemType::string));
Expand Down Expand Up @@ -114,7 +115,15 @@ namespace NekoGui_fmt {
}
}

QString DisplayType() override { return proxy_type == proxy_TUIC ? "TUIC" : "Hysteria"; };
QString DisplayType() override {
if (proxy_type == proxy_TUIC) {
return "TUIC";
} else if (proxy_type == proxy_Hysteria) {
return "Hysteria1";
} else {
return "Hysteria2";
}
};

int NeedExternal(bool isFirstProfile) override;

Expand Down
10 changes: 9 additions & 1 deletion sub/GroupUpdater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,22 @@ namespace NekoGui_sub {
if (!ok) return;
}

// Hysteria
// Hysteria1
if (str.startsWith("hysteria://")) {
needFix = false;
ent = NekoGui::ProfileManager::NewProxyEntity("hysteria");
auto ok = ent->QUICBean()->TryParseLink(str);
if (!ok) return;
}

// Hysteria2
if (str.startsWith("hysteria2://") || str.startsWith("hy2://")) {
needFix = false;
ent = NekoGui::ProfileManager::NewProxyEntity("hysteria2");
auto ok = ent->QUICBean()->TryParseLink(str);
if (!ok) return;
}

// TUIC
if (str.startsWith("tuic://")) {
needFix = false;
Expand Down
4 changes: 0 additions & 4 deletions translations/fa_IR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -972,10 +972,6 @@ This needs to be run NekoBox with administrator privileges.</source>
<source>UDP Relay Mode</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Username</source>
<translation type="unfinished">نام کاربری</translation>
</message>
<message>
<source>Force use external core</source>
<translation type="unfinished"></translation>
Expand Down
4 changes: 0 additions & 4 deletions translations/ru_RU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -979,10 +979,6 @@ https://matsuridayo.github.io/n-configuration/#vpn-tun</translation>
<source>UDP Relay Mode</source>
<translation>Режим UDP Relay</translation>
</message>
<message>
<source>Username</source>
<translation type="unfinished">Имя пользователя</translation>
</message>
<message>
<source>Force use external core</source>
<translation type="unfinished"></translation>
Expand Down
4 changes: 0 additions & 4 deletions translations/zh_CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -980,10 +980,6 @@ This needs to be run NekoBox with administrator privileges.</source>
<source>Disable SNI</source>
<translation>不发送服务器名称指示</translation>
</message>
<message>
<source>Username</source>
<translation>用户名</translation>
</message>
<message>
<source>Force use external core</source>
<translation>强制使用外部核心</translation>
Expand Down
4 changes: 2 additions & 2 deletions ui/edit/dialog_edit_profile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ DialogEditProfile::DialogEditProfile(const QString &_type, int profileOrGroupId,
LOAD_TYPE("vmess")
LOAD_TYPE("vless")
LOAD_TYPE("naive")
ui->type->addItem("Hysteria1", "hysteria");
ui->type->addItem("Hysteria2", "hysteria2");
LOAD_TYPE("hysteria")
LOAD_TYPE("hysteria2")
LOAD_TYPE("tuic")
ui->type->addItem(tr("Custom (%1 outbound)").arg(software_core_name), "internal");
ui->type->addItem(tr("Custom (%1 config)").arg(software_core_name), "internal-full");
Expand Down
69 changes: 31 additions & 38 deletions ui/edit/edit_quic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,39 @@ void EditQUIC::onStart(std::shared_ptr<NekoGui::ProxyEntity> _ent) {
this->ent = _ent;
auto bean = this->ent->QUICBean();

if (bean->proxy_type == NekoGui_fmt::QUICBean::proxy_Hysteria || bean->proxy_type == NekoGui_fmt::QUICBean::proxy_Hysteria2) {
P_LOAD_STRING(hopPort);
P_LOAD_INT(hopInterval);
P_LOAD_INT(uploadMbps);
P_LOAD_INT(downloadMbps);
P_LOAD_BOOL(disableMtuDiscovery)
P_LOAD_STRING(obfsPassword);
P_LOAD_STRING(authPayload);
P_LOAD_INT(streamReceiveWindow);
P_LOAD_INT(connectionReceiveWindow);
P_LOAD_BOOL(forceExternal);
P_LOAD_STRING(hopPort);
P_LOAD_INT(hopInterval);
P_LOAD_INT(uploadMbps);
P_LOAD_INT(downloadMbps);
P_LOAD_BOOL(disableMtuDiscovery)
P_LOAD_STRING(obfsPassword);
P_LOAD_STRING(authPayload);
P_LOAD_INT(streamReceiveWindow);
P_LOAD_INT(connectionReceiveWindow);

P_LOAD_BOOL(forceExternal);
P_LOAD_COMBO_INT(hyProtocol);
P_LOAD_COMBO_INT(authPayloadType);
P_LOAD_STRING(uuid);
P_LOAD_STRING(password);

P_LOAD_COMBO_STRING(congestionControl);
P_LOAD_COMBO_STRING(udpRelayMode);
P_LOAD_BOOL(zeroRttHandshake);
P_LOAD_STRING(heartbeat);
P_LOAD_BOOL(uos);

// TLS
P_LOAD_STRING(sni);
P_LOAD_STRING(alpn);
P_C_LOAD_STRING(caText);
P_LOAD_BOOL(allowInsecure);
P_LOAD_BOOL(disableSni);

if (bean->proxy_type == NekoGui_fmt::QUICBean::proxy_Hysteria || bean->proxy_type == NekoGui_fmt::QUICBean::proxy_Hysteria2) {
ui->uuid->hide();
ui->uuid_l->hide();
ui->uuidgen->hide();
ui->password->hide();
ui->password_l->hide();
ui->congestionControl->hide();
ui->congestionControl_l->hide();
ui->udpRelayMode->hide();
Expand All @@ -47,14 +63,9 @@ void EditQUIC::onStart(std::shared_ptr<NekoGui::ProxyEntity> _ent) {
if (!IS_NEKO_BOX) ui->forceExternal->hide();

if (bean->proxy_type == NekoGui_fmt::QUICBean::proxy_Hysteria) { // hy1
P_LOAD_COMBO_INT(hyProtocol);
P_LOAD_COMBO_INT(authPayloadType);

ui->username_l->hide();
ui->username->hide();
ui->password->hide();
ui->password_l->hide();
} else { // hy2
P_LOAD_STRING(username);

ui->hyProtocol->hide();
ui->hyProtocol_l->hide();
ui->hyProtocol->hide();
Expand All @@ -79,14 +90,6 @@ void EditQUIC::onStart(std::shared_ptr<NekoGui::ProxyEntity> _ent) {
}
}
} else if (bean->proxy_type == NekoGui_fmt::QUICBean::proxy_TUIC) {
P_LOAD_STRING(uuid);
P_LOAD_STRING(password);
P_LOAD_COMBO_STRING(congestionControl);
P_LOAD_COMBO_STRING(udpRelayMode);
P_LOAD_BOOL(zeroRttHandshake);
P_LOAD_STRING(heartbeat);
P_LOAD_BOOL(uos);

ui->hopPort->hide();
ui->hopPort_l->hide();
ui->hopInterval->hide();
Expand All @@ -112,13 +115,6 @@ void EditQUIC::onStart(std::shared_ptr<NekoGui::ProxyEntity> _ent) {
ui->uos->hide();
}
}

// TLS
P_LOAD_STRING(sni);
P_LOAD_STRING(alpn);
P_C_LOAD_STRING(caText);
P_LOAD_BOOL(allowInsecure);
P_LOAD_BOOL(disableSni);
}

bool EditQUIC::onEnd() {
Expand All @@ -139,9 +135,6 @@ bool EditQUIC::onEnd() {
P_SAVE_INT(streamReceiveWindow);
P_SAVE_INT(connectionReceiveWindow);

// Hysteria2
P_SAVE_STRING(username);

// TUIC
P_SAVE_STRING(uuid);
P_SAVE_STRING(password);
Expand Down
Loading

0 comments on commit 29c6185

Please sign in to comment.