mirror of
https://github.com/MatsuriDayo/nekoray.git
synced 2025-12-17 20:44:38 +03:00
optmize code
This commit is contained in:
@@ -34,14 +34,15 @@ include("cmake/fuck_windows/fuck.cmake")
|
|||||||
|
|
||||||
#### default prefix path ####
|
#### default prefix path ####
|
||||||
|
|
||||||
if (NKR_PACKAGE)
|
if (NOT NKR_LIBS)
|
||||||
|
if (NKR_PACKAGE)
|
||||||
list(APPEND NKR_LIBS ${CMAKE_SOURCE_DIR}/libs/deps/package)
|
list(APPEND NKR_LIBS ${CMAKE_SOURCE_DIR}/libs/deps/package)
|
||||||
else ()
|
else ()
|
||||||
list(APPEND NKR_LIBS ${CMAKE_SOURCE_DIR}/libs/deps/built)
|
list(APPEND NKR_LIBS ${CMAKE_SOURCE_DIR}/libs/deps/built)
|
||||||
|
endif ()
|
||||||
endif ()
|
endif ()
|
||||||
|
|
||||||
if (NKR_DISABLE_LIBS)
|
if (NOT NKR_DISABLE_LIBS)
|
||||||
else ()
|
|
||||||
list(APPEND CMAKE_PREFIX_PATH ${NKR_LIBS})
|
list(APPEND CMAKE_PREFIX_PATH ${NKR_LIBS})
|
||||||
endif ()
|
endif ()
|
||||||
|
|
||||||
|
|||||||
@@ -10,19 +10,21 @@ namespace NekoRay {
|
|||||||
ProfileManager *profileManager = new ProfileManager();
|
ProfileManager *profileManager = new ProfileManager();
|
||||||
|
|
||||||
ProfileManager::ProfileManager() : JsonStore("groups/pm.json") {
|
ProfileManager::ProfileManager() : JsonStore("groups/pm.json") {
|
||||||
_hooks_after_load.push_back([=]() { LoadManager(); });
|
callback_after_load = [this]() { LoadManager(); };
|
||||||
_hooks_before_save.push_back([=]() { SaveManager(); });
|
callback_before_save = [this]() { SaveManager(); };
|
||||||
_add(new configItem("profiles", &_profiles, itemType::integerList));
|
_add(new configItem("profiles", &_profiles, itemType::integerList));
|
||||||
_add(new configItem("groups", &_groups, itemType::integerList));
|
_add(new configItem("groups", &_groups, itemType::integerList));
|
||||||
}
|
}
|
||||||
|
|
||||||
void ProfileManager::LoadManager() {
|
void ProfileManager::LoadManager() {
|
||||||
QList<int> invaild_profile_id;
|
profiles = {};
|
||||||
|
groups = {};
|
||||||
|
QList<int> invalidProfileId;
|
||||||
for (auto id: _profiles) {
|
for (auto id: _profiles) {
|
||||||
auto ent = LoadProxyEntity(QString("profiles/%1.json").arg(id));
|
auto ent = LoadProxyEntity(QString("profiles/%1.json").arg(id));
|
||||||
if (ent == nullptr || ent->bean == nullptr || ent->bean->version == -114514) {
|
if (ent == nullptr || ent->bean == nullptr || ent->bean->version == -114514) {
|
||||||
// clear invaild profile
|
// clear invalid profile
|
||||||
invaild_profile_id << id;
|
invalidProfileId << id;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
profiles[id] = ent;
|
profiles[id] = ent;
|
||||||
@@ -30,7 +32,7 @@ namespace NekoRay {
|
|||||||
for (auto id: _groups) {
|
for (auto id: _groups) {
|
||||||
groups[id] = LoadGroup(QString("groups/%1.json").arg(id));
|
groups[id] = LoadGroup(QString("groups/%1.json").arg(id));
|
||||||
}
|
}
|
||||||
for (auto id: invaild_profile_id) {
|
for (auto id: invalidProfileId) {
|
||||||
DeleteProfile(id);
|
DeleteProfile(id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -55,8 +57,7 @@ namespace NekoRay {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (validType) {
|
if (validType) {
|
||||||
// 加载前设置好 fn
|
ent->load_control_must = true;
|
||||||
ent->load_control_force = true;
|
|
||||||
ent->fn = jsonPath;
|
ent->fn = jsonPath;
|
||||||
ent->Load();
|
ent->Load();
|
||||||
}
|
}
|
||||||
@@ -101,8 +102,9 @@ namespace NekoRay {
|
|||||||
|
|
||||||
// ProxyEntity
|
// ProxyEntity
|
||||||
|
|
||||||
ProxyEntity::ProxyEntity(fmt::AbstractBean *bean, QString _type) {
|
ProxyEntity::ProxyEntity(fmt::AbstractBean *bean, const QString &type_) {
|
||||||
type = std::move(_type);
|
if (type_ != nullptr) this->type = type_;
|
||||||
|
|
||||||
_add(new configItem("type", &type, itemType::string));
|
_add(new configItem("type", &type, itemType::string));
|
||||||
_add(new configItem("id", &id, itemType::integer));
|
_add(new configItem("id", &id, itemType::integer));
|
||||||
_add(new configItem("gid", &gid, itemType::integer));
|
_add(new configItem("gid", &gid, itemType::integer));
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
namespace NekoRay {
|
namespace NekoRay {
|
||||||
class ProfileManager : public JsonStore {
|
class ProfileManager : public JsonStore {
|
||||||
public:
|
public:
|
||||||
|
// Manager
|
||||||
QMap<int, QSharedPointer<ProxyEntity>> profiles;
|
QMap<int, QSharedPointer<ProxyEntity>> profiles;
|
||||||
QMap<int, QSharedPointer<Group>> groups;
|
QMap<int, QSharedPointer<Group>> groups;
|
||||||
|
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ namespace NekoRay {
|
|||||||
// Cache
|
// Cache
|
||||||
QString full_test_report;
|
QString full_test_report;
|
||||||
|
|
||||||
ProxyEntity(fmt::AbstractBean *bean, QString _type);
|
ProxyEntity(fmt::AbstractBean *bean, const QString &type_);
|
||||||
|
|
||||||
[[nodiscard]] QString DisplayLatency() const;
|
[[nodiscard]] QString DisplayLatency() const;
|
||||||
|
|
||||||
|
|||||||
@@ -109,7 +109,7 @@ namespace NekoRay {
|
|||||||
_add(new configItem("custom", &this->custom, itemType::string));
|
_add(new configItem("custom", &this->custom, itemType::string));
|
||||||
}
|
}
|
||||||
|
|
||||||
QString Routing::toString() const {
|
QString Routing::DisplayRouting() const {
|
||||||
return QString("[Proxy] %1\n[Proxy] %2\n[Direct] %3\n[Direct] %4\n[Block] %5\n[Block] %6")
|
return QString("[Proxy] %1\n[Proxy] %2\n[Direct] %3\n[Direct] %4\n[Block] %5\n[Block] %6")
|
||||||
.arg(SplitLines(proxy_domain).join(","))
|
.arg(SplitLines(proxy_domain).join(","))
|
||||||
.arg(SplitLines(proxy_ip).join(","))
|
.arg(SplitLines(proxy_ip).join(","))
|
||||||
@@ -219,9 +219,6 @@ namespace NekoRay {
|
|||||||
void JsonStore::FromJson(QJsonObject object) {
|
void JsonStore::FromJson(QJsonObject object) {
|
||||||
for (const auto &key: object.keys()) {
|
for (const auto &key: object.keys()) {
|
||||||
if (_map.count(key) == 0) {
|
if (_map.count(key) == 0) {
|
||||||
if (debug_verbose) {
|
|
||||||
qDebug() << QString("unknown key\n%1\n%2").arg(key, QJsonObject2QString(object, false));
|
|
||||||
}
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -273,16 +270,12 @@ namespace NekoRay {
|
|||||||
if (value.type() != QJsonValue::Object) {
|
if (value.type() != QJsonValue::Object) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (load_control_no_jsonStore)
|
|
||||||
continue;
|
|
||||||
((JsonStore *) item->ptr)->FromJson(value.toObject());
|
((JsonStore *) item->ptr)->FromJson(value.toObject());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const auto &hook: _hooks_after_load) {
|
if (callback_after_load != nullptr) callback_after_load();
|
||||||
hook();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void JsonStore::FromJsonBytes(const QByteArray &data) {
|
void JsonStore::FromJsonBytes(const QByteArray &data) {
|
||||||
@@ -290,7 +283,7 @@ namespace NekoRay {
|
|||||||
auto document = QJsonDocument::fromJson(data, &error);
|
auto document = QJsonDocument::fromJson(data, &error);
|
||||||
|
|
||||||
if (error.error != error.NoError) {
|
if (error.error != error.NoError) {
|
||||||
if (debug_verbose) qDebug() << "QJsonParseError" << error.errorString();
|
qDebug() << "QJsonParseError" << error.errorString();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -298,9 +291,7 @@ namespace NekoRay {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool JsonStore::Save() {
|
bool JsonStore::Save() {
|
||||||
for (const auto &hook: _hooks_before_save) {
|
if (callback_before_save != nullptr) callback_before_save();
|
||||||
hook();
|
|
||||||
}
|
|
||||||
|
|
||||||
auto save_content = ToJsonBytes();
|
auto save_content = ToJsonBytes();
|
||||||
auto changed = last_save_content != save_content;
|
auto changed = last_save_content != save_content;
|
||||||
@@ -319,8 +310,9 @@ namespace NekoRay {
|
|||||||
QFile file;
|
QFile file;
|
||||||
file.setFileName(fn);
|
file.setFileName(fn);
|
||||||
|
|
||||||
if (!file.exists() && !load_control_force)
|
if (!file.exists() && !load_control_must) {
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
bool ok = file.open(QIODevice::ReadOnly);
|
bool ok = file.open(QIODevice::ReadOnly);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
|
|||||||
@@ -4,3 +4,15 @@
|
|||||||
#include "NekoRay_Utils.hpp"
|
#include "NekoRay_Utils.hpp"
|
||||||
#include "NekoRay_ConfigItem.hpp"
|
#include "NekoRay_ConfigItem.hpp"
|
||||||
#include "NekoRay_DataStore.hpp"
|
#include "NekoRay_DataStore.hpp"
|
||||||
|
|
||||||
|
// Switch core support
|
||||||
|
|
||||||
|
namespace NekoRay {
|
||||||
|
inline int coreType = NekoRay::CoreType::V2RAY;
|
||||||
|
|
||||||
|
QString FindCoreAsset(const QString &name);
|
||||||
|
} // namespace NekoRay
|
||||||
|
|
||||||
|
#define IS_NEKO_BOX (NekoRay::coreType == NekoRay::CoreType::SING_BOX)
|
||||||
|
#define ROUTES_PREFIX_NAME QString(IS_NEKO_BOX ? "routes_box" : "routes")
|
||||||
|
#define ROUTES_PREFIX QString(ROUTES_PREFIX_NAME + "/")
|
||||||
|
|||||||
@@ -29,12 +29,12 @@ namespace NekoRay {
|
|||||||
class JsonStore {
|
class JsonStore {
|
||||||
public:
|
public:
|
||||||
QMap<QString, QSharedPointer<configItem>> _map;
|
QMap<QString, QSharedPointer<configItem>> _map;
|
||||||
QList<std::function<void()>> _hooks_after_load;
|
|
||||||
QList<std::function<void()>> _hooks_before_save;
|
std::function<void()> callback_after_load = nullptr;
|
||||||
|
std::function<void()> callback_before_save = nullptr;
|
||||||
|
|
||||||
QString fn;
|
QString fn;
|
||||||
bool debug_verbose = false;
|
bool load_control_must = false; // must load from file
|
||||||
bool load_control_force = false;
|
|
||||||
bool load_control_no_jsonStore = false; //不加载 json object
|
|
||||||
bool save_control_compact = false;
|
bool save_control_compact = false;
|
||||||
QByteArray last_save_content;
|
QByteArray last_save_content;
|
||||||
|
|
||||||
|
|||||||
@@ -2,8 +2,6 @@
|
|||||||
|
|
||||||
namespace NekoRay {
|
namespace NekoRay {
|
||||||
|
|
||||||
QString FindCoreAsset(const QString &name);
|
|
||||||
|
|
||||||
class Routing : public JsonStore {
|
class Routing : public JsonStore {
|
||||||
public:
|
public:
|
||||||
QString direct_ip;
|
QString direct_ip;
|
||||||
@@ -16,7 +14,7 @@ namespace NekoRay {
|
|||||||
|
|
||||||
explicit Routing(int preset = 0);
|
explicit Routing(int preset = 0);
|
||||||
|
|
||||||
[[nodiscard]] QString toString() const;
|
[[nodiscard]] QString DisplayRouting() const;
|
||||||
|
|
||||||
static QStringList List();
|
static QStringList List();
|
||||||
|
|
||||||
@@ -134,6 +132,8 @@ namespace NekoRay {
|
|||||||
// Other Core
|
// Other Core
|
||||||
ExtraCore *extraCore = new ExtraCore;
|
ExtraCore *extraCore = new ExtraCore;
|
||||||
|
|
||||||
|
// Methods
|
||||||
|
|
||||||
DataStore();
|
DataStore();
|
||||||
|
|
||||||
void UpdateStartedId(int id);
|
void UpdateStartedId(int id);
|
||||||
@@ -141,10 +141,4 @@ namespace NekoRay {
|
|||||||
|
|
||||||
extern DataStore *dataStore;
|
extern DataStore *dataStore;
|
||||||
|
|
||||||
inline int coreType = NekoRay::CoreType::V2RAY;
|
|
||||||
|
|
||||||
} // namespace NekoRay
|
} // namespace NekoRay
|
||||||
|
|
||||||
#define IS_NEKO_BOX (NekoRay::coreType == NekoRay::CoreType::SING_BOX)
|
|
||||||
#define ROUTES_PREFIX_NAME QString(IS_NEKO_BOX ? "routes_box" : "routes")
|
|
||||||
#define ROUTES_PREFIX QString(ROUTES_PREFIX_NAME + "/")
|
|
||||||
|
|||||||
@@ -414,8 +414,8 @@
|
|||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item>
|
<item>
|
||||||
<widget class="QGroupBox" name="horizontalGroupBox_2">
|
<widget class="QGroupBox" name="horizontalGroupBox_5">
|
||||||
<layout class="QHBoxLayout" name="horizontalLayout_23">
|
<layout class="QHBoxLayout" name="horizontalLayout_6">
|
||||||
<item>
|
<item>
|
||||||
<widget class="QCheckBox" name="start_minimal">
|
<widget class="QCheckBox" name="start_minimal">
|
||||||
<property name="sizePolicy">
|
<property name="sizePolicy">
|
||||||
|
|||||||
@@ -167,10 +167,10 @@ void DialogManageRoutes::on_load_save_clicked() {
|
|||||||
auto fn = lineEdit->text();
|
auto fn = lineEdit->text();
|
||||||
if (!fn.isEmpty()) {
|
if (!fn.isEmpty()) {
|
||||||
auto r = std::make_unique<NekoRay::Routing>();
|
auto r = std::make_unique<NekoRay::Routing>();
|
||||||
r->load_control_force = true;
|
r->load_control_must = true;
|
||||||
r->fn = ROUTES_PREFIX + fn;
|
r->fn = ROUTES_PREFIX + fn;
|
||||||
if (r->Load()) {
|
if (r->Load()) {
|
||||||
if (QMessageBox::question(nullptr, software_name, tr("Load routing: %1").arg(fn) + "\n" + r->toString()) == QMessageBox::Yes) {
|
if (QMessageBox::question(nullptr, software_name, tr("Load routing: %1").arg(fn) + "\n" + r->DisplayRouting()) == QMessageBox::Yes) {
|
||||||
REFRESH_ACTIVE_ROUTING(fn, r)
|
REFRESH_ACTIVE_ROUTING(fn, r)
|
||||||
w->accept();
|
w->accept();
|
||||||
}
|
}
|
||||||
@@ -183,7 +183,7 @@ void DialogManageRoutes::on_load_save_clicked() {
|
|||||||
auto r = std::make_unique<NekoRay::Routing>();
|
auto r = std::make_unique<NekoRay::Routing>();
|
||||||
SAVE_TO_ROUTING(r)
|
SAVE_TO_ROUTING(r)
|
||||||
r->fn = ROUTES_PREFIX + fn;
|
r->fn = ROUTES_PREFIX + fn;
|
||||||
if (QMessageBox::question(nullptr, software_name, tr("Save routing: %1").arg(fn) + "\n" + r->toString()) == QMessageBox::Yes) {
|
if (QMessageBox::question(nullptr, software_name, tr("Save routing: %1").arg(fn) + "\n" + r->DisplayRouting()) == QMessageBox::Yes) {
|
||||||
r->Save();
|
r->Save();
|
||||||
REFRESH_ACTIVE_ROUTING(fn, r)
|
REFRESH_ACTIVE_ROUTING(fn, r)
|
||||||
w->accept();
|
w->accept();
|
||||||
|
|||||||
@@ -290,10 +290,10 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi
|
|||||||
auto fn = a->text();
|
auto fn = a->text();
|
||||||
if (!fn.isEmpty()) {
|
if (!fn.isEmpty()) {
|
||||||
NekoRay::Routing r;
|
NekoRay::Routing r;
|
||||||
r.load_control_force = true;
|
r.load_control_must = true;
|
||||||
r.fn = ROUTES_PREFIX + fn;
|
r.fn = ROUTES_PREFIX + fn;
|
||||||
if (r.Load()) {
|
if (r.Load()) {
|
||||||
if (QMessageBox::question(GetMessageBoxParent(), software_name, tr("Load routing and apply: %1").arg(fn) + "\n" + r.toString()) == QMessageBox::Yes) {
|
if (QMessageBox::question(GetMessageBoxParent(), software_name, tr("Load routing and apply: %1").arg(fn) + "\n" + r.DisplayRouting()) == QMessageBox::Yes) {
|
||||||
NekoRay::Routing::SetToActive(fn);
|
NekoRay::Routing::SetToActive(fn);
|
||||||
if (NekoRay::dataStore->started_id >= 0) {
|
if (NekoRay::dataStore->started_id >= 0) {
|
||||||
neko_start(NekoRay::dataStore->started_id);
|
neko_start(NekoRay::dataStore->started_id);
|
||||||
@@ -965,7 +965,8 @@ void MainWindow::on_menu_profile_debug_info_triggered() {
|
|||||||
if (btn == 1) {
|
if (btn == 1) {
|
||||||
QDesktopServices::openUrl(QUrl::fromLocalFile(QFileInfo(QString("profiles/%1.json").arg(ents.first()->id)).absoluteFilePath()));
|
QDesktopServices::openUrl(QUrl::fromLocalFile(QFileInfo(QString("profiles/%1.json").arg(ents.first()->id)).absoluteFilePath()));
|
||||||
} else if (btn == 2) {
|
} else if (btn == 2) {
|
||||||
ents.first()->Load();
|
NekoRay::dataStore->Load();
|
||||||
|
NekoRay::profileManager->Load();
|
||||||
refresh_proxy_list();
|
refresh_proxy_list();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user