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"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"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"
|
2020-04-16 20:05:05 +01:00
|
|
|
"storj.io/common/ranger/httpranger"
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/storj"
|
2020-02-21 14:07:29 +00:00
|
|
|
"storj.io/uplink/private/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() {
|
|
|
|
flag.Parse()
|
|
|
|
if flag.Arg(0) == "" {
|
|
|
|
fmt.Printf("usage: %s <targetdir>\n", os.Args[0])
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
err := Main()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Main is the exported CLI executable function.
|
2018-04-17 14:39:14 +01:00
|
|
|
func Main() error {
|
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
|
|
|
|
}
|
|
|
|
pieces, err := ioutil.ReadDir(flag.Arg(0))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-09-14 15:10:43 +01:00
|
|
|
rrs := map[int]ranger.Ranger{}
|
2018-04-17 14:39:14 +01:00
|
|
|
for _, piece := range pieces {
|
|
|
|
piecenum, err := strconv.Atoi(strings.TrimSuffix(piece.Name(), ".piece"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-24 03:16:34 +01:00
|
|
|
r, err := ranger.FileRanger(filepath.Join(flag.Arg(0), piece.Name()))
|
2018-04-17 14:39:14 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-24 03:16:34 +01:00
|
|
|
rrs[piecenum] = r
|
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
|
|
|
|
}
|
2018-06-19 16:59:09 +01:00
|
|
|
ctx := context.Background()
|
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) {
|
2020-04-16 20:05:05 +01:00
|
|
|
httpranger.ServeContent(ctx, w, r, flag.Arg(0), time.Time{}, rr)
|
2018-04-17 14:39:14 +01:00
|
|
|
}))
|
|
|
|
}
|