Skip to content

Commit

Permalink
Enhancements
Browse files Browse the repository at this point in the history
Fix a typo which cause the server to hang.
Add option to enable reload pref config on request.
Optimize codes.
  • Loading branch information
tindy2013 committed Nov 14, 2023
1 parent b71cd1e commit 885a63b
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 15 deletions.
3 changes: 3 additions & 0 deletions base/pref.example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions base/pref.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions base/pref.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ common:
proxy_ruleset: SYSTEM
proxy_subscription: NONE
append_proxy_type: false
reload_conf_on_request: false

userinfo:
stream_rule:
Expand Down
26 changes: 13 additions & 13 deletions src/generator/config/subexport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand Down Expand Up @@ -2078,14 +2078,23 @@ 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);
proxy.AddMember("server", rapidjson::StringRef(x.Hostname.c_str()), allocator);
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();
Expand Down Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/handler/interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion src/handler/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"))
{
Expand Down
1 change: 1 addition & 0 deletions src/handler/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 885a63b

Please sign in to comment.