-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.go
More file actions
251 lines (214 loc) · 5.78 KB
/
node.go
File metadata and controls
251 lines (214 loc) · 5.78 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package ipfs
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"sync"
"time"
"github.com/ipfs/boxo/files"
boxopath "github.com/ipfs/boxo/path"
icore "github.com/ipfs/kubo/core/coreiface"
ma "github.com/multiformats/go-multiaddr"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/ipfs/kubo/config"
"github.com/ipfs/kubo/core"
"github.com/ipfs/kubo/core/coreapi"
"github.com/ipfs/kubo/plugin/loader" // This package is needed so that all the preloaded plugins are loaded automatically
"github.com/ipfs/kubo/repo/fsrepo"
"github.com/libp2p/go-libp2p/core/peer"
)
// Node -
type Node struct {
api icore.CoreAPI
node *core.IpfsNode
providers []Provider
limit int64
wg *sync.WaitGroup
}
// NewNode -
func NewNode(ctx context.Context, dir string, limit int64, blacklist []string, providers []Provider) (*Node, error) {
api, node, err := spawn(ctx, dir, blacklist, providers)
if err != nil {
return nil, errors.Wrap(err, "failed to spawn node")
}
return &Node{
api: api,
node: node,
providers: providers,
limit: limit,
wg: new(sync.WaitGroup),
}, nil
}
// Start -
func (n *Node) Start(ctx context.Context, bootstrap ...string) error {
log.Info().Msg("going to connect to bootstrap nodes...")
connected, err := n.api.Swarm().Peers(ctx)
if err != nil {
log.Warn().Msg("can't get perrs")
return nil
}
for i := range connected {
log.Info().
Str("peer_id", connected[i].ID().String()).
Str("address", connected[i].Address().String()).
Msg("connected to peer")
}
n.wg.Add(1)
go n.reconnect(ctx)
return nil
}
// Close -
func (n *Node) Close() error {
n.wg.Wait()
return n.node.Close()
}
// Get -
func (n *Node) Get(ctx context.Context, cid string) (Data, error) {
cidObj, err := boxopath.NewPath(cid)
if err != nil {
return Data{}, errors.Wrap(ErrInvalidCID, cid)
}
start := time.Now()
rootNode, err := n.api.Unixfs().Get(ctx, cidObj)
if err != nil {
return Data{}, errors.Wrapf(err, "could not get file with CID: %s", cid)
}
defer rootNode.Close()
responseTime := time.Since(start).Milliseconds()
file := files.ToFile(rootNode)
if file == nil {
return Data{}, errors.Errorf("could not get file with CID: %s", cid)
}
data, err := io.ReadAll(io.LimitReader(file, n.limit))
if err != nil {
return Data{}, err
}
return Data{
Raw: data,
Node: "ipfs-metadata-node",
ResponseTime: responseTime,
}, nil
}
var loadPluginsOnce sync.Once
func spawn(ctx context.Context, dir string, blacklist []string, providers []Provider) (icore.CoreAPI, *core.IpfsNode, error) {
var onceErr error
loadPluginsOnce.Do(func() {
onceErr = setupPlugins("")
})
if onceErr != nil {
return nil, nil, onceErr
}
repoPath, err := createRepository(dir, blacklist, providers)
if err != nil {
return nil, nil, err
}
r, err := fsrepo.Open(repoPath)
if err != nil {
return nil, nil, err
}
node, err := core.NewNode(ctx, &core.BuildCfg{
Online: true,
Repo: r,
ExtraOpts: map[string]bool{
"enable-gc": true,
},
})
if err != nil {
return nil, nil, err
}
api, err := coreapi.NewCoreAPI(node)
return api, node, err
}
func createRepository(dir string, blacklist []string, providers []Provider) (string, error) {
if _, err := os.Stat(dir); err != nil {
if os.IsNotExist(err) {
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
return "", fmt.Errorf("failed to get dir: %s", err)
}
} else {
return "", err
}
}
// Create a config with default options and a 2048 bit key
cfg, err := config.Init(io.Discard, 2048)
if err != nil {
return "", err
}
cfg.Swarm.DisableBandwidthMetrics = true
cfg.Swarm.Transports.Network.Relay = config.False
cfg.Swarm.AddrFilters = blacklist
cfg.Swarm.ConnMgr.HighWater = config.NewOptionalInteger(900)
cfg.Swarm.ConnMgr.LowWater = config.NewOptionalInteger(600)
cfg.Swarm.ConnMgr.GracePeriod = config.NewOptionalDuration(time.Minute * 5)
cfg.Routing.AcceleratedDHTClient = config.True
cfg.Routing.Type = config.NewOptionalString("auto")
peers, err := providersToAddrInfo(providers)
if err != nil {
return "", errors.Wrap(err, "collecting providers info error")
}
cfg.Peering = config.Peering{
Peers: peers,
}
// Create the repo with the config
if err = fsrepo.Init(dir, cfg); err != nil {
return "", errors.Wrap(err, "failed to init node")
}
return dir, nil
}
func setupPlugins(externalPluginsPath string) error {
// Load any external plugins if available on externalPluginsPath
plugins, err := loader.NewPluginLoader(filepath.Join(externalPluginsPath, "plugins"))
if err != nil {
return fmt.Errorf("error loading plugins: %s", err)
}
// Load preloaded and external plugins
if err := plugins.Initialize(); err != nil {
return fmt.Errorf("error initializing plugins: %s", err)
}
if err := plugins.Inject(); err != nil {
return fmt.Errorf("error initializing plugins: %s", err)
}
return nil
}
func (n *Node) reconnect(ctx context.Context) {
defer n.wg.Done()
ticker := time.NewTicker(time.Minute * 3)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
peers, err := n.api.Swarm().Peers(ctx)
if err != nil {
log.Err(err).Msg("receiving peers")
continue
}
for _, pi := range peers {
log.Info().Str("peer_id", pi.ID().String()).Str("address", pi.Address().String()).Msg("connected to peer")
}
}
}
}
func providersToAddrInfo(providers []Provider) ([]peer.AddrInfo, error) {
peers := make([]peer.AddrInfo, 0)
for i := range providers {
id, err := peer.Decode(providers[i].ID)
if err != nil {
return nil, errors.Wrap(err, "providersToAddrInfo")
}
info := peer.AddrInfo{
ID: id,
}
if providers[i].Address != "" {
info.Addrs = []ma.Multiaddr{
ma.StringCast(providers[i].Address),
}
}
peers = append(peers, info)
}
return peers, nil
}