-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
125 lines (108 loc) · 2.74 KB
/
init.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package config
import (
"encoding/base64"
"errors"
"fmt"
"io"
peer "gx/ipfs/QmfMmLGoKzCHDN7cGgk64PJr4iipzidDRME8HABSJqvmhC/go-libp2p-peer"
ci "gx/ipfs/QmfWDLQjGjVe4fr5CoztYW2DYYjRysMJrFe1RCsXLPTf46/go-libp2p-crypto"
)
func Init(out io.Writer, nBitsForKeypair int) (*Config, error) {
identity, err := identityConfig(out, nBitsForKeypair)
if err != nil {
return nil, err
}
bootstrapPeers, err := DefaultBootstrapPeers()
if err != nil {
return nil, err
}
datastore, err := datastoreConfig()
if err != nil {
return nil, err
}
conf := &Config{
// setup the node's default addresses.
// NOTE: two swarm listen addrs, one tcp, one utp.
Addresses: Addresses{
Swarm: []string{
"/ip4/0.0.0.0/tcp/4001",
// "/ip4/0.0.0.0/udp/4002/utp", // disabled for now.
"/ip6/::/tcp/4001",
},
API: "/ip4/127.0.0.1/tcp/5001",
Gateway: "/ip4/127.0.0.1/tcp/8080",
},
Datastore: datastore,
Bootstrap: BootstrapPeerStrings(bootstrapPeers),
Identity: identity,
Discovery: Discovery{MDNS{
Enabled: true,
Interval: 10,
}},
// setup the node mount points.
Mounts: Mounts{
IPFS: "/ipfs",
IPNS: "/ipns",
},
Ipns: Ipns{
ResolveCacheSize: 128,
},
Gateway: Gateway{
RootRedirect: "",
Writable: false,
PathPrefixes: []string{},
HTTPHeaders: map[string][]string{
"Access-Control-Allow-Origin": []string{"*"},
"Access-Control-Allow-Methods": []string{"GET"},
"Access-Control-Allow-Headers": []string{"X-Requested-With"},
},
},
Reprovider: Reprovider{
Interval: "12h",
},
}
return conf, nil
}
func datastoreConfig() (Datastore, error) {
dspath, err := DataStorePath("")
if err != nil {
return Datastore{}, err
}
return Datastore{
Path: dspath,
Type: "leveldb",
StorageMax: "10GB",
StorageGCWatermark: 90, // 90%
GCPeriod: "1h",
HashOnRead: false,
BloomFilterSize: 0,
}, nil
}
// identityConfig initializes a new identity.
func identityConfig(out io.Writer, nbits int) (Identity, error) {
// TODO guard higher up
ident := Identity{}
if nbits < 1024 {
return ident, errors.New("Bitsize less than 1024 is considered unsafe.")
}
fmt.Fprintf(out, "generating %v-bit RSA keypair...", nbits)
sk, pk, err := ci.GenerateKeyPair(ci.RSA, nbits)
if err != nil {
return ident, err
}
fmt.Fprintf(out, "done\n")
// currently storing key unencrypted. in the future we need to encrypt it.
// TODO(security)
skbytes, err := sk.Bytes()
if err != nil {
return ident, err
}
ident.PrivKey = base64.StdEncoding.EncodeToString(skbytes)
id, err := peer.IDFromPublicKey(pk)
if err != nil {
return ident, err
}
ident.PeerID = id.Pretty()
fmt.Fprintf(out, "peer identity: %s\n", ident.PeerID)
return ident, nil
}