Enhancements

Fix a typo which cause the server to hang.
Add option to enable reload pref config on request.
Optimize codes.
This commit is contained in:
Tindy X
2023-11-14 16:42:22 +08:00
parent b71cd1e668
commit 885a63b837
7 changed files with 26 additions and 15 deletions

View File

@@ -73,6 +73,9 @@ proxy_subscription=NONE
;Append a proxy type string ([SS] [SSR] [VMess]) to node remark.
append_proxy_type=false
;When requesting /sub, reload this config file first.
reload_conf_on_request=false
[userinfo]
;Rules to extract stream data from node
;Format: full_match_regex|new_format_regex

View File

@@ -79,6 +79,9 @@ proxy_subscription = "NONE"
# Append a proxy type string ([SS] [SSR] [VMess]) to node remark.
append_proxy_type = false
# When requesting /sub, reload this config file first.
reload_conf_on_request = false
[[userinfo.stream_rule]]
# Rules to extract stream data from node
# Format: full_match_regex|new_format_regex

View File

@@ -24,6 +24,7 @@ common:
proxy_ruleset: SYSTEM
proxy_subscription: NONE
append_proxy_type: false
reload_conf_on_request: false
userinfo:
stream_rule:

View File

@@ -172,7 +172,7 @@ void processRemark(std::string &remark, const string_array &remarks_list, bool p
}
std::string tempRemark = remark;
int cnt = 2;
while(std::find(remarks_list.cbegin(), remarks_list.cend(), tempRemark) != remarks_list.cbegin())
while(std::find(remarks_list.cbegin(), remarks_list.cend(), tempRemark) != remarks_list.cend())
{
tempRemark = remark + " " + std::to_string(cnt);
cnt++;
@@ -2078,7 +2078,7 @@ static rapidjson::Value buildSingBoxTransport(const Proxy& proxy, rapidjson::Mem
return transport;
}
void addSingBoxCommonMembers(rapidjson::Value &proxy, const Proxy &x, const rapidjson::GenericStringRef<rapidjson::Value::Ch> &type, rapidjson::MemoryPoolAllocator<> &allocator)
static void addSingBoxCommonMembers(rapidjson::Value &proxy, const Proxy &x, const rapidjson::GenericStringRef<rapidjson::Value::Ch> &type, rapidjson::MemoryPoolAllocator<> &allocator)
{
proxy.AddMember("type", type, allocator);
proxy.AddMember("tag", rapidjson::StringRef(x.Remark.c_str()), allocator);
@@ -2086,6 +2086,15 @@ void addSingBoxCommonMembers(rapidjson::Value &proxy, const Proxy &x, const rapi
proxy.AddMember("server_port", x.Port, allocator);
}
static rapidjson::Value stringArrayToJsonArray(const std::string &array, const std::string &delimiter, rapidjson::MemoryPoolAllocator<> &allocator)
{
rapidjson::Value result(rapidjson::kArrayType);
string_array vArray = split(array, delimiter);
for (const auto &x : vArray)
result.PushBack(rapidjson::Value(trim(x).c_str(), allocator), allocator);
return result;
}
void proxyToSingBox(std::vector<Proxy> &nodes, rapidjson::Document &json, std::vector<RulesetContent> &ruleset_content_array, const ProxyGroupConfigs &extra_proxy_group, extra_settings &ext) {
using namespace rapidjson_ext;
rapidjson::Document::AllocatorType &allocator = json.GetAllocator();
@@ -2181,22 +2190,13 @@ void proxyToSingBox(std::vector<Proxy> &nodes, rapidjson::Document &json, std::v
if (!x.AllowedIPs.empty())
{
auto allowed = split(x.AllowedIPs, ",");
rapidjson::Value allowed_ips(rapidjson::kArrayType);
for (const auto &ip: allowed) {
allowed_ips.PushBack(rapidjson::Value(trim(ip).c_str(), allocator), allocator);
}
auto allowed_ips = stringArrayToJsonArray(x.AllowedIPs, ",", allocator);
peer.AddMember("allowed_ips", allowed_ips, allocator);
}
if (!x.ClientId.empty())
{
auto client_id = split(x.ClientId, ",");
rapidjson::Value reserved(rapidjson::kArrayType);
for (const auto &id : client_id)
{
reserved.PushBack(to_int(trim(id)), allocator);
}
auto reserved = stringArrayToJsonArray(x.ClientId, ",", allocator);
peer.AddMember("reserved", reserved, allocator);
}

View File

@@ -323,7 +323,7 @@ std::string subconverter(RESPONSE_CALLBACK_ARGS)
return "Invalid target!";
}
//check if we need to read configuration
if((!global.APIMode || global.CFWChildProcess) && !global.generatorMode)
if(global.reloadConfOnRequest && (!global.APIMode || global.CFWChildProcess) && !global.generatorMode)
readConf();
/// string values

View File

@@ -338,6 +338,7 @@ void readYAMLConf(YAML::Node &node)
section["proxy_config"] >> global.proxyConfig;
section["proxy_ruleset"] >> global.proxyRuleset;
section["proxy_subscription"] >> global.proxySubscription;
section["reload_conf_on_request"] >> global.reloadConfOnRequest;
if(node["userinfo"].IsDefined())
{
@@ -612,7 +613,8 @@ void readTOMLConf(toml::value &root)
"proxy_config", global.proxyConfig,
"proxy_ruleset", global.proxyRuleset,
"proxy_subscription", global.proxySubscription,
"append_proxy_type", global.appendType
"append_proxy_type", global.appendType,
"reload_conf_on_request", global.reloadConfOnRequest
);
if(filter)
@@ -854,6 +856,7 @@ void readConf()
ini.get_if_exist("proxy_config", global.proxyConfig);
ini.get_if_exist("proxy_ruleset", global.proxyRuleset);
ini.get_if_exist("proxy_subscription", global.proxySubscription);
ini.get_bool_if_exist("reload_conf_on_request", global.reloadConfOnRequest);
if(ini.section_exist("surge_external_proxy"))
{

View File

@@ -42,6 +42,7 @@ struct Settings
std::string generateProfiles;
//preferences
bool reloadConfOnRequest = false;
RegexMatchConfigs renames, emojis;
bool addEmoji = false, removeEmoji = false, appendType = false, filterDeprecated = true;
tribool UDPFlag, TFOFlag, skipCertVerify, TLS13Flag, enableInsert;