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

Commit bb072f8

Browse files
bors[bot]ljedrz
andauthored
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 #356 Co-authored-by: ljedrz <[email protected]>
2 parents 335a23a + d0297c0 commit bb072f8

File tree

4 files changed

+32
-14
lines changed

4 files changed

+32
-14
lines changed

http/src/config.rs

+20-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! go-ipfs compatible configuration file handling or at least setup.
22
3+
use parity_multiaddr::Multiaddr;
34
use serde::{Deserialize, Serialize};
45
use std::fs::{self, File};
56
use std::num::NonZeroU16;
@@ -115,6 +116,9 @@ fn create(
115116
peer_id,
116117
private_key,
117118
},
119+
addresses: Addresses {
120+
swarm: vec!["/ip4/127.0.0.1/tcp/0".parse().unwrap()],
121+
},
118122
};
119123

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

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

148-
let CompatibleConfigFile { identity } = serde_json::from_reader(BufReader::new(config))
153+
let CompatibleConfigFile {
154+
identity,
155+
addresses,
156+
} = serde_json::from_reader(BufReader::new(config))
149157
.map_err(LoadingError::ConfigurationFileFormat)?;
150158

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

162-
Ok(kp)
170+
Ok((kp, addresses.swarm))
163171
}
164172

165173
/// 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> {
230238
#[serde(rename_all = "PascalCase")]
231239
struct CompatibleConfigFile {
232240
identity: Identity,
241+
addresses: Addresses,
242+
}
243+
244+
#[derive(Debug, Serialize, Deserialize)]
245+
#[serde(rename_all = "PascalCase")]
246+
struct Addresses {
247+
swarm: Vec<Multiaddr>,
233248
}
234249

235250
#[derive(Debug, Serialize, Deserialize)]

http/src/main.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ fn main() {
5959

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

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

@@ -73,7 +73,7 @@ fn main() {
7373

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

137137
rt.block_on(async move {
138-
let opts: IpfsOptions =
139-
IpfsOptions::new(home.clone(), keypair, Vec::new(), false, None, Vec::new());
138+
let opts: IpfsOptions = IpfsOptions::new(
139+
home.clone(),
140+
keypair,
141+
Vec::new(),
142+
false,
143+
None,
144+
listening_addrs,
145+
);
140146

141147
let (ipfs, task): (Ipfs<ipfs::Types>, _) = UninitializedIpfs::new(opts, None)
142148
.await

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ impl IpfsOptions {
140140
bootstrap: Default::default(),
141141
// default to lan kad for go-ipfs use in tests
142142
kad_protocol: Some("/ipfs/lan/kad/1.0.0".to_owned()),
143-
listening_addrs: Vec::new(),
143+
listening_addrs: vec!["/ip4/127.0.0.1/tcp/0".parse().unwrap()],
144144
}
145145
}
146146
}

src/p2p/mod.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,10 @@ pub async fn create_swarm<TIpfsTypes: IpfsTypes>(
5959
let behaviour = behaviour::build_behaviour(options, ipfs).await;
6060

6161
// Create a Swarm
62-
let mut swarm = libp2p::swarm::SwarmBuilder::new(transport, behaviour, peer_id)
62+
let swarm = libp2p::swarm::SwarmBuilder::new(transport, behaviour, peer_id)
6363
.executor(Box::new(SpannedExecutor(swarm_span)))
6464
.build();
6565

66-
// Listen on all interfaces and whatever port the OS assigns
67-
Swarm::listen_on(&mut swarm, "/ip4/127.0.0.1/tcp/0".parse().unwrap()).unwrap();
68-
6966
Ok(swarm)
7067
}
7168

0 commit comments

Comments
 (0)