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"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"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() {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-09-26 14:32:23 +01:00
|
|
|
encKey := eestream.Key(sha256.Sum256([]byte(*key)))
|
2018-04-17 14:39:14 +01:00
|
|
|
fc, err := infectious.NewFEC(*rsk, *rsn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
es := eestream.NewRSScheme(fc, *pieceBlockSize)
|
2018-09-26 14:32:23 +01:00
|
|
|
var firstNonce eestream.Nonce
|
|
|
|
cipher := eestream.AESGCM
|
|
|
|
decrypter, err := cipher.NewDecrypter(&encKey, &firstNonce, es.DecodedBlockSize())
|
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
|
|
|
}
|
2018-07-03 09:35:01 +01:00
|
|
|
rc, err := eestream.Decode(rrs, es, 4*1024*1024)
|
2018-04-17 14:39:14 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-07-03 09:35:01 +01:00
|
|
|
rr, err := eestream.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()
|
|
|
|
rr, err = eestream.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
|
|
|
}))
|
|
|
|
}
|