diff --git a/Cargo.lock b/Cargo.lock index cd7b36c6..00993791 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1407,8 +1407,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8030735ecb0d128428b64cd379809817e620a40e5001c54465b99ec5feec2857" dependencies = [ "futures-core", - "prost", - "prost-types", + "prost 0.13.5", + "prost-types 0.13.5", "tonic", "tracing-core", ] @@ -1426,8 +1426,8 @@ dependencies = [ "hdrhistogram", "humantime", "hyper-util", - "prost", - "prost-types", + "prost 0.13.5", + "prost-types 0.13.5", "serde", "serde_json", "thread_local", @@ -2304,18 +2304,18 @@ dependencies = [ "ordered_hash_map", "parking_lot", "paste", + "pbjson", + "pbjson-build", "percent-encoding", - "petgraph 0.8.1", + "petgraph", "pin-project-lite", "pnet", "prefix-trie", "proc-macro2", - "prost", + "prost 0.14.3", "prost-build", "prost-reflect", "prost-reflect-build", - "prost-wkt", - "prost-wkt-build", "prost-wkt-types", "quinn", "quinn-plaintext", @@ -2907,12 +2907,6 @@ dependencies = [ "rustc_version", ] -[[package]] -name = "fixedbitset" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80" - [[package]] name = "fixedbitset" version = "0.5.7" @@ -4039,7 +4033,7 @@ dependencies = [ "libc", "percent-encoding", "pin-project-lite", - "socket2 0.5.10", + "socket2 0.6.3", "tokio", "tower-service", "tracing", @@ -4383,9 +4377,9 @@ dependencies = [ [[package]] name = "inventory" -version = "0.3.22" +version = "0.3.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "009ae045c87e7082cb72dab0ccd01ae075dd00141ddc108f43a0ea150a9e7227" +checksum = "a4f0c30c76f2f4ccee3fe55a2435f691ca00c0e4bd87abe4f4a851b1d4dac39b" dependencies = [ "rustversion", ] @@ -6156,6 +6150,28 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df94ce210e5bc13cb6651479fa48d14f601d9858cfe0467f43ae157023b938d3" +[[package]] +name = "pbjson" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8edd1efdd8ab23ba9cb9ace3d9987a72663d5d7c9f74fa00b51d6213645cf6c" +dependencies = [ + "base64 0.22.1", + "serde", +] + +[[package]] +name = "pbjson-build" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ed4d5c6ae95e08ac768883c8401cf0e8deb4e6e1d6a4e1fd3d2ec4f0ec63200" +dependencies = [ + "heck 0.5.0", + "itertools 0.14.0", + "prost 0.14.3", + "prost-types 0.14.3", +] + [[package]] name = "pbkdf2" version = "0.12.2" @@ -6191,23 +6207,13 @@ version = "2.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" -[[package]] -name = "petgraph" -version = "0.6.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4c5cc86750666a3ed20bdaf5ca2a0344f9c67674cae0515bec2da16fbaa47db" -dependencies = [ - "fixedbitset 0.4.2", - "indexmap 2.14.0", -] - [[package]] name = "petgraph" version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a98c6720655620a521dcc722d0ad66cd8afd5d86e34a89ef691c50b7b24de06" dependencies = [ - "fixedbitset 0.5.7", + "fixedbitset", "hashbrown 0.15.3", "indexmap 2.14.0", "serde", @@ -6787,24 +6793,33 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2796faa41db3ec313a31f7624d9286acf277b52de526150b7e69f3debf891ee5" dependencies = [ "bytes", - "prost-derive", + "prost-derive 0.13.5", +] + +[[package]] +name = "prost" +version = "0.14.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2ea70524a2f82d518bce41317d0fae74151505651af45faf1ffbd6fd33f0568" +dependencies = [ + "bytes", + "prost-derive 0.14.3", ] [[package]] name = "prost-build" -version = "0.13.5" +version = "0.14.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be769465445e8c1474e9c5dac2018218498557af32d9ed057325ec9a41ae81bf" +checksum = "343d3bd7056eda839b03204e68deff7d1b13aba7af2b2fd16890697274262ee7" dependencies = [ "heck 0.5.0", "itertools 0.14.0", "log", "multimap", - "once_cell", - "petgraph 0.6.5", + "petgraph", "prettyplease", - "prost", - "prost-types", + "prost 0.14.3", + "prost-types 0.14.3", "regex", "syn 2.0.117", "tempfile", @@ -6824,22 +6839,34 @@ dependencies = [ ] [[package]] -name = "prost-reflect" -version = "0.14.5" +name = "prost-derive" +version = "0.14.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e92b959d24e05a3e2da1d0beb55b48bc8a97059b8336ea617780bd6addbbfb5a" +checksum = "27c6023962132f4b30eb4c172c91ce92d933da334c59c23cddee82358ddafb0b" dependencies = [ - "once_cell", - "prost", + "anyhow", + "itertools 0.14.0", + "proc-macro2", + "quote", + "syn 2.0.117", +] + +[[package]] +name = "prost-reflect" +version = "0.16.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "590aa145fee8f7a26b5a6055365e7c5e89a5c1caae9869de76ec0ee73181a2f9" +dependencies = [ + "prost 0.14.3", "prost-reflect-derive", - "prost-types", + "prost-types 0.14.3", ] [[package]] name = "prost-reflect-build" -version = "0.14.0" +version = "0.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50e2537231d94dd2778920c2ada37dd9eb1ac0325bb3ee3ee651bd44c1134123" +checksum = "8214ae2c30bbac390db0134d08300e770ef89b6d4e5abf855e8d300eded87e28" dependencies = [ "prost-build", "prost-reflect", @@ -6847,9 +6874,9 @@ dependencies = [ [[package]] name = "prost-reflect-derive" -version = "0.14.0" +version = "0.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f4fce6b22f15cc8d8d400a2b98ad29202b33bd56c7d9ddd815bc803a807ecb65" +checksum = "7b6d90e29fa6c0d13c2c19ba5e4b3fb0efbf5975d27bcf4e260b7b15455bcabe" dependencies = [ "proc-macro2", "quote", @@ -6862,18 +6889,27 @@ version = "0.13.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "52c2c1bf36ddb1a1c396b3601a3cec27c2462e45f07c386894ec3ccf5332bd16" dependencies = [ - "prost", + "prost 0.13.5", +] + +[[package]] +name = "prost-types" +version = "0.14.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8991c4cbdb8bc5b11f0b074ffe286c30e523de90fee5ba8132f1399f23cb3dd7" +dependencies = [ + "prost 0.14.3", ] [[package]] name = "prost-wkt" -version = "0.6.1" +version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "497e1e938f0c09ef9cabe1d49437b4016e03e8f82fbbe5d1c62a9b61b9decae1" +checksum = "cd3de5e9c9e84fcb5efa204b8e283d23e615a8bc8c777bf1d6622bb01dc61445" dependencies = [ "chrono", "inventory", - "prost", + "prost 0.14.3", "serde", "serde_derive", "serde_json", @@ -6882,27 +6918,27 @@ dependencies = [ [[package]] name = "prost-wkt-build" -version = "0.6.1" +version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07b8bf115b70a7aa5af1fd5d6e9418492e9ccb6e4785e858c938e28d132a884b" +checksum = "fe500dc80e757a75e1e8fb7290e448d62dfba3105ece1d058579cb00b58151cd" dependencies = [ "heck 0.5.0", - "prost", + "prost 0.14.3", "prost-build", - "prost-types", + "prost-types 0.14.3", "quote", ] [[package]] name = "prost-wkt-types" -version = "0.6.1" +version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8cdde6df0a98311c839392ca2f2f0bcecd545f86a62b4e3c6a49c336e970fe5" +checksum = "13807eaa7e15833d06e899008371926201cdcd11d74b6d490f49130cdb3f415e" dependencies = [ "chrono", - "prost", + "prost 0.14.3", "prost-build", - "prost-types", + "prost-types 0.14.3", "prost-wkt", "prost-wkt-build", "regex", @@ -10039,7 +10075,7 @@ dependencies = [ "hyper-util", "percent-encoding", "pin-project", - "prost", + "prost 0.13.5", "socket2 0.5.10", "tokio", "tokio-stream", @@ -10323,7 +10359,7 @@ checksum = "b8765b90061cba6c22b5831f675da109ae5561588290f9fa2317adab2714d5a6" dependencies = [ "memchr", "nom 8.0.0", - "petgraph 0.8.1", + "petgraph", ] [[package]] @@ -10383,9 +10419,9 @@ checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" [[package]] name = "typetag" -version = "0.2.21" +version = "0.2.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be2212c8a9b9bcfca32024de14998494cf9a5dfa59ea1b829de98bac374b86bf" +checksum = "c5a897b12c6c1151ad0b138b8db50252dc301f93bc3b027db05eec82aeed298c" dependencies = [ "erased-serde", "inventory", @@ -10396,9 +10432,9 @@ dependencies = [ [[package]] name = "typetag-impl" -version = "0.2.21" +version = "0.2.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "27a7a9b72ba121f6f1f6c3632b85604cac41aedb5ddc70accbebb6cac83de846" +checksum = "cf808357c6ed7e13ba0f3277ec8d8f21b2d501274895104263985330c726c1c5" dependencies = [ "proc-macro2", "quote", diff --git a/easytier/Cargo.toml b/easytier/Cargo.toml index 80ed550a..a2fdb63b 100644 --- a/easytier/Cargo.toml +++ b/easytier/Cargo.toml @@ -127,9 +127,11 @@ uuid = { version = "1.5.0", features = [ once_cell = "1.18.0" # for rpc -prost = "0.13.5" -prost-wkt = "0.6" -prost-wkt-types = "0.6" +prost = "0.14.3" +prost-reflect = { version = "0.16.4", default-features = false, features = ["derive"] } +prost-wkt-types = "0.7.1" +pbjson = "0.9.0" + anyhow = "1.0" url = { version = "2.5", features = ["serde"] } @@ -226,10 +228,6 @@ zstd = { version = "0.13", optional = true } kcp-sys = { git = "https://github.com/EasyTier/kcp-sys", rev = "d7427c22d764deb1860a7d37acc446ed5033464c", optional = true } -prost-reflect = { version = "0.14.5", default-features = false, features = [ - "derive", -] } - # for http connector http_req = { git = "https://github.com/EasyTier/http_req.git", default-features = false, features = [ "rust-tls", @@ -320,9 +318,9 @@ cfg_aliases = "0.2.1" indoc = "2.0" globwalk = "0.8.1" regex = "1" -prost-build = "0.13.5" -prost-wkt-build = "0.6" -prost-reflect-build = { version = "0.14.0" } +prost-build = "0.14.3" +prost-reflect-build = "0.16.0" +pbjson-build = "0.9.0" proc-macro2 = "1" quote = "1" thunk-rs = { git = "https://github.com/easytier/thunk.git", default-features = false, features = [ diff --git a/easytier/build/main.rs b/easytier/build/main.rs index 8d111d80..49cbdbae 100644 --- a/easytier/build/main.rs +++ b/easytier/build/main.rs @@ -2,7 +2,6 @@ mod rpc; use crate::rpc::ServiceGenerator; use cfg_aliases::cfg_aliases; -use prost_wkt_build::{FileDescriptorSet, Message as _}; #[cfg(target_os = "windows")] use std::io::Cursor; use std::{env, path::PathBuf}; @@ -174,32 +173,15 @@ fn main() -> Result<(), Box> { println!("cargo:rerun-if-changed={proto_file}"); } - let out = PathBuf::from(env::var("OUT_DIR").unwrap()); - let descriptor_file = out.join("descriptors.bin"); + let out = PathBuf::from(env::var("OUT_DIR")?); + let descriptor = out.join("descriptors.bin"); let mut config = prost_build::Config::new(); config - .type_attribute(".", "#[derive(serde::Serialize,serde::Deserialize)]") .extern_path(".google.protobuf.Any", "::prost_wkt_types::Any") .extern_path(".google.protobuf.Timestamp", "::prost_wkt_types::Timestamp") .extern_path(".google.protobuf.Value", "::prost_wkt_types::Value") - .file_descriptor_set_path(&descriptor_file) - .protoc_arg("--experimental_allow_proto3_optional") - .type_attribute("peer_rpc.DirectConnectedPeerInfo", "#[derive(Hash)]") - .type_attribute("peer_rpc.PeerInfoForGlobalMap", "#[derive(Hash)]") - .type_attribute("peer_rpc.ForeignNetworkRouteInfoKey", "#[derive(Hash, Eq)]") - .type_attribute( - "peer_rpc.RouteForeignNetworkSummary.Info", - "#[derive(Hash, Eq)]", - ) - .type_attribute("peer_rpc.RouteForeignNetworkSummary", "#[derive(Hash, Eq)]") - .type_attribute("common.RpcDescriptor", "#[derive(Hash, Eq)]") - .type_attribute("acl.Acl", "#[serde(default)]") - .type_attribute("acl.AclV1", "#[serde(default)]") - .type_attribute("acl.Chain", "#[serde(default)]") - .type_attribute("acl.Rule", "#[serde(default)]") - .type_attribute("acl.GroupInfo", "#[serde(default)]") - .field_attribute(".api.manage.NetworkConfig", "#[serde(default)]") + .file_descriptor_set_path(&descriptor) .service_generator(Box::new(ServiceGenerator::default())) .btree_map(["."]) .skip_debug([".common.Ipv4Addr", ".common.Ipv6Addr", ".common.UUID"]); @@ -210,9 +192,12 @@ fn main() -> Result<(), Box> { .file_descriptor_set_bytes("crate::proto::DESCRIPTOR_POOL_BYTES") .compile_protos_with_config(config, &proto_files_reflect, &["src/proto/"])?; - let descriptor_bytes = std::fs::read(descriptor_file).unwrap(); - let descriptor = FileDescriptorSet::decode(&descriptor_bytes[..]).unwrap(); - prost_wkt_build::add_serde(out, descriptor); + let descriptor = std::fs::read(descriptor)?; + pbjson_build::Builder::new() + .register_descriptors(&descriptor)? + .preserve_proto_field_names() + .btree_map(["."]) + .build(&["."])?; check_locale(); Ok(()) diff --git a/easytier/src/common/config.rs b/easytier/src/common/config.rs index fc1977c2..e997a2be 100644 --- a/easytier/src/common/config.rs +++ b/easytier/src/common/config.rs @@ -636,21 +636,12 @@ impl TomlConfigLoader { Ok(ret) } - fn gen_flags(mut flags_hashmap: serde_json::Map) -> Flags { - let default_flags_json = serde_json::to_string(&gen_default_flags()).unwrap(); - let default_flags_hashmap = - serde_json::from_str::>(&default_flags_json) - .unwrap(); - - let mut merged_hashmap = serde_json::Map::new(); - for (key, value) in default_flags_hashmap { - if let Some(v) = flags_hashmap.remove(&key) { - merged_hashmap.insert(key, v); - } else { - merged_hashmap.insert(key, value); - } - } - + fn gen_flags(flags_hashmap: serde_json::Map) -> Flags { + let mut merged_hashmap = match serde_json::to_value(gen_default_flags()) { + Ok(serde_json::Value::Object(map)) => map, + _ => serde_json::Map::new(), + }; + merged_hashmap.extend(flags_hashmap); serde_json::from_value(serde_json::Value::Object(merged_hashmap)).unwrap() } } diff --git a/easytier/src/peers/peer_conn.rs b/easytier/src/peers/peer_conn.rs index 5e2b6d55..d7fdbde8 100644 --- a/easytier/src/peers/peer_conn.rs +++ b/easytier/src/peers/peer_conn.rs @@ -654,7 +654,7 @@ impl PeerConn { .and_then(|p| p.peer_public_key) } - async fn send_noise_msg( + async fn send_noise_msg( &self, pb: Msg, packet_type: PacketType, diff --git a/easytier/src/peers/peer_manager.rs b/easytier/src/peers/peer_manager.rs index d5502ac4..43a2a6c7 100644 --- a/easytier/src/peers/peer_manager.rs +++ b/easytier/src/peers/peer_manager.rs @@ -1397,7 +1397,7 @@ impl PeerManager { let f = OneForeignNetwork { network_name: info.key.as_ref().unwrap().network_name.clone(), peer_ids: route_info.foreign_peer_ids.clone(), - last_updated: format!("{}", route_info.last_update.unwrap()), + last_updated: serde_json::to_string(&route_info.last_update.unwrap()).unwrap(), version: route_info.version, }; diff --git a/easytier/src/peers/peer_ospf_route.rs b/easytier/src/peers/peer_ospf_route.rs index 6081c0cd..4011c78b 100644 --- a/easytier/src/peers/peer_ospf_route.rs +++ b/easytier/src/peers/peer_ospf_route.rs @@ -73,7 +73,9 @@ use super::{ }, }; +use crate::proto::common::TimestampExt; use atomic_shim::AtomicU64; +use prost_wkt_types::Timestamp; static SERVICE_ID: u32 = 7; static UPDATE_PEER_INFO_PERIOD: Duration = Duration::from_secs(3600); @@ -757,7 +759,7 @@ impl SyncedRouteInfo { if !guard.contains_key(peer_id) { let mut peer_info = RoutePeerInfo::new(); let mut guard = RwLockUpgradableReadGuard::upgrade(guard); - peer_info.last_update = Some(SystemTime::now().into()); + peer_info.last_update = Some(Timestamp::now()); guard.insert(*peer_id, peer_info); need_inc_version = true; } else { @@ -867,7 +869,7 @@ impl SyncedRouteInfo { let mut guard = self.peer_infos.write(); // time between peers may not be synchronized, so update last_update to local now. // note only last_update with larger version will be updated to local saved peer info. - route_info.last_update = Some(SystemTime::now().into()); + route_info.last_update = Some(Timestamp::now()); if guard .get_mut(&route_info.peer_id) .is_none_or(|old| route_info.version > old.version) @@ -962,7 +964,7 @@ impl SyncedRouteInfo { continue; }; - entry.last_update = Some(SystemTime::now().into()); + entry.last_update = Some(Timestamp::now()); self.foreign_network .entry(key.clone()) @@ -1010,7 +1012,7 @@ impl SyncedRouteInfo { }; guard.with_upgraded(|peer_infos| { - new.last_update = Some(SystemTime::now().into()); + new.last_update = Some(Timestamp::now()); new.version = new_version; peer_infos.insert(my_peer_id, new) }); @@ -1086,7 +1088,7 @@ impl SyncedRouteInfo { foreign_networks.remove(key).unwrap(); } else if !item.foreign_peer_ids.is_empty() { item.foreign_peer_ids.clear(); - item.last_update = Some(SystemTime::now().into()); + item.last_update = Some(Timestamp::now()); item.version = std::cmp::max(item.version + 1, now_version); updated = true; } @@ -2542,7 +2544,7 @@ impl PeerRouteServiceImpl { for (peer_id, peer_info) in peer_infos.iter().rev() { // stop iter if last_update of peer info is older than session.last_sync_succ_timestamp if let Some(last_update) = peer_info.last_update { - let last_update = TryInto::::try_into(last_update).unwrap(); + let last_update = SystemTime::try_from(last_update).unwrap(); if last_sync_succ_timestamp.is_some_and(|t| last_update < t) { break; } @@ -4158,7 +4160,9 @@ mod tests { use dashmap::DashMap; use parking_lot::Mutex; use prefix_trie::PrefixMap; + use prost::Message; use prost_reflect::{DynamicMessage, ReflectMessage}; + use prost_wkt_types::Timestamp; use std::net::IpAddr; use std::{ collections::{BTreeSet, HashMap}, @@ -4170,6 +4174,7 @@ mod tests { }; use super::{NextHopInfo, PeerRoute, REMOVE_DEAD_PEER_INFO_AFTER, RouteConnInfo}; + use crate::proto::common::TimestampExt; use crate::{ common::{ PeerId, @@ -4198,7 +4203,7 @@ mod tests { }, tunnel::common::tests::wait_for_condition, }; - use prost::Message; + struct AuthOnlyInterface { my_peer_id: PeerId, identity_type: DashMap, @@ -5489,7 +5494,7 @@ mod tests { ); let mut self_info = self_info; self_info.version = 1; - self_info.last_update = Some(SystemTime::now().into()); + self_info.last_update = Some(Timestamp::now()); { let mut guard = service_impl.synced_route_info.peer_infos.write(); guard.insert(service_impl.my_peer_id, self_info); diff --git a/easytier/src/proto/acl.rs b/easytier/src/proto/acl.rs index 03d11866..a4cf5854 100644 --- a/easytier/src/proto/acl.rs +++ b/easytier/src/proto/acl.rs @@ -1,6 +1,7 @@ use std::fmt::Display; include!(concat!(env!("OUT_DIR"), "/acl.rs")); +include!(concat!(env!("OUT_DIR"), "/acl.serde.rs")); impl Acl { pub fn is_empty(&self) -> bool { diff --git a/easytier/src/proto/api.rs b/easytier/src/proto/api.rs index d3b81be6..010ed25b 100644 --- a/easytier/src/proto/api.rs +++ b/easytier/src/proto/api.rs @@ -1,5 +1,7 @@ pub mod config { include!(concat!(env!("OUT_DIR"), "/api.config.rs")); + include!(concat!(env!("OUT_DIR"), "/api.config.serde.rs")); + pub struct Patchable { pub action: Option, pub value: Option, @@ -77,6 +79,7 @@ pub mod config { pub mod instance { include!(concat!(env!("OUT_DIR"), "/api.instance.rs")); + include!(concat!(env!("OUT_DIR"), "/api.instance.serde.rs")); impl PeerRoutePair { pub fn get_latency_ms(&self) -> Option { @@ -229,10 +232,12 @@ pub mod instance { pub mod logger { include!(concat!(env!("OUT_DIR"), "/api.logger.rs")); + include!(concat!(env!("OUT_DIR"), "/api.logger.serde.rs")); } pub mod manage { include!(concat!(env!("OUT_DIR"), "/api.manage.rs")); + include!(concat!(env!("OUT_DIR"), "/api.manage.serde.rs")); } #[cfg(test)] diff --git a/easytier/src/proto/common.rs b/easytier/src/proto/common.rs index 067560ce..df955735 100644 --- a/easytier/src/proto/common.rs +++ b/easytier/src/proto/common.rs @@ -1,15 +1,26 @@ +use anyhow::Context; +use base64::{Engine as _, prelude::BASE64_STANDARD}; +use std::time::SystemTime; use std::{ fmt::{self, Display}, str::FromStr, }; - -use anyhow::Context; -use base64::{Engine as _, prelude::BASE64_STANDARD}; use strum::VariantArray; use crate::tunnel::{IpScheme, packet_def::CompressorAlgo}; include!(concat!(env!("OUT_DIR"), "/common.rs")); +include!(concat!(env!("OUT_DIR"), "/common.serde.rs")); + +pub trait TimestampExt { + fn now() -> Self; +} + +impl TimestampExt for prost_wkt_types::Timestamp { + fn now() -> Self { + SystemTime::now().into() + } +} impl From for Uuid { fn from(uuid: uuid::Uuid) -> Self { diff --git a/easytier/src/proto/error.rs b/easytier/src/proto/error.rs index d5d6607e..712b4fe2 100644 --- a/easytier/src/proto/error.rs +++ b/easytier/src/proto/error.rs @@ -1,10 +1,9 @@ #![allow(clippy::module_inception)] -use prost::DecodeError; - use super::rpc_types; include!(concat!(env!("OUT_DIR"), "/error.rs")); +include!(concat!(env!("OUT_DIR"), "/error.serde.rs")); impl From<&rpc_types::error::Error> for Error { fn from(e: &rpc_types::error::Error) -> Self { @@ -15,10 +14,10 @@ impl From<&rpc_types::error::Error> for Error { error_message: format!("{:?}", e), })), }, - rpc_types::error::Error::DecodeError(_) => Self { + rpc_types::error::Error::DecodeError => Self { error_kind: Some(ProtoError::ProstDecodeError(ProstDecodeError {})), }, - rpc_types::error::Error::EncodeError(_) => Self { + rpc_types::error::Error::EncodeError => Self { error_kind: Some(ProtoError::ProstEncodeError(ProstEncodeError {})), }, rpc_types::error::Error::InvalidMethodIndex(m, s) => Self { @@ -59,12 +58,8 @@ impl From<&Error> for rpc_types::error::Error { Some(ProtoError::ExecuteError(e)) => { Self::ExecutionError(anyhow::anyhow!(e.error_message.clone())) } - Some(ProtoError::ProstDecodeError(_)) => { - Self::DecodeError(DecodeError::new("decode error")) - } - Some(ProtoError::ProstEncodeError(_)) => { - Self::DecodeError(DecodeError::new("encode error")) - } + Some(ProtoError::ProstDecodeError(_)) => Self::DecodeError, + Some(ProtoError::ProstEncodeError(_)) => Self::EncodeError, Some(ProtoError::InvalidMethodIndex(e)) => { Self::InvalidMethodIndex(e.method_index as u8, e.service_name.clone()) } diff --git a/easytier/src/proto/magic_dns.rs b/easytier/src/proto/magic_dns.rs index f87812da..f78dd08a 100644 --- a/easytier/src/proto/magic_dns.rs +++ b/easytier/src/proto/magic_dns.rs @@ -1 +1,2 @@ include!(concat!(env!("OUT_DIR"), "/magic_dns.rs")); +include!(concat!(env!("OUT_DIR"), "/magic_dns.serde.rs")); diff --git a/easytier/src/proto/peer_rpc.rs b/easytier/src/proto/peer_rpc.rs index aa3d091e..142129c7 100644 --- a/easytier/src/proto/peer_rpc.rs +++ b/easytier/src/proto/peer_rpc.rs @@ -5,6 +5,7 @@ use sha2::Sha256; use crate::common::PeerId; include!(concat!(env!("OUT_DIR"), "/peer_rpc.rs")); +include!(concat!(env!("OUT_DIR"), "/peer_rpc.serde.rs")); impl PeerGroupInfo { pub fn generate_with_proof(group_name: String, group_secret: String, peer_id: PeerId) -> Self { diff --git a/easytier/src/proto/rpc_types/error.rs b/easytier/src/proto/rpc_types/error.rs index 351bb461..8a4df29b 100644 --- a/easytier/src/proto/rpc_types/error.rs +++ b/easytier/src/proto/rpc_types/error.rs @@ -9,11 +9,11 @@ pub enum Error { #[error("Rust error: {0}")] ExecutionError(#[from] anyhow::Error), - #[error("Decode error: {0}")] - DecodeError(#[from] prost::DecodeError), + #[error("Decode error")] + DecodeError, - #[error("Encode error: {0}")] - EncodeError(#[from] prost::EncodeError), + #[error("Encode error")] + EncodeError, #[error("Invalid method index: {0}, service: {1}")] InvalidMethodIndex(u8, String), @@ -34,4 +34,16 @@ pub enum Error { Shutdown, } +impl From for Error { + fn from(_: prost::DecodeError) -> Self { + Error::DecodeError + } +} + +impl From for Error { + fn from(_: prost::EncodeError) -> Self { + Error::EncodeError + } +} + pub type Result = result::Result; diff --git a/easytier/src/proto/tests.rs b/easytier/src/proto/tests.rs index 89258a77..4bc4f6c0 100644 --- a/easytier/src/proto/tests.rs +++ b/easytier/src/proto/tests.rs @@ -1,4 +1,5 @@ include!(concat!(env!("OUT_DIR"), "/tests.rs")); +include!(concat!(env!("OUT_DIR"), "/tests.serde.rs")); use std::sync::{Arc, Mutex}; diff --git a/easytier/src/proto/web.rs b/easytier/src/proto/web.rs index a3254ec7..2058b6df 100644 --- a/easytier/src/proto/web.rs +++ b/easytier/src/proto/web.rs @@ -1 +1,2 @@ include!(concat!(env!("OUT_DIR"), "/web.rs")); +include!(concat!(env!("OUT_DIR"), "/web.serde.rs"));