Skip to content

Commit

Permalink
feat(api_server): simple config by default
Browse files Browse the repository at this point in the history
  • Loading branch information
spacemeowx2 committed Feb 20, 2024
1 parent d6a8eb3 commit 14cd4f0
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 24 deletions.
1 change: 1 addition & 0 deletions protocol/ss/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use shadowsocks::{
#[derive(Debug, Clone)]
pub struct SSNetConfig {
pub(crate) server: Address,
#[serde(skip_serializing_if = "rd_interface::config::detailed_field")]
pub(crate) password: String,
#[serde(default)]
pub(crate) udp: bool,
Expand Down
1 change: 1 addition & 0 deletions protocol/ss/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ mod source;
#[derive(Debug, Clone)]
pub struct SSServerConfig {
pub(crate) bind: Address,
#[serde(skip_serializing_if = "rd_interface::config::detailed_field")]
pub(crate) password: String,
#[serde(default)]
pub(crate) udp: bool,
Expand Down
2 changes: 2 additions & 0 deletions protocol/trojan/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ pub struct TrojanNetConfig {
/// hostname:port
server: Address,
/// password in plain text
#[serde(skip_serializing_if = "rd_interface::config::detailed_field")]
password: String,

/// sni
Expand All @@ -100,6 +101,7 @@ pub struct TrojancNetConfig {
/// hostname:port
server: RdAddress,
/// password in plain text
#[serde(skip_serializing_if = "rd_interface::config::detailed_field")]
password: String,

/// enabled websocket support
Expand Down
44 changes: 21 additions & 23 deletions rabbit-digger/src/rabbit_digger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ use futures::{
Stream, StreamExt, TryStreamExt,
};
use rd_interface::{
config::{CompactVecString, NetRef, VisitorContext},
config::{
serialize_with_fields, CompactVecString, NetRef, VisitorContext, ALL_SERIALIZE_FIELDS,
},
registry::NetGetter,
Arc, Error, IntoDyn, Net, Server, Value,
};
Expand All @@ -37,7 +39,8 @@ struct RunningEntities {

struct SerializedConfig {
id: String,
str: String,
all_fields: String,
simple_fields: String,
}

#[allow(dead_code)]
Expand Down Expand Up @@ -158,19 +161,6 @@ impl RabbitDigger {
Ok(())
}

// get current config if it's running
pub async fn config(&self) -> Result<String> {
let state = self.inner.state.read().await;
match &*state {
State::Running(Running { config, .. }) => {
return Ok(config.read().await.str.clone());
}
_ => {
return Err(anyhow!("Not running"));
}
};
}

// get current connection state
pub async fn connection<F, R>(&self, f: F) -> R
where
Expand Down Expand Up @@ -223,7 +213,10 @@ impl RabbitDigger {

*state = State::Running(Running {
config: RwLock::new(SerializedConfig {
str: serde_json::to_string(&config)?,
all_fields: serialize_with_fields(ALL_SERIALIZE_FIELDS.to_vec(), || {
serde_json::to_string(&config)
})?,
simple_fields: serde_json::to_string(&config)?,
id: config.id,
}),
entities,
Expand Down Expand Up @@ -331,8 +324,9 @@ impl RabbitDigger {
entities: RunningEntities { nets, .. },
..
}) => {
let config_str = &mut config.write().await.str;
let mut config: config::Config = serde_json::from_str(config_str)?;
let mut serialized_config = config.write().await;
let mut config: config::Config =
serde_json::from_str(&serialized_config.all_fields)?;

if let (Some(cfg), Some(running_net)) =
(config.net.get_mut(net_name), nets.get(net_name))
Expand All @@ -354,7 +348,11 @@ impl RabbitDigger {
running_net.update_net(net);

*cfg = new_cfg;
*config_str = serde_json::to_string(&config)?;
serialized_config.all_fields =
serialize_with_fields(ALL_SERIALIZE_FIELDS.to_vec(), || {
serde_json::to_string(&config)
})?;
serialized_config.simple_fields = serde_json::to_string(&config)?;
}
return Ok(());
}
Expand All @@ -372,14 +370,14 @@ impl RabbitDigger {
}
}

pub async fn get_config<F, R>(&self, f: F) -> R
pub async fn get_config<F, R>(&self, f: F) -> Result<R>
where
F: FnOnce(Option<&str>) -> R,
F: FnOnce(&str) -> R,
{
let state = self.inner.state.read().await;
match state.running() {
Some(i) => f(Some(&*i.config.read().await.str)),
None => f(None),
Some(i) => Ok(f(&*i.config.read().await.simple_fields)),
None => Err(anyhow!("Not running")),
}
}

Expand Down
36 changes: 36 additions & 0 deletions rd-interface/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::cell::RefCell;

use crate::{self as rd_interface, Address, Net};
pub use resolvable::{Resolvable, ResolvableSchema};
use schemars::{
Expand Down Expand Up @@ -209,3 +211,37 @@ impl JsonSchema for Address {
.into()
}
}

#[derive(PartialEq, Clone)]
pub enum ConfigField {
/// Default field type
Common,
/// Field type for detailed mode, which may be very large
Detail,
/// Field type for sensitive mode, which may contain sensitive information
Sensitive,
}

pub const ALL_SERIALIZE_FIELDS: [ConfigField; 3] = [
ConfigField::Common,
ConfigField::Detail,
ConfigField::Sensitive,
];
thread_local!(static SERIALIZE_FIELDS: RefCell<Vec<ConfigField>> = RefCell::new(vec![ConfigField::Common]));

pub fn detailed_field<T>(_: T) -> bool {
!SERIALIZE_FIELDS.with(|x| x.borrow().contains(&ConfigField::Detail))
}

pub fn sensitive_field<T>(_: T) -> bool {
!SERIALIZE_FIELDS.with(|x| x.borrow().contains(&ConfigField::Sensitive))
}

pub fn serialize_with_fields<T, F: FnOnce() -> T>(fields: Vec<ConfigField>, f: F) -> T {
SERIALIZE_FIELDS.with(|x| {
let old_fields = std::mem::replace(&mut *x.borrow_mut(), fields);
let ret = f();
*x.borrow_mut() = old_fields;
ret
})
}
1 change: 1 addition & 0 deletions rd-std/src/rule/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ impl RuleItem {
pub struct RuleNetConfig {
#[serde(default = "default_lru_cache_size")]
pub lru_cache_size: usize,
#[serde(skip_serializing_if = "rd_interface::config::detailed_field")]
pub rule: Vec<RuleItem>,
}

Expand Down
2 changes: 1 addition & 1 deletion src/api_server/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub(super) async fn get_config(
HeaderValue::from_static("application/json"),
);

let config_str = rd.config().await?;
let config_str = rd.get_config(|c| c.to_owned()).await?;
Ok((headers, config_str))
}

Expand Down

0 comments on commit 14cd4f0

Please sign in to comment.