Merge #383
383: remove the default random listening address r=ljedrz a=ljedrz Remove the default random listening address, listen to no addresses by default. Cc https://github.com/rs-ipfs/rust-ipfs/issues/356 Co-authored-by: ljedrz <ljedrz@gmail.com>
This commit is contained in:
commit
bb072f81fe
@ -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;
|
||||
@ -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)
|
||||
@ -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()?;
|
||||
@ -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
|
||||
@ -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)]
|
||||
|
@ -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);
|
||||
|
||||
@ -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();
|
||||
@ -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
|
||||
|
@ -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()],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user