From 88b0b29b51d10704fcfc98b6c1231d87a574a254 Mon Sep 17 00:00:00 2001 From: zonyitoo Date: Wed, 30 Oct 2024 23:20:36 +0800 Subject: [PATCH] feat(shadowsocks): impls serde ser/de for Mode/ServerAddr/ManagerAddr --- Cargo.lock | 2 +- crates/shadowsocks/Cargo.toml | 2 +- crates/shadowsocks/src/config.rs | 221 ++++++++++++++++++++++++++++++- 3 files changed, 217 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d460bb1a2de3..2801cc798902 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3221,7 +3221,7 @@ dependencies = [ [[package]] name = "shadowsocks" -version = "1.21.0" +version = "1.21.1" dependencies = [ "aes", "arc-swap", diff --git a/crates/shadowsocks/Cargo.toml b/crates/shadowsocks/Cargo.toml index 7d0cedf4de0c..4406f81f2466 100644 --- a/crates/shadowsocks/Cargo.toml +++ b/crates/shadowsocks/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "shadowsocks" -version = "1.21.0" +version = "1.21.1" authors = ["Shadowsocks Contributors"] description = "shadowsocks is a fast tunnel proxy that helps you bypass firewalls." repository = "https://github.com/shadowsocks/shadowsocks-rust" diff --git a/crates/shadowsocks/src/config.rs b/crates/shadowsocks/src/config.rs index 4195eab98ff6..f990e84c01ab 100644 --- a/crates/shadowsocks/src/config.rs +++ b/crates/shadowsocks/src/config.rs @@ -7,7 +7,7 @@ use std::{ error, fmt::{self, Debug, Display}, net::SocketAddr, - str::FromStr, + str::{self, FromStr}, sync::Arc, time::Duration, }; @@ -100,15 +100,20 @@ impl Mode { _ => unreachable!(), } } + + /// String representation of Mode + pub fn as_str(&self) -> &'static str { + match *self { + Mode::TcpOnly => "tcp_only", + Mode::TcpAndUdp => "tcp_and_udp", + Mode::UdpOnly => "udp_only", + } + } } impl fmt::Display for Mode { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match *self { - Mode::TcpOnly => f.write_str("tcp_only"), - Mode::TcpAndUdp => f.write_str("tcp_and_udp"), - Mode::UdpOnly => f.write_str("udp_only"), - } + f.write_str(self.as_str()) } } @@ -125,6 +130,74 @@ impl FromStr for Mode { } } +struct ModeVisitor; + +impl<'de> serde::de::Visitor<'de> for ModeVisitor { + type Value = Mode; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter.write_str("Mode") + } + + fn visit_str(self, v: &str) -> Result + where + E: serde::de::Error, + { + match v.parse::() { + Ok(m) => Ok(m), + Err(_) => Err(serde::de::Error::invalid_value(serde::de::Unexpected::Str(v), &self)), + } + } + + fn visit_string(self, v: String) -> Result + where + E: serde::de::Error, + { + self.visit_str::(v.as_str()) + } + + fn visit_bytes(self, v: &[u8]) -> Result + where + E: serde::de::Error, + { + match str::from_utf8(v) { + Ok(v) => self.visit_str(v), + Err(_) => Err(serde::de::Error::invalid_value(serde::de::Unexpected::Bytes(v), &self)), + } + } + + fn visit_byte_buf(self, v: Vec) -> Result + where + E: serde::de::Error, + { + match String::from_utf8(v) { + Ok(v) => self.visit_string(v), + Err(e) => Err(serde::de::Error::invalid_value( + serde::de::Unexpected::Bytes(&e.into_bytes()), + &self, + )), + } + } +} + +impl<'de> serde::Deserialize<'de> for Mode { + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + deserializer.deserialize_string(ModeVisitor) + } +} + +impl serde::Serialize for Mode { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + serializer.serialize_str(self.as_str()) + } +} + /// Server's weight /// /// Commonly for using in balancer @@ -986,6 +1059,74 @@ impl Display for ServerAddr { } } +struct ServerAddrVisitor; + +impl<'de> serde::de::Visitor<'de> for ServerAddrVisitor { + type Value = ServerAddr; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter.write_str("ServerAddr") + } + + fn visit_str(self, v: &str) -> Result + where + E: serde::de::Error, + { + match v.parse::() { + Ok(m) => Ok(m), + Err(_) => Err(serde::de::Error::invalid_value(serde::de::Unexpected::Str(v), &self)), + } + } + + fn visit_string(self, v: String) -> Result + where + E: serde::de::Error, + { + self.visit_str::(v.as_str()) + } + + fn visit_bytes(self, v: &[u8]) -> Result + where + E: serde::de::Error, + { + match str::from_utf8(v) { + Ok(v) => self.visit_str(v), + Err(_) => Err(serde::de::Error::invalid_value(serde::de::Unexpected::Bytes(v), &self)), + } + } + + fn visit_byte_buf(self, v: Vec) -> Result + where + E: serde::de::Error, + { + match String::from_utf8(v) { + Ok(v) => self.visit_string(v), + Err(e) => Err(serde::de::Error::invalid_value( + serde::de::Unexpected::Bytes(&e.into_bytes()), + &self, + )), + } + } +} + +impl<'de> serde::Deserialize<'de> for ServerAddr { + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + deserializer.deserialize_string(ServerAddrVisitor) + } +} + +impl serde::Serialize for ServerAddr { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + serializer.serialize_str(self.to_string().as_str()) + } +} + impl From for ServerAddr { fn from(addr: SocketAddr) -> ServerAddr { ServerAddr::SocketAddr(addr) @@ -1099,6 +1240,74 @@ impl Display for ManagerAddr { } } +struct ManagerAddrVisitor; + +impl<'de> serde::de::Visitor<'de> for ManagerAddrVisitor { + type Value = ManagerAddr; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter.write_str("ManagerAddr") + } + + fn visit_str(self, v: &str) -> Result + where + E: serde::de::Error, + { + match v.parse::() { + Ok(m) => Ok(m), + Err(_) => Err(serde::de::Error::invalid_value(serde::de::Unexpected::Str(v), &self)), + } + } + + fn visit_string(self, v: String) -> Result + where + E: serde::de::Error, + { + self.visit_str::(v.as_str()) + } + + fn visit_bytes(self, v: &[u8]) -> Result + where + E: serde::de::Error, + { + match str::from_utf8(v) { + Ok(v) => self.visit_str(v), + Err(_) => Err(serde::de::Error::invalid_value(serde::de::Unexpected::Bytes(v), &self)), + } + } + + fn visit_byte_buf(self, v: Vec) -> Result + where + E: serde::de::Error, + { + match String::from_utf8(v) { + Ok(v) => self.visit_string(v), + Err(e) => Err(serde::de::Error::invalid_value( + serde::de::Unexpected::Bytes(&e.into_bytes()), + &self, + )), + } + } +} + +impl<'de> serde::Deserialize<'de> for ManagerAddr { + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + deserializer.deserialize_string(ManagerAddrVisitor) + } +} + +impl serde::Serialize for ManagerAddr { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + serializer.serialize_str(self.to_string().as_str()) + } +} + impl From for ManagerAddr { fn from(addr: SocketAddr) -> ManagerAddr { ManagerAddr::SocketAddr(addr)