From 23b0fc33c141952d4ddfcc273c5a6701c5dba77c Mon Sep 17 00:00:00 2001 From: arm64v8a <48624112+arm64v8a@users.noreply.github.com> Date: Sat, 6 May 2023 15:48:41 +0900 Subject: [PATCH] refactor mux --- db/ConfigBuilder.cpp | 56 ++++++++++++++++++++++++------------ main/GuiUtils.hpp | 6 ++-- main/NekoRay.cpp | 3 +- main/NekoRay_DataStore.hpp | 3 +- ui/dialog_basic_settings.cpp | 19 ++++++------ ui/dialog_basic_settings.ui | 18 +++++------- ui/edit/edit_custom.cpp | 8 +++--- ui/edit/edit_naive.cpp | 4 +-- 8 files changed, 70 insertions(+), 47 deletions(-) diff --git a/db/ConfigBuilder.cpp b/db/ConfigBuilder.cpp index 7dfa632..a2882de 100644 --- a/db/ConfigBuilder.cpp +++ b/db/ConfigBuilder.cpp @@ -546,6 +546,7 @@ namespace NekoRay { // Outbound QJsonObject outbound; + auto stream = GetStreamSettings(ent->bean.data()); if (thisExternalStat > 0) { const auto extR = ent->bean->BuildExternal(ext_mapping_port, ext_socks_port, thisExternalStat); @@ -601,31 +602,50 @@ namespace NekoRay { ent->traffic_data->tag = tagOut.toStdString(); status->result->outboundStats += ent->traffic_data; + // mux common + auto needMux = ent->type == "vmess" || ent->type == "trojan" || ent->type == "vless"; + needMux &= !dataStore->mux_protocol.isEmpty() && dataStore->mux_concurrency > 0; + + if (stream != nullptr) { + if (IS_NEKO_BOX) { + if (stream->network == "grpc" || stream->network == "quic" || (stream->network == "http" && stream->security == "tls")) { + needMux = false; + } + } else { + if (stream->network == "grpc" || stream->network == "quic") { + needMux = false; + } + } + } + + // common if (IS_NEKO_BOX) { // apply domain_strategy outbound["domain_strategy"] = dataStore->outbound_domain_strategy; - // TODO apply mux + // apply mux + if (!muxApplied && needMux) { + auto muxObj = QJsonObject{ + {"enabled", true}, + {"protocol", dataStore->mux_protocol}, + {"max_streams", dataStore->mux_concurrency}, + }; + outbound["multiplex"] = muxObj; + muxApplied = true; + } } else { + // apply domain_strategy if (!status->forTest) outbound["domainStrategy"] = dataStore->outbound_domain_strategy; // apply mux - if (dataStore->mux_cool > 0 && !muxApplied) { - // TODO refactor mux settings - if (ent->type == "vmess" || ent->type == "trojan" || ent->type == "vless") { - auto muxObj = QJsonObject{ - {"enabled", true}, - {"concurrency", dataStore->mux_cool}, - }; - auto stream = GetStreamSettings(ent->bean.data()); - if (stream != nullptr && !stream->packet_encoding.isEmpty()) { - muxObj["packetEncoding"] = stream->packet_encoding; - } - if (stream != nullptr && stream->network == "grpc") { - // ignore mux.cool for gRPC - } else { - outbound["mux"] = muxObj; - muxApplied = true; - } + if (!muxApplied && needMux) { + auto muxObj = QJsonObject{ + {"enabled", true}, + {"concurrency", dataStore->mux_concurrency}, + }; + if (stream != nullptr && !stream->packet_encoding.isEmpty()) { + muxObj["packetEncoding"] = stream->packet_encoding; } + outbound["mux"] = muxObj; + muxApplied = true; } } diff --git a/main/GuiUtils.hpp b/main/GuiUtils.hpp index ca69fab..acafbfc 100644 --- a/main/GuiUtils.hpp +++ b/main/GuiUtils.hpp @@ -36,8 +36,10 @@ ui->a->setText(Int2String(NekoRay::dataStore->a)); \ ui->a->setValidator(QRegExpValidator_Number); #define D_SAVE_INT(a) NekoRay::dataStore->a = ui->a->text().toInt(); -#define P_LOAD_COMBO_STR(a) ui->a->setCurrentText(bean->a); -#define P_SAVE_COMBO_STR(a) bean->a = ui->a->currentText(); +#define P_LOAD_COMBO_STRING(a) ui->a->setCurrentText(bean->a); +#define P_SAVE_COMBO_STRING(a) bean->a = ui->a->currentText(); +#define D_LOAD_COMBO_STRING(a) ui->a->setCurrentText(NekoRay::dataStore->a); +#define D_SAVE_COMBO_STRING(a) NekoRay::dataStore->a = ui->a->currentText(); #define P_LOAD_COMBO_INT(a) ui->a->setCurrentIndex(bean->a); #define P_SAVE_COMBO_INT(a) bean->a = ui->a->currentIndex(); #define D_LOAD_BOOL(a) ui->a->setChecked(NekoRay::dataStore->a); diff --git a/main/NekoRay.cpp b/main/NekoRay.cpp index aa57fe7..9e9077e 100644 --- a/main/NekoRay.cpp +++ b/main/NekoRay.cpp @@ -39,7 +39,8 @@ namespace NekoRay { _add(new configItem("domain_strategy", &domain_strategy, itemType::string)); _add(new configItem("outbound_domain_strategy", &outbound_domain_strategy, itemType::string)); _add(new configItem("sniffing_mode", &sniffing_mode, itemType::integer)); - _add(new configItem("mux_cool", &mux_cool, itemType::integer)); + _add(new configItem("mux_protocol", &mux_protocol, itemType::string)); + _add(new configItem("mux_concurrency", &mux_concurrency, itemType::integer)); _add(new configItem("traffic_loop_interval", &traffic_loop_interval, itemType::integer)); _add(new configItem("dns_routing", &dns_routing, itemType::boolean)); _add(new configItem("test_concurrent", &test_concurrent, itemType::integer)); diff --git a/main/NekoRay_DataStore.hpp b/main/NekoRay_DataStore.hpp index 11fe718..1325faa 100644 --- a/main/NekoRay_DataStore.hpp +++ b/main/NekoRay_DataStore.hpp @@ -82,7 +82,8 @@ namespace NekoRay { int traffic_loop_interval = 500; bool connection_statistics = false; int current_group = 0; // group id - int mux_cool = -8; + QString mux_protocol = ""; + int mux_concurrency = 8; QString theme = "0"; QString v2ray_asset_dir = ""; int language = 0; diff --git a/ui/dialog_basic_settings.cpp b/ui/dialog_basic_settings.cpp index b6b4595..4391e7d 100644 --- a/ui/dialog_basic_settings.cpp +++ b/ui/dialog_basic_settings.cpp @@ -59,23 +59,25 @@ DialogBasicSettings::DialogBasicSettings(QWidget *parent) // Common if (IS_NEKO_BOX) { - ui->groupBox_mux->hide(); ui->groupBox_http->hide(); ui->inbound_socks_port_l->setText(ui->inbound_socks_port_l->text().replace("Socks", "Mixed")); - ui->hlayout_l2->addWidget(ui->groupBox_log); + ui->hlayout_l2->addWidget(ui->groupbox_custom_inbound); ui->log_level->addItems(QString("trace debug info warn error fatal panic").split(" ")); + ui->mux_protocol->addItems({"", "h2mux", "smux", "yamux"}); } else { ui->log_level->addItems({"debug", "info", "warning", "none"}); + ui->mux_protocol->addItems({"", "mux.cool"}); } refresh_auth(); - ui->socks_ip->setText(NekoRay::dataStore->inbound_address); - ui->log_level->setCurrentText(NekoRay::dataStore->log_level); + D_LOAD_STRING(inbound_address) + D_LOAD_COMBO_STRING(log_level) CACHE.custom_inbound = NekoRay::dataStore->custom_inbound; D_LOAD_INT(inbound_socks_port) D_LOAD_INT_ENABLE(inbound_http_port, http_enable) - D_LOAD_INT_ENABLE(mux_cool, mux_cool_enable) + D_LOAD_INT(mux_concurrency) + D_LOAD_COMBO_STRING(mux_protocol) D_LOAD_INT(test_concurrent) D_LOAD_STRING(test_url) @@ -260,12 +262,13 @@ DialogBasicSettings::~DialogBasicSettings() { void DialogBasicSettings::accept() { // Common - NekoRay::dataStore->inbound_address = ui->socks_ip->text(); - NekoRay::dataStore->log_level = ui->log_level->currentText(); + D_SAVE_STRING(inbound_address) + D_SAVE_COMBO_STRING(log_level) NekoRay::dataStore->custom_inbound = CACHE.custom_inbound; D_SAVE_INT(inbound_socks_port) D_SAVE_INT_ENABLE(inbound_http_port, http_enable) - D_SAVE_INT_ENABLE(mux_cool, mux_cool_enable) + D_SAVE_INT(mux_concurrency) + D_SAVE_COMBO_STRING(mux_protocol) D_SAVE_INT(test_concurrent) D_SAVE_STRING(test_url) diff --git a/ui/dialog_basic_settings.ui b/ui/dialog_basic_settings.ui index 13bb17a..15a8adf 100644 --- a/ui/dialog_basic_settings.ui +++ b/ui/dialog_basic_settings.ui @@ -31,7 +31,7 @@ - + @@ -43,7 +43,7 @@ - + @@ -56,7 +56,7 @@ - + @@ -147,7 +147,7 @@ - + @@ -170,16 +170,12 @@ - mux.cool + Mux - - - Enable - - + @@ -189,7 +185,7 @@ - + diff --git a/ui/edit/edit_custom.cpp b/ui/edit/edit_custom.cpp index 7968a5d..479e79f 100644 --- a/ui/edit/edit_custom.cpp +++ b/ui/edit/edit_custom.cpp @@ -26,10 +26,10 @@ EditCustom::~EditCustom() { } #define SAVE_CUSTOM_BEAN \ - P_SAVE_COMBO_STR(core) \ + P_SAVE_COMBO_STRING(core) \ bean->command = ui->command->text().split(" "); \ P_SAVE_STRING_QTEXTEDIT(config_simple) \ - P_SAVE_COMBO_STR(config_suffix) \ + P_SAVE_COMBO_STRING(config_suffix) \ P_SAVE_INT(mapping_port) \ P_SAVE_INT(socks_port) @@ -60,10 +60,10 @@ void EditCustom::onStart(QSharedPointer _ent) { } // load core ui - P_LOAD_COMBO_STR(core) + P_LOAD_COMBO_STRING(core) ui->command->setText(bean->command.join(" ")); P_LOAD_STRING(config_simple) - P_LOAD_COMBO_STR(config_suffix) + P_LOAD_COMBO_STRING(config_suffix) P_LOAD_INT(mapping_port) P_LOAD_INT(socks_port) diff --git a/ui/edit/edit_naive.cpp b/ui/edit/edit_naive.cpp index a0ba3e7..bc37666 100644 --- a/ui/edit/edit_naive.cpp +++ b/ui/edit/edit_naive.cpp @@ -19,7 +19,7 @@ void EditNaive::onStart(QSharedPointer _ent) { P_LOAD_STRING(username); P_LOAD_STRING(password); - P_LOAD_COMBO_STR(protocol); + P_LOAD_COMBO_STRING(protocol); P_C_LOAD_STRING(extra_headers); P_LOAD_STRING(sni); P_C_LOAD_STRING(certificate); @@ -31,7 +31,7 @@ bool EditNaive::onEnd() { P_SAVE_STRING(username); P_SAVE_STRING(password); - P_SAVE_COMBO_STR(protocol); + P_SAVE_COMBO_STRING(protocol); P_C_SAVE_STRING(extra_headers); P_SAVE_STRING(sni); P_C_SAVE_STRING(certificate);