]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/cypherpunks.ru/nncp/tx.go
Merge branch 'develop'
[nncp.git] / src / cypherpunks.ru / nncp / tx.go
index a04f5bdb2dadaaf59012b3ab54939d14bfaff95c..5dff27c3be2aa5dcd030eabb0ea8d1bc20191da6 100644 (file)
@@ -22,9 +22,11 @@ import (
        "bufio"
        "bytes"
        "compress/zlib"
+       "crypto/rand"
        "errors"
        "hash"
        "io"
+       "io/ioutil"
        "os"
        "path/filepath"
        "strconv"
@@ -103,8 +105,52 @@ func (ctx *Ctx) Tx(node *Node, pkt *Pkt, nice uint8, size, minSize int64, src io
        return lastNode, err
 }
 
+func prepareTxFile(srcPath string) (io.Reader, *os.File, int64, error) {
+       var reader io.Reader
+       var src *os.File
+       var fileSize int64
+       var err error
+       if srcPath == "-" {
+               src, err = ioutil.TempFile("", "nncp-file")
+               if err != nil {
+                       return nil, nil, 0, err
+               }
+               os.Remove(src.Name())
+               tmpW := bufio.NewWriter(src)
+               tmpKey := new([32]byte)
+               if _, err = rand.Read(tmpKey[:]); err != nil {
+                       return nil, nil, 0, err
+               }
+               written, err := ae(tmpKey, bufio.NewReader(os.Stdin), tmpW)
+               if err != nil {
+                       return nil, nil, 0, err
+               }
+               fileSize = int64(written)
+               tmpW.Flush()
+               src.Seek(0, 0)
+               r, w := io.Pipe()
+               go ae(tmpKey, bufio.NewReader(src), w)
+               reader = r
+       } else {
+               src, err = os.Open(srcPath)
+               if err != nil {
+                       return nil, nil, 0, err
+               }
+               srcStat, err := src.Stat()
+               if err != nil {
+                       return nil, nil, 0, err
+               }
+               fileSize = srcStat.Size()
+               reader = bufio.NewReader(src)
+       }
+       return reader, src, fileSize, nil
+}
+
 func (ctx *Ctx) TxFile(node *Node, nice uint8, srcPath, dstPath string, minSize int64) error {
        if dstPath == "" {
+               if srcPath == "-" {
+                       return errors.New("Must provide destination filename")
+               }
                dstPath = filepath.Base(srcPath)
        }
        dstPath = filepath.Clean(dstPath)
@@ -115,16 +161,14 @@ func (ctx *Ctx) TxFile(node *Node, nice uint8, srcPath, dstPath string, minSize
        if err != nil {
                return err
        }
-       src, err := os.Open(srcPath)
-       if err != nil {
-               return err
+       reader, src, fileSize, err := prepareTxFile(srcPath)
+       if src != nil {
+               defer src.Close()
        }
-       defer src.Close()
-       srcStat, err := src.Stat()
        if err != nil {
                return err
        }
-       _, err = ctx.Tx(node, pkt, nice, srcStat.Size(), minSize, bufio.NewReader(src))
+       _, err = ctx.Tx(node, pkt, nice, fileSize, minSize, reader)
        if err == nil {
                ctx.LogI("tx", SDS{
                        "type": "file",
@@ -132,7 +176,7 @@ func (ctx *Ctx) TxFile(node *Node, nice uint8, srcPath, dstPath string, minSize
                        "nice": strconv.Itoa(int(nice)),
                        "src":  srcPath,
                        "dst":  dstPath,
-                       "size": strconv.FormatInt(srcStat.Size(), 10),
+                       "size": strconv.FormatInt(fileSize, 10),
                }, "sent")
        } else {
                ctx.LogE("tx", SDS{
@@ -141,7 +185,7 @@ func (ctx *Ctx) TxFile(node *Node, nice uint8, srcPath, dstPath string, minSize
                        "nice": strconv.Itoa(int(nice)),
                        "src":  srcPath,
                        "dst":  dstPath,
-                       "size": strconv.FormatInt(srcStat.Size(), 10),
+                       "size": strconv.FormatInt(fileSize, 10),
                        "err":  err,
                }, "sent")
        }
@@ -150,23 +194,23 @@ func (ctx *Ctx) TxFile(node *Node, nice uint8, srcPath, dstPath string, minSize
 
 func (ctx *Ctx) TxFileChunked(node *Node, nice uint8, srcPath, dstPath string, minSize int64, chunkSize int64) error {
        if dstPath == "" {
+               if srcPath == "-" {
+                       return errors.New("Must provide destination filename")
+               }
                dstPath = filepath.Base(srcPath)
        }
        dstPath = filepath.Clean(dstPath)
        if filepath.IsAbs(dstPath) {
                return errors.New("Relative destination path required")
        }
-       src, err := os.Open(srcPath)
-       if err != nil {
-               return err
+       reader, src, fileSize, err := prepareTxFile(srcPath)
+       if src != nil {
+               defer src.Close()
        }
-       defer src.Close()
-       srcStat, err := src.Stat()
        if err != nil {
                return err
        }
-       srcReader := bufio.NewReader(src)
-       fileSize := srcStat.Size()
+
        leftSize := fileSize
        metaPkt := ChunkedMeta{
                Magic:     MagicNNCPMv1,
@@ -204,10 +248,10 @@ func (ctx *Ctx) TxFileChunked(node *Node, nice uint8, srcPath, dstPath string, m
                        nice,
                        sizeToSend,
                        minSize,
-                       io.TeeReader(srcReader, hsh),
+                       io.TeeReader(reader, hsh),
                )
                if err == nil {
-                       ctx.LogD("tx", SDS{
+                       ctx.LogI("tx", SDS{
                                "type": "file",
                                "node": node.Id,
                                "nice": strconv.Itoa(int(nice)),
@@ -247,7 +291,7 @@ func (ctx *Ctx) TxFileChunked(node *Node, nice uint8, srcPath, dstPath string, m
        metaPktSize := int64(metaBuf.Len())
        _, err = ctx.Tx(node, pkt, nice, metaPktSize, minSize, &metaBuf)
        if err == nil {
-               ctx.LogD("tx", SDS{
+               ctx.LogI("tx", SDS{
                        "type": "file",
                        "node": node.Id,
                        "nice": strconv.Itoa(int(nice)),
@@ -255,14 +299,6 @@ func (ctx *Ctx) TxFileChunked(node *Node, nice uint8, srcPath, dstPath string, m
                        "dst":  path,
                        "size": strconv.FormatInt(metaPktSize, 10),
                }, "sent")
-               ctx.LogI("tx", SDS{
-                       "type": "file",
-                       "node": node.Id,
-                       "nice": strconv.Itoa(int(nice)),
-                       "src":  srcPath,
-                       "dst":  dstPath,
-                       "size": strconv.FormatInt(fileSize, 10),
-               }, "sent")
        } else {
                ctx.LogE("tx", SDS{
                        "type": "file",