]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/cfg.go
Autotoss call options
[nncp.git] / src / cfg.go
index 42fa918ae431687c2a038000637c0ae66b4746db..a72d371c141fecd588e8762fa2a758ece1ed18bd 100644 (file)
@@ -1,6 +1,6 @@
 /*
 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
-Copyright (C) 2016-2020 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
@@ -30,7 +30,7 @@ import (
        "github.com/gorhill/cronexpr"
        "github.com/hjson/hjson-go"
        "golang.org/x/crypto/ed25519"
-       "golang.org/x/crypto/ssh/terminal"
+       "golang.org/x/term"
 )
 
 const (
@@ -81,6 +81,13 @@ type CallJSON struct {
        Addr           *string `json:"addr,omitempty"`
        OnlineDeadline *uint   `json:"onlinedeadline,omitempty"`
        MaxOnlineTime  *uint   `json:"maxonlinetime,omitempty"`
+
+       AutoToss       *bool `json:"autotoss,omitempty"`
+       AutoTossDoSeen *bool `json:"autotoss-doseen,omitempty"`
+       AutoTossNoFile *bool `json:"autotoss-nofile,omitempty"`
+       AutoTossNoFreq *bool `json:"autotoss-nofreq,omitempty"`
+       AutoTossNoExec *bool `json:"autotoss-noexec,omitempty"`
+       AutoTossNoTrns *bool `json:"autotoss-notrns,omitempty"`
 }
 
 type NodeOurJSON struct {
@@ -107,9 +114,9 @@ type NotifyJSON struct {
 type CfgJSON struct {
        Spool string `json:"spool"`
        Log   string `json:"log"`
-       Umask string `json:"umask",omitempty`
+       Umask string `json:"umask,omitempty"`
 
-       OmitPrgrs bool `json:"noprogress",omitempty`
+       OmitPrgrs bool `json:"noprogress,omitempty"`
 
        Notify *NotifyJSON `json:"notify,omitempty"`
 
@@ -123,7 +130,7 @@ func NewNode(name string, cfg NodeJSON) (*Node, error) {
                return nil, err
        }
 
-       exchPub, err := FromBase32(cfg.ExchPub)
+       exchPub, err := Base32Codec.DecodeString(cfg.ExchPub)
        if err != nil {
                return nil, err
        }
@@ -131,7 +138,7 @@ func NewNode(name string, cfg NodeJSON) (*Node, error) {
                return nil, errors.New("Invalid exchPub size")
        }
 
-       signPub, err := FromBase32(cfg.SignPub)
+       signPub, err := Base32Codec.DecodeString(cfg.SignPub)
        if err != nil {
                return nil, err
        }
@@ -141,7 +148,7 @@ func NewNode(name string, cfg NodeJSON) (*Node, error) {
 
        var noisePub []byte
        if cfg.NoisePub != nil {
-               noisePub, err = FromBase32(*cfg.NoisePub)
+               noisePub, err = Base32Codec.DecodeString(*cfg.NoisePub)
                if err != nil {
                        return nil, err
                }
@@ -195,12 +202,12 @@ func NewNode(name string, cfg NodeJSON) (*Node, error) {
                defTxRate = *cfg.TxRate
        }
 
-       defOnlineDeadline := uint(DefaultDeadline)
+       defOnlineDeadline := DefaultDeadline
        if cfg.OnlineDeadline != nil {
                if *cfg.OnlineDeadline <= 0 {
                        return nil, errors.New("OnlineDeadline must be at least 1 second")
                }
-               defOnlineDeadline = *cfg.OnlineDeadline
+               defOnlineDeadline = time.Duration(*cfg.OnlineDeadline) * time.Second
        }
        var defMaxOnlineTime time.Duration
        if cfg.MaxOnlineTime != nil {
@@ -257,15 +264,10 @@ func NewNode(name string, cfg NodeJSON) (*Node, error) {
                        if *callCfg.OnlineDeadline == 0 {
                                return nil, errors.New("OnlineDeadline must be at least 1 second")
                        }
-                       onlineDeadline = *callCfg.OnlineDeadline
-               }
-
-               var maxOnlineTime time.Duration
-               if callCfg.MaxOnlineTime != nil {
-                       maxOnlineTime = time.Duration(*callCfg.MaxOnlineTime) * time.Second
+                       onlineDeadline = time.Duration(*callCfg.OnlineDeadline) * time.Second
                }
 
-               calls = append(calls, &Call{
+               call := Call{
                        Cron:           expr,
                        Nice:           nice,
                        Xx:             xx,
@@ -273,8 +275,31 @@ func NewNode(name string, cfg NodeJSON) (*Node, error) {
                        TxRate:         txRate,
                        Addr:           addr,
                        OnlineDeadline: onlineDeadline,
-                       MaxOnlineTime:  maxOnlineTime,
-               })
+               }
+
+               if callCfg.MaxOnlineTime != nil {
+                       call.MaxOnlineTime = time.Duration(*callCfg.MaxOnlineTime) * time.Second
+               }
+               if callCfg.AutoToss != nil {
+                       call.AutoToss = *callCfg.AutoToss
+               }
+               if callCfg.AutoTossDoSeen != nil {
+                       call.AutoTossDoSeen = *callCfg.AutoTossDoSeen
+               }
+               if callCfg.AutoTossNoFile != nil {
+                       call.AutoTossNoFile = *callCfg.AutoTossNoFile
+               }
+               if callCfg.AutoTossNoFreq != nil {
+                       call.AutoTossNoFreq = *callCfg.AutoTossNoFreq
+               }
+               if callCfg.AutoTossNoExec != nil {
+                       call.AutoTossNoExec = *callCfg.AutoTossNoExec
+               }
+               if callCfg.AutoTossNoTrns != nil {
+                       call.AutoTossNoTrns = *callCfg.AutoTossNoTrns
+               }
+
+               calls = append(calls, &call)
        }
 
        node := Node{
@@ -309,7 +334,7 @@ func NewNodeOur(cfg *NodeOurJSON) (*NodeOur, error) {
                return nil, err
        }
 
-       exchPub, err := FromBase32(cfg.ExchPub)
+       exchPub, err := Base32Codec.DecodeString(cfg.ExchPub)
        if err != nil {
                return nil, err
        }
@@ -317,7 +342,7 @@ func NewNodeOur(cfg *NodeOurJSON) (*NodeOur, error) {
                return nil, errors.New("Invalid exchPub size")
        }
 
-       exchPrv, err := FromBase32(cfg.ExchPrv)
+       exchPrv, err := Base32Codec.DecodeString(cfg.ExchPrv)
        if err != nil {
                return nil, err
        }
@@ -325,7 +350,7 @@ func NewNodeOur(cfg *NodeOurJSON) (*NodeOur, error) {
                return nil, errors.New("Invalid exchPrv size")
        }
 
-       signPub, err := FromBase32(cfg.SignPub)
+       signPub, err := Base32Codec.DecodeString(cfg.SignPub)
        if err != nil {
                return nil, err
        }
@@ -333,7 +358,7 @@ func NewNodeOur(cfg *NodeOurJSON) (*NodeOur, error) {
                return nil, errors.New("Invalid signPub size")
        }
 
-       signPrv, err := FromBase32(cfg.SignPrv)
+       signPrv, err := Base32Codec.DecodeString(cfg.SignPrv)
        if err != nil {
                return nil, err
        }
@@ -341,7 +366,7 @@ func NewNodeOur(cfg *NodeOurJSON) (*NodeOur, error) {
                return nil, errors.New("Invalid signPrv size")
        }
 
-       noisePub, err := FromBase32(cfg.NoisePub)
+       noisePub, err := Base32Codec.DecodeString(cfg.NoisePub)
        if err != nil {
                return nil, err
        }
@@ -349,7 +374,7 @@ func NewNodeOur(cfg *NodeOurJSON) (*NodeOur, error) {
                return nil, errors.New("Invalid noisePub size")
        }
 
-       noisePrv, err := FromBase32(cfg.NoisePrv)
+       noisePrv, err := Base32Codec.DecodeString(cfg.NoisePrv)
        if err != nil {
                return nil, err
        }
@@ -376,12 +401,12 @@ func NewNodeOur(cfg *NodeOurJSON) (*NodeOur, error) {
 func CfgParse(data []byte) (*Ctx, error) {
        var err error
        if bytes.Compare(data[:8], MagicNNCPBv3[:]) == 0 {
-               os.Stderr.WriteString("Passphrase:")
-               password, err := terminal.ReadPassword(0)
+               os.Stderr.WriteString("Passphrase:") // #nosec G104
+               password, err := term.ReadPassword(0)
                if err != nil {
                        log.Fatalln(err)
                }
-               os.Stderr.WriteString("\n")
+               os.Stderr.WriteString("\n") // #nosec G104
                data, err = DeEBlob(data, password)
                if err != nil {
                        return nil, err