diff --git a/Cargo.lock b/Cargo.lock index 9e2204b6..dfd3a3be 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -216,7 +216,7 @@ version = "1.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" dependencies = [ - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -227,7 +227,7 @@ checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" dependencies = [ "anstyle", "once_cell_polyfill", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -2214,6 +2214,17 @@ dependencies = [ "thiserror 2.0.20", ] +[[package]] +name = "delegate" +version = "0.13.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "780eb241654bf097afb00fc5f054a09b687dad862e485fdcf8399bb056565370" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "der" version = "0.7.10" @@ -2365,7 +2376,7 @@ dependencies = [ "libc", "option-ext", "redox_users 0.5.3", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -2844,6 +2855,9 @@ dependencies = [ "bytes", "chrono", "cidr", + "delegate", + "derive_more", + "educe", "hmac 0.12.1", "indoc", "optionize", @@ -2992,6 +3006,18 @@ dependencies = [ "zeroize", ] +[[package]] +name = "educe" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ebdb1f8f8d2815bbe7b369a7da10343c82026a4503b68bd1279ba557611e96d" +dependencies = [ + "enum-ordinalize", + "proc-macro2", + "quote", + "syn 3.0.6", +] + [[package]] name = "either" version = "1.18.0" @@ -3165,6 +3191,26 @@ dependencies = [ "syn 2.0.119", ] +[[package]] +name = "enum-ordinalize" +version = "4.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89dd01549b09589510cf0647475075d12071456586d70f5c75c98ae2a5537677" +dependencies = [ + "enum-ordinalize-derive", +] + +[[package]] +name = "enum-ordinalize-derive" +version = "4.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a65863d15a4ce2888bd2f0f543cc963d3879c3a022c8ee43f6141d479a3ac815" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.6", +] + [[package]] name = "enumflags2" version = "0.7.12" @@ -3220,7 +3266,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -5508,7 +5554,7 @@ dependencies = [ "png 0.18.1", "serde", "thiserror 2.0.20", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -5910,7 +5956,7 @@ version = "0.50.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" dependencies = [ - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -6477,7 +6523,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7d8fae84b431384b68627d0f9b3b1245fcf9f46f6c0e3dc902e9dce64edd1967" dependencies = [ "libc", - "windows-sys 0.45.0", + "windows-sys 0.61.2", ] [[package]] @@ -7467,7 +7513,7 @@ dependencies = [ "once_cell", "socket2", "tracing", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -8059,7 +8105,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys 0.12.1", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -8117,7 +8163,7 @@ dependencies = [ "security-framework", "security-framework-sys", "webpki-root-certs", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -8970,7 +9016,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3d1e2c7f27f8d4cb10542a02c49005dbd6e93095799d6f3be745fae9f8fedd4" dependencies = [ "libc", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -9902,10 +9948,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" dependencies = [ "fastrand", - "getrandom 0.3.4", + "getrandom 0.4.3", "once_cell", "rustix 1.1.5", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -9924,7 +9970,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "230a1b821ccbd75b185820a1f1ff7b14d21da1e442e22c0863ea5f08771a8874" dependencies = [ "rustix 1.1.5", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -10608,7 +10654,7 @@ dependencies = [ "png 0.18.1", "serde", "thiserror 2.0.20", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -10715,7 +10761,7 @@ checksum = "f2f6fb2847f6742cd76af783a2a2c49e9375d0a111c7bef6f71cd9e738c72d6e" dependencies = [ "memoffset", "tempfile", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -11361,7 +11407,7 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ - "windows-sys 0.48.0", + "windows-sys 0.61.2", ] [[package]] @@ -11986,7 +12032,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7d6f32a0ff4a9f6f01231eb2059cc85479330739333e0e58cadf03b6af2cca10" dependencies = [ "cfg-if", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 098d9683..d2aedb43 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,6 +41,9 @@ clap = "4.6.7" crossbeam = "0.8.5" crossbeam-utils = "0.8.23" dashmap = "6.2.1" +delegate = "0.13.5" +derive_more = { version = "2.1.1", features = ["full"] } +educe = "0.8.1" easytier = { version = "2.7.0", path = "easytier", default-features = false } easytier-core = { version = "2.7.0", path = "easytier-core", default-features = false } easytier-ffi = { version = "0.1.0", path = "easytier-contrib/easytier-ffi", default-features = false } diff --git a/easytier-core/src/config/mod.rs b/easytier-core/src/config/mod.rs index 07c0d3e2..c784e875 100644 --- a/easytier-core/src/config/mod.rs +++ b/easytier-core/src/config/mod.rs @@ -50,7 +50,7 @@ use std::{ use anyhow::Context as _; use base64::{Engine as _, prelude::BASE64_STANDARD}; -use easytier_proto::{common as common_pb, core_config as pb}; +use easytier_proto::{CollectionExt, common as common_pb, core_config as pb}; use optionize::Optionizable; use serde::{Deserialize, Serialize}; use url::Url; @@ -420,21 +420,9 @@ impl TryFrom for RouteConfig { Ok(Self { ipv4: value.ipv4.map(TryInto::try_into).transpose()?, ipv6: value.ipv6.map(TryInto::try_into).transpose()?, - advertised_routes: value - .advertised_routes - .into_iter() - .map(TryInto::try_into) - .collect::>()?, - proxy_networks: value - .proxy_networks - .into_iter() - .map(TryInto::try_into) - .collect::>()?, - foreign_networks: value - .foreign_networks - .into_iter() - .map(TryInto::try_into) - .collect::>()?, + advertised_routes: value.advertised_routes.try_convert()?, + proxy_networks: value.proxy_networks.try_convert()?, + foreign_networks: value.foreign_networks.try_convert()?, }) } } @@ -444,13 +432,9 @@ impl From for pb::RouteConfig { Self { ipv4: value.ipv4.map(Into::into), ipv6: value.ipv6.map(Into::into), - advertised_routes: value - .advertised_routes - .into_iter() - .map(Into::into) - .collect(), - proxy_networks: value.proxy_networks.into_iter().map(Into::into).collect(), - foreign_networks: value.foreign_networks.into_iter().map(Into::into).collect(), + advertised_routes: value.advertised_routes.convert(), + proxy_networks: value.proxy_networks.convert(), + foreign_networks: value.foreign_networks.convert(), } } } @@ -470,6 +454,14 @@ impl TryFrom for IpPrefix { } } +impl<'m> TryFrom<&'m pb::IpPrefix> for IpPrefix { + type Error = ConfigError; + + fn try_from(value: &'m pb::IpPrefix) -> Result { + (*value).try_into() + } +} + impl From for pb::IpPrefix { fn from(value: IpPrefix) -> Self { Self { @@ -493,6 +485,14 @@ impl TryFrom for ProxyNetworkConfig { } } +impl<'m> TryFrom<&'m pb::ProxyNetworkConfig> for ProxyNetworkConfig { + type Error = ConfigError; + + fn try_from(value: &'m pb::ProxyNetworkConfig) -> Result { + (*value).try_into() + } +} + impl From for pb::ProxyNetworkConfig { fn from(value: ProxyNetworkConfig) -> Self { Self { @@ -508,20 +508,24 @@ impl TryFrom for ForeignNetworkConfig { fn try_from(value: pb::ForeignNetworkConfig) -> Result { Ok(Self { name: value.name, - cidrs: value - .cidrs - .into_iter() - .map(TryInto::try_into) - .collect::>()?, + cidrs: value.cidrs.try_convert()?, }) } } +impl<'m> TryFrom<&'m pb::ForeignNetworkConfig> for ForeignNetworkConfig { + type Error = ConfigError; + + fn try_from(value: &'m pb::ForeignNetworkConfig) -> Result { + value.clone().try_into() + } +} + impl From for pb::ForeignNetworkConfig { fn from(value: ForeignNetworkConfig) -> Self { Self { name: value.name, - cidrs: value.cidrs.into_iter().map(Into::into).collect(), + cidrs: value.cidrs.convert(), } } } diff --git a/easytier-proto/Cargo.toml b/easytier-proto/Cargo.toml index 3b53209c..1e74dc7f 100644 --- a/easytier-proto/Cargo.toml +++ b/easytier-proto/Cargo.toml @@ -21,6 +21,9 @@ base64 = { workspace = true, optional = true } bytes = { workspace = true, optional = true } chrono = { workspace = true, features = ["serde"], optional = true } cidr = { workspace = true, features = ["serde"], optional = true } +delegate = { workspace = true, optional = true } +derive_more = { workspace = true, optional = true } +educe = { workspace = true, optional = true } hmac = { workspace = true, optional = true } prost = { workspace = true, optional = true } prost-types = { workspace = true, optional = true } @@ -86,6 +89,9 @@ core = [ "dep:bytes", "dep:chrono", "dep:cidr", + "dep:delegate", + "dep:derive_more", + "dep:educe", "dep:hmac", "dep:pbjson", "dep:prost", diff --git a/easytier-proto/src/lib.rs b/easytier-proto/src/lib.rs index e99019fa..de93b4ac 100644 --- a/easytier-proto/src/lib.rs +++ b/easytier-proto/src/lib.rs @@ -19,9 +19,14 @@ pub mod magic_dns; pub mod peer_rpc; #[cfg(feature = "api")] pub mod tests; +#[cfg(feature = "core")] +pub mod utils; #[cfg(feature = "api")] pub mod web; +#[cfg(feature = "core")] +pub use utils::{CollectionExt, MessageModel, RepeatedMessageModel}; + pub const DESCRIPTOR_POOL_BYTES: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/file_descriptor_set.bin")); diff --git a/easytier-proto/src/utils.rs b/easytier-proto/src/utils.rs new file mode 100644 index 00000000..d78ea624 --- /dev/null +++ b/easytier-proto/src/utils.rs @@ -0,0 +1,152 @@ +use delegate::delegate; +use derive_more::{AsMut, AsRef, Deref, DerefMut, From, IntoIterator}; +use educe::Educe; +use serde::{Deserialize, Deserializer, Serialize, Serializer}; + +pub trait CollectionExt: IntoIterator + Sized { + fn convert(self) -> C + where + Self::Item: Into, + C: FromIterator, + { + self.into_iter().map(Into::into).collect() + } + + fn try_convert(self) -> Result + where + Self::Item: TryInto, + C: FromIterator, + { + self.into_iter().map(TryInto::try_into).collect() + } +} + +impl CollectionExt for I {} + +pub trait MessageModel: + Into + TryFrom + for<'m> TryFrom<&'m Message> +{ +} + +impl MessageModel for Model +where + Message: prost::Message, + Model: Into + TryFrom + for<'m> TryFrom<&'m Message>, +{ +} + +#[derive( + Educe, Debug, Clone, PartialEq, Eq, Hash, From, Deref, DerefMut, AsRef, AsMut, IntoIterator, +)] +#[educe(Default)] +#[as_ref(forward)] +#[as_mut(forward)] +#[into_iterator(owned, ref, ref_mut)] +pub struct RepeatedMessageModel { + pub models: Vec, +} + +impl FromIterator for RepeatedMessageModel { + fn from_iter>(iter: I) -> Self { + Self { + models: iter.convert(), + } + } +} + +impl Extend for RepeatedMessageModel { + delegate! { + to self.models { + fn extend>(&mut self, iter: T); + } + } +} + +impl<'m, Message, Model> TryFrom<&'m [Message]> for RepeatedMessageModel +where + Message: prost::Message, + Model: MessageModel, +{ + type Error = >::Error; + + fn try_from(value: &'m [Message]) -> Result { + value.try_convert() + } +} + +impl From> for Vec +where + Message: prost::Message, + Model: MessageModel, +{ + fn from(value: RepeatedMessageModel) -> Self { + value.convert() + } +} + +impl RepeatedMessageModel { + pub fn try_from_iter( + messages: impl IntoIterator, + ) -> Result>::Error> + where + Message: prost::Message, + Model: MessageModel, + { + messages.try_convert() + } +} + +pub trait RepeatedSerialize: Serialize + Sized { + fn serialize(models: &RepeatedMessageModel, serializer: S) -> Result + where + S: Serializer, + { + Serialize::serialize(&**models, serializer) + } +} + +pub trait RepeatedDeserialize<'de>: Deserialize<'de> { + fn deserialize(deserializer: D) -> Result, D::Error> + where + D: Deserializer<'de>, + { + Vec::deserialize(deserializer).map(Into::into) + } + + #[doc(hidden)] + fn deserialize_in_place( + deserializer: D, + place: &mut RepeatedMessageModel, + ) -> Result<(), D::Error> + where + D: Deserializer<'de>, + { + Deserialize::deserialize_in_place(deserializer, &mut place.models) + } +} + +impl Serialize for RepeatedMessageModel { + delegate! { + #[through(RepeatedSerialize)] + to self { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer; + } + } +} + +impl<'de, Model: RepeatedDeserialize<'de>> Deserialize<'de> for RepeatedMessageModel { + delegate! { + to RepeatedDeserialize { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>; + + #[doc(hidden)] + fn deserialize_in_place(deserializer: D, place: &mut Self) -> Result<(), D::Error> + where + D: Deserializer<'de>; + } + } +}