mirror of
https://github.com/MatsuriDayo/nekoray.git
synced 2025-12-17 12:34:37 +03:00
minor refactor
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
|
||||
#include "db/Database.hpp"
|
||||
#include "db/ConfigBuilder.hpp"
|
||||
#include "db/TrafficLooper.hpp"
|
||||
#include "db/traffic/TrafficLooper.hpp"
|
||||
#include "rpc/gRPC.h"
|
||||
#include "ui/widget/MessageBoxTimer.h"
|
||||
|
||||
@@ -16,11 +16,11 @@
|
||||
|
||||
// ext core
|
||||
|
||||
std::list<QSharedPointer<NekoRay::sys::ExternalProcess>> CreateExtCFromExtR(const std::list<std::shared_ptr<NekoRay::fmt::ExternalBuildResult>> &extRs, bool start) {
|
||||
std::list<std::shared_ptr<NekoGui_sys::ExternalProcess>> CreateExtCFromExtR(const std::list<std::shared_ptr<NekoGui_fmt::ExternalBuildResult>> &extRs, bool start) {
|
||||
// plz run and start in same thread
|
||||
std::list<QSharedPointer<NekoRay::sys::ExternalProcess>> l;
|
||||
std::list<std::shared_ptr<NekoGui_sys::ExternalProcess>> l;
|
||||
for (const auto &extR: extRs) {
|
||||
QSharedPointer<NekoRay::sys::ExternalProcess> extC(new NekoRay::sys::ExternalProcess());
|
||||
std::shared_ptr<NekoGui_sys::ExternalProcess> extC(new NekoGui_sys::ExternalProcess());
|
||||
extC->tag = extR->tag;
|
||||
extC->program = extR->program;
|
||||
extC->arguments = extR->arguments;
|
||||
@@ -35,7 +35,7 @@ std::list<QSharedPointer<NekoRay::sys::ExternalProcess>> CreateExtCFromExtR(cons
|
||||
// grpc
|
||||
|
||||
#ifndef NKR_NO_GRPC
|
||||
using namespace NekoRay::rpc;
|
||||
using namespace NekoGui_rpc;
|
||||
#endif
|
||||
|
||||
void MainWindow::setup_grpc() {
|
||||
@@ -45,10 +45,10 @@ void MainWindow::setup_grpc() {
|
||||
[=](const QString &errStr) {
|
||||
MW_show_log("[Error] gRPC: " + errStr);
|
||||
},
|
||||
"127.0.0.1:" + Int2String(NekoRay::dataStore->core_port), NekoRay::dataStore->core_token);
|
||||
"127.0.0.1:" + Int2String(NekoGui::dataStore->core_port), NekoGui::dataStore->core_token);
|
||||
|
||||
// Looper
|
||||
runOnNewThread([=] { NekoRay::traffic::trafficLooper->Loop(); });
|
||||
runOnNewThread([=] { NekoGui_traffic::trafficLooper->Loop(); });
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ inline bool speedtesting = false;
|
||||
void MainWindow::speedtest_current_group(int mode) {
|
||||
auto profiles = get_selected_or_group();
|
||||
if (profiles.isEmpty()) return;
|
||||
auto group = NekoRay::profileManager->CurrentGroup();
|
||||
auto group = NekoGui::profileManager->CurrentGroup();
|
||||
if (group->archive) return;
|
||||
|
||||
#ifndef NKR_NO_GRPC
|
||||
@@ -82,7 +82,7 @@ void MainWindow::speedtest_current_group(int mode) {
|
||||
runOnNewThread([this, profiles, mode, full_test_flags]() {
|
||||
QMutex lock_write;
|
||||
QMutex lock_return;
|
||||
int threadN = NekoRay::dataStore->test_concurrent;
|
||||
int threadN = NekoGui::dataStore->test_concurrent;
|
||||
int threadN_finished = 0;
|
||||
auto profiles_test = profiles; // copy
|
||||
|
||||
@@ -106,13 +106,13 @@ void MainWindow::speedtest_current_group(int mode) {
|
||||
libcore::TestReq req;
|
||||
req.set_mode((libcore::TestMode) mode);
|
||||
req.set_timeout(3000);
|
||||
req.set_url(NekoRay::dataStore->test_url.toStdString());
|
||||
req.set_url(NekoGui::dataStore->test_url.toStdString());
|
||||
|
||||
//
|
||||
std::list<QSharedPointer<NekoRay::sys::ExternalProcess>> extCs;
|
||||
std::list<std::shared_ptr<NekoGui_sys::ExternalProcess>> extCs;
|
||||
|
||||
if (mode == libcore::TestMode::UrlTest || mode == libcore::FullTest) {
|
||||
auto c = NekoRay::BuildConfig(profile, true, false);
|
||||
auto c = BuildConfig(profile, true, false);
|
||||
// TODO refactor external test
|
||||
if (!c->extRs.empty()) {
|
||||
extCs = CreateExtCFromExtR(c->extRs, true);
|
||||
@@ -177,7 +177,7 @@ void MainWindow::speedtest_current() {
|
||||
libcore::TestReq req;
|
||||
req.set_mode(libcore::UrlTest);
|
||||
req.set_timeout(3000);
|
||||
req.set_url(NekoRay::dataStore->test_url.toStdString());
|
||||
req.set_url(NekoGui::dataStore->test_url.toStdString());
|
||||
|
||||
bool rpcOK;
|
||||
auto result = defaultClient->Test(&rpcOK, req);
|
||||
@@ -202,15 +202,15 @@ void MainWindow::speedtest_current() {
|
||||
|
||||
void MainWindow::stop_core_daemon() {
|
||||
#ifndef NKR_NO_GRPC
|
||||
NekoRay::rpc::defaultClient->Exit();
|
||||
NekoGui_rpc::defaultClient->Exit();
|
||||
#endif
|
||||
}
|
||||
|
||||
void MainWindow::neko_start(int _id) {
|
||||
if (NekoRay::dataStore->prepare_exit) return;
|
||||
if (NekoGui::dataStore->prepare_exit) return;
|
||||
|
||||
auto ents = get_now_selected();
|
||||
auto ent = (_id < 0 && !ents.isEmpty()) ? ents.first() : NekoRay::profileManager->GetProfile(_id);
|
||||
auto ent = (_id < 0 && !ents.isEmpty()) ? ents.first() : NekoGui::profileManager->GetProfile(_id);
|
||||
if (ent == nullptr) return;
|
||||
|
||||
if (select_mode) {
|
||||
@@ -220,10 +220,10 @@ void MainWindow::neko_start(int _id) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto group = NekoRay::profileManager->GetGroup(ent->gid);
|
||||
auto group = NekoGui::profileManager->GetGroup(ent->gid);
|
||||
if (group == nullptr || group->archive) return;
|
||||
|
||||
auto result = NekoRay::BuildConfig(ent, false, false);
|
||||
auto result = BuildConfig(ent, false, false);
|
||||
if (!result->error.isEmpty()) {
|
||||
MessageBoxWarning("BuildConfig return error", result->error);
|
||||
return;
|
||||
@@ -233,8 +233,8 @@ void MainWindow::neko_start(int _id) {
|
||||
#ifndef NKR_NO_GRPC
|
||||
libcore::LoadConfigReq req;
|
||||
req.set_core_config(QJsonObject2QString(result->coreConfig, true).toStdString());
|
||||
req.set_enable_nekoray_connections(NekoRay::dataStore->connection_statistics);
|
||||
if (NekoRay::dataStore->traffic_loop_interval > 0) {
|
||||
req.set_enable_nekoray_connections(NekoGui::dataStore->connection_statistics);
|
||||
if (NekoGui::dataStore->traffic_loop_interval > 0) {
|
||||
req.add_stats_outbounds("proxy");
|
||||
req.add_stats_outbounds("bypass");
|
||||
}
|
||||
@@ -248,20 +248,20 @@ void MainWindow::neko_start(int _id) {
|
||||
return false;
|
||||
}
|
||||
//
|
||||
NekoRay::traffic::trafficLooper->proxy = result->outboundStat.get();
|
||||
NekoRay::traffic::trafficLooper->items = result->outboundStats;
|
||||
NekoRay::dataStore->ignoreConnTag = result->ignoreConnTag;
|
||||
NekoRay::traffic::trafficLooper->loop_enabled = true;
|
||||
NekoGui_traffic::trafficLooper->proxy = result->outboundStat.get();
|
||||
NekoGui_traffic::trafficLooper->items = result->outboundStats;
|
||||
NekoGui::dataStore->ignoreConnTag = result->ignoreConnTag;
|
||||
NekoGui_traffic::trafficLooper->loop_enabled = true;
|
||||
#endif
|
||||
|
||||
runOnUiThread(
|
||||
[=] {
|
||||
auto extCs = CreateExtCFromExtR(result->extRs, true);
|
||||
NekoRay::sys::running_ext.splice(NekoRay::sys::running_ext.end(), extCs);
|
||||
NekoGui_sys::running_ext.splice(NekoGui_sys::running_ext.end(), extCs);
|
||||
},
|
||||
DS_cores);
|
||||
|
||||
NekoRay::dataStore->UpdateStartedId(ent->id);
|
||||
NekoGui::dataStore->UpdateStartedId(ent->id);
|
||||
running = ent;
|
||||
|
||||
runOnUiThread([=] {
|
||||
@@ -284,7 +284,7 @@ void MainWindow::neko_start(int _id) {
|
||||
mu_stopping.unlock();
|
||||
|
||||
// check core state
|
||||
if (!NekoRay::dataStore->core_running) {
|
||||
if (!NekoGui::dataStore->core_running) {
|
||||
runOnUiThread(
|
||||
[=] {
|
||||
show_log_impl("Try to start the config, but the core has not listened to the grpc port, so restart it...");
|
||||
@@ -304,7 +304,7 @@ void MainWindow::neko_start(int _id) {
|
||||
|
||||
runOnNewThread([=] {
|
||||
// stop current running
|
||||
if (NekoRay::dataStore->started_id >= 0) {
|
||||
if (NekoGui::dataStore->started_id >= 0) {
|
||||
runOnUiThread([=] { neko_stop(false, true); });
|
||||
sem_stopped.acquire();
|
||||
}
|
||||
@@ -324,7 +324,7 @@ void MainWindow::neko_start(int _id) {
|
||||
}
|
||||
|
||||
void MainWindow::neko_stop(bool crash, bool sem) {
|
||||
auto id = NekoRay::dataStore->started_id;
|
||||
auto id = NekoGui::dataStore->started_id;
|
||||
if (id < 0) {
|
||||
if (sem) sem_stopped.release();
|
||||
return;
|
||||
@@ -333,25 +333,25 @@ void MainWindow::neko_stop(bool crash, bool sem) {
|
||||
auto neko_stop_stage2 = [=] {
|
||||
runOnUiThread(
|
||||
[=] {
|
||||
while (!NekoRay::sys::running_ext.empty()) {
|
||||
auto extC = NekoRay::sys::running_ext.front();
|
||||
while (!NekoGui_sys::running_ext.empty()) {
|
||||
auto extC = NekoGui_sys::running_ext.front();
|
||||
extC->Kill();
|
||||
NekoRay::sys::running_ext.pop_front();
|
||||
NekoGui_sys::running_ext.pop_front();
|
||||
}
|
||||
},
|
||||
DS_cores);
|
||||
|
||||
#ifndef NKR_NO_GRPC
|
||||
NekoRay::traffic::trafficLooper->loop_enabled = false;
|
||||
NekoRay::traffic::trafficLooper->loop_mutex.lock();
|
||||
if (NekoRay::dataStore->traffic_loop_interval != 0) {
|
||||
NekoRay::traffic::trafficLooper->UpdateAll();
|
||||
for (const auto &item: NekoRay::traffic::trafficLooper->items) {
|
||||
NekoRay::profileManager->GetProfile(item->id)->Save();
|
||||
NekoGui_traffic::trafficLooper->loop_enabled = false;
|
||||
NekoGui_traffic::trafficLooper->loop_mutex.lock();
|
||||
if (NekoGui::dataStore->traffic_loop_interval != 0) {
|
||||
NekoGui_traffic::trafficLooper->UpdateAll();
|
||||
for (const auto &item: NekoGui_traffic::trafficLooper->items) {
|
||||
NekoGui::profileManager->GetProfile(item->id)->Save();
|
||||
runOnUiThread([=] { refresh_proxy_list(item->id); });
|
||||
}
|
||||
}
|
||||
NekoRay::traffic::trafficLooper->loop_mutex.unlock();
|
||||
NekoGui_traffic::trafficLooper->loop_mutex.unlock();
|
||||
|
||||
if (!crash) {
|
||||
bool rpcOK;
|
||||
@@ -365,8 +365,8 @@ void MainWindow::neko_stop(bool crash, bool sem) {
|
||||
}
|
||||
#endif
|
||||
|
||||
NekoRay::dataStore->UpdateStartedId(-1919);
|
||||
NekoRay::dataStore->need_keep_vpn_off = false;
|
||||
NekoGui::dataStore->UpdateStartedId(-1919);
|
||||
NekoGui::dataStore->need_keep_vpn_off = false;
|
||||
running = nullptr;
|
||||
|
||||
runOnUiThread([=] {
|
||||
@@ -411,8 +411,8 @@ void MainWindow::CheckUpdate() {
|
||||
bool ok;
|
||||
libcore::UpdateReq request;
|
||||
request.set_action(libcore::UpdateAction::Check);
|
||||
request.set_check_pre_release(NekoRay::dataStore->check_include_pre);
|
||||
auto response = NekoRay::rpc::defaultClient->Update(&ok, request);
|
||||
request.set_check_pre_release(NekoGui::dataStore->check_include_pre);
|
||||
auto response = NekoGui_rpc::defaultClient->Update(&ok, request);
|
||||
if (!ok) return;
|
||||
|
||||
auto err = response.error();
|
||||
@@ -431,7 +431,7 @@ void MainWindow::CheckUpdate() {
|
||||
}
|
||||
|
||||
runOnUiThread([=] {
|
||||
auto allow_updater = !NekoRay::dataStore->flag_use_appdata;
|
||||
auto allow_updater = !NekoGui::dataStore->flag_use_appdata;
|
||||
auto note_pre_release = response.is_pre_release() ? " (Pre-release)" : "";
|
||||
QMessageBox box(QMessageBox::Question, QObject::tr("Update") + note_pre_release,
|
||||
QObject::tr("Update found: %1\nRelease note:\n%2").arg(response.assets_name().c_str(), response.release_note().c_str()));
|
||||
@@ -450,7 +450,7 @@ void MainWindow::CheckUpdate() {
|
||||
bool ok2;
|
||||
libcore::UpdateReq request2;
|
||||
request2.set_action(libcore::UpdateAction::Download);
|
||||
auto response2 = NekoRay::rpc::defaultClient->Update(&ok2, request2);
|
||||
auto response2 = NekoGui_rpc::defaultClient->Update(&ok2, request2);
|
||||
runOnUiThread([=] {
|
||||
if (response2.error().empty()) {
|
||||
auto q = QMessageBox::question(nullptr, QObject::tr("Update"),
|
||||
|
||||
Reference in New Issue
Block a user