[EasyTier-ohrs] Use NetworkConfig instead of TomlConfig, and add CompressionAlgorithm and EncryptionAlgorithm to NetworkConfig. (#1654)

This commit is contained in:
韩嘉乐
2025-12-06 23:23:22 +08:00
committed by GitHub
parent 2a656d6a0c
commit 056c9da781
4 changed files with 69 additions and 6 deletions

View File

@@ -30,7 +30,12 @@ serde_json = "1.0.125"
tracing-subscriber = "0.3.19"
tracing-core = "0.1.33"
tracing = "0.1.41"
uuid = { version = "1.17.0", features = ["v4"] }
uuid = { version = "1.5.0", features = [
"v4",
"fast-rng",
"macro-diagnostics",
"serde",
] }
[build-dependencies]
napi-build-ohos = "1.1"

View File

@@ -3,6 +3,7 @@ mod native_log;
use easytier::common::config::{ConfigFileControl, ConfigLoader, TomlConfigLoader};
use easytier::common::constants::EASYTIER_VERSION;
use easytier::instance_manager::NetworkInstanceManager;
use easytier::proto::api::manage::NetworkConfig;
use napi_derive_ohos::napi;
use ohos_hilog_binding::{hilog_debug, hilog_error};
use std::format;
@@ -43,9 +44,46 @@ pub fn set_tun_fd(inst_id: String, fd: i32) -> bool {
}
#[napi]
pub fn parse_config(cfg_str: String) -> bool {
pub fn default_network_config() -> String {
match NetworkConfig::new_from_config(TomlConfigLoader::default()) {
Ok(result) => serde_json::to_string(&result).unwrap_or_else(|e| format!("ERROR {}", e)),
Err(e) => {
hilog_error!("[Rust] default_network_config failed {}", e);
format!("ERROR {}", e)
}
}
}
#[napi]
pub fn convert_toml_to_network_config(cfg_str: String) -> String {
match TomlConfigLoader::new_from_str(&cfg_str) {
Ok(_) => true,
Ok(cfg) => match NetworkConfig::new_from_config(cfg) {
Ok(result) => serde_json::to_string(&result).unwrap_or_else(|e| format!("ERROR {}", e)),
Err(e) => {
hilog_error!("[Rust] convert_toml_to_network_config failed {}", e);
format!("ERROR {}", e)
}
},
Err(e) => {
hilog_error!("[Rust] convert_toml_to_network_config failed {}", e);
format!("ERROR {}", e)
}
}
}
#[napi]
pub fn parse_network_config(cfg_json: String) -> bool {
match serde_json::from_str::<NetworkConfig>(&cfg_json) {
Ok(cfg) => match cfg.gen_config() {
Ok(toml) => {
hilog_debug!("[Rust] Convert to Toml {}", toml.dump());
true
}
Err(e) => {
hilog_error!("[Rust] parse config failed {}", e);
false
}
},
Err(e) => {
hilog_error!("[Rust] parse config failed {}", e);
false
@@ -54,9 +92,15 @@ pub fn parse_config(cfg_str: String) -> bool {
}
#[napi]
pub fn run_network_instance(cfg_str: String) -> bool {
let cfg = match TomlConfigLoader::new_from_str(&cfg_str) {
Ok(cfg) => cfg,
pub fn run_network_instance(cfg_json: String) -> bool {
let cfg = match serde_json::from_str::<NetworkConfig>(&cfg_json) {
Ok(cfg) => match cfg.gen_config() {
Ok(toml) => toml,
Err(e) => {
hilog_error!("[Rust] parse config failed {}", e);
return false;
}
},
Err(e) => {
hilog_error!("[Rust] parse config failed {}", e);
return false;

View File

@@ -760,6 +760,18 @@ impl NetworkConfig {
flags.private_mode = enable_private_mode;
}
if let Some(encryption_algorithm) = self.encryption_algorithm.clone() {
flags.encryption_algorithm = encryption_algorithm;
}
if let Some(data_compress_algo) = self.data_compress_algo {
if data_compress_algo < 1 {
flags.data_compress_algo = 1;
} else {
flags.data_compress_algo = data_compress_algo
}
}
cfg.set_flags(flags);
Ok(cfg)
}

View File

@@ -78,6 +78,8 @@ message NetworkConfig {
optional bool disable_sym_hole_punching = 49;
optional bool p2p_only = 51;
optional common.CompressionAlgoPb data_compress_algo = 52;
optional string encryption_algorithm = 53;
}
message PortForwardConfig {