From 4cd2a1d6fb874efd8b2de2cf02e26bc2748a1b48 Mon Sep 17 00:00:00 2001 From: Qv2ray-dev <59914293+Qv2ray-dev@users.noreply.github.com> Date: Sun, 17 May 2020 09:15:48 +0800 Subject: [PATCH 01/10] add: adapt plugin interface V2 --- TrojanPlugin.cpp | 5 +-- TrojanPlugin.hpp | 7 ++-- core/Kernel.cpp | 95 +++++++++++++++++++++++------------------------- core/Kernel.hpp | 22 ++++++----- interface | 2 +- 5 files changed, 65 insertions(+), 66 deletions(-) diff --git a/TrojanPlugin.cpp b/TrojanPlugin.cpp index 90f398b..86c2990 100644 --- a/TrojanPlugin.cpp +++ b/TrojanPlugin.cpp @@ -6,9 +6,9 @@ #include #include -std::shared_ptr TrojanPlugin::GetKernel() +std::unique_ptr TrojanPlugin::CreateKernel() { - return kernel; + return std::make_unique(); } bool TrojanPlugin::UpdateSettings(const QJsonObject &conf) @@ -22,7 +22,6 @@ bool TrojanPlugin::Initialize(const QString &, const QJsonObject &settings) this->settings = settings; serializer = std::make_shared(this); eventHandler = std::make_shared(this); - kernel = std::make_shared(this); return true; } diff --git a/TrojanPlugin.hpp b/TrojanPlugin.hpp index ec34ee8..d93e55e 100644 --- a/TrojanPlugin.hpp +++ b/TrojanPlugin.hpp @@ -23,7 +23,7 @@ class TrojanPlugin // Basic metainfo of this plugin const QvPluginMetadata GetMetadata() const override { - return QvPluginMetadata{ + auto x = QvPluginMetadata{ "Trojan Plugin", // "Qv2ray Workgroup", // "qvtrojan_plugin", // @@ -33,9 +33,11 @@ class TrojanPlugin { SPECIAL_TYPE_KERNEL, // SPECIAL_TYPE_SERIALIZOR } // }; + x.KernelOutboundCapabilities = { { "Trojan", "trojan" } }; + return x; } // - std::shared_ptr GetKernel() override; + std::unique_ptr CreateKernel() override; std::shared_ptr GetSerializer() override; std::shared_ptr GetEventHandler() override; std::unique_ptr GetEditorWidget(UI_TYPE) override; @@ -53,5 +55,4 @@ class TrojanPlugin QJsonObject settings; std::shared_ptr serializer; std::shared_ptr eventHandler; - std::shared_ptr kernel; }; diff --git a/core/Kernel.cpp b/core/Kernel.cpp index 50f9a9c..956189d 100644 --- a/core/Kernel.cpp +++ b/core/Kernel.cpp @@ -1,7 +1,5 @@ #include "Kernel.hpp" -#include "Common.hpp" - #include std::atomic_ulong sentSize{}; @@ -11,7 +9,7 @@ TrojanKernelThread *TrojanKernelThread::self = nullptr; void TrojanPluginKernelLogger(const std::string &str, Log::Level) { - TrojanKernelThread::self->OnKernelLogAvaliable_s(QString::fromStdString(str)); + TrojanKernelThread::self->OnKernelLogAvailable_s(QString::fromStdString(str)); } void TrojanPluginAddSentAmout(unsigned long s) @@ -26,36 +24,44 @@ void TrojanPluginAddRcvdAmout(unsigned long v) TrojanKernel::TrojanKernel(QObject *parent) : Qv2rayPlugin::QvPluginKernel(parent), thread(this) { connect(&thread, &TrojanKernelThread::OnKernelCrashed_s, this, &TrojanKernel::OnKernelCrashed); - connect(&thread, &TrojanKernelThread::OnKernelLogAvaliable_s, this, &TrojanKernel::OnKernelLogAvaliable); + connect(&thread, &TrojanKernelThread::OnKernelLogAvailable_s, this, &TrojanKernel::OnKernelLogAvailable); connect(&thread, &TrojanKernelThread::OnKernelStatsAvailable_s, this, &TrojanKernel::OnKernelStatsAvailable); } +void TrojanKernel::SetConnectionSettings(const QMap &options, const QJsonObject &settings) +{ + httpPort = options[KERNEL_HTTP_ENABLED].toBool() ? options[KERNEL_HTTP_PORT].toInt() : 0; + socksPort = options[KERNEL_SOCKS_ENABLED].toBool() ? options[KERNEL_SOCKS_PORT].toInt() : 0; + listenAddress = options[KERNEL_LISTEN_ADDRESS].toString(); + trojanConfig = settings; +} + bool TrojanKernel::StartKernel() { + if (socksPort == 0 && httpPort == 0) + { + emit OnKernelCrashed("Both HTTP and SOCKS inbounds are disabled."); + } + if (socksPort == 0) + { + socksPort = httpPort + 100; + } sentSize.store(0); rcvdSize.store(0); + thread.SetConfig(TrojanObject::fromJson(trojanConfig), listenAddress, socksPort); statsTimerId = startTimer(1000); - if (hasHttpConfigured) + if (httpPort != 0) { - httpHelper.httpListen(QHostAddress(httpListenAddress), httpPort, socksPort); + httpHelper.httpListen(QHostAddress(listenAddress), httpPort, socksPort); } thread.start(); return true; } -void TrojanKernel::timerEvent(QTimerEvent *event) -{ - if (event->timerId() == statsTimerId) - { - emit OnKernelStatsAvailable(rcvdSize.exchange(0), sentSize.exchange(0)); - } - QObject::timerEvent(event); -} - bool TrojanKernel::StopKernel() { killTimer(statsTimerId); - if (hasHttpConfigured) + if (httpPort != 0) { httpHelper.close(); } @@ -63,39 +69,13 @@ bool TrojanKernel::StopKernel() return true; } -void TrojanKernel::SetConnectionSettings(const QString &listenAddress, const QMap &inbound, const QJsonObject &settings) -{ - hasHttpConfigured = inbound.contains("http"); - hasSocksConfigured = inbound.contains("socks"); - // - httpPort = inbound["http"]; - socksPort = inbound["socks"]; - httpListenAddress = listenAddress; - // - const auto o = TrojanObject::fromJson(settings); - // - Config config; - config.run_type = Config::CLIENT; - config.log_level = Log::INFO; // - config.password[Config::SHA224(o.password.toStdString())] = o.password.toStdString(); - config.remote_addr = o.address.toStdString(); - config.remote_port = o.port; - config.ssl.sni = o.sni.toStdString(); - config.ssl.verify = !o.ignoreCertificate; - config.ssl.verify_hostname = !o.ignoreHostname; - config.ssl.reuse_session = o.reuseSession; - config.ssl.session_ticket = o.sessionTicket; - config.tcp.reuse_port = o.reusePort; - config.tcp.fast_open = o.tcpFastOpen; - // - config.local_addr = listenAddress.toStdString(); - config.local_port = socksPort; - thread.SetConfig(config); -} - -const QList TrojanKernel::KernelOutboundCapabilities() const +void TrojanKernel::timerEvent(QTimerEvent *event) { - return { { "Trojan", "trojan" } }; + if (event->timerId() == statsTimerId) + { + emit OnKernelStatsAvailable(rcvdSize.exchange(0), sentSize.exchange(0)); + } + QObject::timerEvent(event); } TrojanKernel::~TrojanKernel() @@ -121,9 +101,26 @@ TrojanKernelThread::~TrojanKernelThread() void TrojanKernelThread::run() { + Config configX; + configX.run_type = Config::CLIENT; + configX.log_level = Log::INFO; + configX.password[Config::SHA224(_config.password.toStdString())] = _config.password.toStdString(); + configX.remote_addr = _config.address.toStdString(); + configX.remote_port = _config.port; + configX.ssl.sni = _config.sni.toStdString(); + configX.ssl.verify = !_config.ignoreCertificate; + configX.ssl.verify_hostname = !_config.ignoreHostname; + configX.ssl.reuse_session = _config.reuseSession; + configX.ssl.session_ticket = _config.sessionTicket; + configX.tcp.reuse_port = _config.reusePort; + configX.tcp.fast_open = _config.tcpFastOpen; + // + configX.local_addr = listenIp.toStdString(); + configX.local_port = listenPort; + try { - service = std::make_unique(config); + service = std::make_unique(configX); service->run(); } catch (const std::exception &e) diff --git a/core/Kernel.hpp b/core/Kernel.hpp index 580b211..b56a98e 100644 --- a/core/Kernel.hpp +++ b/core/Kernel.hpp @@ -1,9 +1,9 @@ #pragma once +#include "Common.hpp" #include "QvPluginProcessor.hpp" #include "trojan/src/core/config.h" #include "trojan/src/core/service.h" #include "utils/HttpProxy.hpp" - void TrojanPluginKernelLogger(const std::string &, Log::Level); void TrojanPluginAddSentAmout(unsigned long); void TrojanPluginAddRcvdAmout(unsigned long); @@ -24,9 +24,11 @@ class TrojanKernelThread : public QThread static TrojanKernelThread *self; public: - void SetConfig(const Config &conf) + void SetConfig(const TrojanObject &conf, const QString &listenIp, int listenPort) { - config = conf; + _config = conf; + this->listenIp = listenIp; + this->listenPort = listenPort; } protected: @@ -34,12 +36,14 @@ class TrojanKernelThread : public QThread signals: void OnKernelCrashed_s(const QString &); - void OnKernelLogAvaliable_s(const QString &); + void OnKernelLogAvailable_s(const QString &); void OnKernelStatsAvailable_s(quint64 upSpeed, quint64 downSpeed); private: std::unique_ptr service; - Config config; + TrojanObject _config; + QString listenIp; + int listenPort; }; class TrojanKernel : public Qv2rayPlugin::QvPluginKernel @@ -48,23 +52,21 @@ class TrojanKernel : public Qv2rayPlugin::QvPluginKernel public: explicit TrojanKernel(QObject *parent = nullptr); ~TrojanKernel(); - void SetConnectionSettings(const QString &listenAddress, const QMap &inbound, const QJsonObject &settings) override; + void SetConnectionSettings(const QMap &options, const QJsonObject &settings) override; bool StartKernel() override; bool StopKernel() override; - const QList KernelOutboundCapabilities() const override; protected: void timerEvent(QTimerEvent *event) override; private: + QJsonObject trojanConfig; unsigned long lastSent; unsigned long lastRcvd; int statsTimerId = -1; Qv2rayPlugin::Utils::HttpProxy httpHelper; int httpPort; int socksPort; - QString httpListenAddress; - bool hasSocksConfigured; - bool hasHttpConfigured; + QString listenAddress; TrojanKernelThread thread; }; diff --git a/interface b/interface index 8ec8004..4138f84 160000 --- a/interface +++ b/interface @@ -1 +1 @@ -Subproject commit 8ec800431c7ca133e3d6fa1270a263b0e0f836c1 +Subproject commit 4138f84c09167f06de2ba373dccfcf31ca5be09e From 8dd9eadd8b10de1ab875faaf5bf3e987fb455e46 Mon Sep 17 00:00:00 2001 From: ymshenyu Date: Wed, 27 May 2020 22:38:42 +0800 Subject: [PATCH 02/10] mac: use xcode 10.3 --- .github/workflows/build-simpleplugin.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/build-simpleplugin.yml b/.github/workflows/build-simpleplugin.yml index 1726e90..480103d 100644 --- a/.github/workflows/build-simpleplugin.yml +++ b/.github/workflows/build-simpleplugin.yml @@ -91,6 +91,7 @@ jobs: shell: bash if: matrix.platform == 'macos-latest' run: | + sudo xcode-select -s "/Applications/Xcode_10.3.app" mkdir build cd build cmake .. -DCMAKE_BUILD_TYPE=Release -DOPENSSL_ROOT_DIR=/usr/local/opt/openssl@1.1 -DOPENSSL_LIBRARIES=/usr/local/opt/openssl@1.1/lib -DBoost_USE_STATIC_LIBS=ON -DOPENSSL_USE_STATIC_LIBS=ON -DMACOSX_DEPLOYMENT_TARGET=10.13 From 12e0b7b6046ca43bf9f31d14509f8592ced52546 Mon Sep 17 00:00:00 2001 From: Qv2ray-dev <59914293+Qv2ray-dev@users.noreply.github.com> Date: Fri, 5 Jun 2020 21:24:04 +0800 Subject: [PATCH 03/10] Update TrojanPlugin.hpp --- TrojanPlugin.hpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/TrojanPlugin.hpp b/TrojanPlugin.hpp index d93e55e..8c4d85b 100644 --- a/TrojanPlugin.hpp +++ b/TrojanPlugin.hpp @@ -24,14 +24,14 @@ class TrojanPlugin const QvPluginMetadata GetMetadata() const override { auto x = QvPluginMetadata{ - "Trojan Plugin", // - "Qv2ray Workgroup", // - "qvtrojan_plugin", // - "Connect to Trojan server in Qv2ray", // - QIcon(":/assets/logo.png"), // - {}, // - { SPECIAL_TYPE_KERNEL, // - SPECIAL_TYPE_SERIALIZOR } // + "Trojan-GFW Plugin", // + "Qv2ray Workgroup", // + "qvtrojan_plugin", // + "Connect to Trojan server in Qv2ray, conflicts with Trojan-Go plugin", // + QIcon(":/assets/logo.png"), // + {}, // + { SPECIAL_TYPE_KERNEL, // + SPECIAL_TYPE_SERIALIZOR } // }; x.KernelOutboundCapabilities = { { "Trojan", "trojan" } }; return x; From 16a51d736633773bdea672f5d8979f53f704edbb Mon Sep 17 00:00:00 2001 From: Qv2ray-dev <59914293+Qv2ray-dev@users.noreply.github.com> Date: Fri, 5 Jun 2020 21:24:57 +0800 Subject: [PATCH 04/10] Update TrojanPlugin.hpp --- TrojanPlugin.hpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/TrojanPlugin.hpp b/TrojanPlugin.hpp index 8c4d85b..9d40d3a 100644 --- a/TrojanPlugin.hpp +++ b/TrojanPlugin.hpp @@ -24,14 +24,14 @@ class TrojanPlugin const QvPluginMetadata GetMetadata() const override { auto x = QvPluginMetadata{ - "Trojan-GFW Plugin", // - "Qv2ray Workgroup", // - "qvtrojan_plugin", // - "Connect to Trojan server in Qv2ray, conflicts with Trojan-Go plugin", // - QIcon(":/assets/logo.png"), // - {}, // - { SPECIAL_TYPE_KERNEL, // - SPECIAL_TYPE_SERIALIZOR } // + "Trojan-GFW Plugin", // + "Qv2ray Workgroup", // + "qvtrojan_plugin", // + "Connect to Trojan server in Qv2ray, conflicts with the Trojan-Go plugin", // + QIcon(":/assets/logo.png"), // + {}, // + { SPECIAL_TYPE_KERNEL, // + SPECIAL_TYPE_SERIALIZOR } // }; x.KernelOutboundCapabilities = { { "Trojan", "trojan" } }; return x; From 2961999ad9f38650b80b0cfcb96f677baba02489 Mon Sep 17 00:00:00 2001 From: Qv2ray-dev <59914293+Qv2ray-dev@users.noreply.github.com> Date: Fri, 12 Jun 2020 17:43:16 +0800 Subject: [PATCH 05/10] update: update submodule --- interface | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interface b/interface index 4138f84..24d6cde 160000 --- a/interface +++ b/interface @@ -1 +1 @@ -Subproject commit 4138f84c09167f06de2ba373dccfcf31ca5be09e +Subproject commit 24d6cdeabb50cf753e23efea225f66c59f23c06e From 290a5b4053cdfd10a88eb9e0df9e24bb9190a5fb Mon Sep 17 00:00:00 2001 From: Qv2ray-dev <59914293+Qv2ray-dev@users.noreply.github.com> Date: Fri, 19 Jun 2020 09:23:16 +0800 Subject: [PATCH 06/10] Update .gitmodules --- .gitmodules | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitmodules b/.gitmodules index e5380f9..e178a81 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,6 @@ [submodule "interface"] path = interface - url = https://github.com/Qv2ray/QvPlugin-Interface/ + url = https://github.com/Qv2ray/QvPlugin-Interface [submodule "trojan"] path = trojan url = https://github.com/Qv2ray/trojan From b92fe69a95ab5c9e622f5c4129866643e5747f09 Mon Sep 17 00:00:00 2001 From: Qv2ray-dev <59914293+Qv2ray-dev@users.noreply.github.com> Date: Mon, 6 Jul 2020 23:50:51 +0800 Subject: [PATCH 07/10] fix: fixed incomplete password/name url decode --- core/Serializer.hpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/core/Serializer.hpp b/core/Serializer.hpp index e47df5c..1d5771f 100644 --- a/core/Serializer.hpp +++ b/core/Serializer.hpp @@ -47,20 +47,24 @@ class TrojanSerializer : public Qv2rayPlugin::QvPluginSerializer const auto trueList = QStringList{ "true", "1", "yes", "y" }; const QUrl trojanUrl(link); const QUrlQuery query(trojanUrl.query()); - *alias = trojanUrl.fragment(); + *alias = trojanUrl.fragment(QUrl::FullyDecoded); + + auto getQueryValue = [&](const QString &key) { + return query.queryItemValue(key, QUrl::FullyDecoded); + }; // TrojanObject result; result.address = trojanUrl.host(); result.password = trojanUrl.userName(); result.port = trojanUrl.port(); - result.sni = query.queryItemValue("sni"); + result.sni = getQueryValue("sni"); // - result.tcpFastOpen = trueList.contains(query.queryItemValue("tfo").toLower()); - result.sessionTicket = trueList.contains(query.queryItemValue("sessionTicket").toLower()); + result.tcpFastOpen = trueList.contains(getQueryValue("tfo").toLower()); + result.sessionTicket = trueList.contains(getQueryValue("sessionTicket").toLower()); // - bool allowAllInsecure = trueList.contains(query.queryItemValue("allowInsecure").toLower()); - result.ignoreHostname = allowAllInsecure || trueList.contains(query.queryItemValue("allowInsecureHostname").toLower()); - result.ignoreCertificate = allowAllInsecure || trueList.contains(query.queryItemValue("allowInsecureCertificate").toLower()); + bool allowAllInsecure = trueList.contains(getQueryValue("allowInsecure").toLower()); + result.ignoreHostname = allowAllInsecure || trueList.contains(getQueryValue("allowInsecureHostname").toLower()); + result.ignoreCertificate = allowAllInsecure || trueList.contains(getQueryValue("allowInsecureCertificate").toLower()); // return { "trojan", result.toJson() }; } From 6e84849918da24312304cc3c01c06a6d470c1430 Mon Sep 17 00:00:00 2001 From: Qv2ray-dev <59914293+Qv2ray-dev@users.noreply.github.com> Date: Tue, 7 Jul 2020 16:55:00 +0800 Subject: [PATCH 08/10] update: update interface --- interface | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interface b/interface index 24d6cde..177676b 160000 --- a/interface +++ b/interface @@ -1 +1 @@ -Subproject commit 24d6cdeabb50cf753e23efea225f66c59f23c06e +Subproject commit 177676bdf6c44cd7640e8a4f719e711c12f160de From d95732fb0117ec31f1b69c51a5fe85b0323f02b5 Mon Sep 17 00:00:00 2001 From: ymshenyu Date: Wed, 8 Jul 2020 08:35:35 +0800 Subject: [PATCH 09/10] fix build --- .github/workflows/build-simpleplugin.yml | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build-simpleplugin.yml b/.github/workflows/build-simpleplugin.yml index 480103d..6fc3347 100644 --- a/.github/workflows/build-simpleplugin.yml +++ b/.github/workflows/build-simpleplugin.yml @@ -49,10 +49,16 @@ jobs: - name: Restoring submodules run: git submodule update --init # ========================================================================================================= - - name: Install Packages + - name: Install Packages - macOS if: matrix.platform == 'macos-latest' run: | brew install boost openssl@1.1 zlib + - name: Install Packages - Linux + if: matrix.platform == 'ubuntu-16.04' + run: | + sudo add-apt-repository -y ppa:ymshenyu/boost + sudo apt-get update + sudo apt-get install -y libboost-system1.71-dev libboost-program-options1.71-dev - name: Install MSVC compiler if: matrix.platform == 'windows-latest' uses: ilammy/msvc-dev-cmd@v1 @@ -103,7 +109,6 @@ jobs: env: CC: gcc-7 CXX: gcc-7 - BOOST_ROOT: "/usr/local/share/boost/1.72.0" CXXFLAGS: -fno-sized-deallocation run: | mkdir build From 4fea306e0862be6d4bdbe4a8cf9207188c689d78 Mon Sep 17 00:00:00 2001 From: Qv2ray-dev <59914293+Qv2ray-dev@users.noreply.github.com> Date: Wed, 8 Jul 2020 09:02:51 +0800 Subject: [PATCH 10/10] fix: fixed crashing when has no inbounds and no V2ray Integration --- core/Kernel.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/core/Kernel.cpp b/core/Kernel.cpp index 956189d..a962514 100644 --- a/core/Kernel.cpp +++ b/core/Kernel.cpp @@ -41,6 +41,7 @@ bool TrojanKernel::StartKernel() if (socksPort == 0 && httpPort == 0) { emit OnKernelCrashed("Both HTTP and SOCKS inbounds are disabled."); + return false; } if (socksPort == 0) {