Allow setting environment variables from config

This commit is contained in:
klzgrad
2025-09-26 07:58:16 +08:00
parent a30cf1b39b
commit 23bb6c784d
3 changed files with 43 additions and 2 deletions

View File

@ -67,7 +67,7 @@ Options:
The default proxy is a direct connection without proxying.
The last PROXY-URI is negotiated automatically for Naive padding.
If multiple proxies are specified, they must match the number of specified
Can be specified multiple times, but they must match the number of specified
LISTEN-URIs, and each LISTEN-URI is routed to the PROXY matched by position.
Limitations:
@ -93,6 +93,7 @@ Options:
--host-resolver-rules="MAP proxy.example.com 1.2.3.4"
Statically resolves a domain name to an IP address.
Multiple rules are comma separated.
--resolver-range=CIDR
@ -114,3 +115,8 @@ Options:
--no-post-quantum
Overrides the default and disables post-quantum key agreement.
--env=NAME=VALUE
Sets the environment variable NAME to the value VALUE. Can be specified
multiple times.

View File

@ -6,6 +6,7 @@
#include <algorithm>
#include <iostream>
#include "base/environment.h"
#include "base/strings/escape.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_tokenizer.h"
@ -285,6 +286,39 @@ bool NaiveConfig::Parse(const base::Value::Dict& value) {
no_post_quantum = true;
}
if (const base::Value* v = value.Find("env")) {
std::vector<std::string> env_strs;
if (const std::string* str = v->GetIfString(); str && !str->empty()) {
env_strs.push_back(*str);
} else if (const base::Value::List* strs = v->GetIfList()) {
for (const auto& str_e : *strs) {
if (const std::string* s = str_e.GetIfString(); s && !s->empty()) {
env_strs.push_back(*s);
} else {
std::cerr << "Invalid env element" << std::endl;
return false;
}
}
} else {
std::cerr << "Invalid env argument" << std::endl;
return false;
}
auto env = base::Environment::Create();
for (const std::string& str : env_strs) {
size_t equal_pos = str.find_first_of('=');
if (equal_pos != std::string::npos && equal_pos > 0 &&
equal_pos + 1 < str.size()) {
std::string env_name = str.substr(0, equal_pos);
std::string env_value = str.substr(equal_pos + 1);
if (!env->SetVar(env_name, env_value)) {
std::cerr << "Invalid env element " << str << std::endl;
}
} else {
std::cerr << "Invalid env element " << str << std::endl;
}
}
}
return true;
}

View File

@ -13,6 +13,7 @@
#include "base/at_exit.h"
#include "base/check.h"
#include "base/command_line.h"
#include "base/environment.h"
#include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/json/json_file_value_serializer.h"
@ -533,7 +534,7 @@ int main(int argc, char* argv[]) {
naive_proxies.push_back(std::move(naive_proxy));
}
if (getenv("TEST_MARK_STARTUP") != nullptr) {
if (base::Environment::Create()->HasVar("TEST_MARK_STARTUP")) {
LOG(INFO) << "TEST_MARK_STARTUP";
}
base::RunLoop().Run();