refactor: refactored route generation, WARN: NOT TESTED

This commit is contained in:
QwQ 2020-08-03 17:15:26 +08:00
parent 13052b5e99
commit c017401840
No known key found for this signature in database
GPG Key ID: E7FAEFAFCD031D4B
8 changed files with 39 additions and 55 deletions

View File

@ -1 +1 @@
5849
5850

View File

@ -121,7 +121,6 @@ namespace Qv2ray::base::objects
{
// Added due to the request of @aliyuchang33
bool QV2RAY_RULE_ENABLED;
bool QV2RAY_RULE_USE_BALANCER;
QString QV2RAY_RULE_TAG;
//
QString type;
@ -137,10 +136,10 @@ namespace Qv2ray::base::objects
QString outboundTag;
QString balancerTag;
RuleObject()
: QV2RAY_RULE_ENABLED(true), QV2RAY_RULE_USE_BALANCER(false), QV2RAY_RULE_TAG("new rule"), type("field"), domain(), ip(),
port("1-65535"), network(""), source(), user(), inboundTag(), protocol(), attrs(), outboundTag(""), balancerTag(""){};
JSONSTRUCT_REGISTER(RuleObject, F(QV2RAY_RULE_ENABLED, QV2RAY_RULE_USE_BALANCER, QV2RAY_RULE_TAG, type, domain, ip, port, network,
source, user, inboundTag, protocol, attrs, outboundTag, balancerTag))
: QV2RAY_RULE_ENABLED(true), QV2RAY_RULE_TAG("new rule"), type("field"), domain(), ip(), port("1-65535"), network(""), source(),
user(), inboundTag(), protocol(), attrs(), outboundTag(""), balancerTag(""){};
JSONSTRUCT_REGISTER(RuleObject, F(QV2RAY_RULE_ENABLED, QV2RAY_RULE_TAG, type, domain, ip, port, network, source, user, inboundTag,
protocol, attrs, outboundTag, balancerTag))
};
//
//

View File

@ -67,8 +67,10 @@ namespace Qv2ray::core::connection::generation::filters
{
for (auto i = 0; i < root["outbounds"].toArray().count(); i++)
{
bool isEmptySeed = QJsonIO::GetValue(root, "outbounds", i, "streamSettings", "kcpSettings", "seed").toString().isEmpty();
if (isEmptySeed)
const auto seedItem = QJsonIO::GetValue(root, "outbounds", i, "streamSettings", "kcpSettings", "seed");
bool shouldProcess = !seedItem.isNull() && !seedItem.isUndefined();
bool isEmptySeed = seedItem.toString().isEmpty();
if (shouldProcess && isEmptySeed)
QJsonIO::SetValue(root, QJsonIO::Undefined, "outbounds", i, "streamSettings", "kcpSettings", "seed");
}
}
@ -79,8 +81,8 @@ namespace Qv2ray::core::connection::generation::filters
{
if (QJsonIO::GetValue(root, subKey, i, "tag").toString().isEmpty())
{
LOG(MODULE_SETTINGS, "Adding a tag to an inbound.")
const auto tag = GenerateRandomString(8);
LOG(MODULE_SETTINGS, "Adding tag " + tag + " to inbound.")
QJsonIO::SetValue(root, tag, subKey, i, "tag");
}
}

View File

@ -136,9 +136,9 @@ namespace Qv2ray::core::handler
// logObject.insert("error", QV2RAY_CONFIG_PATH + QV2RAY_VCORE_LOG_DIRNAME + QV2RAY_VCORE_ERROR_LOG_FILENAME);
QJsonIO::SetValue(root, V2RayLogLevel[GlobalConfig.logLevel], "log", "loglevel");
//
// Since Qv2ray does not support settings DNS manually for now.
// These settings are being added for both complex config AND simple config.
if (root.contains("dns") && !root.value("dns").toObject().isEmpty())
// Process DNS
const auto hasDNS = root.contains("dns") && !root.value("dns").toObject().isEmpty();
if (hasDNS)
{
// We assume the users are using THEIR DNS settings.
LOG(MODULE_CONNECTION, "Found DNS settings specified manually, skipping inserting GlobalConfig")
@ -156,8 +156,8 @@ namespace Qv2ray::core::handler
{
#define INCONF GlobalConfig.inboundConfig
INBOUNDS inboundsList;
const QJsonObject sniffingOff{ { "enabled", false } };
const QJsonObject sniffingOn{ { "enabled", true }, { "destOverride", QJsonArray{ "http", "tls" } } };
const static QJsonObject sniffingOff{ { "enabled", false } };
const static QJsonObject sniffingOn{ { "enabled", true }, { "destOverride", QJsonArray{ "http", "tls" } } };
// HTTP Inbound
if (GlobalConfig.inboundConfig.useHTTP)
@ -261,12 +261,13 @@ namespace Qv2ray::core::handler
for (const auto &_rule : routing["rules"].toArray())
{
auto _b = _rule.toObject();
auto rule = _rule.toObject();
if (_b.contains("QV2RAY_RULE_USE_BALANCER"))
// For compatibility
if (rule.contains("QV2RAY_RULE_USE_BALANCER"))
{
// We use balancer, or the normal outbound
_b.remove(_b["QV2RAY_RULE_USE_BALANCER"].toBool(false) ? "outboundTag" : "balancerTag");
rule.remove(rule["QV2RAY_RULE_USE_BALANCER"].toBool(false) ? "outboundTag" : "balancerTag");
}
else
{
@ -274,13 +275,13 @@ namespace Qv2ray::core::handler
}
// If this entry has been disabled.
if (_b.contains("QV2RAY_RULE_ENABLED") && _b["QV2RAY_RULE_ENABLED"].toBool() == false)
if (rule.contains("QV2RAY_RULE_ENABLED") && rule["QV2RAY_RULE_ENABLED"].toBool() == false)
{
LOG(MODULE_SETTINGS, "Discarded a rule as it's been set DISABLED")
}
else
{
rules.append(_b);
rules.append(rule);
}
}
@ -297,7 +298,7 @@ namespace Qv2ray::core::handler
LOG(MODULE_CONNECTION, "WARN: --> The config file has NO routing section, however more than 1 outbounds are detected.")
}
//
auto tag = getTag(OUTBOUND(QJsonIO::GetValue(root, "outbounds", 0).toObject()));
auto tag = QJsonIO::GetValue(root, "outbounds", 0, "tag").toString();
if (tag.isEmpty())
{
LOG(MODULE_CONNECTION, "Applying workaround when an outbound tag is empty")
@ -329,11 +330,11 @@ namespace Qv2ray::core::handler
}
else if (!fpConf.type.isEmpty())
{
DEBUG(MODULE_CONNECTION, "WARNING: Unsupported outbound type: " + fpConf.type)
DEBUG(MODULE_CONNECTION, "WARNING: Unsupported forward proxy type: " + fpConf.type)
}
else
{
DEBUG(MODULE_CONNECTION, "WARNING: Empty outbound type.")
DEBUG(MODULE_CONNECTION, "WARNING: Empty forward proxy type.")
}
}
else
@ -361,7 +362,7 @@ namespace Qv2ray::core::handler
{
const auto hasTProxy = GlobalConfig.inboundConfig.useTPROXY && GlobalConfig.inboundConfig.tProxySettings.hasUDP;
const auto hasIPv6 = hasTProxy && (!GlobalConfig.inboundConfig.tProxySettings.tProxyV6IP.isEmpty());
const bool hasSocksUDP = GlobalConfig.inboundConfig.useSocks && GlobalConfig.inboundConfig.socksSettings.enableUDP;
const auto hasSocksUDP = GlobalConfig.inboundConfig.useSocks && GlobalConfig.inboundConfig.socksSettings.enableUDP;
DNSInterceptFilter(root, hasTProxy, hasIPv6, hasSocksUDP);
}
@ -391,9 +392,9 @@ namespace Qv2ray::core::handler
// Routes
QJsonObject routing = root["routing"].toObject();
QJsonArray routingRules = routing["rules"].toArray();
QJsonObject APIRouteRoot{ { "type", "field" }, //
{ "outboundTag", API_TAG_DEFAULT }, //
{ "inboundTag", QJsonArray{ API_TAG_INBOUND } } };
const static QJsonObject APIRouteRoot{ { "type", "field" }, //
{ "outboundTag", API_TAG_DEFAULT }, //
{ "inboundTag", QJsonArray{ API_TAG_INBOUND } } };
routingRules.push_front(APIRouteRoot);
routing["rules"] = routingRules;
root["routing"] = routing;

View File

@ -64,8 +64,7 @@ namespace Qv2ray::ui
"certificates",
"serverName",
"QV2RAY_RULE_ENABLED",
"QV2RAY_RULE_TAG",
"QV2RAY_RULE_USE_BALANCER" };
"QV2RAY_RULE_TAG" };
int i = 0;
for (const QString &pattern : keywordPatterns)
{

View File

@ -70,15 +70,17 @@ void NodeDispatcher::LoadFullConfig(const CONFIGROOT &root)
}
}
const auto outboundTag = rule->QV2RAY_RULE_USE_BALANCER ? rule->balancerTag : rule->outboundTag;
if (outboundNodes.contains(outboundTag))
for (const auto &outboundTag : { rule->outboundTag, rule->balancerTag })
{
const auto outboundNodeId = outboundNodes[outboundTag];
ruleScene->createConnection(*ruleScene->node(outboundNodeId), 0, *ruleScene->node(ruleNodeId), 0);
}
else
{
LOG(MODULE_NODE, "Could not find outbound: " + outboundTag)
if (outboundNodes.contains(outboundTag))
{
const auto &outboundNodeId = outboundNodes[outboundTag];
ruleScene->createConnection(*ruleScene->node(outboundNodeId), 0, *ruleScene->node(ruleNodeId), 0);
}
else
{
LOG(MODULE_NODE, "Could not find outbound: " + outboundTag)
}
}
}
isOperationLocked = false;

View File

@ -80,7 +80,6 @@ void RuleNodeModel::outputConnectionDeleted(const QtNodes::Connection &c)
{
if (dispatcher->IsNodeConstructing())
return;
dataptr->QV2RAY_RULE_USE_BALANCER = false;
dataptr->balancerTag.clear();
dataptr->outboundTag.clear();
}

View File

@ -40,24 +40,6 @@ void QvNodeRuleWidget::setValue(std::shared_ptr<RuleObject> _ruleptr)
ruleTagLineEdit->setEnabled(true);
LOAD_FLAG_BEGIN
ruleTagLineEdit->setText(rule.QV2RAY_RULE_TAG);
// balancerSelectionCombo->clear();
// for (auto out : outbounds)
// {
// balancerSelectionCombo->addItem(getTag(OUTBOUND(out)));
// }
// //
// // Balancers combo and balancer list.
// enableBalancerCB->setChecked(CurrentRule.QV2RAY_RULE_USE_BALANCER);
// balancersWidget->setEnabled(CurrentRule.QV2RAY_RULE_USE_BALANCER);
// if (!CurrentRule.balancerTag.isEmpty())
// {
// balancerList->clear();
// balancerList->addItems(balancers[CurrentRule.balancerTag]);
// }
isLoading = false;
// Networks
auto network = rule.network.toLower();