Skip to content

Commit

Permalink
feat(shadowsocks): impls serde ser/de for Mode/ServerAddr/ManagerAddr
Browse files Browse the repository at this point in the history
  • Loading branch information
zonyitoo committed Oct 30, 2024
1 parent adaf85a commit 88b0b29
Show file tree
Hide file tree
Showing 3 changed files with 217 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/shadowsocks/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
221 changes: 215 additions & 6 deletions crates/shadowsocks/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
error,
fmt::{self, Debug, Display},
net::SocketAddr,
str::FromStr,
str::{self, FromStr},
sync::Arc,
time::Duration,
};
Expand Down Expand Up @@ -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())
}
}

Expand All @@ -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<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
match v.parse::<Mode>() {
Ok(m) => Ok(m),
Err(_) => Err(serde::de::Error::invalid_value(serde::de::Unexpected::Str(v), &self)),
}
}

fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
self.visit_str::<E>(v.as_str())
}

fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
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<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
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<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
deserializer.deserialize_string(ModeVisitor)
}
}

impl serde::Serialize for Mode {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(self.as_str())
}
}

/// Server's weight
///
/// Commonly for using in balancer
Expand Down Expand Up @@ -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<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
match v.parse::<ServerAddr>() {
Ok(m) => Ok(m),
Err(_) => Err(serde::de::Error::invalid_value(serde::de::Unexpected::Str(v), &self)),
}
}

fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
self.visit_str::<E>(v.as_str())
}

fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
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<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
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<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
deserializer.deserialize_string(ServerAddrVisitor)
}
}

impl serde::Serialize for ServerAddr {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(self.to_string().as_str())
}
}

impl From<SocketAddr> for ServerAddr {
fn from(addr: SocketAddr) -> ServerAddr {
ServerAddr::SocketAddr(addr)
Expand Down Expand Up @@ -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<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
match v.parse::<ManagerAddr>() {
Ok(m) => Ok(m),
Err(_) => Err(serde::de::Error::invalid_value(serde::de::Unexpected::Str(v), &self)),
}
}

fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
self.visit_str::<E>(v.as_str())
}

fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
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<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
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<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
deserializer.deserialize_string(ManagerAddrVisitor)
}
}

impl serde::Serialize for ManagerAddr {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(self.to_string().as_str())
}
}

impl From<SocketAddr> for ManagerAddr {
fn from(addr: SocketAddr) -> ManagerAddr {
ManagerAddr::SocketAddr(addr)
Expand Down

0 comments on commit 88b0b29

Please sign in to comment.