Skip to content
This repository was archived by the owner on Oct 23, 2022. It is now read-only.

remove the default random listening address #383

Merged
merged 3 commits into from
Sep 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions http/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! go-ipfs compatible configuration file handling or at least setup.

use parity_multiaddr::Multiaddr;
use serde::{Deserialize, Serialize};
use std::fs::{self, File};
use std::num::NonZeroU16;
Expand Down Expand Up @@ -115,6 +116,9 @@ fn create(
peer_id,
private_key,
},
addresses: Addresses {
swarm: vec!["/ip4/127.0.0.1/tcp/0".parse().unwrap()],
},
};

serde_json::to_writer_pretty(BufWriter::new(config), &config_contents)
Expand All @@ -140,12 +144,16 @@ pub enum LoadingError {

/// Loads a `go-ipfs` compatible configuration file from the given file.
///
/// Returns only the [`ipfs::KeyPair`] or [`LoadingError`] but this should be extended to contain
/// the bootstrap nodes at least later when we need to support those for testing purposes.
pub fn load(config: File) -> Result<ipfs::Keypair, LoadingError> {
/// Returns only the keypair and listening addresses or [`LoadingError`] but this should be
/// extended to contain the bootstrap nodes at least later when we need to support those for
/// testing purposes.
pub fn load(config: File) -> Result<(ipfs::Keypair, Vec<Multiaddr>), LoadingError> {
use std::io::BufReader;

let CompatibleConfigFile { identity } = serde_json::from_reader(BufReader::new(config))
let CompatibleConfigFile {
identity,
addresses,
} = serde_json::from_reader(BufReader::new(config))
.map_err(LoadingError::ConfigurationFileFormat)?;

let kp = identity.load_keypair()?;
Expand All @@ -159,7 +167,7 @@ pub fn load(config: File) -> Result<ipfs::Keypair, LoadingError> {
});
}

Ok(kp)
Ok((kp, addresses.swarm))
}

/// Converts a PEM format to DER where PEM is a container for Base64 data with padding, starting on
Expand Down Expand Up @@ -230,6 +238,13 @@ fn pem_to_der(bytes: &[u8]) -> Vec<u8> {
#[serde(rename_all = "PascalCase")]
struct CompatibleConfigFile {
identity: Identity,
addresses: Addresses,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
struct Addresses {
swarm: Vec<Multiaddr>,
}

#[derive(Debug, Serialize, Deserialize)]
Expand Down
14 changes: 10 additions & 4 deletions http/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ fn main() {

let config_path = home.join("config");

let keypair = match opts {
let (keypair, listening_addrs) = match opts {
Options::Init { bits, profile } => {
println!("initializing IPFS node at {:?}", home);

Expand All @@ -73,7 +73,7 @@ fn main() {

match result {
Ok(_) => {
let kp = std::fs::File::open(config_path)
let (kp, _) = std::fs::File::open(config_path)
.map_err(config::LoadingError::ConfigurationFileOpening)
.and_then(config::load)
.unwrap();
Expand Down Expand Up @@ -135,8 +135,14 @@ fn main() {
let mut rt = tokio::runtime::Runtime::new().expect("Failed to create event loop");

rt.block_on(async move {
let opts: IpfsOptions =
IpfsOptions::new(home.clone(), keypair, Vec::new(), false, None, Vec::new());
let opts: IpfsOptions = IpfsOptions::new(
home.clone(),
keypair,
Vec::new(),
false,
None,
listening_addrs,
);

let (ipfs, task): (Ipfs<ipfs::Types>, _) = UninitializedIpfs::new(opts, None)
.await
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl IpfsOptions {
bootstrap: Default::default(),
// default to lan kad for go-ipfs use in tests
kad_protocol: Some("/ipfs/lan/kad/1.0.0".to_owned()),
listening_addrs: Vec::new(),
listening_addrs: vec!["/ip4/127.0.0.1/tcp/0".parse().unwrap()],
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions src/p2p/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,10 @@ pub async fn create_swarm<TIpfsTypes: IpfsTypes>(
let behaviour = behaviour::build_behaviour(options, ipfs).await;

// Create a Swarm
let mut swarm = libp2p::swarm::SwarmBuilder::new(transport, behaviour, peer_id)
let swarm = libp2p::swarm::SwarmBuilder::new(transport, behaviour, peer_id)
.executor(Box::new(SpannedExecutor(swarm_span)))
.build();

// Listen on all interfaces and whatever port the OS assigns
Swarm::listen_on(&mut swarm, "/ip4/127.0.0.1/tcp/0".parse().unwrap()).unwrap();

Ok(swarm)
}

Expand Down