storj/examples/eestream/serve/main.go
aligeti 0376dc4bd2 AES GCM implementation and unit test code (#19)
* AES GCM implementation and unit test code

* modified and tested per the code review comments

* modified and tested per the code review comments

* updated to return err

* removed the debug printf commented code

* support of aes-gcm

* updated the renaming convention per GOLANG coding standards

* Initial Go & C biniding with libstorj

* Initial GO & C bindings with libstorj

* fixing the callback

* moved a millimeter :-) on c to go to gone....

*  added error condition, per review comment

* removed the .idea directory and also movie.avi file

* deleting files not to be in this pull request

* Revert "deleting files not to be in this pull request"

This reverts commit 026b834fe00f6b20a7566e71973fe224c12f533f.

* deleting files not to be in this pull request

* resolving conflicts

* syncing the file to master

* Use aes gcm in eestream rs tests

* Use aes gcm in serve example

* Fixed comment
2018-05-15 18:11:03 +03:00

95 lines
2.0 KiB
Go

// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"context"
"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)
}
}
// Main is the exported CLI executable function
func Main() error {
encKey := sha256.Sum256([]byte(*key))
fc, err := infectious.NewFEC(*rsk, *rsn)
if err != nil {
return err
}
es := eestream.NewRSScheme(fc, *pieceBlockSize)
var firstNonce [12]byte
decrypter, err := eestream.NewAESGCMDecrypter(
&encKey, &firstNonce, es.DecodedBlockSize())
if err != nil {
return err
}
pieces, err := ioutil.ReadDir(flag.Arg(0))
if err != nil {
return err
}
rrs := map[int]ranger.Ranger{}
for _, piece := range pieces {
piecenum, err := strconv.Atoi(strings.TrimSuffix(piece.Name(), ".piece"))
if err != nil {
return err
}
r, err := ranger.FileRanger(filepath.Join(flag.Arg(0), piece.Name()))
if err != nil {
return err
}
defer r.Close()
rrs[piecenum] = r
}
rr, err := eestream.Decode(context.Background(), rrs, es, 4*1024*1024)
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)
}))
}