]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/cmd/nncp-daemon/main.go
Autotoss ability
[nncp.git] / src / cmd / nncp-daemon / main.go
index 2e55cd654ef23ce88b9d904271a9c8e6c37c4776..b7aceb75e11f90eb1f99f26c7da7915c981739b8 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,10 +24,9 @@ import (
        "log"
        "net"
        "os"
-       "strconv"
        "time"
 
-       "go.cypherpunks.ru/nncp/v4"
+       "go.cypherpunks.ru/nncp/v5"
        "golang.org/x/net/netutil"
 )
 
@@ -43,23 +42,31 @@ type InetdConn struct {
        w *os.File
 }
 
-func (ic *InetdConn) Read(p []byte) (n int, err error) {
-       return ic.r.Read(p)
+func (InetdConn) Read(p []byte) (n int, err error) {
+       return c.r.Read(p)
 }
 
-func (ic *InetdConn) Write(p []byte) (n int, err error) {
-       return ic.w.Write(p)
+func (InetdConn) Write(p []byte) (n int, err error) {
+       return c.w.Write(p)
 }
 
-func (ic *InetdConn) SetReadDeadline(t time.Time) error {
-       return ic.r.SetReadDeadline(t)
+func (InetdConn) SetReadDeadline(t time.Time) error {
+       return c.r.SetReadDeadline(t)
 }
 
-func (ic *InetdConn) SetWriteDeadline(t time.Time) error {
-       return ic.w.SetWriteDeadline(t)
+func (InetdConn) SetWriteDeadline(t time.Time) error {
+       return c.w.SetWriteDeadline(t)
 }
 
-func performSP(ctx *nncp.Ctx, conn nncp.ConnDeadlined, nice uint8) {
+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) *nncp.SPState {
        state := nncp.SPState{
                Ctx:  ctx,
                Nice: nice,
@@ -69,19 +76,20 @@ func performSP(ctx *nncp.Ctx, conn nncp.ConnDeadlined, nice uint8) {
                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 {
                        nodeId = state.Node.Id.String()
                }
-               ctx.LogE("call-start", nncp.SDS{"node": nodeId, "err": err}, "")
+               ctx.LogE("call-start", nncp.SDS{"node": nodeId}, err, "")
        }
+       return &state
 }
 
 func main() {
@@ -94,9 +102,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()
@@ -113,18 +130,28 @@ 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)
        }
        if ctx.Self == nil {
                log.Fatalln("Config lacks private keys")
        }
+       ctx.Umask()
 
        if *inetd {
-               os.Stderr.Close()
+               os.Stderr.Close() // #nosec G104
                conn := &InetdConn{os.Stdin, os.Stdout}
                performSP(ctx, conn, nice)
+               conn.Close() // #nosec G104
                return
        }
 
@@ -140,8 +167,20 @@ func main() {
                }
                ctx.LogD("daemon", nncp.SDS{"addr": conn.RemoteAddr()}, "accepted")
                go func(conn net.Conn) {
-                       performSP(ctx, conn, nice)
-                       conn.Close()
+                       state := performSP(ctx, conn, nice)
+                       conn.Close() // #nosec G104
+                       if *autotoss && state.Node != nil {
+                               ctx.Toss(
+                                       state.Node.Id,
+                                       nice,
+                                       false,
+                                       *autotossDoSeen,
+                                       *autotossNoFile,
+                                       *autotossNoFreq,
+                                       *autotossNoExec,
+                                       *autotossNoTrns,
+                               )
+                       }
                }(conn)
        }
 }