2018-04-17 14:39:14 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// 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"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/eestream"
|
|
|
|
"storj.io/storj/pkg/ranger"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
addr = flag.String("addr", "localhost:8080", "address to serve from")
|
|
|
|
pieceBlockSize = flag.Int("piece_block_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")
|
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
|
|
|
encKey := sha256.Sum256([]byte(*key))
|
|
|
|
fc, err := infectious.NewFEC(*rsk, *rsn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
es := eestream.NewRSScheme(fc, *pieceBlockSize)
|
2018-05-15 16:11:03 +01:00
|
|
|
var firstNonce [12]byte
|
|
|
|
decrypter, err := eestream.NewAESGCMDecrypter(
|
2018-04-17 14:39:14 +01:00
|
|
|
&encKey, &firstNonce, es.DecodedBlockSize())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-26 13:51:49 +01:00
|
|
|
// initialize http rangers in parallel to save from network latency
|
2018-04-17 14:39:14 +01:00
|
|
|
rrs := map[int]ranger.Ranger{}
|
2018-04-26 18:51:28 +01:00
|
|
|
type indexRangerError struct {
|
|
|
|
i int
|
|
|
|
rr ranger.Ranger
|
|
|
|
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)
|
2018-04-26 13:51:49 +01:00
|
|
|
rr, err := ranger.HTTPRanger(url)
|
|
|
|
result <- indexRangerError{i, rr, err}
|
|
|
|
}(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
|
|
|
}
|
2018-05-14 21:30:57 +01:00
|
|
|
rr, err := eestream.Decode(context.Background(), rrs, es, 4*1024*1024)
|
2018-04-17 14:39:14 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rr, err = eestream.Transform(rr, decrypter)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rr, err = eestream.UnpadSlow(rr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.ListenAndServe(*addr, http.HandlerFunc(
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ranger.ServeContent(w, r, flag.Arg(0), time.Time{}, rr)
|
|
|
|
}))
|
|
|
|
}
|