This repository has been archived by the owner on Mar 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
api_ipfs.go
286 lines (256 loc) · 7.75 KB
/
api_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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package api
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"github.com/gin-gonic/gin"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes/any"
iface "github.com/ipfs/interface-go-ipfs-core"
"github.com/mr-tron/base58/base58"
"github.com/segmentio/ksuid"
"github.com/textileio/go-textile/crypto"
"github.com/textileio/go-textile/ipfs"
"github.com/textileio/go-textile/pb"
)
// ipfsId godoc
// @Summary Get IPFS peer ID
// @Description Displays underlying IPFS peer ID
// @Tags ipfs
// @Produce text/plain
// @Success 200 {string} string "peer id"
// @Failure 500 {string} string "Internal Server Error"
// @Router /ipfs/id [get]
func (a *Api) ipfsId(g *gin.Context) {
pid, err := a.Node.PeerId()
if err != nil {
a.abort500(g, err)
return
}
g.String(http.StatusOK, pid.Pretty())
}
// ipfsSwarmConnect godoc
// @Summary Opens a new direct connection to a peer address
// @Description Opens a new direct connection to a peer using an IPFS multiaddr
// @Tags ipfs
// @Produce application/json
// @Param X-Textile-Args header string true "peer address"
// @Success 200 {array} string "ok"
// @Failure 400 {string} string "Bad Request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /ipfs/swarm/connect [post]
func (a *Api) ipfsSwarmConnect(g *gin.Context) {
args, err := a.readArgs(g)
if err != nil {
a.abort500(g, err)
return
}
if len(args) == 0 {
g.String(http.StatusBadRequest, "missing peer multi address")
return
}
res, err := ipfs.SwarmConnect(a.Node.Ipfs(), []string{args[0]})
if err != nil {
g.String(http.StatusBadRequest, err.Error())
return
}
g.JSON(http.StatusOK, res)
}
// ipfsSwarmPeers godoc
// @Summary List swarm peers
// @Description Lists the set of peers this node is connected to
// @Tags ipfs
// @Produce application/json
// @Param X-Textile-Opts header string false "verbose: Display all extra information, latency: Also list information about latency to each peer, streams: Also list information about open streams for each peer, direction: Also list information about the direction of connection" default(verbose="false",latency="false",streams="false",direction="false")
// @Success 200 {object} ipfs.ConnInfos "connection"
// @Failure 400 {string} string "Bad Request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /ipfs/swarm/peers [get]
func (a *Api) ipfsSwarmPeers(g *gin.Context) {
opts, err := a.readOpts(g)
if err != nil {
a.abort500(g, err)
return
}
verbose := opts["verbose"] == "true"
latency := opts["latency"] == "true"
streams := opts["streams"] == "true"
direction := opts["direction"] == "true"
res, err := ipfs.SwarmPeers(a.Node.Ipfs(), verbose, latency, streams, direction)
if err != nil {
g.String(http.StatusBadRequest, err.Error())
return
}
g.JSON(http.StatusOK, res)
}
// ipfsCat godoc
// @Summary Cat IPFS data
// @Description Displays the data behind an IPFS CID (hash) or Path
// @Tags ipfs
// @Produce application/octet-stream
// @Param path path string true "ipfs/ipns cid"
// @Param X-Textile-Opts header string false "key: Key to decrypt data on-the-fly" default(key=)
// @Success 200 {array} byte "data"
// @Failure 400 {string} string "Bad Request"
// @Failure 401 {string} string "Unauthorized"
// @Failure 404 {string} string "Not Found"
// @Failure 500 {string} string "Internal Server Error"
// @Router /ipfs/cat/{path} [get]
func (a *Api) ipfsCat(g *gin.Context) {
pth := g.Param("path")
if pth == "" {
g.String(http.StatusBadRequest, "Missing IPFS CID")
}
opts, err := a.readOpts(g)
if err != nil {
a.abort500(g, err)
return
}
data, err := ipfs.DataAtPath(a.Node.Ipfs(), pth[1:])
if err != nil {
g.String(http.StatusNotFound, err.Error())
return
}
var plaintext []byte
if opts["key"] != "" {
key, err := base58.Decode(opts["key"])
if err != nil {
g.String(http.StatusBadRequest, err.Error())
}
plaintext, err = crypto.DecryptAES(data, key)
if err != nil {
g.String(http.StatusUnauthorized, err.Error())
}
} else {
plaintext = data
}
g.Data(http.StatusOK, "application/octet-stream", plaintext)
}
// ipfsPubsubPub godoc
// @Summary Publish a message
// @Description Publishes a message to a given pubsub topic
// @Tags ipfs
// @Accept application/octet-stream
// @Produce text/plain
// @Success 204 {string} string "ok"
// @Failure 500 {string} string "Internal Server Error"
// @Router /ipfs/pubsub/pub/{topic} [post]
func (a *Api) ipfsPubsubPub(g *gin.Context) {
topic := g.Param("topic")
if topic == "" {
g.String(http.StatusBadRequest, "missing topic")
}
payload, err := ioutil.ReadAll(g.Request.Body)
if err != nil {
g.String(http.StatusBadRequest, err.Error())
return
}
err = ipfs.Publish(a.Node.Ipfs(), topic, payload)
if err != nil {
a.abort500(g, err)
return
}
g.Status(http.StatusNoContent)
}
// ipfsPubsubSub godoc
// @Summary Subscribe messages
// @Description Subscribes to messages on a given topic
// @Tags ipfs
// @Produce text/event-stream with events, or just application/json
// @Success 200 {string} string with events, or []byte "results stream"
// @Failure 400 {string} string "Bad Request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /ipfs/pubsub/sub/{topic} [get]
func (a *Api) ipfsPubsubSub(g *gin.Context) {
topic := g.Param("topic")
if topic == "" {
g.String(http.StatusBadRequest, "Missing topic")
}
events := g.Query("events")
queryId := g.Query("queryId")
opts, err := a.readOpts(g)
if err != nil {
a.abort500(g, err)
return
}
msgs := make(chan iface.PubSubMessage, 10)
ctx := a.Node.Ipfs().Context()
var id string
if queryId != "" {
id = queryId
} else if opts["queryId"] != "" {
id = opts["queryId"]
} else {
id = ksuid.New().String()
}
go func() {
if err := ipfs.Subscribe(a.Node.Ipfs(), ctx, topic, true, msgs); err != nil {
close(msgs)
a.abort500(g, err)
log.Errorf("ipfs pubsub sub stopped with error: %s", err.Error())
return
}
}()
log.Infof("ipfs pubsub sub started for %s", topic)
// EventSource of web browser will not pending on first msg from g.SSEvent,
// initHttpStatusOK here will not pending on first msg from g.Data for consistency
initHttpStatusOK := false
g.Stream(func(w io.Writer) bool {
if events != "true" && !initHttpStatusOK {
initHttpStatusOK = true
g.Data(http.StatusOK, "application/octet-stream", []byte{})
return true
}
select {
case <-g.Request.Context().Done():
log.Infof("ipfs pubsub sub shutdown for %s", topic)
return false
case msg, ok := <-msgs:
if !ok {
log.Infof("ipfs pubsub sub shutdown for %s", topic)
return false
}
mPeer := msg.From()
if mPeer.Pretty() == a.Node.Ipfs().Identity.Pretty() {
return true
}
value, err := proto.Marshal(&pb.Strings{
Values: []string{string(msg.Data())},
})
if err != nil {
g.String(http.StatusBadRequest, err.Error())
break
}
res := &pb.QueryResult{
Id: fmt.Sprintf("%x", msg.Seq()),
Value: &any.Any{
// Can't let TypeUrl to use ipfs official "/pubsub.pb.Message"
// from github.com/libp2p/go-libp2p-pubsub/pb/rpc.pb.go ,
// because rpc.pb.go import github.com/gogo/protobuf/proto , so
// proto.RegisterType((*Message)(nil), "pubsub.pb.Message") in
// rpc.pb.go , can't let "/pubsub.pb.Message" be found in
// pbMarshaler.MarshalToString which import github.com/golang/protobuf
TypeUrl: "/Strings",
Value: value,
},
}
str, err := pbMarshaler.MarshalToString(&pb.MobileQueryEvent{
Id: id,
Type: pb.MobileQueryEvent_DATA,
Data: res,
})
if err != nil {
g.String(http.StatusBadRequest, err.Error())
break
}
if events == "true" || opts["events"] == "true" {
g.SSEvent("update", str)
} else {
g.Data(http.StatusOK, "application/json", []byte(str))
g.Writer.Write([]byte("\n"))
}
}
return true
})
}