Skip to content

Commit

Permalink
minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
arm64v8a committed May 22, 2023
1 parent 1b050dd commit c5122b7
Show file tree
Hide file tree
Showing 98 changed files with 1,173 additions and 1,142 deletions.
2 changes: 1 addition & 1 deletion 3rdparty/WinCommander.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ uint WinCommander::runProcessElevated(const QString &path,

LPCTSTR pszParameters = (LPCTSTR)params.utf16();
QString dir;
if (workingDir.isEmpty())
if (workingDir.count() == 0)
dir = QDir::toNativeSeparators(QDir::currentPath());
else
dir = QDir::toNativeSeparators(workingDir);
Expand Down
4 changes: 4 additions & 0 deletions 3rdparty/base64.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include "base64.h"

#ifndef qsizetype
#define qsizetype size_t
#endif

namespace Qt515Base64 {
namespace {
struct fromBase64_helper_result {
Expand Down
11 changes: 11 additions & 0 deletions 3rdparty/fix_old_qt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <QString>

#if QT_VERSION < QT_VERSION_CHECK(5, 10, 0)

inline QString qEnvironmentVariable(const char *varName) {
return qgetenv(varName);
}

#endif
5 changes: 3 additions & 2 deletions 3rdparty/qv2ray/v2/components/proxy/QvProxyConfigurator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
#include <QStandardPaths>
#include <QProcess>

#include "3rdparty/fix_old_qt.h"
#include "3rdparty/qv2ray/wrapper.hpp"
#include "fmt/Preset.hpp"
#include "main/NekoRay.hpp"
#include "main/NekoGui.hpp"

#define QV_MODULE_NAME "SystemProxy"

Expand Down Expand Up @@ -253,7 +254,7 @@ namespace Qv2ray::components::proxy {
#endif

#ifdef Q_OS_WIN
QString str = NekoRay::dataStore->system_proxy_format;
QString str = NekoGui::dataStore->system_proxy_format;
if (str.isEmpty()) str = Preset::Windows::system_proxy_format[0];
str = str.replace("{ip}", address)
.replace("{http_port}", Int2String(httpPort))
Expand Down
2 changes: 1 addition & 1 deletion 3rdparty/qv2ray/v2/ui/LogHighlighter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ namespace Qv2ray::ui {
}

void SyntaxHighlighter::highlightBlock(const QString &text) {
for (const HighlightingRule &rule: qAsConst(highlightingRules)) {
for (const HighlightingRule &rule: highlightingRules) {
QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text);

while (matchIterator.hasNext()) {
Expand Down
56 changes: 29 additions & 27 deletions 3rdparty/qv2ray/v2/ui/LogHighlighter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,36 +55,38 @@
#include <QTextCharFormat>
#include <QTextDocument>

namespace Qv2ray::ui {
class SyntaxHighlighter : public QSyntaxHighlighter {
Q_OBJECT
namespace Qv2ray {
namespace ui {
class SyntaxHighlighter : public QSyntaxHighlighter {
Q_OBJECT

public:
explicit SyntaxHighlighter(bool darkMode, QTextDocument *parent = nullptr);
public:
explicit SyntaxHighlighter(bool darkMode, QTextDocument *parent = nullptr);

protected:
void highlightBlock(const QString &text) override;
protected:
void highlightBlock(const QString &text) override;

private:
struct HighlightingRule {
QRegularExpression pattern;
QTextCharFormat format;
};
QVector<HighlightingRule> highlightingRules;
private:
struct HighlightingRule {
QRegularExpression pattern;
QTextCharFormat format;
};
QVector<HighlightingRule> highlightingRules;

QTextCharFormat tcpudpFormat;
QTextCharFormat dateFormat;
QTextCharFormat acceptedFormat;
QTextCharFormat rejectedFormat;
QTextCharFormat failedFormat;
QTextCharFormat warningFormat;
QTextCharFormat warningFormat2;
QTextCharFormat infoFormat;
QTextCharFormat debugFormat;
QTextCharFormat timeFormat;
QTextCharFormat ipHostFormat;
QTextCharFormat v2rayComponentFormat;
};
} // namespace Qv2ray::ui
QTextCharFormat tcpudpFormat;
QTextCharFormat dateFormat;
QTextCharFormat acceptedFormat;
QTextCharFormat rejectedFormat;
QTextCharFormat failedFormat;
QTextCharFormat warningFormat;
QTextCharFormat warningFormat2;
QTextCharFormat infoFormat;
QTextCharFormat debugFormat;
QTextCharFormat timeFormat;
QTextCharFormat ipHostFormat;
QTextCharFormat v2rayComponentFormat;
};
} // namespace ui
} // namespace Qv2ray

using namespace Qv2ray::ui;
2 changes: 1 addition & 1 deletion 3rdparty/qv2ray/v2/ui/QvAutoCompleteTextEdit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ namespace Qv2ray::ui::widgets {
c->setWidget(this);
c->setCompletionMode(QCompleter::PopupCompletion);
c->setCaseSensitivity(Qt::CaseInsensitive);
QObject::connect(c, QOverload<const QString &>::of(&QCompleter::activated), this, &AutoCompleteTextEdit::insertCompletion);
QObject::connect(c, static_cast<void (QCompleter::*)(const QString &)>(&QCompleter::activated), this, &AutoCompleteTextEdit::insertCompletion);
}

AutoCompleteTextEdit::~AutoCompleteTextEdit() {
Expand Down
40 changes: 22 additions & 18 deletions 3rdparty/qv2ray/v2/ui/QvAutoCompleteTextEdit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,27 +55,31 @@ QT_BEGIN_NAMESPACE
class QCompleter;
QT_END_NAMESPACE

namespace Qv2ray::ui::widgets {
class AutoCompleteTextEdit : public QPlainTextEdit {
Q_OBJECT
namespace Qv2ray {
namespace ui {
namespace widgets {
class AutoCompleteTextEdit : public QPlainTextEdit {
Q_OBJECT

public:
AutoCompleteTextEdit(const QString &prefix, const QStringList &sourceStrings, QWidget *parent = nullptr);
~AutoCompleteTextEdit();
public:
AutoCompleteTextEdit(const QString &prefix, const QStringList &sourceStrings, QWidget *parent = nullptr);
~AutoCompleteTextEdit();

protected:
void keyPressEvent(QKeyEvent *e) override;
void focusInEvent(QFocusEvent *e) override;
protected:
void keyPressEvent(QKeyEvent *e) override;
void focusInEvent(QFocusEvent *e) override;

private slots:
void insertCompletion(const QString &completion);
private slots:
void insertCompletion(const QString &completion);

private:
QString lineUnderCursor() const;
QString wordUnderCursor() const;
private:
QString lineUnderCursor() const;
QString wordUnderCursor() const;

QString prefix;
QCompleter *c = nullptr;
};
} // namespace Qv2ray::ui::widgets
QString prefix;
QCompleter *c = nullptr;
};
} // namespace widgets
} // namespace ui
} // namespace Qv2ray
using namespace Qv2ray::ui::widgets;
2 changes: 1 addition & 1 deletion 3rdparty/qv2ray/v2/ui/widgets/editors/w_JsonEditor.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "w_JsonEditor.hpp"

#include "main/NekoRay.hpp"
#include "main/NekoGui.hpp"

JsonEditor::JsonEditor(const QJsonObject& rootObject, QWidget* parent) : QDialog(parent) {
setupUi(this);
Expand Down
10 changes: 6 additions & 4 deletions 3rdparty/qv2ray/wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@

#define LOG(...) Qv2ray::base::log_internal(__VA_ARGS__)
#define DEBUG(...) Qv2ray::base::log_internal(__VA_ARGS__)
namespace Qv2ray::base {
template<typename... T>
inline void log_internal(T... v) {}
} // namespace Qv2ray::base
namespace Qv2ray {
namespace base {
template<typename... T>
inline void log_internal(T... v) {}
} // namespace base
} // namespace Qv2ray

#define JsonToString(a) QJsonObject2QString(a, false)
#define JsonFromString(a) QString2QJsonObject(a)
Expand Down
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ set(PROJECT_SOURCES
${PLATFORM_SOURCES}

main/main.cpp
main/NekoRay.cpp
main/NekoRay_Utils.cpp
main/NekoGui.cpp
main/NekoGui_Utils.cpp
main/QJS.cpp
main/HTTPRequestHelper.cpp

Expand All @@ -161,7 +161,7 @@ set(PROJECT_SOURCES
rpc/gRPC.cpp

db/Database.cpp
db/TrafficLooper.cpp
db/traffic/TrafficLooper.cpp
db/ProfileFilter.cpp
db/ConfigBuilder.cpp

Expand Down
48 changes: 24 additions & 24 deletions db/ConfigBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
#include <QFile>
#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() ? (status->forExport ? "local" : "underlying://0.0.0.0") : NekoRay::dataStore->core_box_underlying_dns
#define BOX_UNDERLYING_DNS dataStore->core_box_underlying_dns.isEmpty() ? "underlying://0.0.0.0" : dataStore->core_box_underlying_dns
#define BOX_UNDERLYING_DNS_EXPORT dataStore->core_box_underlying_dns.isEmpty() ? (status->forExport ? "local" : "underlying://0.0.0.0") : dataStore->core_box_underlying_dns

namespace NekoRay {
namespace NekoGui {

QStringList getAutoBypassExternalProcessPaths(const QSharedPointer<BuildConfigResult> &result) {
QStringList getAutoBypassExternalProcessPaths(const std::shared_ptr<BuildConfigResult> &result) {
QStringList paths;
for (const auto &extR: result->extRs) {
auto path = extR->program;
Expand Down Expand Up @@ -54,15 +54,15 @@ namespace NekoRay {

// Common

QSharedPointer<BuildConfigResult> BuildConfig(const QSharedPointer<ProxyEntity> &ent, bool forTest, bool forExport) {
auto result = QSharedPointer<BuildConfigResult>(new BuildConfigResult);
auto status = QSharedPointer<BuildConfigStatus>(new BuildConfigStatus);
std::shared_ptr<BuildConfigResult> BuildConfig(const std::shared_ptr<ProxyEntity> &ent, bool forTest, bool forExport) {
auto result = std::make_shared<BuildConfigResult>();
auto status = std::make_shared<BuildConfigStatus>();
status->ent = ent;
status->result = result;
status->forTest = forTest;
status->forExport = forExport;

auto customBean = dynamic_cast<fmt::CustomBean *>(ent->bean.get());
auto customBean = dynamic_cast<NekoGui_fmt::CustomBean *>(ent->bean.get());
if (customBean != nullptr && customBean->core == "internal-full") {
result->coreConfig = QString2QJsonObject(customBean->config_simple);
} else {
Expand Down Expand Up @@ -92,9 +92,9 @@ namespace NekoRay {
return result;
}

QString BuildChain(int chainId, const QSharedPointer<BuildConfigStatus> &status) {
QString BuildChain(int chainId, const std::shared_ptr<BuildConfigStatus> &status) {
// Make list
QList<QSharedPointer<ProxyEntity>> ents;
QList<std::shared_ptr<ProxyEntity>> ents;
auto ent = status->ent;
auto result = status->result;

Expand Down Expand Up @@ -155,7 +155,7 @@ namespace NekoRay {

// V2Ray

void BuildConfigV2Ray(const QSharedPointer<BuildConfigStatus> &status) {
void BuildConfigV2Ray(const std::shared_ptr<BuildConfigStatus> &status) {
// Log
auto logObj = QJsonObject{{"loglevel", dataStore->log_level}};
status->result->coreConfig.insert("log", logObj);
Expand Down Expand Up @@ -397,8 +397,8 @@ namespace NekoRay {
status->result->coreConfig.insert("stats", QJsonObject());
}

QString BuildChainInternal(int chainId, const QList<QSharedPointer<ProxyEntity>> &ents,
const QSharedPointer<BuildConfigStatus> &status) {
QString BuildChainInternal(int chainId, const QList<std::shared_ptr<ProxyEntity>> &ents,
const std::shared_ptr<BuildConfigStatus> &status) {
QString chainTag = "c-" + Int2String(chainId);
QString chainTagOut;
bool muxApplied = false;
Expand Down Expand Up @@ -550,7 +550,7 @@ namespace NekoRay {
// Outbound

QJsonObject outbound;
auto stream = GetStreamSettings(ent->bean.data());
auto stream = GetStreamSettings(ent->bean.get());

if (thisExternalStat > 0) {
auto extR = ent->bean->BuildExternal(ext_mapping_port, ext_socks_port, thisExternalStat);
Expand All @@ -563,7 +563,7 @@ namespace NekoRay {
return {};
}
extR.tag = ent->bean->DisplayType();
status->result->extRs.emplace_back(std::make_shared<fmt::ExternalBuildResult>(extR));
status->result->extRs.emplace_back(std::make_shared<NekoGui_fmt::ExternalBuildResult>(extR));

// SOCKS OUTBOUND
if (IS_NEKO_BOX) {
Expand Down Expand Up @@ -664,7 +664,7 @@ namespace NekoRay {
// Bypass Lookup for the first profile
auto serverAddress = ent->bean->serverAddress;

auto customBean = dynamic_cast<fmt::CustomBean *>(ent->bean.get());
auto customBean = dynamic_cast<NekoGui_fmt::CustomBean *>(ent->bean.get());
if (customBean != nullptr && customBean->core == "internal") {
auto server = QString2QJsonObject(customBean->config_simple)["server"].toString();
if (!server.isEmpty()) serverAddress = server;
Expand All @@ -685,7 +685,7 @@ namespace NekoRay {

// SingBox

void BuildConfigSingBox(const QSharedPointer<BuildConfigStatus> &status) {
void BuildConfigSingBox(const std::shared_ptr<BuildConfigStatus> &status) {
// Log
status->result->coreConfig["log"] = QJsonObject{{"level", dataStore->log_level}};

Expand Down Expand Up @@ -921,7 +921,7 @@ namespace NekoRay {

// tun user rule
if (IS_NEKO_BOX_INTERNAL_TUN && dataStore->spmode_vpn) {
auto match_out = NekoRay::dataStore->vpn_rule_white ? "proxy" : "bypass";
auto match_out = dataStore->vpn_rule_white ? "proxy" : "bypass";

QString process_name_rule = dataStore->vpn_rule_process.trimmed();
if (!process_name_rule.isEmpty()) {
Expand Down Expand Up @@ -984,10 +984,10 @@ namespace NekoRay {
// experimental
QJsonObject experimentalObj;

if (!status->forTest && NekoRay::dataStore->core_box_clash_api > 0) {
if (!status->forTest && dataStore->core_box_clash_api > 0) {
QJsonObject clash_api = {
{"external_controller", "127.0.0.1:" + Int2String(NekoRay::dataStore->core_box_clash_api)},
{"secret", NekoRay::dataStore->core_box_clash_api_secret},
{"external_controller", "127.0.0.1:" + Int2String(dataStore->core_box_clash_api)},
{"secret", dataStore->core_box_clash_api_secret},
{"external_ui", "dashboard"},
};
experimentalObj["clash_api"] = clash_api;
Expand All @@ -998,8 +998,8 @@ namespace NekoRay {

QString WriteVPNSingBoxConfig() {
// tun user rule
auto match_out = NekoRay::dataStore->vpn_rule_white ? "nekoray-socks" : "direct";
auto no_match_out = NekoRay::dataStore->vpn_rule_white ? "direct" : "nekoray-socks";
auto match_out = dataStore->vpn_rule_white ? "nekoray-socks" : "direct";
auto no_match_out = dataStore->vpn_rule_white ? "direct" : "nekoray-socks";

QString process_name_rule = dataStore->vpn_rule_process.trimmed();
if (!process_name_rule.isEmpty()) {
Expand Down Expand Up @@ -1090,4 +1090,4 @@ namespace NekoRay {
return QFileInfo(file2).absoluteFilePath();
}

} // namespace NekoRay
} // namespace NekoGui
Loading

0 comments on commit c5122b7

Please sign in to comment.