/
gateway.go
516 lines (463 loc) · 13.5 KB
/
gateway.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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
package gateway
import (
"context"
"errors"
"fmt"
"html/template"
"io/ioutil"
"net/http"
"net/url"
"strings"
"sync"
"time"
"github.com/gin-contrib/location"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
"github.com/ipfs/go-cid"
logging "github.com/ipfs/go-log"
iface "github.com/ipfs/interface-go-ipfs-core"
isd "github.com/jbenet/go-is-domain"
"github.com/libp2p/go-libp2p-core/peer"
ma "github.com/multiformats/go-multiaddr"
mbase "github.com/multiformats/go-multibase"
"github.com/rs/cors"
gincors "github.com/rs/cors/wrapper/gin"
threadsclient "github.com/textileio/go-threads/api/client"
"github.com/textileio/go-threads/broadcast"
"github.com/textileio/go-threads/core/thread"
tutil "github.com/textileio/go-threads/util"
bucketsclient "github.com/textileio/textile/api/buckets/client"
"github.com/textileio/textile/api/common"
mdb "github.com/textileio/textile/mongodb"
"go.mongodb.org/mongo-driver/mongo"
"google.golang.org/grpc"
)
var log = logging.Logger("gateway")
const handlerTimeout = time.Minute
func init() {
gin.SetMode(gin.ReleaseMode)
}
// link is used for Unixfs directory templates.
type link struct {
Name string
Path string
Size string
Links string
}
// Gateway provides HTTP-based access to Textile.
type Gateway struct {
sync.Mutex
server *http.Server
addr ma.Multiaddr
url string
subdomains bool
bucketsDomain string
collections *mdb.Collections
apiSession string
threads *threadsclient.Client
buckets *bucketsclient.Client
hub bool
ipfs iface.CoreAPI
emailSessionBus *broadcast.Broadcaster
}
// Config defines the gateway configuration.
type Config struct {
Addr ma.Multiaddr
URL string
Subdomains bool
BucketsDomain string
APIAddr ma.Multiaddr
APISession string
Collections *mdb.Collections
IPFSClient iface.CoreAPI
EmailSessionBus *broadcast.Broadcaster
Hub bool
Debug bool
}
// NewGateway returns a new gateway.
func NewGateway(conf Config) (*Gateway, error) {
if conf.Debug {
if err := tutil.SetLogLevels(map[string]logging.LogLevel{
"gateway": logging.LevelDebug,
}); err != nil {
return nil, err
}
}
apiTarget, err := tutil.TCPAddrFromMultiAddr(conf.APIAddr)
if err != nil {
return nil, err
}
opts := []grpc.DialOption{
grpc.WithInsecure(),
grpc.WithPerRPCCredentials(common.Credentials{}),
}
tc, err := threadsclient.NewClient(apiTarget, opts...)
if err != nil {
return nil, err
}
bc, err := bucketsclient.NewClient(apiTarget, opts...)
if err != nil {
return nil, err
}
return &Gateway{
addr: conf.Addr,
url: conf.URL,
subdomains: conf.Subdomains,
bucketsDomain: conf.BucketsDomain,
collections: conf.Collections,
apiSession: conf.APISession,
threads: tc,
buckets: bc,
hub: conf.Hub,
ipfs: conf.IPFSClient,
emailSessionBus: conf.EmailSessionBus,
}, nil
}
// Start the gateway.
func (g *Gateway) Start() {
addr, err := tutil.TCPAddrFromMultiAddr(g.addr)
if err != nil {
log.Fatal(err)
}
router := gin.Default()
temp, err := loadTemplate()
if err != nil {
log.Fatal(err)
}
router.SetHTMLTemplate(temp)
router.Use(location.Default())
router.Use(static.Serve("", &fileSystem{Assets}))
router.Use(serveBucket(&bucketFS{
client: g.buckets,
keys: g.collections.IPNSKeys,
session: g.apiSession,
host: g.bucketsDomain,
}))
router.Use(gincors.New(cors.Options{}))
router.GET("/health", func(c *gin.Context) {
c.Writer.WriteHeader(http.StatusNoContent)
})
router.GET("/thread/:thread/:collection", g.subdomainOptionHandler, g.collectionHandler)
router.GET("/thread/:thread/:collection/:id", g.subdomainOptionHandler, g.instanceHandler)
router.GET("/thread/:thread/:collection/:id/*path", g.subdomainOptionHandler, g.instanceHandler)
router.GET("/ipfs/:root", g.subdomainOptionHandler, g.ipfsHandler)
router.GET("/ipfs/:root/*path", g.subdomainOptionHandler, g.ipfsHandler)
router.GET("/ipns/:key", g.subdomainOptionHandler, g.ipnsHandler)
router.GET("/ipns/:key/*path", g.subdomainOptionHandler, g.ipnsHandler)
router.GET("/p2p/:key", g.subdomainOptionHandler, g.p2pHandler)
router.GET("/ipld/:root", g.subdomainOptionHandler, g.ipldHandler)
router.GET("/ipld/:root/*path", g.subdomainOptionHandler, g.ipldHandler)
if g.hub {
router.GET("/dashboard/:username", g.dashboardHandler)
router.GET("/confirm/:secret", g.confirmEmail)
router.GET("/consent/:invite", g.consentInvite)
}
router.NoRoute(g.subdomainHandler)
g.server = &http.Server{
Addr: addr,
Handler: router,
}
go func() {
if err := g.server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("gateway error: %s", err)
}
log.Info("gateway was shutdown")
}()
log.Infof("gateway listening at %s", g.server.Addr)
}
// loadTemplate loads HTML templates.
func loadTemplate() (*template.Template, error) {
t := template.New("")
for name, file := range Assets.Files {
if file.IsDir() || !strings.HasSuffix(name, ".gohtml") {
continue
}
h, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}
t, err = t.New(name).Parse(string(h))
if err != nil {
return nil, err
}
}
return t, nil
}
// Addr returns the gateway's address.
func (g *Gateway) Addr() string {
return g.server.Addr
}
// Stop the gateway.
func (g *Gateway) Stop() error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
if err := g.server.Shutdown(ctx); err != nil {
return err
}
if err := g.threads.Close(); err != nil {
return err
}
if err := g.buckets.Close(); err != nil {
return err
}
return nil
}
// subdomainOptionHandler redirects valid namespaces to subdomains if the option is enabled.
func (g *Gateway) subdomainOptionHandler(c *gin.Context) {
if !g.subdomains {
return
}
loc, ok := g.toSubdomainURL(c.Request)
if !ok {
render404(c)
return
}
// See security note https://github.com/ipfs/go-ipfs/blob/dbfa7bf2b216bad9bec1ff66b1f3814f4faac31e/core/corehttp/hostname.go#L105
c.Request.Header.Set("Clear-Site-Data", "\"cookies\", \"storage\"")
c.Redirect(http.StatusPermanentRedirect, loc)
}
// dashboardHandler renders a dev or org dashboard.
func (g *Gateway) dashboardHandler(c *gin.Context) {
render404(c)
}
// confirmEmail verifies an emailed secret.
func (g *Gateway) confirmEmail(c *gin.Context) {
if err := g.emailSessionBus.Send(c.Param("secret")); err != nil {
renderError(c, http.StatusInternalServerError, err)
return
}
c.HTML(http.StatusOK, "/public/html/confirm.gohtml", nil)
}
// consentInvite marks an invite as accepted.
// If the associated email belongs to an existing user, they will be added to the org.
func (g *Gateway) consentInvite(c *gin.Context) {
ctx, cancel := context.WithTimeout(context.Background(), handlerTimeout)
defer cancel()
invite, err := g.collections.Invites.Get(ctx, c.Param("invite"))
if err != nil {
if errors.Is(err, mongo.ErrNoDocuments) {
render404(c)
} else {
renderError(c, http.StatusInternalServerError, err)
}
return
}
if !invite.Accepted {
if time.Now().After(invite.ExpiresAt) {
if err := g.collections.Invites.Delete(ctx, invite.Token); err != nil {
renderError(c, http.StatusInternalServerError, err)
} else {
renderError(c, http.StatusPreconditionFailed, fmt.Errorf("this invitation has expired"))
}
return
}
dev, err := g.collections.Accounts.GetByUsernameOrEmail(ctx, invite.EmailTo)
if err != nil {
if errors.Is(err, mongo.ErrNoDocuments) {
if err := g.collections.Invites.Accept(ctx, invite.Token); err != nil {
renderError(c, http.StatusInternalServerError, err)
}
} else {
renderError(c, http.StatusInternalServerError, err)
return
}
}
if dev != nil {
if err := g.collections.Accounts.AddMember(ctx, invite.Org, mdb.Member{
Key: dev.Key,
Username: dev.Username,
Role: mdb.OrgMember,
}); err != nil {
if err == mongo.ErrNoDocuments {
if err := g.collections.Invites.Delete(ctx, invite.Token); err != nil {
renderError(c, http.StatusInternalServerError, err)
} else {
renderError(c, http.StatusNotFound, fmt.Errorf("org not found"))
}
} else {
renderError(c, http.StatusInternalServerError, err)
}
return
}
if err = g.collections.Invites.Delete(ctx, invite.Token); err != nil {
renderError(c, http.StatusInternalServerError, err)
return
}
}
}
c.HTML(http.StatusOK, "/public/html/consent.gohtml", gin.H{
"Org": invite.Org,
"Email": invite.EmailTo,
})
}
// render404 renders the 404 template.
func render404(c *gin.Context) {
c.HTML(http.StatusNotFound, "/public/html/404.gohtml", nil)
}
// renderError renders the error template.
func renderError(c *gin.Context, code int, err error) {
c.HTML(code, "/public/html/error.gohtml", gin.H{
"Code": code,
"Error": formatError(err),
})
}
// formatError formats a go error for browser display.
func formatError(err error) string {
words := strings.SplitN(err.Error(), " ", 2)
words[0] = strings.Title(words[0])
return strings.Join(words, " ") + "."
}
// subdomainHandler handles requests by parsing the request subdomain.
func (g *Gateway) subdomainHandler(c *gin.Context) {
c.Status(200)
host := strings.SplitN(c.Request.Host, ":", 2)[0]
parts := strings.Split(host, ".")
key := parts[0]
// Render buckets if the domain matches
if g.bucketsDomain != "" && strings.HasSuffix(host, g.bucketsDomain) {
g.renderWWWBucket(c, key)
return
}
if len(parts) < 3 {
render404(c)
return
}
ns := parts[1]
if !isSubdomainNamespace(ns) {
render404(c)
return
}
switch ns {
case "ipfs":
g.renderIPFSPath(c, "ipfs/"+key, "/ipfs/"+key+c.Request.URL.Path)
case "ipns":
g.renderIPNSKey(c, key, c.Request.URL.Path)
case "p2p":
g.renderP2PKey(c, key)
case "ipld":
g.renderIPLDPath(c, key+c.Request.URL.Path)
case "thread":
threadID, err := thread.Decode(key)
if err != nil {
renderError(c, http.StatusBadRequest, fmt.Errorf("invalid thread ID"))
return
}
parts := strings.SplitN(strings.TrimSuffix(c.Request.URL.Path, "/"), "/", 4)
switch len(parts) {
case 1:
// @todo: Render something at the thread root
render404(c)
case 2:
if parts[1] != "" {
g.renderCollection(c, threadID, parts[1])
} else {
render404(c)
}
case 3:
g.renderInstance(c, threadID, parts[1], parts[2], "")
case 4:
g.renderInstance(c, threadID, parts[1], parts[2], parts[3])
default:
render404(c)
}
default:
render404(c)
}
}
// Modified from https://github.com/ipfs/go-ipfs/blob/dbfa7bf2b216bad9bec1ff66b1f3814f4faac31e/core/corehttp/hostname.go#L251
func isSubdomainNamespace(ns string) bool {
switch ns {
case "ipfs", "ipns", "p2p", "ipld", "thread":
return true
default:
return false
}
}
// Copied from https://github.com/ipfs/go-ipfs/blob/dbfa7bf2b216bad9bec1ff66b1f3814f4faac31e/core/corehttp/hostname.go#L260
func isPeerIDNamespace(ns string) bool {
switch ns {
case "ipns", "p2p":
return true
default:
return false
}
}
// Converts a hostname/path to a subdomain-based URL, if applicable.
// Modified from https://github.com/ipfs/go-ipfs/blob/dbfa7bf2b216bad9bec1ff66b1f3814f4faac31e/core/corehttp/hostname.go#L270
func (g *Gateway) toSubdomainURL(r *http.Request) (redirURL string, ok bool) {
var ns, rootID, rest string
query := r.URL.RawQuery
parts := strings.SplitN(r.URL.Path, "/", 4)
safeRedirectURL := func(in string) (out string, ok bool) {
safeURI, err := url.ParseRequestURI(in)
if err != nil {
return "", false
}
return safeURI.String(), true
}
switch len(parts) {
case 4:
rest = parts[3]
fallthrough
case 3:
ns = parts[1]
rootID = parts[2]
default:
return "", false
}
if !isSubdomainNamespace(ns) {
return "", false
}
// add prefix if query is present
if query != "" {
query = "?" + query
}
// Normalize problematic PeerIDs (eg. ed25519+identity) to CID representation
if isPeerIDNamespace(ns) && !isd.IsDomain(rootID) {
peerID, err := peer.Decode(rootID)
// Note: PeerID CIDv1 with protobuf multicodec will fail, but we fix it
// in the next block
if err == nil {
rootID = peer.ToCid(peerID).String()
}
}
// If rootID is a CID, ensure it uses DNS-friendly text representation
if rootCid, err := cid.Decode(rootID); err == nil {
multicodec := rootCid.Type()
// PeerIDs represented as CIDv1 are expected to have libp2p-key
// multicodec (https://github.com/libp2p/specs/pull/209).
// We ease the transition by fixing multicodec on the fly:
// https://github.com/ipfs/go-ipfs/issues/5287#issuecomment-492163929
if isPeerIDNamespace(ns) && multicodec != cid.Libp2pKey {
multicodec = cid.Libp2pKey
}
// if object turns out to be a valid CID,
// ensure text representation used in subdomain is CIDv1 in Base32
// https://github.com/ipfs/in-web-browsers/issues/89
rootID, err = cid.NewCidV1(multicodec, rootCid.Hash()).StringOfBase(mbase.Base32)
if err != nil {
// should not error, but if it does, its clealy not possible to
// produce a subdomain URL
return "", false
}
}
urlparts := strings.Split(g.url, "://")
if len(urlparts) < 2 {
return "", false
}
scheme := urlparts[0]
host := urlparts[1]
return safeRedirectURL(fmt.Sprintf("%s://%s.%s.%s/%s%s", scheme, rootID, ns, host, rest, query))
}
func byteCountDecimal(b int64) string {
const unit = 1000
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cB", float64(b)/float64(div), "kMGTPE"[exp])
}