Add ability to specify our public addr for people to contact us back

This commit is contained in:
Alex 2020-12-11 15:53:59 +01:00
parent fd8c487b9b
commit 609f0387ca
5 changed files with 26 additions and 6 deletions

View file

@ -1,6 +1,8 @@
all:
cargo build
RUST_LOG=netapp=debug cargo run --example fullmesh -- -n 3242ce79e05e8b6a0e43441fbd140a906e13f335f298ae3a52f29784abbab500 -p 6c304114a0e1018bbe60502a34d33f4f439f370856c3333dda2726da01eb93a4894b7ef7249a71f11d342b69702f1beb7c93ec95fbcf122ad1eca583bb0629e7
cargo build --example fullmesh
cargo build --example basalt
#RUST_LOG=netapp=debug cargo run --example fullmesh -- -n 3242ce79e05e8b6a0e43441fbd140a906e13f335f298ae3a52f29784abbab500 -p 6c304114a0e1018bbe60502a34d33f4f439f370856c3333dda2726da01eb93a4894b7ef7249a71f11d342b69702f1beb7c93ec95fbcf122ad1eca583bb0629e7
docker_basalt:
cargo build --release --example basalt

View file

@ -31,6 +31,9 @@ pub struct Opt {
#[structopt(long = "listen-addr", short = "l", default_value = "127.0.0.1:1980")]
listen_addr: String,
#[structopt(long = "public-addr", short = "a")]
public_addr: Option<String>,
#[structopt(long = "view-size", short = "v", default_value = "100")]
view_size: usize,
@ -83,7 +86,8 @@ async fn main() {
info!("KYEV PK {}", hex::encode(&privkey.public_key()));
let listen_addr = opt.listen_addr.parse().unwrap();
let netapp = NetApp::new(listen_addr, netid, privkey);
let public_addr = opt.public_addr.map(|x| x.parse().unwrap());
let netapp = NetApp::new(listen_addr, public_addr, netid, privkey);
let mut bootstrap_peers = vec![];
for peer in opt.bootstrap_peers.iter() {

View file

@ -25,6 +25,9 @@ pub struct Opt {
#[structopt(long = "listen-addr", short = "l", default_value = "127.0.0.1:1980")]
listen_addr: String,
#[structopt(long = "public-addr", short = "a")]
public_addr: Option<String>,
}
#[tokio::main]
@ -63,7 +66,8 @@ async fn main() {
info!("Node public key: {}", hex::encode(&privkey.public_key()));
let listen_addr = opt.listen_addr.parse().unwrap();
let netapp = NetApp::new(listen_addr, netid, privkey);
let public_addr = opt.public_addr.map(|x| x.parse().unwrap());
let netapp = NetApp::new(listen_addr, public_addr, netid, privkey);
let mut bootstrap_peers = vec![];
for peer in opt.bootstrap_peers.iter() {

View file

@ -1,3 +1,5 @@
use std::net::IpAddr;
use serde::{Deserialize, Serialize};
pub type MessageKind = u32;
@ -24,6 +26,7 @@ pub trait Message: Serialize + for<'de> Deserialize<'de> + Send + Sync {
#[derive(Serialize, Deserialize)]
pub(crate) struct HelloMessage {
pub server_addr: Option<IpAddr>,
pub server_port: u16,
}

View file

@ -1,6 +1,6 @@
use std::any::Any;
use std::collections::HashMap;
use std::net::SocketAddr;
use std::net::{IpAddr, SocketAddr};
use std::pin::Pin;
use std::sync::{Arc, RwLock};
use std::time::Instant;
@ -48,6 +48,8 @@ pub(crate) struct Handler {
/// and RPS peering strategies take care of the most common use cases.
pub struct NetApp {
pub listen_addr: SocketAddr,
pub public_addr: Option<IpAddr>,
pub netid: auth::Key,
pub pubkey: ed25519::PublicKey,
pub privkey: ed25519::SecretKey,
@ -111,12 +113,14 @@ impl NetApp {
/// Creates a new instance of NetApp. No background process is
pub fn new(
listen_addr: SocketAddr,
public_addr: Option<IpAddr>,
netid: auth::Key,
privkey: ed25519::SecretKey,
) -> Arc<Self> {
let pubkey = privkey.public_key();
let netapp = Arc::new(Self {
listen_addr,
public_addr,
netid,
pubkey,
privkey,
@ -311,7 +315,9 @@ impl NetApp {
fn handle_hello_message(&self, id: ed25519::PublicKey, msg: HelloMessage) {
if let Some(h) = self.on_connected_handler.load().as_ref() {
if let Some(c) = self.server_conns.read().unwrap().get(&id) {
let remote_addr = SocketAddr::new(c.remote_addr.ip(), msg.server_port);
let remote_ip = msg.server_addr
.unwrap_or(c.remote_addr.ip());
let remote_addr = SocketAddr::new(remote_ip, msg.server_port);
h(id, remote_addr, true);
}
}
@ -357,9 +363,10 @@ impl NetApp {
h(conn.peer_pk, conn.remote_addr, false);
}
let server_addr = self.public_addr;
let server_port = self.listen_addr.port();
tokio::spawn(async move {
conn.request(HelloMessage { server_port }, PRIO_NORMAL)
conn.request(HelloMessage { server_addr, server_port }, PRIO_NORMAL)
.await
.log_err("Sending hello message");
});