From 0b783d91305a25aaab558addf967afb0a39fac5f Mon Sep 17 00:00:00 2001 From: arm64v8a <48624112+arm64v8a@users.noreply.github.com> Date: Tue, 27 Sep 2022 15:02:31 +0800 Subject: [PATCH] add package compatible --- CMakeLists.txt | 4 ++++ db/ConfigBuilder.cpp | 10 ++++------ examples/docs/Build_Linux.md | 13 +++++++------ go/cmd/nekoray_core/grpc_ray.go | 4 ++-- main/NekoRay.cpp | 20 ++++++++++++++++++++ main/NekoRay_DataStore.hpp | 4 +++- main/main.cpp | 3 +++ sys/ExternalProcess.cpp | 10 ++++------ ui/dialog_manage_routes.cpp | 8 ++------ 9 files changed, 49 insertions(+), 27 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d3c68af..48ab939 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -47,6 +47,10 @@ include("cmake/nkr.cmake") find_package(Threads) +if (NKR_PACKAGE) + add_compile_definitions(NKR_PACKAGE) +endif() + if (NKR_NO_EXTERNAL) add_compile_definitions(NKR_NO_EXTERNAL) else () diff --git a/db/ConfigBuilder.cpp b/db/ConfigBuilder.cpp index 06c1ae4..43ec0cb 100644 --- a/db/ConfigBuilder.cpp +++ b/db/ConfigBuilder.cpp @@ -696,12 +696,10 @@ namespace NekoRay { {"outbound", "dns-out"}}; // geopath - auto geopath = dataStore->v2ray_asset_dir; - if (geopath.isEmpty()) geopath = QApplication::applicationDirPath(); - auto geoip = geopath + "/geoip.db"; - auto geosite = geopath + "/geosite.db"; - if (!QFile::exists(geoip)) result->error = geoip + " not found"; - if (!QFile::exists(geosite)) result->error = geosite + " not found"; + auto geoip = FindCoreAsset("geoip.db"); + auto geosite = FindCoreAsset("geosite.db"); + if (geoip.isEmpty()) result->error = geoip + " not found"; + if (geosite.isEmpty()) result->error = geosite + " not found"; // final add routing rule QJSONARRAY_ADD(routingRules, QString2QJsonObject(dataStore->custom_route_global)["rules"].toArray()) diff --git a/examples/docs/Build_Linux.md b/examples/docs/Build_Linux.md index cd5551e..8fe5512 100644 --- a/examples/docs/Build_Linux.md +++ b/examples/docs/Build_Linux.md @@ -30,12 +30,13 @@ ninja #### CMake 参数 -| CMake 参数 | 默认值 | 含义 | -|-------------------------|-----|-------------------------| -| QT_VERSION_MAJOR | 5 | QT版本 | -| NKR_NO_EXTERNAL | | 不包含外部C++依赖(如ZXing/gRPC) | -| NKR_NO_GRPC | | 不包含gRPC | -| NKR_CROSS | | | +| CMake 参数 | 默认值 | 含义 | +|------------------|-----|-------------------------| +| QT_VERSION_MAJOR | 5 | QT版本 | +| NKR_NO_EXTERNAL | | 不包含外部C++依赖(如ZXing/gRPC) | +| NKR_NO_GRPC | | 不包含gRPC | +| NKR_CROSS | | | +| NKR_PACKAGE | | 打包 | #### C++ 部分 diff --git a/go/cmd/nekoray_core/grpc_ray.go b/go/cmd/nekoray_core/grpc_ray.go index 95ede17..015e2a3 100644 --- a/go/cmd/nekoray_core/grpc_ray.go +++ b/go/cmd/nekoray_core/grpc_ray.go @@ -122,7 +122,7 @@ func (s *server) Test(ctx context.Context, in *gen.TestReq) (out *gen.TestResp, // Latency var t int32 - t, err = speedtest.UrlTest(getProxyHttpClient(i), in.Address, in.Timeout) + t, err = speedtest.UrlTest(getProxyHttpClient(i), in.Url, in.Timeout) out.Ms = t // sn: ms==0 是错误 } else if in.Mode == gen.TestMode_TcpPing { out.Ms, err = speedtest.TcpPing(in.Address, in.Timeout) @@ -149,7 +149,7 @@ func (s *server) Test(ctx context.Context, in *gen.TestReq) (out *gen.TestResp, // Latency var latency string if in.FullLatency { - t, _ := speedtest.UrlTest(getProxyHttpClient(i), in.Address, in.Timeout) + t, _ := speedtest.UrlTest(getProxyHttpClient(i), in.Url, in.Timeout) out.Ms = t if t > 0 { latency = fmt.Sprint(t, "ms") diff --git a/main/NekoRay.cpp b/main/NekoRay.cpp index 91b0372..d782c12 100644 --- a/main/NekoRay.cpp +++ b/main/NekoRay.cpp @@ -2,6 +2,8 @@ #include #include +#include +#include namespace NekoRay { @@ -324,4 +326,22 @@ namespace NekoRay { return ok; } + // + + QString FindCoreAsset(const QString &name) { + QStringList search{dataStore->v2ray_asset_dir}; + search << QApplication::applicationDirPath(); + search << "/usr/share/v2ray"; + search << "/usr/local/share/v2ray"; + search << "/opt/v2ray"; + for (const auto &dir: search) { + if (dir.isEmpty()) continue; + QFileInfo asset(dir + "/" + name); + if (asset.exists()) { + return asset.absoluteFilePath(); + } + } + return {}; + } + } diff --git a/main/NekoRay_DataStore.hpp b/main/NekoRay_DataStore.hpp index 32ddfe1..2ea7b3f 100644 --- a/main/NekoRay_DataStore.hpp +++ b/main/NekoRay_DataStore.hpp @@ -2,6 +2,8 @@ namespace NekoRay { + QString FindCoreAsset(const QString& name); + class Routing : public JsonStore { public: QString direct_ip; @@ -14,7 +16,7 @@ namespace NekoRay { explicit Routing(int preset = 0); - QString toString() const; + [[nodiscard]] QString toString() const; static QStringList List(); diff --git a/main/main.cpp b/main/main.cpp index 1f28e5c..594833c 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -50,6 +50,9 @@ int main(int argc, char *argv[]) { auto args = QApplication::arguments(); if (args.contains("-many")) NekoRay::dataStore->flag_many = true; if (args.contains("-appdata")) NekoRay::dataStore->flag_use_appdata = true; +#ifdef NKR_PACKAGE + NekoRay::dataStore->flag_use_appdata = true; +#endif // dirs & clean auto wd = QDir(QApplication::applicationDirPath()); diff --git a/sys/ExternalProcess.cpp b/sys/ExternalProcess.cpp index b1bca3a..c413d08 100644 --- a/sys/ExternalProcess.cpp +++ b/sys/ExternalProcess.cpp @@ -109,13 +109,11 @@ namespace NekoRay::sys { void CoreProcess::Start() { show_stderr = false; - auto v2ray_asset_dir = dataStore->v2ray_asset_dir; - if (v2ray_asset_dir.isEmpty() || QDir(v2ray_asset_dir).exists()) { - v2ray_asset_dir = QApplication::applicationDirPath(); + auto v2ray_asset_dir = FindCoreAsset("geoip.dat"); + if (!v2ray_asset_dir.isEmpty()) { + v2ray_asset_dir = QFileInfo(v2ray_asset_dir).absolutePath(); + env = QStringList{"V2RAY_LOCATION_ASSET=" + v2ray_asset_dir}; } - env = QStringList{ - "V2RAY_LOCATION_ASSET=" + v2ray_asset_dir - }; ExternalProcess::Start(); write((dataStore->core_token + "\n").toUtf8()); } diff --git a/ui/dialog_manage_routes.cpp b/ui/dialog_manage_routes.cpp index 15d15fc..6646e85 100644 --- a/ui/dialog_manage_routes.cpp +++ b/ui/dialog_manage_routes.cpp @@ -58,12 +58,8 @@ DialogManageRoutes::DialogManageRoutes(QWidget *parent) : builtInSchemesMenu->addActions(this->getBuiltInSchemes()); ui->preset->setMenu(builtInSchemesMenu); - QString geoipFn = QApplication::applicationDirPath() + "/geoip.dat"; - QString geositeFn = QApplication::applicationDirPath() + +"/geosite.dat"; - if (!NekoRay::dataStore->v2ray_asset_dir.isEmpty()) { - geoipFn = NekoRay::dataStore->v2ray_asset_dir + "/geoip.dat"; - geositeFn = NekoRay::dataStore->v2ray_asset_dir + "/geosite.dat"; - } + QString geoipFn = NekoRay::FindCoreAsset("geoip.dat"); + QString geositeFn = NekoRay::FindCoreAsset("geosite.dat"); // const auto sourceStringsDomain = Qv2ray::components::GeositeReader::ReadGeoSiteFromFile(geoipFn); directDomainTxt = new AutoCompleteTextEdit("geosite", sourceStringsDomain, this);