mirror of
https://github.com/MatsuriDayo/nekoray.git
synced 2025-12-17 12:34:37 +03:00
improve switch core
use different datastore for each core
This commit is contained in:
@@ -66,6 +66,7 @@ launcher 参数
|
||||
- [MatsuriDayo/Matsuri](https://github.com/MatsuriDayo/Matsuri)
|
||||
- [MatsuriDayo/v2ray-core](https://github.com/MatsuriDayo/v2ray-core)
|
||||
- [SagerNet/sing-box](https://github.com/SagerNet/sing-box)
|
||||
- [Qv2ray](https://github.com/Qv2ray/Qv2ray)
|
||||
- [Qt](https://www.qt.io/)
|
||||
- [protobuf](https://github.com/protocolbuffers/protobuf)
|
||||
- [yaml-cpp](https://github.com/jbeder/yaml-cpp)
|
||||
|
||||
@@ -724,17 +724,17 @@ namespace NekoRay {
|
||||
});
|
||||
|
||||
// api
|
||||
result->coreConfig.insert("experimental", QJsonObject{
|
||||
{"v2ray_api", QJsonObject{
|
||||
{"listen", "127.0.0.1:" + Int2String(dataStore->inbound_socks_port + 10)},
|
||||
{"stats", QJsonObject{
|
||||
{"enabled", true},
|
||||
{"outbounds", QJsonArray{
|
||||
tagProxy, "bypass", "block"
|
||||
}},
|
||||
}}
|
||||
}},
|
||||
});
|
||||
if (dataStore->traffic_loop_interval > 0) {
|
||||
result->coreConfig.insert("experimental", QJsonObject{
|
||||
{"v2ray_api", QJsonObject{
|
||||
{"listen", "127.0.0.1:" + Int2String(dataStore->inbound_socks_port + 10)},
|
||||
{"stats", QJsonObject{
|
||||
{"enabled", true},
|
||||
{"outbounds", QJsonArray{tagProxy, "bypass"}},
|
||||
}}
|
||||
}},
|
||||
});
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ namespace NekoRay {
|
||||
|
||||
// datastore
|
||||
|
||||
DataStore::DataStore() : JsonStore("groups/nekoray.json") {
|
||||
DataStore::DataStore() : JsonStore() {
|
||||
_add(new configItem("extraCore", dynamic_cast<JsonStore *>(extraCore), itemType::jsonStore));
|
||||
|
||||
_add(new configItem("user_agent", &user_agent, itemType::string));
|
||||
@@ -58,7 +58,6 @@ namespace NekoRay {
|
||||
_add(new configItem("vpn_bypass_process", &vpn_bypass_process, itemType::string));
|
||||
_add(new configItem("vpn_bypass_cidr", &vpn_bypass_cidr, itemType::string));
|
||||
_add(new configItem("check_include_pre", &check_include_pre, itemType::boolean));
|
||||
_add(new configItem("neko_core", &neko_core, itemType::integer));
|
||||
}
|
||||
|
||||
void DataStore::UpdateStartedId(int id) {
|
||||
@@ -110,15 +109,15 @@ namespace NekoRay {
|
||||
QStringList Routing::List() {
|
||||
QStringList l;
|
||||
QDir d;
|
||||
if (d.exists("routes")) {
|
||||
QDir dr("routes");
|
||||
if (d.exists(ROUTES_PREFIX)) {
|
||||
QDir dr(ROUTES_PREFIX);
|
||||
return dr.entryList(QDir::Files);
|
||||
}
|
||||
return l;
|
||||
}
|
||||
|
||||
void Routing::SetToActive(const QString &name) {
|
||||
dataStore->routing->fn = "routes/" + name;
|
||||
dataStore->routing->fn = ROUTES_PREFIX + name;
|
||||
dataStore->routing->Load();
|
||||
dataStore->active_routing = name;
|
||||
dataStore->Save();
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace NekoRay {
|
||||
|
||||
QString FindCoreAsset(const QString& name);
|
||||
QString FindCoreAsset(const QString &name);
|
||||
|
||||
class Routing : public JsonStore {
|
||||
public:
|
||||
@@ -58,8 +58,6 @@ namespace NekoRay {
|
||||
// Saved
|
||||
|
||||
// Misc
|
||||
int neko_core = CoreType::V2RAY;
|
||||
|
||||
QString log_level = "warning";
|
||||
QString user_agent = "Nekoray/1.0 (Prefer Clash Format)";
|
||||
bool sub_use_proxy = false;
|
||||
@@ -129,6 +127,10 @@ namespace NekoRay {
|
||||
|
||||
extern DataStore *dataStore;
|
||||
|
||||
inline int coreType = NekoRay::CoreType::V2RAY;
|
||||
|
||||
}
|
||||
|
||||
#define IS_NEKO_BOX (NekoRay::dataStore->neko_core == NekoRay::CoreType::SING_BOX)
|
||||
#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 + "/" )
|
||||
|
||||
@@ -86,6 +86,9 @@ int main(int argc, char *argv[]) {
|
||||
QIcon::setThemeName("breeze");
|
||||
}
|
||||
|
||||
// Load coreType
|
||||
NekoRay::coreType = ReadFileText("groups/coreType").toInt(); // default to 0
|
||||
|
||||
// Dir
|
||||
QDir dir;
|
||||
bool dir_success = true;
|
||||
@@ -95,8 +98,8 @@ int main(int argc, char *argv[]) {
|
||||
if (!dir.exists("groups")) {
|
||||
dir_success = dir_success && dir.mkdir("groups");
|
||||
}
|
||||
if (!dir.exists("routes")) {
|
||||
dir_success = dir_success && dir.mkdir("routes");
|
||||
if (!dir.exists(ROUTES_PREFIX_NAME)) {
|
||||
dir_success = dir_success && dir.mkdir(ROUTES_PREFIX_NAME);
|
||||
}
|
||||
if (!dir_success) {
|
||||
QMessageBox::warning(nullptr, "Error", "No permission to write " + dir.absolutePath());
|
||||
@@ -104,13 +107,24 @@ int main(int argc, char *argv[]) {
|
||||
}
|
||||
|
||||
// Load dataStore
|
||||
switch (NekoRay::coreType) {
|
||||
case NekoRay::CoreType::V2RAY:
|
||||
NekoRay::dataStore->fn = "groups/nekoray.json";
|
||||
break;
|
||||
case NekoRay::CoreType::SING_BOX:
|
||||
NekoRay::dataStore->fn = "groups/nekobox.json";
|
||||
break;
|
||||
default:
|
||||
MessageBoxWarning("Error", "Unknown coreType.");
|
||||
return 0;
|
||||
}
|
||||
auto isLoaded = NekoRay::dataStore->Load();
|
||||
if (!isLoaded) {
|
||||
NekoRay::dataStore->Save();
|
||||
}
|
||||
|
||||
// load routing
|
||||
NekoRay::dataStore->routing->fn = "routes/" + NekoRay::dataStore->active_routing;
|
||||
NekoRay::dataStore->routing->fn = ROUTES_PREFIX + NekoRay::dataStore->active_routing;
|
||||
isLoaded = NekoRay::dataStore->routing->Load();
|
||||
if (!isLoaded) {
|
||||
NekoRay::dataStore->routing->Save();
|
||||
|
||||
@@ -194,8 +194,11 @@ DialogBasicSettings::DialogBasicSettings(QWidget *parent)
|
||||
tr("Switching the core to %1, click \"Yes\" to complete the switch and the program will restart. This feature may be unstable, please do not switch frequently.")
|
||||
.arg(core_name_new)
|
||||
) == QMessageBox::StandardButton::Yes) {
|
||||
NekoRay::dataStore->neko_core = neko_core_new;
|
||||
NekoRay::dataStore->Save();
|
||||
QFile file;
|
||||
file.setFileName("groups/coreType");
|
||||
file.open(QIODevice::ReadWrite | QIODevice::Truncate);
|
||||
file.write(Int2String(neko_core_new).toUtf8());
|
||||
file.close();
|
||||
dialog_message("", "SwitchCore");
|
||||
}
|
||||
};
|
||||
@@ -268,14 +271,12 @@ void DialogBasicSettings::on_set_custom_icon_clicked() {
|
||||
if (c == 0) {
|
||||
auto fn = QFileDialog::getOpenFileName(this, QObject::tr("Select"), QDir::currentPath(),
|
||||
"*.png", nullptr, QFileDialog::Option::ReadOnly);
|
||||
if (!fn.isEmpty()) {
|
||||
QImage img(fn);
|
||||
if (img.isNull() || img.height() != img.width()) {
|
||||
MessageBoxWarning(title, tr("Please select a valid square image."));
|
||||
return;
|
||||
}
|
||||
QFile::copy(fn, user_icon_path);
|
||||
QImage img(fn);
|
||||
if (img.isNull() || img.height() != img.width()) {
|
||||
MessageBoxWarning(title, tr("Please select a valid square image."));
|
||||
return;
|
||||
}
|
||||
QFile::copy(fn, user_icon_path);
|
||||
} else if (c == 1) {
|
||||
QFile::remove(user_icon_path);
|
||||
} else {
|
||||
|
||||
@@ -101,7 +101,7 @@ void DialogManageRoutes::accept() {
|
||||
if (NekoRay::dataStore->active_routing != active_routing) routeChanged = true;
|
||||
SAVE_TO_ROUTING(NekoRay::dataStore->routing)
|
||||
NekoRay::dataStore->active_routing = active_routing;
|
||||
NekoRay::dataStore->routing->fn = "routes/" + NekoRay::dataStore->active_routing;
|
||||
NekoRay::dataStore->routing->fn = ROUTES_PREFIX + NekoRay::dataStore->active_routing;
|
||||
if (NekoRay::dataStore->routing->Save()) routeChanged = true;
|
||||
//
|
||||
QString info = "UpdateDataStore";
|
||||
@@ -170,7 +170,7 @@ void DialogManageRoutes::on_load_save_clicked() {
|
||||
if (!fn.isEmpty()) {
|
||||
auto r = std::make_unique<NekoRay::Routing>();
|
||||
r->load_control_force = true;
|
||||
r->fn = "routes/" + fn;
|
||||
r->fn = ROUTES_PREFIX + fn;
|
||||
if (r->Load()) {
|
||||
auto btn = QMessageBox::question(nullptr,
|
||||
software_name, tr("Load routing: %1").arg(fn) + "\n" + r->toString());
|
||||
@@ -186,7 +186,7 @@ void DialogManageRoutes::on_load_save_clicked() {
|
||||
if (!fn.isEmpty()) {
|
||||
auto r = std::make_unique<NekoRay::Routing>();
|
||||
SAVE_TO_ROUTING(r)
|
||||
r->fn = "routes/" + fn;
|
||||
r->fn = ROUTES_PREFIX + fn;
|
||||
auto btn = QMessageBox::question(nullptr, software_name,
|
||||
tr("Save routing: %1").arg(fn) + "\n" + r->toString());
|
||||
if (btn == QMessageBox::Yes) {
|
||||
@@ -201,7 +201,7 @@ void DialogManageRoutes::on_load_save_clicked() {
|
||||
if (!fn.isEmpty() && NekoRay::Routing::List().length() > 1) {
|
||||
auto btn = QMessageBox::question(nullptr, software_name, tr("Remove routing: %1").arg(fn));
|
||||
if (btn == QMessageBox::Yes) {
|
||||
QFile f("routes/" + fn);
|
||||
QFile f(ROUTES_PREFIX + fn);
|
||||
f.remove();
|
||||
if (NekoRay::dataStore->active_routing == fn) {
|
||||
NekoRay::Routing::SetToActive(NekoRay::Routing::List().first());
|
||||
|
||||
@@ -299,7 +299,7 @@ MainWindow::MainWindow(QWidget *parent)
|
||||
if (!fn.isEmpty()) {
|
||||
NekoRay::Routing r;
|
||||
r.load_control_force = true;
|
||||
r.fn = "routes/" + fn;
|
||||
r.fn = ROUTES_PREFIX + fn;
|
||||
if (r.Load()) {
|
||||
auto btn = QMessageBox::question(GetMessageBoxParent(), software_name,
|
||||
tr("Load routing and apply: %1").arg(fn) + "\n" + r.toString());
|
||||
|
||||
Reference in New Issue
Block a user