|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/rand" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + |
| 10 | + lcli "github.com/filecoin-project/lotus/cli" |
| 11 | + "github.com/libp2p/go-libp2p" |
| 12 | + crypto "github.com/libp2p/go-libp2p-core/crypto" |
| 13 | + "github.com/libp2p/go-libp2p-core/host" |
| 14 | + "github.com/libp2p/go-libp2p-core/network" |
| 15 | + peer "github.com/libp2p/go-libp2p-core/peer" |
| 16 | + "github.com/libp2p/go-libp2p/p2p/muxer/mplex" |
| 17 | + "github.com/libp2p/go-libp2p/p2p/muxer/yamux" |
| 18 | + quic "github.com/libp2p/go-libp2p/p2p/transport/quic" |
| 19 | + "github.com/libp2p/go-libp2p/p2p/transport/tcp" |
| 20 | + "github.com/urfave/cli/v2" |
| 21 | +) |
| 22 | + |
| 23 | +func configureRepo(ctx context.Context, cfgDir string, createIfNotExist bool) (peer.ID, crypto.PrivKey, error) { |
| 24 | + if cfgDir == "" { |
| 25 | + return "", nil, fmt.Errorf("dataDir must be set") |
| 26 | + } |
| 27 | + |
| 28 | + if err := os.MkdirAll(cfgDir, 0744); err != nil { |
| 29 | + return "", nil, err |
| 30 | + } |
| 31 | + |
| 32 | + peerkey, err := loadPeerKey(cfgDir, createIfNotExist) |
| 33 | + if err != nil { |
| 34 | + return "", nil, err |
| 35 | + } |
| 36 | + |
| 37 | + selfPid, err := peer.IDFromPrivateKey(peerkey) |
| 38 | + if err != nil { |
| 39 | + return "", nil, err |
| 40 | + } |
| 41 | + |
| 42 | + return selfPid, peerkey, nil |
| 43 | +} |
| 44 | + |
| 45 | +func setupHost(ctx context.Context, cfgDir string, port int) (host.Host, error) { |
| 46 | + _, peerKey, err := configureRepo(ctx, cfgDir, false) |
| 47 | + if err != nil { |
| 48 | + return nil, err |
| 49 | + } |
| 50 | + return libp2p.New( |
| 51 | + libp2p.ListenAddrStrings( |
| 52 | + fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", port), |
| 53 | + fmt.Sprintf("/ip4/0.0.0.0/udp/%d/quic", port), |
| 54 | + ), |
| 55 | + libp2p.Transport(tcp.NewTCPTransport), |
| 56 | + libp2p.Transport(quic.NewTransport), |
| 57 | + libp2p.Muxer("/mplex/6.7.0", mplex.DefaultTransport), |
| 58 | + libp2p.Muxer("/yamux/1.0.0", yamux.DefaultTransport), |
| 59 | + libp2p.Identity(peerKey), |
| 60 | + libp2p.ResourceManager(network.NullResourceManager), |
| 61 | + ) |
| 62 | +} |
| 63 | + |
| 64 | +func loadPeerKey(cfgDir string, createIfNotExists bool) (crypto.PrivKey, error) { |
| 65 | + var peerkey crypto.PrivKey |
| 66 | + keyPath := filepath.Join(cfgDir, "libp2p.key") |
| 67 | + keyFile, err := os.ReadFile(keyPath) |
| 68 | + if err != nil { |
| 69 | + if !os.IsNotExist(err) { |
| 70 | + return nil, err |
| 71 | + } |
| 72 | + if os.IsNotExist(err) && !createIfNotExists { |
| 73 | + return nil, err |
| 74 | + } |
| 75 | + log.Infof("Generating new peer key...") |
| 76 | + |
| 77 | + key, _, err := crypto.GenerateEd25519Key(rand.Reader) |
| 78 | + if err != nil { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + peerkey = key |
| 82 | + |
| 83 | + data, err := crypto.MarshalPrivateKey(key) |
| 84 | + if err != nil { |
| 85 | + return nil, err |
| 86 | + } |
| 87 | + |
| 88 | + if err := os.WriteFile(keyPath, data, 0600); err != nil { |
| 89 | + return nil, err |
| 90 | + } |
| 91 | + } else { |
| 92 | + key, err := crypto.UnmarshalPrivateKey(keyFile) |
| 93 | + if err != nil { |
| 94 | + return nil, err |
| 95 | + } |
| 96 | + |
| 97 | + peerkey = key |
| 98 | + } |
| 99 | + |
| 100 | + if peerkey == nil { |
| 101 | + panic("sanity check: peer key is uninitialized") |
| 102 | + } |
| 103 | + |
| 104 | + return peerkey, nil |
| 105 | +} |
| 106 | + |
| 107 | +var initCmd = &cli.Command{ |
| 108 | + Name: "init", |
| 109 | + Usage: "Init booster-bitswap config", |
| 110 | + Before: before, |
| 111 | + Flags: []cli.Flag{}, |
| 112 | + Action: func(cctx *cli.Context) error { |
| 113 | + |
| 114 | + ctx := lcli.ReqContext(cctx) |
| 115 | + |
| 116 | + repoDir := cctx.String(FlagRepo.Name) |
| 117 | + |
| 118 | + peerID, _, err := configureRepo(ctx, repoDir, true) |
| 119 | + fmt.Println(peerID) |
| 120 | + return err |
| 121 | + }, |
| 122 | +} |
0 commit comments