/
node_bin_ipfs.go
245 lines (222 loc) · 5.39 KB
/
node_bin_ipfs.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
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
package controller
import (
"context"
"fmt"
"github.com/glvd/accipfs/basis"
"github.com/glvd/accipfs/config"
"github.com/glvd/accipfs/core"
"github.com/goextension/io"
files "github.com/ipfs/go-ipfs-files"
httpapi "github.com/ipfs/go-ipfs-http-client"
iface "github.com/ipfs/interface-go-ipfs-core"
"github.com/ipfs/interface-go-ipfs-core/options"
"github.com/ipfs/interface-go-ipfs-core/path"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/multiformats/go-multiaddr"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"
)
var _ core.ControllerService = &nodeBinIPFS{}
type nodeBinIPFS struct {
ctx context.Context
cancel context.CancelFunc
cfg *config.Config
name string
cmd *exec.Cmd
msg func(string)
api *httpapi.HttpApi
}
// MessageHandle ...
func (n *nodeBinIPFS) MessageHandle(f func(s string)) {
n.msg = f
}
// Msg ...
func (n *nodeBinIPFS) Msg(s string) {
if n.msg != nil {
n.msg(s)
}
}
// Start ...
func (n *nodeBinIPFS) Start() error {
n.cmd = exec.CommandContext(n.ctx, n.name, "daemon", "--routing", "none")
output("ipfs cmd: ", n.cmd.Args)
pipe, err2 := n.cmd.StderrPipe()
if err2 != nil {
return err2
}
stdoutPipe, err2 := n.cmd.StdoutPipe()
if err2 != nil {
return err2
}
m := io.MultiReader(pipe, stdoutPipe)
if n.cfg.IPFS.LogOutput {
go basis.PipeReader(n.ctx, m, n.msg)
}
//else {
// go basis.PipeDummy(n.ctx, module, m)
//}
err := n.cmd.Start()
if err != nil {
return err
}
return nil
}
// Stop ...
func (n *nodeBinIPFS) Stop() error {
if n.cmd != nil {
n.cancel()
n.cmd = nil
}
return nil
}
// Initialize ...
func (n *nodeBinIPFS) Initialize() error {
_, err := os.Stat(config.DataDirIPFS())
if err != nil && os.IsNotExist(err) {
_ = os.MkdirAll(config.DataDirIPFS(), 0755)
//os.Setenv("IPFS_PATH", filepath.Join(n.cfg.Path, config.DataDirIPFS()))
cmd := exec.Command(n.name, "init", "--profile", "badgerds")
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("init:%w", err)
}
version := n.getVersion()
logI("ipfs init", "log", string(out), "version", version)
n.Msg(string(out))
if version[1] < 5 {
cmd = exec.Command(n.name, "config", "Swarm.EnableAutoNATService", "--bool", "true")
out, err = cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("config(nat):%w", err)
}
}
n.Msg(string(out))
logI("ipfs init config set", "log", string(out))
cmd = exec.Command(n.name, "config", "Swarm.EnableRelayHop", "--bool", "true")
out, err = cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("config(relay):%w", err)
}
n.Msg(string(out))
logI("ipfs init config set", "log", string(out))
logI("ipfs init end")
}
return nil
}
func newNodeBinIPFS(cfg *config.Config) *nodeBinIPFS {
path := filepath.Join(cfg.Path, "bin", basis.BinName(cfg.IPFS.Name))
ctx, cancelFunc := context.WithCancel(context.Background())
return &nodeBinIPFS{
ctx: ctx,
cancel: cancelFunc,
cfg: cfg,
name: path,
}
}
func (n *nodeBinIPFS) getVersion() (ver [3]int) {
cmd := exec.Command(n.name, "version")
out, err := cmd.CombinedOutput()
if err != nil {
return
}
split := strings.Split(string(out), " ")
if len(split) < 3 {
return
}
verS := strings.Split(split[2], ".")
for i := range verS {
parseInt, err := strconv.ParseInt(verS[i], 32, 10)
if err != nil {
return
}
ver[i] = int(parseInt)
}
return
}
// IsReady ...
func (n *nodeBinIPFS) IsReady() bool {
ctx, cancel := context.WithTimeout(context.TODO(), 3*time.Second)
defer cancel()
_, err := n.ID(ctx)
if err != nil {
return false
}
return true
}
// APIContext ...
func (n *nodeBinIPFS) API() *httpapi.HttpApi {
if n.api == nil {
ma, err := multiaddr.NewMultiaddr(config.IPFSAPIAddr())
if err != nil {
panic(err)
}
api, e := httpapi.NewApi(ma)
if e != nil {
panic(e)
}
n.api = api
}
return n.api
}
// ID get self serviceNode info
func (n *nodeBinIPFS) ID(ctx context.Context) (pid *core.DataStoreInfo, e error) {
pid = &core.DataStoreInfo{}
e = n.API().Request("id").Exec(ctx, pid)
if e != nil {
return nil, e
}
return pid, nil
}
// PinAdd ...
func (n *nodeBinIPFS) AddFile(ctx context.Context, filename string, option options.UnixfsAddOption) (hash string, e error) {
stat, e := os.Stat(filename)
if e != nil {
return "", e
}
var node files.Node
//var err error
if !stat.IsDir() {
file, e := os.Open(filename)
if e != nil {
return "", e
}
node = files.NewReaderFile(file)
} else {
sf, e := files.NewSerialFile(filename, false, stat)
if e != nil {
return "", e
}
node = sf
}
resolved, e := n.API().Unixfs().Add(ctx, node, option)
if e != nil {
return "", e
}
return resolved.Cid().String(), nil
}
// PinAdd ...
func (n *nodeBinIPFS) PinAdd(ctx context.Context, hash string) (e error) {
p := path.New(hash)
return n.API().Pin().Add(ctx, p, options.Pin.Recursive(true))
}
// PinLS ...
func (n *nodeBinIPFS) PinLS(ctx context.Context) (pins <-chan iface.Pin, e error) {
return n.API().Pin().Ls(ctx, options.Pin.Ls.Recursive())
}
// PinRm ...
func (n *nodeBinIPFS) PinRm(ctx context.Context, hash string) (e error) {
p := path.New(hash)
return n.API().Pin().Rm(ctx, p)
}
// SwarmPeers ...
func (n *nodeBinIPFS) SwarmPeers(ctx context.Context) ([]iface.ConnectionInfo, error) {
return n.API().Swarm().Peers(ctx)
}
// SwarmPeers ...
func (n *nodeBinIPFS) SwarmConnect(ctx context.Context, info peer.AddrInfo) error {
return n.API().Swarm().Connect(ctx, info)
}