Skip to content

Commit

Permalink
Add hysteria gui
Browse files Browse the repository at this point in the history
  • Loading branch information
arm64v8a committed Apr 27, 2023
1 parent bc9c579 commit 8b52d46
Show file tree
Hide file tree
Showing 22 changed files with 769 additions and 121 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ 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_custom.h
ui/edit/edit_custom.cpp
ui/edit/edit_custom.ui
Expand Down
5 changes: 3 additions & 2 deletions db/ConfigBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <QFileInfo>

#define BOX_UNDERLYING_DNS NekoRay::dataStore->core_box_underlying_dns.isEmpty() ? "underlying://0.0.0.0" : NekoRay::dataStore->core_box_underlying_dns
#define BOX_UNDERLYING_DNS_EXPORT NekoRay::dataStore->core_box_underlying_dns.isEmpty() ? "local" : NekoRay::dataStore->core_box_underlying_dns

namespace NekoRay {

Expand Down Expand Up @@ -769,7 +770,7 @@ namespace NekoRay {

// Direct
auto directDNSAddress = dataStore->direct_dns;
if (directDNSAddress == "localhost") directDNSAddress = BOX_UNDERLYING_DNS;
if (directDNSAddress == "localhost") directDNSAddress = BOX_UNDERLYING_DNS_EXPORT;
if (!status->forTest)
dnsServers += QJsonObject{
{"tag", "dns-direct"},
Expand All @@ -782,7 +783,7 @@ namespace NekoRay {
// Underlying 100% Working DNS
dnsServers += QJsonObject{
{"tag", "dns-local"},
{"address", BOX_UNDERLYING_DNS},
{"address", BOX_UNDERLYING_DNS_EXPORT},
{"detour", "direct"},
};

Expand Down
2 changes: 2 additions & 0 deletions db/Database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ namespace NekoRay {
bean = new fmt::TrojanVLESSBean(fmt::TrojanVLESSBean::proxy_VLESS);
} else if (type == "naive") {
bean = new fmt::NaiveBean();
} else if (type == "hysteria") {
bean = new fmt::HysteriaBean();
} else if (type == "custom") {
bean = new fmt::CustomBean();
} else {
Expand Down
6 changes: 6 additions & 0 deletions db/ProxyEntity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ namespace NekoRay {

class NaiveBean;

class HysteriaBean;

class CustomBean;

class ChainBean;
Expand Down Expand Up @@ -63,6 +65,10 @@ namespace NekoRay {
return (fmt::NaiveBean *) bean.get();
};

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

[[nodiscard]] fmt::CustomBean *CustomBean() const {
return (fmt::CustomBean *) bean.get();
};
Expand Down
28 changes: 28 additions & 0 deletions fmt/Bean2CoreObj_box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,34 @@ namespace NekoRay::fmt {
return result;
}

CoreObjOutboundBuildResult HysteriaBean::BuildCoreObjSingBox() {
CoreObjOutboundBuildResult result;

QJsonObject coreTlsObj{
{"enabled", true},
{"insecure", allowInsecure},
};
if (!alpn.trimmed().isEmpty()) coreTlsObj["alpn"] = QJsonArray{alpn};

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

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

result.outbound = coreHysteriaObj;
return result;
}

CoreObjOutboundBuildResult CustomBean::BuildCoreObjSingBox() {
CoreObjOutboundBuildResult result;

Expand Down
94 changes: 81 additions & 13 deletions fmt/Bean2External.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,28 @@ namespace NekoRay::fmt {
return 1;
}

int CustomBean::NeedExternal(bool isFirstProfile, bool isVPN) {
if (core == "internal" || core == "internal-full") return 0;
if (core == "hysteria") {
int HysteriaBean::NeedExternal(bool isFirstProfile, bool isVPN) {
if (IS_NEKO_BOX) {
if (protocol == hysteria_protocol_udp && hopPort.trimmed().isEmpty()) {
// sing-box support
return 0;
} else {
// hysteria core support
if (isFirstProfile && !isVPN) {
return 2;
}
return 1;
}
} else {
if (isFirstProfile && !isVPN) {
return 2;
}
return 1;
}
}

int CustomBean::NeedExternal(bool isFirstProfile, bool isVPN) {
if (core == "internal" || core == "internal-full") return 0;
return 1;
}

Expand All @@ -64,8 +79,8 @@ namespace NekoRay::fmt {
if (domain_address != connect_address)
result.arguments += "--host-resolver-rules=MAP " + domain_address + " " + connect_address;
if (insecure_concurrency > 0) result.arguments += "--insecure-concurrency=" + Int2String(insecure_concurrency);
if (!extra_headers.isEmpty()) result.arguments += "--extra-headers=" + extra_headers;
if (!certificate.isEmpty()) {
if (!extra_headers.trimmed().isEmpty()) result.arguments += "--extra-headers=" + extra_headers;
if (!certificate.trimmed().isEmpty()) {
WriteTempFile("naive_" + GetRandomString(10) + ".crt", certificate.toUtf8());
result.env += "SSL_CERT_FILE=" + TempFile;
}
Expand All @@ -77,6 +92,67 @@ namespace NekoRay::fmt {
return result;
}

ExternalBuildResult HysteriaBean::BuildExternal(int mapping_port, int socks_port, int external_stat) {
ExternalBuildResult result{dataStore->extraCore->Get("hysteria")};

QJsonObject config;

// determine server format
auto is_direct = external_stat == 2;
auto sni2 = sni;
if (sni.isEmpty() && is_direct) sni2 = 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 (protocol == hysteria_protocol_facktcp) config["protocol"] = "faketcp";
if (protocol == hysteria_protocol_wechat_video) config["protocol"] = "wechat-video";

if (!sni2.isEmpty()) config["server_name"] = sni2;
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) {
ExternalBuildResult result{dataStore->extraCore->Get(core)};

Expand Down Expand Up @@ -107,14 +183,6 @@ namespace NekoRay::fmt {
suffix = ".json";
}

// known core direct out
if (external_stat == 2) {
if (core == "hysteria") {
config = config.replace(QString("\"127.0.0.1:%1\"").arg(mapping_port),
"\"" + DisplayAddress() + "\"");
}
}

// write config
WriteTempFile("custom_" + GetRandomString(10) + suffix, config.toUtf8());
for (int i = 0; i < result.arguments.count(); i++) {
Expand Down
28 changes: 27 additions & 1 deletion fmt/Bean2Link.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ namespace NekoRay::fmt {
{"tls", stream->security == "tls" ? "tls" : ""},
{"sni", stream->sni},
};
return "vmess://" + QJsonObject2QString(N, false).toUtf8().toBase64();
return "vmess://" + QJsonObject2QString(N, true).toUtf8().toBase64();
}

QString NaiveBean::ToShareLink() {
Expand All @@ -116,4 +116,30 @@ namespace NekoRay::fmt {
return url.toString(QUrl::FullyEncoded);
}

QString HysteriaBean::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 (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);
}

} // namespace NekoRay::fmt
80 changes: 80 additions & 0 deletions fmt/HysteriaBean.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#pragma once

#include "fmt/AbstractBean.hpp"

namespace NekoRay::fmt {
class HysteriaBean : public AbstractBean {
public:
static constexpr int hysteria_protocol_udp = 0;
static constexpr int hysteria_protocol_facktcp = 1;
static constexpr int hysteria_protocol_wechat_video = 2;

int protocol = 0;

//

static constexpr int hysteria_auth_none = 0;
static constexpr int hysteria_auth_string = 1;
static constexpr int hysteria_auth_base64 = 2;

int authPayloadType = 0;
QString authPayload = "";
QString obfsPassword = "";

//

int uploadMbps = 100;
int downloadMbps = 100;

qint64 streamReceiveWindow = 0;
qint64 connectionReceiveWindow = 0;
bool disableMtuDiscovery = false;

bool allowInsecure = false;
QString sni = "";
QString alpn = ""; // only 1
QString caText = "";

//

int hopInterval = 10;
QString hopPort = "";

HysteriaBean() : AbstractBean(0) {
_add(new configItem("protocol", &protocol, itemType::integer));
_add(new configItem("authPayloadType", &authPayloadType, itemType::integer));
_add(new configItem("authPayload", &authPayload, itemType::string));
_add(new configItem("obfsPassword", &obfsPassword, itemType::string));
_add(new configItem("uploadMbps", &uploadMbps, itemType::integer));
_add(new configItem("downloadMbps", &downloadMbps, itemType::integer));
_add(new configItem("streamReceiveWindow", &streamReceiveWindow, itemType::integer64));
_add(new configItem("connectionReceiveWindow", &connectionReceiveWindow, itemType::integer64));
_add(new configItem("disableMtuDiscovery", &disableMtuDiscovery, itemType::boolean));
_add(new configItem("allowInsecure", &allowInsecure, itemType::boolean));
_add(new configItem("sni", &sni, itemType::string));
_add(new configItem("alpn", &alpn, itemType::string));
_add(new configItem("caText", &caText, itemType::string));
_add(new configItem("hopInterval", &hopInterval, itemType::integer));
_add(new configItem("hopPort", &hopPort, itemType::string));
};

QString DisplayAddress() override {
if (!hopPort.trimmed().isEmpty()) return WrapIPV6Host(serverAddress) + ":" + hopPort;
return ::DisplayAddress(serverAddress, serverPort);
}

QString DisplayCoreType() override { return NeedExternal(false, false) == 0 ? software_core_name : "Hysteria"; };

QString DisplayType() override { return "Hysteria"; };

int NeedExternal(bool isFirstProfile, bool isVPN) override;

ExternalBuildResult BuildExternal(int mapping_port, int socks_port, int external_stat) override;

CoreObjOutboundBuildResult BuildCoreObjSingBox() override;

bool TryParseLink(const QString &link);

QString ToShareLink() override;
};
} // namespace NekoRay::fmt
37 changes: 37 additions & 0 deletions fmt/Link2Bean.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,41 @@ namespace NekoRay::fmt {
return !(username.isEmpty() || password.isEmpty());
}

bool HysteriaBean::TryParseLink(const QString &link) {
// https://hysteria.network/docs/uri-scheme/
auto url = QUrl(link);
auto query = QUrlQuery(url.query());
if (url.host().isEmpty() || url.port() == -1 || !query.hasQueryItem("upmbps") || !query.hasQueryItem("downmbps")) return false;

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";
uploadMbps = query.queryItemValue("upmbps").toInt();
downloadMbps = query.queryItemValue("downmbps").toInt();

auto protocolStr = (query.hasQueryItem("protocol") ? query.queryItemValue("protocol") : "udp").toLower();
if (protocolStr == "faketcp") {
protocol = fmt::HysteriaBean::hysteria_protocol_facktcp;
} else if (protocolStr.startsWith("wechat")) {
protocol = fmt::HysteriaBean::hysteria_protocol_wechat_video;
}

if (query.hasQueryItem("auth")) {
authPayload = query.queryItemValue("auth");
authPayloadType = fmt::HysteriaBean::hysteria_auth_string;
}

alpn = query.queryItemValue("alpn");
sni = FIRST_OR_SECOND(query.queryItemValue("peer"), query.queryItemValue("sni"));

connectionReceiveWindow = query.queryItemValue("recv_window").toInt();
streamReceiveWindow = query.queryItemValue("recv_window_conn").toInt();

return true;
}

} // namespace NekoRay::fmt
Loading

0 comments on commit 8b52d46

Please sign in to comment.