]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/cmd/nncp-daemon/main.go
Merge branch 'develop'
[nncp.git] / src / cmd / nncp-daemon / main.go
index 897dc06f1d181f8c72614ad9279c08884158ee2b..b2b8911ec21b2abbbc6daaf30b40d253d7b9fcc0 100644 (file)
@@ -1,6 +1,6 @@
 /*
 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
-Copyright (C) 2016-2019 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2016-2021 Sergey Matveev <stargrave@stargrave.org>
 
 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
@@ -24,7 +24,6 @@ import (
        "log"
        "net"
        "os"
-       "strconv"
        "time"
 
        "go.cypherpunks.ru/nncp/v5"
@@ -60,32 +59,46 @@ func (c InetdConn) SetWriteDeadline(t time.Time) error {
 }
 
 func (c InetdConn) Close() error {
+       if err := c.r.Close(); err != nil {
+               c.w.Close() // #nosec G104
+               return err
+       }
        return c.w.Close()
 }
 
-func performSP(ctx *nncp.Ctx, conn nncp.ConnDeadlined, nice uint8) {
+func performSP(
+       ctx *nncp.Ctx,
+       conn nncp.ConnDeadlined,
+       nice uint8,
+       nodeIdC chan *nncp.NodeId,
+) {
        state := nncp.SPState{
                Ctx:  ctx,
                Nice: nice,
        }
        if err := state.StartR(conn); err == nil {
                ctx.LogI("call-start", nncp.SDS{"node": state.Node.Id}, "connected")
+               nodeIdC <- state.Node.Id
                state.Wait()
                ctx.LogI("call-finish", nncp.SDS{
                        "node":     state.Node.Id,
-                       "duration": strconv.FormatInt(int64(state.Duration.Seconds()), 10),
-                       "rxbytes":  strconv.FormatInt(state.RxBytes, 10),
-                       "txbytes":  strconv.FormatInt(state.TxBytes, 10),
-                       "rxspeed":  strconv.FormatInt(state.RxSpeed, 10),
-                       "txspeed":  strconv.FormatInt(state.TxSpeed, 10),
+                       "duration": int64(state.Duration.Seconds()),
+                       "rxbytes":  state.RxBytes,
+                       "txbytes":  state.TxBytes,
+                       "rxspeed":  state.RxSpeed,
+                       "txspeed":  state.TxSpeed,
                }, "")
        } else {
                nodeId := "unknown"
-               if state.Node != nil {
+               if state.Node == nil {
+                       nodeIdC <- nil
+               } else {
+                       nodeIdC <- state.Node.Id
                        nodeId = state.Node.Id.String()
                }
-               ctx.LogE("call-start", nncp.SDS{"node": nodeId, "err": err}, "")
+               ctx.LogE("call-start", nncp.SDS{"node": nodeId}, err, "")
        }
+       close(nodeIdC)
 }
 
 func main() {
@@ -98,9 +111,18 @@ func main() {
                spoolPath = flag.String("spool", "", "Override path to spool")
                logPath   = flag.String("log", "", "Override path to logfile")
                quiet     = flag.Bool("quiet", false, "Print only errors")
+               showPrgrs = flag.Bool("progress", false, "Force progress showing")
+               omitPrgrs = flag.Bool("noprogress", false, "Omit progress showing")
                debug     = flag.Bool("debug", false, "Print debug messages")
                version   = flag.Bool("version", false, "Print version information")
                warranty  = flag.Bool("warranty", false, "Print warranty information")
+
+               autoToss       = flag.Bool("autotoss", false, "Toss after call is finished")
+               autoTossDoSeen = flag.Bool("autotoss-seen", false, "Create .seen files during tossing")
+               autoTossNoFile = flag.Bool("autotoss-nofile", false, "Do not process \"file\" packets during tossing")
+               autoTossNoFreq = flag.Bool("autotoss-nofreq", false, "Do not process \"freq\" packets during tossing")
+               autoTossNoExec = flag.Bool("autotoss-noexec", false, "Do not process \"exec\" packets during tossing")
+               autoTossNoTrns = flag.Bool("autotoss-notrns", false, "Do not process \"trns\" packets during tossing")
        )
        flag.Usage = usage
        flag.Parse()
@@ -117,7 +139,15 @@ func main() {
                log.Fatalln(err)
        }
 
-       ctx, err := nncp.CtxFromCmdline(*cfgPath, *spoolPath, *logPath, *quiet, *debug)
+       ctx, err := nncp.CtxFromCmdline(
+               *cfgPath,
+               *spoolPath,
+               *logPath,
+               *quiet,
+               *showPrgrs,
+               *omitPrgrs,
+               *debug,
+       )
        if err != nil {
                log.Fatalln("Error during initialization:", err)
        }
@@ -127,9 +157,13 @@ func main() {
        ctx.Umask()
 
        if *inetd {
-               os.Stderr.Close()
+               os.Stderr.Close() // #nosec G104
                conn := &InetdConn{os.Stdin, os.Stdout}
-               performSP(ctx, conn, nice)
+               nodeIdC := make(chan *nncp.NodeId)
+               go performSP(ctx, conn, nice, nodeIdC)
+               <-nodeIdC    // nodeId
+               <-nodeIdC    // call completion
+               conn.Close() // #nosec G104
                return
        }
 
@@ -145,8 +179,28 @@ func main() {
                }
                ctx.LogD("daemon", nncp.SDS{"addr": conn.RemoteAddr()}, "accepted")
                go func(conn net.Conn) {
-                       performSP(ctx, conn, nice)
-                       conn.Close()
+                       nodeIdC := make(chan *nncp.NodeId)
+                       go performSP(ctx, conn, nice, nodeIdC)
+                       nodeId := <-nodeIdC
+                       var autoTossFinish chan struct{}
+                       var autoTossBadCode chan bool
+                       if *autoToss && nodeId != nil {
+                               autoTossFinish, autoTossBadCode = ctx.AutoToss(
+                                       nodeId,
+                                       nice,
+                                       *autoTossDoSeen,
+                                       *autoTossNoFile,
+                                       *autoTossNoFreq,
+                                       *autoTossNoExec,
+                                       *autoTossNoTrns,
+                               )
+                       }
+                       <-nodeIdC // call completion
+                       if *autoToss {
+                               close(autoTossFinish)
+                               <-autoTossBadCode
+                       }
+                       conn.Close() // #nosec G104
                }(conn)
        }
 }