Full mesh peering strategy uses our local address if necessary

This commit is contained in:
Alex 2021-10-15 15:34:03 +02:00
parent 48d6a72ebd
commit dfb0ebb8e1
No known key found for this signature in database
GPG key ID: EDABF9711E244EB1
2 changed files with 30 additions and 5 deletions

View file

@ -1,4 +1,5 @@
use std::io::Write; use std::io::Write;
use std::net::SocketAddr;
use log::info; use log::info;
@ -65,6 +66,11 @@ async fn main() {
info!("Node private key: {}", hex::encode(&privkey)); info!("Node private key: {}", hex::encode(&privkey));
info!("Node public key: {}", hex::encode(&privkey.public_key())); info!("Node public key: {}", hex::encode(&privkey.public_key()));
let public_addr = opt.public_addr.map(|x| x.parse().unwrap());
let listen_addr: SocketAddr = opt.listen_addr.parse().unwrap();
info!("Node public address: {:?}", public_addr);
info!("Node listen address: {}", listen_addr);
let netapp = NetApp::new(netid.clone(), privkey.clone()); let netapp = NetApp::new(netid.clone(), privkey.clone());
let mut bootstrap_peers = vec![]; let mut bootstrap_peers = vec![];
@ -72,9 +78,11 @@ async fn main() {
bootstrap_peers.push(parse_peer_addr(peer).expect("Invalid peer address")); bootstrap_peers.push(parse_peer_addr(peer).expect("Invalid peer address"));
} }
let peering = FullMeshPeeringStrategy::new(netapp.clone(), bootstrap_peers); let peering = FullMeshPeeringStrategy::new(
netapp.clone(),
let listen_addr = opt.listen_addr.parse().unwrap(); bootstrap_peers,
public_addr.map(|a| SocketAddr::new(a, listen_addr.port())),
);
info!("Add more peers to this mesh by running: fullmesh -n {} -l 127.0.0.1:$((1000 + $RANDOM)) -b {}@{}", info!("Add more peers to this mesh by running: fullmesh -n {} -l 127.0.0.1:$((1000 + $RANDOM)) -b {}@{}",
hex::encode(&netid), hex::encode(&netid),
@ -83,7 +91,6 @@ async fn main() {
let watch_cancel = netapp::util::watch_ctrl_c(); let watch_cancel = netapp::util::watch_ctrl_c();
let public_addr = opt.public_addr.map(|x| x.parse().unwrap());
tokio::join!( tokio::join!(
netapp.listen(listen_addr, public_addr, watch_cancel.clone()), netapp.listen(listen_addr, public_addr, watch_cancel.clone()),
peering.run(watch_cancel), peering.run(watch_cancel),

View file

@ -163,7 +163,11 @@ impl FullMeshPeeringStrategy {
/// The strategy will not be run until `.run()` is called and awaited. /// The strategy will not be run until `.run()` is called and awaited.
/// Once that happens, the peering strategy will try to connect /// Once that happens, the peering strategy will try to connect
/// to all of the nodes specified in the bootstrap list. /// to all of the nodes specified in the bootstrap list.
pub fn new(netapp: Arc<NetApp>, bootstrap_list: Vec<(NodeID, SocketAddr)>) -> Arc<Self> { pub fn new(
netapp: Arc<NetApp>,
bootstrap_list: Vec<(NodeID, SocketAddr)>,
our_addr: Option<SocketAddr>,
) -> Arc<Self> {
let mut known_hosts = KnownHosts::new(); let mut known_hosts = KnownHosts::new();
for (id, addr) in bootstrap_list { for (id, addr) in bootstrap_list {
if id != netapp.id { if id != netapp.id {
@ -179,6 +183,18 @@ impl FullMeshPeeringStrategy {
} }
} }
if let Some(addr) = our_addr {
known_hosts.list.insert(
netapp.id,
PeerInfoInternal {
addr,
state: PeerConnState::Ourself,
last_seen: None,
ping: VecDeque::new(),
},
);
}
let strat = Arc::new(Self { let strat = Arc::new(Self {
netapp: netapp.clone(), netapp: netapp.clone(),
known_hosts: RwLock::new(known_hosts), known_hosts: RwLock::new(known_hosts),
@ -188,6 +204,8 @@ impl FullMeshPeeringStrategy {
peer_list_endpoint: netapp.endpoint("__netapp/peering/fullmesh.rs/PeerList".into()), peer_list_endpoint: netapp.endpoint("__netapp/peering/fullmesh.rs/PeerList".into()),
}); });
strat.update_public_peer_list(&strat.known_hosts.read().unwrap());
strat.ping_endpoint.set_handler(strat.clone()); strat.ping_endpoint.set_handler(strat.clone());
strat.peer_list_endpoint.set_handler(strat.clone()); strat.peer_list_endpoint.set_handler(strat.clone());