Skip to content
This repository has been archived by the owner on Aug 18, 2021. It is now read-only.

Commit

Permalink
Merge pull request #10 from Qv2ray/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
QxQ committed Jul 9, 2020
2 parents 4f327d6 + 4fea306 commit 3c736ef
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 84 deletions.
10 changes: 8 additions & 2 deletions .github/workflows/build-simpleplugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -91,6 +97,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
Expand All @@ -102,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
Expand Down
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -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
5 changes: 2 additions & 3 deletions TrojanPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
#include <QLabel>
#include <QMetaEnum>

std::shared_ptr<QvPluginKernel> TrojanPlugin::GetKernel()
std::unique_ptr<QvPluginKernel> TrojanPlugin::CreateKernel()
{
return kernel;
return std::make_unique<TrojanKernel>();
}

bool TrojanPlugin::UpdateSettings(const QJsonObject &conf)
Expand All @@ -22,7 +22,6 @@ bool TrojanPlugin::Initialize(const QString &, const QJsonObject &settings)
this->settings = settings;
serializer = std::make_shared<TrojanSerializer>(this);
eventHandler = std::make_shared<TrojanEventHandler>(this);
kernel = std::make_shared<TrojanKernel>(this);
return true;
}

Expand Down
23 changes: 12 additions & 11 deletions TrojanPlugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,21 @@ class TrojanPlugin
// Basic metainfo of this plugin
const QvPluginMetadata GetMetadata() const override
{
return QvPluginMetadata{
"Trojan Plugin", //
"Qv2ray Workgroup", //
"qvtrojan_plugin", //
"Connect to Trojan server in Qv2ray", //
QIcon(":/assets/logo.png"), //
{}, //
{ SPECIAL_TYPE_KERNEL, //
SPECIAL_TYPE_SERIALIZOR } //
auto x = QvPluginMetadata{
"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;
}
//
std::shared_ptr<QvPluginKernel> GetKernel() override;
std::unique_ptr<QvPluginKernel> CreateKernel() override;
std::shared_ptr<QvPluginSerializer> GetSerializer() override;
std::shared_ptr<QvPluginEventHandler> GetEventHandler() override;
std::unique_ptr<QvPluginEditor> GetEditorWidget(UI_TYPE) override;
Expand All @@ -53,5 +55,4 @@ class TrojanPlugin
QJsonObject settings;
std::shared_ptr<TrojanSerializer> serializer;
std::shared_ptr<TrojanEventHandler> eventHandler;
std::shared_ptr<TrojanKernel> kernel;
};
96 changes: 47 additions & 49 deletions core/Kernel.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "Kernel.hpp"

#include "Common.hpp"

#include <atomic>

std::atomic_ulong sentSize{};
Expand All @@ -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)
Expand All @@ -26,76 +24,59 @@ 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<KernelSetting, QVariant> &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.");
return false;
}
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();
}
thread.stop();
return true;
}

void TrojanKernel::SetConnectionSettings(const QString &listenAddress, const QMap<QString, int> &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<Qv2rayPlugin::QvPluginOutboundProtocolObject> 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()
Expand All @@ -121,9 +102,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<Service>(config);
service = std::make_unique<Service>(configX);
service->run();
}
catch (const std::exception &e)
Expand Down
22 changes: 12 additions & 10 deletions core/Kernel.hpp
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -24,22 +24,26 @@ 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:
void run() override;

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> service;
Config config;
TrojanObject _config;
QString listenIp;
int listenPort;
};

class TrojanKernel : public Qv2rayPlugin::QvPluginKernel
Expand All @@ -48,23 +52,21 @@ class TrojanKernel : public Qv2rayPlugin::QvPluginKernel
public:
explicit TrojanKernel(QObject *parent = nullptr);
~TrojanKernel();
void SetConnectionSettings(const QString &listenAddress, const QMap<QString, int> &inbound, const QJsonObject &settings) override;
void SetConnectionSettings(const QMap<KernelSetting, QVariant> &options, const QJsonObject &settings) override;
bool StartKernel() override;
bool StopKernel() override;
const QList<Qv2rayPlugin::QvPluginOutboundProtocolObject> 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;
};
18 changes: 11 additions & 7 deletions core/Serializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() };
}
Expand Down

0 comments on commit 3c736ef

Please sign in to comment.