X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=src%2Fcypherpunks.ru%2Fnncp%2Ftx.go;h=cb8153fe0419705b991c621a662396e1238c5e64;hb=dd92823db3d72fb21a4c712a7fb052dce16443dd;hp=0692ecaa66433128026799f3b80d609fc6a7d111;hpb=10cca26e7daf070e661e546d9e8b627a61375936;p=nncp.git diff --git a/src/cypherpunks.ru/nncp/tx.go b/src/cypherpunks.ru/nncp/tx.go index 0692eca..cb8153f 100644 --- a/src/cypherpunks.ru/nncp/tx.go +++ b/src/cypherpunks.ru/nncp/tx.go @@ -1,11 +1,10 @@ /* NNCP -- Node to Node copy, utilities for store-and-forward data exchange -Copyright (C) 2016-2018 Sergey Matveev +Copyright (C) 2016-2019 Sergey Matveev This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. +the Free Software Foundation, version 3 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -34,6 +33,7 @@ import ( "github.com/davecgh/go-xdr/xdr2" "golang.org/x/crypto/blake2b" + "golang.org/x/crypto/chacha20poly1305" ) func (ctx *Ctx) Tx( @@ -41,7 +41,8 @@ func (ctx *Ctx) Tx( pkt *Pkt, nice uint8, size, minSize int64, - src io.Reader) (*Node, error) { + src io.Reader, +) (*Node, error) { tmp, err := ctx.NewTmpFileWHash() if err != nil { return nil, err @@ -53,7 +54,11 @@ func (ctx *Ctx) Tx( lastNode = ctx.Neigh[*node.Via[i-1]] hops = append(hops, lastNode) } - padSize := minSize - size - int64(len(hops))*(PktOverhead+PktEncOverhead) + expectedSize := size + for i := 0; i < len(hops); i++ { + expectedSize = PktEncOverhead + PktSizeOverhead + sizeWithTags(PktOverhead+expectedSize) + } + padSize := minSize - expectedSize if padSize < 0 { padSize = 0 } @@ -69,12 +74,11 @@ func (ctx *Ctx) Tx( errs <- PktEncWrite(ctx.Self, hops[0], pkt, nice, size, padSize, src, dst) dst.Close() }(curSize, src, pipeW) - curSize += padSize + curSize = PktEncOverhead + PktSizeOverhead + sizeWithTags(PktOverhead+curSize) + padSize var pipeRPrev io.Reader for i := 1; i < len(hops); i++ { pktTrns, _ := NewPkt(PktTypeTrns, 0, hops[i-1].Id[:]) - curSize += PktOverhead + PktEncOverhead pipeRPrev = pipeR pipeR, pipeW = io.Pipe() go func(node *Node, pkt *Pkt, size int64, src io.Reader, dst io.WriteCloser) { @@ -86,6 +90,7 @@ func (ctx *Ctx) Tx( errs <- PktEncWrite(ctx.Self, node, pkt, nice, size, 0, src, dst) dst.Close() }(hops[i], pktTrns, curSize, pipeRPrev, pipeW) + curSize = PktEncOverhead + PktSizeOverhead + sizeWithTags(PktOverhead+curSize) } go func() { _, err := io.Copy(tmp.W, pipeR) @@ -116,19 +121,30 @@ func prepareTxFile(srcPath string) (io.Reader, *os.File, int64, error) { } os.Remove(src.Name()) tmpW := bufio.NewWriter(src) - tmpKey := new([32]byte) + tmpKey := make([]byte, chacha20poly1305.KeySize) if _, err = rand.Read(tmpKey[:]); err != nil { return nil, nil, 0, err } - written, err := ae(tmpKey, bufio.NewReader(os.Stdin), tmpW) + aead, err := chacha20poly1305.New(tmpKey) + if err != nil { + return nil, nil, 0, err + } + nonce := make([]byte, aead.NonceSize()) + written, err := aeadProcess(aead, nonce, true, bufio.NewReader(os.Stdin), tmpW) if err != nil { return nil, nil, 0, err } fileSize = int64(written) - tmpW.Flush() - src.Seek(0, 0) + if err = tmpW.Flush(); err != nil { + return nil, nil, 0, err + } + src.Seek(0, io.SeekStart) r, w := io.Pipe() - go ae(tmpKey, bufio.NewReader(src), w) + go func() { + if _, err := aeadProcess(aead, nonce, false, bufio.NewReader(src), w); err != nil { + panic(err) + } + }() reader = r } else { src, err = os.Open(srcPath) @@ -190,7 +206,8 @@ func (ctx *Ctx) TxFileChunked( nice uint8, srcPath, dstPath string, minSize int64, - chunkSize int64) error { + chunkSize int64, +) error { if dstPath == "" { if srcPath == "-" { return errors.New("Must provide destination filename") @@ -365,7 +382,8 @@ func (ctx *Ctx) TxExec( handle string, args []string, body []byte, - minSize int64) error { + minSize int64, +) error { path := make([][]byte, 0, 1+len(args)) path = append(path, []byte(handle)) for _, arg := range args {