/
node.go
430 lines (396 loc) · 12.1 KB
/
node.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
// Package flubber Flubber API
//
// This a a REST server for accessing the capabilities of a Flubber node.
//
// Schemes: http
// Host: localhost:4242
// BasePath: /
// Version: 0.0.1
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
// - text/plain
//
// swagger:meta
package node
import (
"bytes"
"context"
"encoding/gob"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"os/signal"
"path/filepath"
"time"
ginzap "github.com/gin-contrib/zap"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"github.com/ipfs/go-cid"
logging "github.com/ipfs/go-log/v2"
"github.com/wabarc/ipfs-pinner/pkg/pinata"
"github.com/allisterb/flubber/blockchain"
"github.com/allisterb/flubber/did"
"github.com/allisterb/flubber/ipfs"
"github.com/allisterb/flubber/p2p"
"github.com/allisterb/flubber/util"
)
type Config struct {
Did string
IPFSPubKey []byte
IPFSPrivKey []byte
InfuraSecretKey string
W3SSecretKey string
PinataApiKey string
PinataSecretKey string
IPNSKeys map[byte]byte
}
type NodeRun struct {
Ctx context.Context
Ipfs ipfs.IPFSCore
}
var log = logging.Logger("flubber/node")
var CurrentConfig = Config{}
var CurrentConfigInitialized = false
var nodeRun = NodeRun{}
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024 * 1024 * 1024,
WriteBufferSize: 1024 * 1024 * 1024,
//Solving cross-domain problems
CheckOrigin: func(r *http.Request) bool {
return true
},
}
func PanicIfNotInitialized() {
if !CurrentConfigInitialized {
panic("node configuration is not initialized")
}
}
func LoadConfig() (Config, error) {
f := filepath.Join(filepath.Join(util.GetUserHomeDir(), ".flubber"), "node.json")
if _, err := os.Stat(f); err != nil {
log.Errorf("could not find node configuration file %s", f)
return Config{}, err
}
c, err := os.ReadFile(f)
if err != nil {
log.Errorf("could not read data from node configuration file: %v", err)
return Config{}, err
}
var config Config
if err = json.Unmarshal(c, &config); err != nil {
log.Errorf("could not read JSON data from node configuration file: %v", err)
return Config{}, err
}
if config.IPFSPrivKey == nil || config.IPFSPubKey == nil {
log.Errorf("IPFS node private or public key not set in configuration file")
return Config{}, fmt.Errorf("IPFS NODE PRIVATE OR PUBLIC KEY NOT SET IN CONFIGURATION FILE")
}
if config.InfuraSecretKey == "" {
log.Warnf("Infura API secret key not set in configuration file")
return Config{}, fmt.Errorf("INFURA API SECRET KEY NOT SET IN CONFIGURATION FILE")
}
if config.W3SSecretKey == "" {
log.Warnf("Web3.Storage API secret key not set in configuration file")
}
if config.PinataApiKey == "" {
log.Warnf("Pinata API key not set in configuration file")
return Config{}, fmt.Errorf("PINATA API KEY NOT SET IN CONFIGURATION FILE")
}
if config.PinataSecretKey == "" {
log.Warnf("Pinata secret key not set in configuration file")
return Config{}, fmt.Errorf("PINATA SECRET KEY NOT SET IN CONFIGURATION FILE")
}
CurrentConfig = config
CurrentConfigInitialized = true
return config, nil
}
func Run(ctx context.Context, cancel context.CancelFunc) error {
_, err := LoadConfig()
if err != nil {
return err
}
ddd, _ := did.Parse(CurrentConfig.Did)
_, err = blockchain.ResolveENS(ddd.ID.ID, CurrentConfig.InfuraSecretKey)
if err != nil {
return err
}
log.Info("starting Flubber node...")
ipfs, err := ipfs.StartIPFSNode(ctx, CurrentConfig.IPFSPrivKey, CurrentConfig.IPFSPubKey)
if err != nil {
log.Errorf("error starting IPFS node: %v", err)
return err
}
ipfs.W3S.SetAuthToken(CurrentConfig.W3SSecretKey)
//c, _ := cid.NewPrefixV1(cid.Raw, mh.SHA2_256).Sum([]byte("patr"))
//tctx, _ := context.WithTimeout(ctx, time.Second*10)
//if err := ipfs.Node.DHTClient.Provide(tctx, c, true); err != nil {
// log.Errorf("could not provide patr topic: %v", err)
//}
p2p.SetDMStreamHandler(*ipfs, CurrentConfig.InfuraSecretKey)
gin.SetMode(gin.DebugMode)
r := gin.New()
r.Use(ginzap.Ginzap(log.Desugar(), time.RFC3339Nano, true))
r.Use(ginzap.RecoveryWithZap(log.Desugar(), true))
r.GET("/DM", getDM)
r.PUT("/DM", putDM)
r.GET("/messages", getMessages)
r.PUT("/messages", putMessages)
r.GET("/subscriptions", getSubscriptions)
r.PUT("/subscriptions", putSubscriptions)
r.GET("/peers", getPeers)
r.GET("/did", getDid)
r.GET("/stream/messages", wsMessages)
r.PUT("/files", putFiles)
r.GET("/files", getFiles)
srv := &http.Server{
Addr: ":4242",
Handler: r,
}
go func() {
log.Infof("starting REST server on %s...", srv.Addr)
if err := srv.ListenAndServe(); err != nil {
log.Infof("REST server shutdown requested: %s", err)
}
}()
nodeRun = NodeRun{
Ctx: ctx,
Ipfs: *ipfs,
}
log.Info("Flubber node started, press Ctrl-C to stop...")
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
cancel()
ipfs.Shutdown()
srv.Shutdown(ctx)
return err
}
// getMessages swagger:route GET /messages/ message getMessages
//
// Gets the messages for a subscription topic.
//
// Responses:
//
// 200: body:message
func getMessages(c *gin.Context) {
topic := c.Query("Topic")
if topic == "" {
c.String(http.StatusBadRequest, "Subscribe query-string must be in the form Topic=(topic).\n")
return
}
ctx, _ := context.WithDeadline(nodeRun.Ctx, time.Now().Add(time.Duration(5*time.Second)))
messages, err := ipfs.GetSubscriptionMessages(ctx, nodeRun.Ipfs, topic)
if err != nil {
c.String(http.StatusInternalServerError, "Could not retrieve messages for topic %s: %v.\n", topic, err)
} else {
c.JSON(http.StatusOK, messages)
}
}
func putDM(c *gin.Context) {
var dm p2p.DM
if err := c.BindQuery(&dm); err != nil || dm.Did == "" || dm.Message == "" {
c.String(http.StatusBadRequest, "Message query-string must be in the form Did=(did)&Message=(message).\n")
return
}
err := p2p.SendDM(nodeRun.Ctx, nodeRun.Ipfs, CurrentConfig.InfuraSecretKey, dm.Did, CurrentConfig.Did, dm.Message)
//c.String(http.StatusOK, "Sent DM to %s.", dm.Did)
if err != nil {
c.String(http.StatusInternalServerError, "Could not send DM to %s: %v.\n", dm.Did, err)
} else {
c.String(http.StatusOK, "Sent DM to %s.\n", dm.Did)
}
}
func getDM(c *gin.Context) {
c.JSON(http.StatusOK, p2p.DMs)
}
func putSubscriptions(c *gin.Context) {
topic := c.Query("Topic")
if topic == "" {
c.String(http.StatusBadRequest, "Subscribe query-string must be in the form Topic=(topic).\n")
return
}
err := ipfs.SubscribeToTopic(nodeRun.Ctx, nodeRun.Ipfs, topic)
if err != nil {
log.Errorf("could not subscribe to topic %s: %v", topic, err)
c.String(http.StatusInternalServerError, "Could not subscribe to topic %s: %v.\n", topic, err)
return
} else {
log.Infof("subscribed to topic %s", topic)
c.String(http.StatusOK, "Subscribed to topic %s.\n", topic)
return
}
}
func getSubscriptions(c *gin.Context) {
topics, err := ipfs.GetSubscriptionTopics(nodeRun.Ctx, nodeRun.Ipfs)
if err != nil {
c.String(http.StatusInternalServerError, "Could not retrieve subscription topics :%v.\n", err)
return
} else {
c.JSON(http.StatusOK, topics)
return
}
}
func putMessages(c *gin.Context) {
topic := c.Query("Topic")
data := c.Query("Data")
t := c.Query("Type")
if topic == "" || data == "" {
c.String(http.StatusBadRequest, "Query-string must be in the form Topic=(topic)&Data=(data)&Type=(type).\n")
return
}
if t == "" {
t = "string"
}
m := ipfs.SubscriptionMessage{
Did: CurrentConfig.Did,
Type: t,
Data: data,
Time: time.Now(),
Topic: topic,
}
err := ipfs.PublishSubscriptionMessage(nodeRun.Ctx, nodeRun.Ipfs, topic, m)
if err != nil {
log.Errorf("error publishing message: %v", err)
c.String(http.StatusInternalServerError, "Error publishing message: %v.\n", err)
} else {
log.Infof("published message: %v to topic %s", m, topic)
c.String(http.StatusOK, "Published message: %v to topic %s.", m, topic)
}
}
func getPeers(c *gin.Context) {
topic := c.Query("Topic")
peers, err := ipfs.GetSubscriptionPeers(nodeRun.Ctx, nodeRun.Ipfs, topic)
if err != nil {
log.Errorf("error getting peers: %v", err)
c.String(http.StatusInternalServerError, "Error getting peers: %v.", err)
} else {
c.JSON(http.StatusOK, peers)
}
}
func getDid(c *gin.Context) {
_did := c.Query("Did")
if _did == "" {
c.String(http.StatusBadRequest, "Query-string must be in the form Did=(did).\n")
return
}
d, err := did.Parse(_did)
if err != nil {
log.Errorf("error parsing DID %s: %v", _did, err)
c.String(http.StatusInternalServerError, "Error parsing did %s: %v.", _did, err)
return
} else if d.ID.Method != "ens" {
log.Errorf("error parsing DID %s: only ENS DIDs are currently supported", _did)
c.String(http.StatusInternalServerError, "Error parsing did %s: only ENS DIDs are currently supported.", _did)
return
}
en, err := blockchain.ResolveENS(d.ID.ID, CurrentConfig.InfuraSecretKey)
if err != nil {
log.Errorf("error resolving ENS name %s: %v", d.ID.ID, err)
c.String(http.StatusInternalServerError, "error resolving ENS name %s: %v.", d.ID.ID, err)
return
} else {
en.IPFSPubKey, _ = blockchain.ConvertIpfsKeyToPeer(en.IPFSPubKey)
c.JSON(http.StatusOK, en)
return
}
}
func wsMessages(c *gin.Context) {
topic := c.Query("Topic")
if topic == "" {
c.String(http.StatusBadRequest, "Query-string must be in the form Topic=(topic).\n")
return
}
conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
if err != nil {
log.Errorf("error creating WebSocket connection: %v", err)
c.String(http.StatusInternalServerError, "Error creating WebSocket connection: %v.", err)
return
}
defer conn.Close()
s, err := nodeRun.Ipfs.Api.PubSub().Subscribe(nodeRun.Ctx, topic)
if err != nil {
log.Errorf("error creating IPFS pubsub subscription: %v", err)
c.String(http.StatusInternalServerError, "Error creating IPFS pubsub subscription: %v.", err)
return
}
defer s.Close()
log.Infof("streaming message topic %s to %v at %v", topic, c.Request.UserAgent(), c.Request.RemoteAddr)
for {
_m, err := s.Next(nodeRun.Ctx)
if err == io.EOF {
time.Sleep(time.Millisecond * 100)
continue
}
if err == context.DeadlineExceeded || err == context.Canceled {
log.Infof("%v, closing connection", err)
break
} else if err != nil {
log.Errorf("error retrieving message for subscription %s: %v", topic, err)
continue
} else {
buf := bytes.NewBuffer(_m.Data())
var m ipfs.SubscriptionMessage
dec := gob.NewDecoder(buf)
err = dec.Decode(&m)
if err != nil {
log.Errorf("error decoding message: %v", err)
continue
} else {
log.Infof("received message for topic %s: %v", topic, m)
conn.WriteJSON(m)
}
}
}
}
func putFiles(c *gin.Context) {
f := c.Query("Path")
if f == "" {
c.String(http.StatusBadRequest, "Query-string must be in the form Path=(path).\n")
return
}
b, err := os.ReadFile(f)
if err != nil {
log.Errorf("error reading file %s: %v", f, err)
c.String(http.StatusInternalServerError, "Error reading file %s: %v", f, err)
return
}
ci_, err := nodeRun.Ipfs.PutIPFSBlock(nodeRun.Ctx, b)
if err != nil {
log.Errorf("coud not store file %s: %v", f, err)
c.String(http.StatusInternalServerError, "coud not store file %s: %v", f, err)
return
}
pnt := pinata.Pinata{Apikey: CurrentConfig.PinataApiKey, Secret: CurrentConfig.PinataSecretKey}
s, err := pnt.PinFile(f)
if err != nil {
log.Errorf("coud not store file %s: %v", f, err)
c.String(http.StatusInternalServerError, "coud not store file %s: %v", f, err)
return
}
pcid, _ := cid.Parse(s)
ipfs.Files = append(ipfs.Files, ipfs.File{Cid: pcid.String(), Size: len(b)})
m := ipfs.SubscriptionMessage{
Did: CurrentConfig.Did,
Type: "cid",
Data: pcid.String(),
Time: time.Now(),
Topic: "files",
}
err = ipfs.PublishSubscriptionMessage(nodeRun.Ctx, nodeRun.Ipfs, "files", m)
if err != nil {
log.Errorf("error publishing message: %v", err)
c.String(http.StatusInternalServerError, "Error publishing file notification message: %v.\n", err)
} else {
log.Infof("published message: %v to topic %s", m, "files")
c.String(http.StatusOK, "put file %s to IPFS block /ipfs/%v and Pinata pin CID %v", f, ci_, pcid)
}
}
func getFiles(c *gin.Context) {
c.JSON(http.StatusOK, ipfs.Files)
}