backwards compatibility

This commit is contained in:
Jędrzej Stuczyński
2023-03-20 16:11:03 +00:00
parent f61ed48240
commit 6ffd211e51
10 changed files with 419 additions and 46 deletions
+1
View File
@@ -15,6 +15,7 @@ use std::fmt::Debug;
use std::path::PathBuf;
use std::str::FromStr;
pub mod old_config;
mod template;
const DEFAULT_CONNECTION_START_SURBS: u32 = 20;
@@ -0,0 +1,66 @@
// Copyright 2023 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0
use crate::client::config::{Config, Socks5, Socks5Debug};
use client_core::config::old_config::OldConfig as OldBaseConfig;
use nym_config::NymConfig;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct OldConfig {
#[serde(flatten)]
base: OldBaseConfig<OldConfig>,
socks5: Socks5,
#[serde(default)]
socks5_debug: Socks5Debug,
}
impl NymConfig for OldConfig {
fn template() -> &'static str {
// not intended to be used
unimplemented!()
}
fn default_root_directory() -> PathBuf {
#[cfg(not(feature = "mobile"))]
let base_dir = dirs::home_dir().expect("Failed to evaluate $HOME value");
#[cfg(feature = "mobile")]
let base_dir = PathBuf::from("/tmp");
base_dir.join(".nym").join("socks5-clients")
}
fn try_default_root_directory() -> Option<PathBuf> {
dirs::home_dir().map(|path| path.join(".nym").join("socks5-clients"))
}
fn root_directory(&self) -> PathBuf {
self.base.client.nym_root_directory.clone()
}
fn config_directory(&self) -> PathBuf {
self.root_directory()
.join(&self.base.client.id)
.join("config")
}
fn data_directory(&self) -> PathBuf {
self.root_directory()
.join(&self.base.client.id)
.join("data")
}
}
impl From<OldConfig> for Config {
fn from(value: OldConfig) -> Self {
Config {
base: value.base.into(),
socks5: value.socks5,
socks5_debug: value.socks5_debug,
}
}
}
+7 -7
View File
@@ -114,15 +114,15 @@ send_anonymously = {{ socks5.send_anonymously }}
[debug]
[[debug.traffic]]
average_packet_delay = '{{ debug.average_packet_delay }}'
message_sending_average_delay = '{{ debug.message_sending_average_delay }}'
[debug.traffic]
average_packet_delay = '{{ debug.traffic.average_packet_delay }}'
message_sending_average_delay = '{{ debug.traffic.message_sending_average_delay }}'
[[debug.acknowledgements]]
average_ack_delay = '{{ debug.average_ack_delay }}'
[debug.acknowledgements]
average_ack_delay = '{{ debug.acknowledgements.average_ack_delay }}'
[[debug.cover_traffic]]
loop_cover_traffic_average_delay = '{{ debug.loop_cover_traffic_average_delay }}'
[debug.cover_traffic]
loop_cover_traffic_average_delay = '{{ debug.cover_traffic.loop_cover_traffic_average_delay }}'
"#
}
+32 -9
View File
@@ -7,6 +7,7 @@ use crate::{
error::Socks5ClientError,
};
use crate::client::config::old_config::OldConfig;
use clap::Args;
use log::*;
use nym_bin_common::version_checker::is_minor_version_compatible;
@@ -105,18 +106,40 @@ fn version_check(cfg: &Config) -> bool {
}
}
fn load_config(id: &str) -> Result<Config, Box<dyn std::error::Error + Send + Sync>> {
// try to load the current config
match Config::load_from_file(id) {
Ok(config) => Ok(config),
Err(err) => {
warn!("Failed to load config for {id} - {err}...\nAttempting to use the old config template...");
// if this failed, try to use the old template
let old_config = match OldConfig::load_from_file(id) {
Ok(cfg) => {
info!("Managed to load config for {id} using old template");
cfg
}
Err(err) => {
error!("Failed to load config for {id} with the old template. Are you sure you have run `init` before? (Error was: {err})");
return Err(Box::new(Socks5ClientError::FailedToLoadConfig(
id.to_string(),
)));
}
};
info!("Updating the client config template...");
let updated: Config = old_config.into();
updated.save_to_file(None)?;
Ok(updated)
}
}
}
pub(crate) async fn execute(args: &Run) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let id = &args.id;
let mut config = match Config::load_from_file(id) {
Ok(cfg) => cfg,
Err(err) => {
error!("Failed to load config for {}. Are you sure you have run `init` before? (Error was: {err})", id);
return Err(Box::new(Socks5ClientError::FailedToLoadConfig(
id.to_string(),
)));
}
};
let mut config = load_config(id)?;
let override_config_fields = OverrideConfig::from(args.clone());
config = override_config(config, override_config_fields);