This commit is contained in:
open-trade 2020-09-08 16:18:33 +08:00
parent 8295278479
commit 2ee99e6140
3 changed files with 22 additions and 17 deletions

6
Cargo.lock generated
View File

@ -141,7 +141,7 @@ dependencies = [
[[package]]
name = "confy"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
source = "git+https://github.com/open-trade/confy#a95119c82d1e4e3f1d4b46bd668990eff5dce2c8"
dependencies = [
"directories 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.115 (registry+https://github.com/rust-lang/crates.io-index)",
@ -465,7 +465,7 @@ version = "0.1.0"
dependencies = [
"anyhow 1.0.32 (registry+https://github.com/rust-lang/crates.io-index)",
"bytes 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)",
"confy 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"confy 0.4.0 (git+https://github.com/open-trade/confy)",
"copypasta 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)",
"directories-next 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"dirs-next 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1722,7 +1722,7 @@ dependencies = [
"checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
"checksum clipboard-win 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e3a093d6fed558e5fe24c3dfc85a68bb68f1c824f440d3ba5aca189e2998786b"
"checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f"
"checksum confy 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2913470204e9e8498a0f31f17f90a0de801ae92c8c5ac18c49af4819e6786697"
"checksum confy 0.4.0 (git+https://github.com/open-trade/confy)" = "<none>"
"checksum constant_time_eq 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc"
"checksum copypasta 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "865e9675691e2a7dfc806b16ef2dd5dd536e26ea9b8046519767d79be03aeb6a"
"checksum core-foundation 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "57d24c7a13c43e870e37c1556b74555437870a04514f7685f5b354e090567171"

@ -1 +1 @@
Subproject commit 3ac832109bb75284a1d195a47300498e3bddc277
Subproject commit dc39de26981c2916c053762eef6961f6b659fb0c

View File

@ -27,6 +27,7 @@ use std::{
struct Peer {
socket_addr: SocketAddr,
last_reg_time: Instant,
uuid: Vec<u8>,
pk: Vec<u8>,
}
@ -37,6 +38,7 @@ impl Default for Peer {
last_reg_time: Instant::now()
.checked_sub(std::time::Duration::from_secs(3600))
.unwrap(),
uuid: Vec::new(),
pk: Vec::new(),
}
}
@ -47,6 +49,8 @@ struct PeerSerde {
#[serde(default)]
ip: String,
#[serde(default)]
uuid: Vec<u8>,
#[serde(default)]
pk: Vec<u8>,
}
@ -65,18 +69,19 @@ impl PeerMap {
}
#[inline]
fn update_pk(&mut self, id: String, socket_addr: SocketAddr, pk: Vec<u8>) {
fn update_pk(&mut self, id: String, socket_addr: SocketAddr, uuid: Vec<u8>, pk: Vec<u8>) {
let mut lock = self.map.write().unwrap();
lock.insert(
id.clone(),
Peer {
socket_addr,
last_reg_time: Instant::now(),
uuid: uuid.clone(),
pk: pk.clone(),
},
);
let ip = socket_addr.ip().to_string();
self.db.insert(id, PeerSerde { ip, pk });
self.db.insert(id, PeerSerde { ip, uuid, pk });
}
#[inline]
@ -91,6 +96,7 @@ impl PeerMap {
self.map.write().unwrap().insert(
id,
Peer {
uuid: v.uuid,
pk: v.pk,
..Default::default()
},
@ -189,16 +195,14 @@ impl RendezvousServer {
let id = rk.id;
let mut res = register_pk_response::Result::OK;
if let Some(peer) = self.pm.get(&id).await {
if peer.pk.is_empty() {
self.pm.update_pk(id, addr, rk.pk);
} else {
if peer.pk != rk.pk {
log::warn!("Peer {} pk mismatch: {:?} vs {:?}", id, rk.pk, peer.pk);
res = register_pk_response::Result::PK_MISMATCH;
}
if peer.uuid != rk.uuid {
log::warn!("Peer {} pk mismatch: {:?} vs {:?}", id, rk.uuid, peer.uuid);
res = register_pk_response::Result::UUID_MISMATCH;
} else if peer.pk != rk.pk {
self.pm.update_pk(id, addr, rk.uuid, rk.pk);
}
} else {
self.pm.update_pk(id, addr, rk.pk);
self.pm.update_pk(id, addr, rk.uuid, rk.pk);
}
let mut msg_out = RendezvousMessage::new();
msg_out.set_register_pk_response(RegisterPkResponse {
@ -260,11 +264,11 @@ impl RendezvousServer {
let tx = self.tx.clone();
tokio::spawn(async move {
let v = pm.db.get(id.clone()).await;
let pk = {
let (uuid, pk) = {
if let Some(v) = super::SledAsync::deserialize::<PeerSerde>(&v) {
v.pk
(v.uuid, v.pk)
} else {
Vec::new()
(Vec::new(), Vec::new())
}
};
let mut msg_out = RendezvousMessage::new();
@ -278,6 +282,7 @@ impl RendezvousServer {
Peer {
socket_addr,
last_reg_time,
uuid,
pk,
},
);