1
1
//! go-ipfs compatible configuration file handling and setup.
2
2
3
- use parity_multiaddr:: Multiaddr ;
3
+ use parity_multiaddr:: { multiaddr , Multiaddr } ;
4
4
use serde:: { Deserialize , Serialize } ;
5
5
use std:: fs:: { self , File } ;
6
6
use std:: num:: NonZeroU16 ;
7
7
use std:: path:: Path ;
8
+ use std:: str:: FromStr ;
9
+ use structopt:: StructOpt ;
8
10
use thiserror:: Error ;
9
11
10
12
/// Temporary module required to de/ser config files base64'd protobuf rsa private key format.
@@ -13,6 +15,25 @@ mod keys_proto {
13
15
include ! ( concat!( env!( "OUT_DIR" ) , "/keys_proto.rs" ) ) ;
14
16
}
15
17
18
+ #[ derive( Debug , StructOpt ) ]
19
+ pub enum Profile {
20
+ Test ,
21
+ Default ,
22
+ }
23
+
24
+ // Required for structopt.
25
+ impl FromStr for Profile {
26
+ type Err = InitializationError ;
27
+
28
+ fn from_str ( profile : & str ) -> Result < Self , Self :: Err > {
29
+ match profile {
30
+ "test" => Ok ( Profile :: Test ) ,
31
+ "default" => Ok ( Profile :: Default ) ,
32
+ _ => Err ( InitializationError :: InvalidProfile ( profile. to_string ( ) ) ) ,
33
+ }
34
+ }
35
+ }
36
+
16
37
/// The way things can go wrong when calling [`initialize`].
17
38
#[ derive( Error , Debug ) ]
18
39
pub enum InitializationError {
@@ -23,7 +44,7 @@ pub enum InitializationError {
23
44
#[ error( "invalid RSA key length given: {0}" ) ]
24
45
InvalidRsaKeyLength ( u16 ) ,
25
46
#[ error( "unsupported profiles selected: {0:?}" ) ]
26
- InvalidProfiles ( Vec < String > ) ,
47
+ InvalidProfile ( String ) ,
27
48
#[ error( "key generation failed: {0}" ) ]
28
49
KeyGeneration ( Box < dyn std:: error:: Error + ' static > ) ,
29
50
#[ error( "key encoding failed: {0}" ) ]
@@ -37,8 +58,14 @@ pub enum InitializationError {
37
58
pub fn initialize (
38
59
ipfs_path : & Path ,
39
60
bits : NonZeroU16 ,
40
- profiles : Vec < String > ,
61
+ profiles : Vec < Profile > ,
41
62
) -> Result < ( ) , InitializationError > {
63
+ // This check is done here to avoid an empty config file being created in the case of an
64
+ // unsupported input.
65
+ if profiles. len ( ) != 1 {
66
+ unimplemented ! ( "Multiple profiles are currently unsupported!" ) ;
67
+ }
68
+
42
69
let config_path = ipfs_path. join ( "config" ) ;
43
70
44
71
fs:: create_dir_all ( & ipfs_path)
@@ -52,27 +79,24 @@ pub fn initialize(
52
79
fn create (
53
80
config : File ,
54
81
bits : NonZeroU16 ,
55
- profiles : Vec < String > ,
82
+ profiles : Vec < Profile > ,
56
83
) -> Result < ( ) , InitializationError > {
57
84
use multibase:: Base :: Base64Pad ;
58
85
use prost:: Message ;
59
86
use std:: io:: BufWriter ;
60
87
88
+ let api_addr = match profiles[ 0 ] {
89
+ Profile :: Test => multiaddr ! ( Ip4 ( [ 127 , 0 , 0 , 1 ] ) , Tcp ( 0u16 ) ) ,
90
+ Profile :: Default => multiaddr ! ( Ip4 ( [ 127 , 0 , 0 , 1 ] ) , Tcp ( 4004u16 ) ) ,
91
+ } ;
92
+
61
93
let bits = bits. get ( ) ;
62
94
63
95
if bits < 2048 || bits > 16 * 1024 {
64
96
// ring will not accept a less than 2048 key
65
97
return Err ( InitializationError :: InvalidRsaKeyLength ( bits) ) ;
66
98
}
67
99
68
- if profiles. len ( ) != 1 || profiles[ 0 ] != "test" {
69
- // profiles are expected to be (comma separated) "test" as there are no bootstrap peer
70
- // handling yet. the conformance test cases seem to init `go-ipfs` in this profile where
71
- // it does not have any bootstrap nodes, and multi node tests later call swarm apis to
72
- // dial the nodes together.
73
- return Err ( InitializationError :: InvalidProfiles ( profiles) ) ;
74
- }
75
-
76
100
let pk = openssl:: rsa:: Rsa :: generate ( bits as u32 )
77
101
. map_err ( |e| InitializationError :: KeyGeneration ( Box :: new ( e) ) ) ?;
78
102
@@ -118,6 +142,7 @@ fn create(
118
142
} ,
119
143
addresses : Addresses {
120
144
swarm : vec ! [ "/ip4/127.0.0.1/tcp/0" . parse( ) . unwrap( ) ] ,
145
+ api : api_addr,
121
146
} ,
122
147
} ;
123
148
@@ -147,7 +172,7 @@ pub enum LoadingError {
147
172
/// Returns only the keypair and listening addresses or [`LoadingError`] but this should be
148
173
/// extended to contain the bootstrap nodes at least later when we need to support those for
149
174
/// testing purposes.
150
- pub fn load ( config : File ) -> Result < ( ipfs:: Keypair , Vec < Multiaddr > ) , LoadingError > {
175
+ pub fn load ( config : File ) -> Result < ( ipfs:: Keypair , Vec < Multiaddr > , Multiaddr ) , LoadingError > {
151
176
use std:: io:: BufReader ;
152
177
153
178
let CompatibleConfigFile {
@@ -167,7 +192,7 @@ pub fn load(config: File) -> Result<(ipfs::Keypair, Vec<Multiaddr>), LoadingErro
167
192
} ) ;
168
193
}
169
194
170
- Ok ( ( kp, addresses. swarm ) )
195
+ Ok ( ( kp, addresses. swarm , addresses . api ) )
171
196
}
172
197
173
198
/// Converts a PEM format to DER where PEM is a container for Base64 data with padding, starting on
@@ -245,6 +270,8 @@ struct CompatibleConfigFile {
245
270
#[ serde( rename_all = "PascalCase" ) ]
246
271
struct Addresses {
247
272
swarm : Vec < Multiaddr > ,
273
+ #[ serde( rename = "API" ) ]
274
+ api : Multiaddr ,
248
275
}
249
276
250
277
#[ derive( Debug , Serialize , Deserialize ) ]
0 commit comments