diff --git a/include/dataStore/RouteEntity.h b/include/dataStore/RouteEntity.h index 2e87b40..250b267 100644 --- a/include/dataStore/RouteEntity.h +++ b/include/dataStore/RouteEntity.h @@ -113,6 +113,7 @@ namespace Configs { QList> Rules; QList castedRules; int defaultOutboundID = proxyID; + std::map tagMap; RoutingChain(); @@ -124,7 +125,7 @@ namespace Configs { void FromJson(QJsonObject object); - QJsonArray get_route_rules(bool forView = false, std::map outboundMap = {}, const QStringList& tagList = {}); + QJsonArray get_route_rules(bool forView = false, std::map outboundMap = {}); bool isViewOnly() const; @@ -140,8 +141,6 @@ namespace Configs { std::shared_ptr get_used_rule_sets(); - std::shared_ptr get_used_remote_rule_sets(); - QStringList get_direct_sites(); QStringList get_direct_ips(); diff --git a/src/configs/ConfigBuilder.cpp b/src/configs/ConfigBuilder.cpp index fb1d684..bef03a1 100644 --- a/src/configs/ConfigBuilder.cpp +++ b/src/configs/ConfigBuilder.cpp @@ -612,7 +612,6 @@ namespace Configs { } auto neededOutbounds = routeChain->get_used_outbounds(); auto neededRuleSets = routeChain->get_used_rule_sets(); - auto neededRemoteRuleSets = routeChain->get_used_remote_rule_sets(); std::map outboundMap; outboundMap[-1] = "proxy"; outboundMap[-2] = "direct"; @@ -642,6 +641,8 @@ namespace Configs { needDirectDnsRules = true; } } + auto routeRules = routeChain->get_route_rules(false, outboundMap); + routeObj["rules"] = routeRules; // DNS hijack deps bool needHijackRules = false; @@ -674,50 +675,38 @@ namespace Configs { auto ruleSetArray = QJsonArray(); auto geoSitePath = GetCoreAssetDir("geosite.db"); auto geoIpPath = GetCoreAssetDir("geoip.db"); - QStringList tagList; for (const auto &item: *neededRuleSets) { - tagList.push_back(item); - ruleSetArray += QJsonObject{ - {"type", "local"}, - {"tag", item}, - {"format", "binary"}, - {"path", RULE_SETS_DIR + QString("/%1.srs").arg(item)}, - }; - if (QFile(QString(RULE_SETS_DIR + "/%1.srs").arg(item)).exists()) continue; - bool ok; - auto mode = API::GeoRuleSetType::site; - auto geoAssertPath = geoSitePath; - if (item.contains("_IP")) { - mode = API::GeoRuleSetType::ip; - geoAssertPath = geoIpPath; + if(item.startsWith("https://") && item.endsWith(".srs")) { + ruleSetArray += QJsonObject{ + {"type", "remote"}, + {"tag", routeChain->tagMap[item]}, + {"format", "binary"}, + {"url", item}, + }; } - auto err = API::defaultClient->CompileGeoSet(&ok, mode, item.toStdString(), geoAssertPath); - if (!ok) { - MW_show_log("Failed to generate rule set asset for " + item); - status->result->error = err; - return; + else { + ruleSetArray += QJsonObject{ + {"type", "local"}, + {"tag", item}, + {"format", "binary"}, + {"path", RULE_SETS_DIR + QString("/%1.srs").arg(item)}, + }; + if (QFile(QString(RULE_SETS_DIR + "/%1.srs").arg(item)).exists()) continue; + bool ok; + auto mode = API::GeoRuleSetType::site; + auto geoAssertPath = geoSitePath; + if (item.contains("_IP")) { + mode = API::GeoRuleSetType::ip; + geoAssertPath = geoIpPath; + } + auto err = API::defaultClient->CompileGeoSet(&ok, mode, item.toStdString(), geoAssertPath); + if (!ok) { + MW_show_log("Failed to generate rule set asset for " + item); + status->result->error = err; + return; + } } } - for (const auto &item: *neededRemoteRuleSets) { - QString tagRemote, tmp = item.section('/', -1); - tmp.chop(4); - tagRemote = tmp; - int index = 1; - while(tagList.contains(tagRemote)) - { - tagRemote = tmp + QString::number(index); - index++; - } - tagList.push_back(tagRemote); - ruleSetArray += QJsonObject{ - {"type", "remote"}, - {"tag", tagRemote}, - {"format", "binary"}, - {"url", item}, - }; - } - auto routeRules = routeChain->get_route_rules(false, outboundMap, tagList); - routeObj["rules"] = routeRules; routeObj["rule_set"] = ruleSetArray; // DNS settings diff --git a/src/dataStore/RouteEntity.cpp b/src/dataStore/RouteEntity.cpp index 6c2a473..e286b37 100644 --- a/src/dataStore/RouteEntity.cpp +++ b/src/dataStore/RouteEntity.cpp @@ -564,8 +564,30 @@ namespace Configs { return rules; } - QJsonArray RoutingChain::get_route_rules(bool forView, std::map outboundMap, const QStringList& tagList) { + QJsonArray RoutingChain::get_route_rules(bool forView, std::map outboundMap) { QJsonArray res; + QStringList tagList; + for (const auto& item: Rules) { + for (const auto& ruleItem: item->rule_set) { + if (!ruleItem.startsWith("https://")) { + tagList.push_back(ruleItem); + } + } + } + for (const auto& item: Rules) { + for (const auto& ruleItem: item->rule_set) { + if (ruleItem.startsWith("https://") && ruleItem.endsWith(".srs")) { + QString tagRemote, tmp = ruleItem.section('/', -1); + tmp.chop(4); + tagRemote = tmp; + int index = 1; + while(tagList.contains(tagRemote)) + tagRemote = tmp + QString::number(index++); + tagMap.insert(std::map::value_type(ruleItem, tagRemote)); + tagList.push_back(tagRemote); + } + } + } for (const auto &item: Rules) { auto outboundTag = QString(); if (outboundMap.count(item->outboundID)) outboundTag = outboundMap[item->outboundID]; @@ -690,19 +712,7 @@ namespace Configs { auto res = std::make_shared(); for (const auto& item: Rules) { for (const auto& ruleItem: item->rule_set) { - if (!ruleItem.startsWith("https://")) - res->push_back(ruleItem); - } - } - return res; - } - - std::shared_ptr RoutingChain::get_used_remote_rule_sets() { - auto res = std::make_shared(); - for (const auto& item: Rules) { - for (const auto& ruleItem: item->rule_set) { - if (ruleItem.startsWith("https://") && ruleItem.endsWith(".srs")) - res->push_back(ruleItem); + res->push_back(ruleItem); } } return res;