/
init.go
126 lines (108 loc) · 3.12 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
126
package main
import (
"crypto/rand"
"fmt"
"os"
"path/filepath"
"github.com/filecoin-project/boost/build"
"github.com/libp2p/go-libp2p"
crypto "github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/network"
peer "github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/p2p/muxer/mplex"
"github.com/libp2p/go-libp2p/p2p/muxer/yamux"
quic "github.com/libp2p/go-libp2p/p2p/transport/quic"
"github.com/libp2p/go-libp2p/p2p/transport/tcp"
"github.com/mitchellh/go-homedir"
"github.com/urfave/cli/v2"
)
func configureRepo(cfgDir string, createIfNotExist bool) (peer.ID, crypto.PrivKey, error) {
if cfgDir == "" {
return "", nil, fmt.Errorf("%s is a required flag", FlagRepo.Name)
}
if err := os.MkdirAll(cfgDir, 0744); err != nil {
return "", nil, err
}
peerkey, err := loadPeerKey(cfgDir, createIfNotExist)
if err != nil {
return "", nil, err
}
selfPid, err := peer.IDFromPrivateKey(peerkey)
if err != nil {
return "", nil, err
}
return selfPid, peerkey, nil
}
func setupHost(cfgDir string, port int) (host.Host, error) {
_, peerKey, err := configureRepo(cfgDir, false)
if err != nil {
return nil, err
}
return libp2p.New(
libp2p.ListenAddrStrings(
fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", port),
fmt.Sprintf("/ip4/0.0.0.0/udp/%d/quic", port),
),
libp2p.Transport(tcp.NewTCPTransport),
libp2p.Transport(quic.NewTransport),
libp2p.Muxer("/mplex/6.7.0", mplex.DefaultTransport),
libp2p.Muxer("/yamux/1.0.0", yamux.DefaultTransport),
libp2p.Identity(peerKey),
libp2p.ResourceManager(&network.NullResourceManager{}),
)
}
func loadPeerKey(cfgDir string, createIfNotExists bool) (crypto.PrivKey, error) {
keyPath := getKeyPath(cfgDir)
keyFile, err := os.ReadFile(keyPath)
if err == nil {
return crypto.UnmarshalPrivateKey(keyFile)
}
if !os.IsNotExist(err) {
return nil, err
}
if !createIfNotExists {
return nil, fmt.Errorf("booster-bitswap has not been initialized. Run the booster-bitswap init command")
}
log.Infof("Generating new peer key...")
key, _, err := crypto.GenerateEd25519Key(rand.Reader)
if err != nil {
return nil, err
}
data, err := crypto.MarshalPrivateKey(key)
if err != nil {
return nil, err
}
if err := os.WriteFile(keyPath, data, 0600); err != nil {
return nil, err
}
return key, nil
}
func getKeyPath(cfgDir string) string {
return filepath.Join(cfgDir, "libp2p.key")
}
var initCmd = &cli.Command{
Name: "init",
Usage: "Init booster-bitswap config",
Before: before,
Flags: []cli.Flag{},
Action: func(cctx *cli.Context) error {
repoDir, err := homedir.Expand(cctx.String(FlagRepo.Name))
if err != nil {
return fmt.Errorf("expanding repo file path: %w", err)
}
peerID, _, err := configureRepo(repoDir, true)
fmt.Println("Initialized booster-bitswap with libp2p peer ID " + peerID.String())
fmt.Println("Key file: " + getKeyPath(repoDir))
return err
},
}
var versionCmd = &cli.Command{
Name: "version",
Usage: "Print booster-bitswap version",
Before: before,
Action: func(cctx *cli.Context) error {
fmt.Println("booster-bitswap " + build.UserVersion())
return nil
},
}