]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/tx.go
Merge branch 'develop'
[nncp.git] / src / tx.go
index a90a49e42d05f0f96e3639e3740099f15a40c29c..e096ed65f873ddeb48f9ac635a01bbeea8849ba2 100644 (file)
--- a/src/tx.go
+++ b/src/tx.go
@@ -60,7 +60,7 @@ func (ctx *Ctx) Tx(
        if areaId != nil {
                area = ctx.AreaId2Area[*areaId]
                if area.Prv == nil {
-                       return nil, errors.New("unknown area id")
+                       return nil, errors.New("area has no encryption keys")
                }
        }
        hops := make([]*Node, 0, 1+len(node.Via))
@@ -116,7 +116,7 @@ func (ctx *Ctx) Tx(
                        )
                        pktEncRaws <- pktEncRaw
                        errs <- err
-                       dst.Close() // #nosec G104
+                       dst.Close()
                }(curSize, src, pipeW)
                curSize = PktEncOverhead + PktSizeOverhead + sizeWithTags(PktOverhead+curSize)
                curSize += padSize
@@ -142,7 +142,7 @@ func (ctx *Ctx) Tx(
                        )
                        pktEncRaws <- pktEncRaw
                        errs <- err
-                       dst.Close() // #nosec G104
+                       dst.Close()
                }(curSize, padSize, src, pipeW)
                curSize = PktEncOverhead + PktSizeOverhead + sizeWithTags(PktOverhead+curSize)
                curSize += padSize
@@ -170,7 +170,7 @@ func (ctx *Ctx) Tx(
                        )
                        pktEncRaws <- pktEncRaw
                        errs <- err
-                       dst.Close() // #nosec G104
+                       dst.Close()
                }(curSize, pipeRPrev, pipeW)
                curSize = PktEncOverhead + PktSizeOverhead + sizeWithTags(PktOverhead+curSize)
        }
@@ -197,7 +197,7 @@ func (ctx *Ctx) Tx(
                        pktEncRaw, err := PktEncWrite(ctx.Self, node, pkt, nice, size, 0, src, dst)
                        pktEncRaws <- pktEncRaw
                        errs <- err
-                       dst.Close() // #nosec G104
+                       dst.Close()
                }(hops[i], pktTrns, curSize, pipeRPrev, pipeW)
                curSize = PktEncOverhead + PktSizeOverhead + sizeWithTags(PktOverhead+curSize)
        }
@@ -220,13 +220,13 @@ func (ctx *Ctx) Tx(
        for i := 0; i <= wrappers; i++ {
                err = <-errs
                if err != nil {
-                       tmp.Fd.Close() // #nosec G104
+                       tmp.Fd.Close()
                        return nil, err
                }
        }
        nodePath := filepath.Join(ctx.Spool, lastNode.Id.String())
        err = tmp.Commit(filepath.Join(nodePath, string(TTx)))
-       os.Symlink(nodePath, filepath.Join(ctx.Spool, lastNode.Name)) // #nosec G104
+       os.Symlink(nodePath, filepath.Join(ctx.Spool, lastNode.Name))
        if err != nil {
                return lastNode, err
        }
@@ -239,7 +239,7 @@ func (ctx *Ctx) Tx(
                seenDir := filepath.Join(
                        ctx.Spool, ctx.SelfId.String(), AreaDir, areaId.String(),
                )
-               seenPath := filepath.Join(seenDir, msgHash+SeenSuffix)
+               seenPath := filepath.Join(seenDir, msgHash)
                les := LEs{
                        {"Node", node.Id},
                        {"Nice", int(nice)},
@@ -257,7 +257,7 @@ func (ctx *Ctx) Tx(
                                msgHash,
                        )
                }
-               if err = os.MkdirAll(seenDir, os.FileMode(0777)); err != nil {
+               if err = ensureDir(seenDir); err != nil {
                        ctx.LogE("tx-mkdir", les, err, logMsg)
                        return lastNode, err
                }
@@ -288,7 +288,7 @@ func throughTmpFile(r io.Reader) (
                rerr = err
                return
        }
-       os.Remove(src.Name()) // #nosec G104
+       os.Remove(src.Name())
        tmpW := bufio.NewWriter(src)
        tmpKey := make([]byte, chacha20poly1305.KeySize)
        if _, rerr = rand.Read(tmpKey[:]); rerr != nil {
@@ -316,8 +316,11 @@ func throughTmpFile(r io.Reader) (
        }
        r, w := io.Pipe()
        go func() {
+               for i := 0; i < aead.NonceSize(); i++ {
+                       nonce[i] = 0
+               }
                if _, err := aeadProcess(aead, nonce, nil, false, bufio.NewReader(src), w); err != nil {
-                       w.CloseWithError(err) // #nosec G104
+                       w.CloseWithError(err)
                }
        }()
        reader = r
@@ -436,14 +439,14 @@ func prepareTxFile(srcPath string) (
                        }
                        fd, err := os.Open(e.path)
                        if err != nil {
-                               fd.Close() // #nosec G104
+                               fd.Close()
                                return w.CloseWithError(err)
                        }
                        if _, err = io.Copy(tarWr, bufio.NewReader(fd)); err != nil {
-                               fd.Close() // #nosec G104
+                               fd.Close()
                                return w.CloseWithError(err)
                        }
-                       fd.Close() // #nosec G104
+                       fd.Close()
                }
                if err = tarWr.Close(); err != nil {
                        return w.CloseWithError(err)
@@ -687,9 +690,9 @@ func (ctx *Ctx) TxExec(
        if noCompress {
                pktType = PktTypeExecFat
        }
-       pkt, err := NewPkt(pktType, replyNice, bytes.Join(path, []byte{0}))
-       if err != nil {
-               return err
+       pkt, rerr := NewPkt(pktType, replyNice, bytes.Join(path, []byte{0}))
+       if rerr != nil {
+               return rerr
        }
        var size int64
 
@@ -703,22 +706,22 @@ func (ctx *Ctx) TxExec(
                        return err
                }
                if _, err = io.Copy(compressor, in); err != nil {
-                       compressor.Close() // #nosec G104
+                       compressor.Close()
                        return err
                }
                if err = compressor.Close(); err != nil {
                        return err
                }
                size = int64(compressed.Len())
-               _, err = ctx.Tx(node, pkt, nice, size, minSize, &compressed, handle, areaId)
+               _, rerr = ctx.Tx(node, pkt, nice, size, minSize, &compressed, handle, areaId)
        }
        if noCompress && !useTmp {
                var data bytes.Buffer
-               if _, err = io.Copy(&data, in); err != nil {
+               if _, err := io.Copy(&data, in); err != nil {
                        return err
                }
                size = int64(data.Len())
-               _, err = ctx.Tx(node, pkt, nice, size, minSize, &data, handle, areaId)
+               _, rerr = ctx.Tx(node, pkt, nice, size, minSize, &data, handle, areaId)
        }
        if !noCompress && useTmp {
                r, w := io.Pipe()
@@ -730,7 +733,7 @@ func (ctx *Ctx) TxExec(
                go func() {
                        _, err := io.Copy(compressor, in)
                        if err != nil {
-                               compressor.Close() // #nosec G104
+                               compressor.Close()
                                copyErr <- err
                        }
                        err = compressor.Close()
@@ -749,7 +752,7 @@ func (ctx *Ctx) TxExec(
                        return err
                }
                size = fileSize
-               _, err = ctx.Tx(node, pkt, nice, size, minSize, tmpReader, handle, areaId)
+               _, rerr = ctx.Tx(node, pkt, nice, size, minSize, tmpReader, handle, areaId)
        }
        if noCompress && useTmp {
                tmpReader, closer, fileSize, err := throughTmpFile(in)
@@ -760,7 +763,7 @@ func (ctx *Ctx) TxExec(
                        return err
                }
                size = fileSize
-               _, err = ctx.Tx(node, pkt, nice, size, minSize, tmpReader, handle, areaId)
+               _, rerr = ctx.Tx(node, pkt, nice, size, minSize, tmpReader, handle, areaId)
        }
 
        dst := strings.Join(append([]string{handle}, args...), " ")
@@ -778,12 +781,12 @@ func (ctx *Ctx) TxExec(
                        ctx.NodeName(node.Id), dst, humanize.IBytes(uint64(size)),
                )
        }
-       if err == nil {
+       if rerr == nil {
                ctx.LogI("tx", les, logMsg)
        } else {
-               ctx.LogE("tx", les, err, logMsg)
+               ctx.LogE("tx", les, rerr, logMsg)
        }
-       return err
+       return rerr
 }
 
 func (ctx *Ctx) TxTrns(node *Node, nice uint8, size int64, src io.Reader) error {
@@ -825,6 +828,6 @@ func (ctx *Ctx) TxTrns(node *Node, nice uint8, size int64, src io.Reader) error
        } else {
                ctx.LogI("tx", append(les, LE{"Err", err}), logMsg)
        }
-       os.Symlink(nodePath, filepath.Join(ctx.Spool, node.Name)) // #nosec G104
+       os.Symlink(nodePath, filepath.Join(ctx.Spool, node.Name))
        return err
 }