2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-04-17 14:39:14 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-05-14 21:30:57 +01:00
|
|
|
"context"
|
2018-04-17 14:39:14 +01:00
|
|
|
"crypto/sha256"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/vivint/infectious"
|
2019-07-31 15:38:44 +01:00
|
|
|
"go.uber.org/zap"
|
2018-04-17 14:39:14 +01:00
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/encryption"
|
|
|
|
"storj.io/common/ranger"
|
|
|
|
"storj.io/common/storj"
|
2020-01-08 13:40:19 +00:00
|
|
|
"storj.io/uplink/eestream"
|
2018-04-17 14:39:14 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-09-27 12:52:18 +01:00
|
|
|
addr = flag.String("addr", "localhost:8080", "address to serve from")
|
|
|
|
erasureShareSize = flag.Int("erasure_share_size", 4*1024, "block size of pieces")
|
|
|
|
key = flag.String("key", "a key", "the secret key")
|
|
|
|
rsk = flag.Int("required", 20, "rs required")
|
|
|
|
rsn = flag.Int("total", 40, "rs total")
|
2018-04-17 14:39:14 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
err := Main()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-25 15:55:26 +01:00
|
|
|
// Main is the exported CLI executable function
|
2018-04-17 14:39:14 +01:00
|
|
|
func Main() error {
|
2019-06-04 12:36:27 +01:00
|
|
|
ctx := context.Background()
|
2018-10-19 14:38:13 +01:00
|
|
|
encKey := storj.Key(sha256.Sum256([]byte(*key)))
|
2018-04-17 14:39:14 +01:00
|
|
|
fc, err := infectious.NewFEC(*rsk, *rsn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-09-27 12:52:18 +01:00
|
|
|
es := eestream.NewRSScheme(fc, *erasureShareSize)
|
2018-10-19 14:38:13 +01:00
|
|
|
var firstNonce storj.Nonce
|
2019-07-03 19:07:44 +01:00
|
|
|
decrypter, err := encryption.NewDecrypter(storj.EncAESGCM, &encKey, &firstNonce, es.StripeSize())
|
2018-04-17 14:39:14 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-26 13:51:49 +01:00
|
|
|
// initialize http rangers in parallel to save from network latency
|
2018-09-14 15:10:43 +01:00
|
|
|
rrs := map[int]ranger.Ranger{}
|
2018-04-26 18:51:28 +01:00
|
|
|
type indexRangerError struct {
|
|
|
|
i int
|
2018-09-14 15:10:43 +01:00
|
|
|
rr ranger.Ranger
|
2018-04-26 18:51:28 +01:00
|
|
|
err error
|
|
|
|
}
|
2018-04-26 13:51:49 +01:00
|
|
|
result := make(chan indexRangerError, *rsn)
|
|
|
|
for i := 0; i < *rsn; i++ {
|
|
|
|
go func(i int) {
|
2018-04-26 18:51:28 +01:00
|
|
|
url := fmt.Sprintf("http://18.184.133.99:%d", 10000+i)
|
2019-06-04 12:36:27 +01:00
|
|
|
rr, err := ranger.HTTPRanger(ctx, url)
|
2018-09-14 15:10:43 +01:00
|
|
|
result <- indexRangerError{i: i, rr: rr, err: err}
|
2018-04-26 13:51:49 +01:00
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
// wait for all goroutines to finish and save result in rrs map
|
|
|
|
for i := 0; i < *rsn; i++ {
|
|
|
|
res := <-result
|
|
|
|
if res.err != nil {
|
|
|
|
// return on the first failure
|
2018-04-17 14:39:14 +01:00
|
|
|
return err
|
|
|
|
}
|
2018-04-26 13:51:49 +01:00
|
|
|
rrs[res.i] = res.rr
|
2018-04-17 14:39:14 +01:00
|
|
|
}
|
2019-07-31 15:38:44 +01:00
|
|
|
rc, err := eestream.Decode(zap.L(), rrs, es, 4*1024*1024, false)
|
2018-04-17 14:39:14 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-18 12:10:29 +01:00
|
|
|
rr, err := encryption.Transform(rc, decrypter)
|
2018-04-17 14:39:14 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-19 15:37:59 +00:00
|
|
|
rr, err = encryption.UnpadSlow(ctx, rr)
|
2018-04-17 14:39:14 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.ListenAndServe(*addr, http.HandlerFunc(
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
2018-06-19 16:59:09 +01:00
|
|
|
ranger.ServeContent(ctx, w, r, flag.Arg(0), time.Time{}, rr)
|
2018-04-17 14:39:14 +01:00
|
|
|
}))
|
|
|
|
}
|