-
Notifications
You must be signed in to change notification settings - Fork 376
/
cfg.go
100 lines (84 loc) · 2.49 KB
/
cfg.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
package ipfsutil
import (
crand "crypto/rand"
"encoding/base64"
"fmt"
"strings"
"berty.tech/berty/v2/go/pkg/errcode"
ipfs_datastore "github.com/ipfs/go-datastore"
ipfs_cfg "github.com/ipfs/go-ipfs-config"
ipfs_node "github.com/ipfs/go-ipfs/core/node"
ipfs_libp2p "github.com/ipfs/go-ipfs/core/node/libp2p"
ipfs_repo "github.com/ipfs/go-ipfs/repo"
libp2p_ci "github.com/libp2p/go-libp2p-core/crypto" // nolint:staticcheck
libp2p_peer "github.com/libp2p/go-libp2p-core/peer" // nolint:staticcheck
)
type BuildOpts struct {
SwarmAddresses []string
ClientMode bool
}
func CreateBuildConfigWithDatastore(opts *BuildOpts, ds ipfs_datastore.Batching) (*ipfs_node.BuildCfg, error) {
if opts == nil {
opts = &BuildOpts{}
}
repo, err := CreateRepo(ds, opts)
if err != nil {
return nil, errcode.TODO.Wrap(err)
}
var routing ipfs_libp2p.RoutingOption
if opts.ClientMode {
routing = ipfs_libp2p.DHTClientOption
} else {
routing = ipfs_libp2p.DHTOption
}
hostopts := ipfs_libp2p.DefaultHostOption
return &ipfs_node.BuildCfg{
Online: true,
Permanent: true,
DisableEncryptedConnections: false,
NilRepo: false,
Routing: routing,
Host: hostopts,
Repo: repo,
ExtraOpts: map[string]bool{
"pubsub": true,
},
}, nil
}
func CreateRepo(dstore ipfs_datastore.Batching, opts *BuildOpts) (ipfs_repo.Repo, error) {
c := ipfs_cfg.Config{}
priv, pub, err := libp2p_ci.GenerateKeyPairWithReader(libp2p_ci.RSA, 2048, crand.Reader) // nolint:staticcheck
if err != nil {
return nil, errcode.TODO.Wrap(err)
}
pid, err := libp2p_peer.IDFromPublicKey(pub) // nolint:staticcheck
if err != nil {
return nil, errcode.TODO.Wrap(err)
}
privkeyb, err := priv.Bytes()
if err != nil {
return nil, errcode.TODO.Wrap(err)
}
c.Bootstrap = ipfs_cfg.DefaultBootstrapAddresses
if len(opts.SwarmAddresses) != 0 {
c.Addresses.Swarm = opts.SwarmAddresses
} else {
c.Addresses.Swarm = []string{
"/ip4/0.0.0.0/tcp/0",
"/ip6/0.0.0.0/tcp/0",
}
}
fmt.Printf("IPFS listening on %s\n", strings.Join(c.Addresses.Swarm, ", "))
c.Experimental.QUIC = true
c.Identity.PeerID = pid.Pretty()
c.Identity.PrivKey = base64.StdEncoding.EncodeToString(privkeyb)
c.Discovery.MDNS.Enabled = true
c.Discovery.MDNS.Interval = 1
c.Swarm.EnableAutoNATService = true
c.Swarm.EnableAutoRelay = true
c.Swarm.EnableRelayHop = false
return &ipfs_repo.Mock{
D: dstore,
C: c,
}, nil
}