]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/cfg.go
Multicast areas
[nncp.git] / src / cfg.go
index fab35879e6d42519b09ac894d0ab81b00f63282e..bacfd7e29243631c2ecb543d47217bb97b73c171 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
@@ -25,11 +25,12 @@ import (
        "os"
        "path"
        "strconv"
+       "time"
 
        "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 (
@@ -80,6 +81,17 @@ type CallJSON struct {
        Addr           *string `json:"addr,omitempty"`
        OnlineDeadline *uint   `json:"onlinedeadline,omitempty"`
        MaxOnlineTime  *uint   `json:"maxonlinetime,omitempty"`
+       WhenTxExists   *bool   `json:"when-tx-exists,omitempty"`
+       NoCK           *bool   `json:"nock"`
+       MCDIgnore      *bool   `json:"mcd-ignore"`
+
+       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"`
+       AutoTossNoArea *bool `json:"autotoss-noarea,omitempty"`
 }
 
 type NodeOurJSON struct {
@@ -103,17 +115,36 @@ type NotifyJSON struct {
        Exec map[string]*FromToJSON `json:"exec,omitempty"`
 }
 
+type AreaJSON struct {
+       Id  string  `json:"id"`
+       Pub string  `json:"pub"`
+       Prv *string `json:"prv,omitempty"`
+
+       Subs []string `json:"subs"`
+
+       Exec     map[string][]string `json:"exec,omitempty"`
+       Incoming *string             `json:"incoming,omitempty"`
+
+       AllowUnknown *bool `json:"allow-unknown,omitempty"`
+}
+
 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"`
+       NoHdr     bool `json:"nohdr,omitempty"`
 
        Notify *NotifyJSON `json:"notify,omitempty"`
 
        Self  *NodeOurJSON        `json:"self"`
        Neigh map[string]NodeJSON `json:"neigh"`
+
+       MCDRxIfis []string       `json:"mcd-listen"`
+       MCDTxIfis map[string]int `json:"mcd-send"`
+
+       Areas map[string]AreaJSON `json:"areas"`
 }
 
 func NewNode(name string, cfg NodeJSON) (*Node, error) {
@@ -122,7 +153,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
        }
@@ -130,7 +161,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
        }
@@ -140,7 +171,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
                }
@@ -194,16 +225,16 @@ 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 uint
-       if yml.MaxOnlineTime != nil {
-               defMaxOnlineTime = *yml.MaxOnlineTime
+       var defMaxOnlineTime time.Duration
+       if cfg.MaxOnlineTime != nil {
+               defMaxOnlineTime = time.Duration(*cfg.MaxOnlineTime) * time.Second
        }
 
        var calls []*Call
@@ -256,15 +287,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 uint
-               if callCfg.MaxOnlineTime != nil {
-                       maxOnlineTime = *callCfg.MaxOnlineTime
+                       onlineDeadline = time.Duration(*callCfg.OnlineDeadline) * time.Second
                }
 
-               calls = append(calls, &Call{
+               call := Call{
                        Cron:           expr,
                        Nice:           nice,
                        Xx:             xx,
@@ -272,8 +298,43 @@ 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.WhenTxExists != nil {
+                       call.WhenTxExists = *callCfg.WhenTxExists
+               }
+               if callCfg.NoCK != nil {
+                       call.NoCK = *callCfg.NoCK
+               }
+               if callCfg.MCDIgnore != nil {
+                       call.MCDIgnore = *callCfg.MCDIgnore
+               }
+               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
+               }
+               if callCfg.AutoTossNoArea != nil {
+                       call.AutoTossNoArea = *callCfg.AutoTossNoArea
+               }
+
+               calls = append(calls, &call)
        }
 
        node := Node{
@@ -308,7 +369,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
        }
@@ -316,7 +377,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
        }
@@ -324,7 +385,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
        }
@@ -332,7 +393,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
        }
@@ -340,7 +401,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
        }
@@ -348,7 +409,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
        }
@@ -372,19 +433,69 @@ func NewNodeOur(cfg *NodeOurJSON) (*NodeOur, error) {
        return &node, nil
 }
 
+func NewArea(ctx *Ctx, name string, cfg *AreaJSON) (*Area, error) {
+       areaId, err := AreaIdFromString(cfg.Id)
+       if err != nil {
+               return nil, err
+       }
+       subs := make([]*NodeId, 0, len(cfg.Subs))
+       for _, s := range cfg.Subs {
+               node, err := ctx.FindNode(s)
+               if err != nil {
+                       return nil, err
+               }
+               subs = append(subs, node.Id)
+       }
+       area := Area{
+               Name:     name,
+               Id:       areaId,
+               Pub:      new([32]byte),
+               Subs:     subs,
+               Exec:     cfg.Exec,
+               Incoming: cfg.Incoming,
+       }
+       pub, err := Base32Codec.DecodeString(cfg.Pub)
+       if err != nil {
+               return nil, err
+       }
+       if len(pub) != 32 {
+               return nil, errors.New("Invalid pub size")
+       }
+       copy(area.Pub[:], pub)
+       if cfg.Prv != nil {
+               prv, err := Base32Codec.DecodeString(*cfg.Prv)
+               if err != nil {
+                       return nil, err
+               }
+               if len(prv) != 32 {
+                       return nil, errors.New("Invalid prv size")
+               }
+               area.Prv = new([32]byte)
+               copy(area.Prv[:], prv)
+       }
+       if cfg.AllowUnknown != nil {
+               area.AllowUnknown = *cfg.AllowUnknown
+       }
+       return &area, nil
+}
+
 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)
+       if bytes.Compare(data[:8], MagicNNCPBv3.B[:]) == 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
                }
+       } else if bytes.Compare(data[:8], MagicNNCPBv2.B[:]) == 0 {
+               log.Fatalln(MagicNNCPBv2.TooOld())
+       } else if bytes.Compare(data[:8], MagicNNCPBv1.B[:]) == 0 {
+               log.Fatalln(MagicNNCPBv1.TooOld())
        }
        var cfgGeneral map[string]interface{}
        if err = hjson.Unmarshal(data, &cfgGeneral); err != nil {
@@ -429,14 +540,21 @@ func CfgParse(data []byte) (*Ctx, error) {
        if cfgJSON.OmitPrgrs {
                showPrgrs = false
        }
+       hdrUsage := true
+       if cfgJSON.NoHdr {
+               hdrUsage = false
+       }
        ctx := Ctx{
                Spool:      spoolPath,
                LogPath:    logPath,
                UmaskForce: umaskForce,
                ShowPrgrs:  showPrgrs,
+               HdrUsage:   hdrUsage,
                Self:       self,
                Neigh:      make(map[NodeId]*Node, len(cfgJSON.Neigh)),
                Alias:      make(map[string]*NodeId),
+               MCDRxIfis:  cfgJSON.MCDRxIfis,
+               MCDTxIfis:  cfgJSON.MCDTxIfis,
        }
        if cfgJSON.Notify != nil {
                if cfgJSON.Notify.File != nil {
@@ -475,5 +593,15 @@ func CfgParse(data []byte) (*Ctx, error) {
                        )
                }
        }
+       ctx.AreaId2Area = make(map[AreaId]*Area, len(cfgJSON.Areas))
+       ctx.AreaName2Id = make(map[string]*AreaId, len(cfgJSON.Areas))
+       for name, areaJSON := range cfgJSON.Areas {
+               area, err := NewArea(&ctx, name, &areaJSON)
+               if err != nil {
+                       return nil, err
+               }
+               ctx.AreaId2Area[*area.Id] = area
+               ctx.AreaName2Id[name] = area.Id
+       }
        return &ctx, nil
 }