]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/cypherpunks.ru/nncp/cfg.go
Store logs in spool directory for convenience by default
[nncp.git] / src / cypherpunks.ru / nncp / cfg.go
index bb6c5207292dd86fed39b9ecfb2d68b688f52019..c05a6a7c90424999c139f474d8a789f8456d4d04 100644 (file)
@@ -1,5 +1,5 @@
 /*
-NNCP -- Node-to-Node CoPy
+NNCP -- Node to Node copy, utilities for store-and-forward data exchange
 Copyright (C) 2016-2017 Sergey Matveev <stargrave@stargrave.org>
 
 This program is free software: you can redistribute it and/or modify
@@ -20,25 +20,49 @@ package nncp
 
 import (
        "errors"
+       "os"
        "path"
 
+       "github.com/gorhill/cronexpr"
        "golang.org/x/crypto/ed25519"
        "gopkg.in/yaml.v2"
 )
 
+const (
+       CfgPathEnv = "NNCPCFG"
+)
+
 var (
        DefaultCfgPath      string = "/usr/local/etc/nncp.yaml"
        DefaultSendmailPath string = "/usr/sbin/sendmail"
+       DefaultSpoolPath    string = "/var/spool/nncp"
+       DefaultLogPath      string = "/var/spool/nncp/log"
 )
 
 type NodeYAML struct {
        Id       string
        ExchPub  string
        SignPub  string
-       NoisePub string
-       Incoming *string  `incoming,omitempty`
-       Freq     *string  `freq,omitempty`
-       Via      []string `via,omitempty`
+       NoisePub *string `noisepub,omitempty`
+       Sendmail []string
+       Incoming *string    `incoming,omitempty`
+       Freq     *string    `freq,omitempty`
+       Via      []string   `via,omitempty`
+       Calls    []CallYAML `calls,omitempty`
+
+       Addrs map[string]string `addrs,omitempty`
+
+       OnlineDeadline *uint `onlinedeadline,omitempty`
+       MaxOnlineTime  *uint `maxonlinetime,omitempty`
+}
+
+type CallYAML struct {
+       Cron           string
+       Nice           *int    `nice,omitempty`
+       Xx             *string `xx,omitempty`
+       Addr           *string `addr,omitempty`
+       OnlineDeadline *uint   `onlinedeadline,omitempty`
+       MaxOnlineTime  *uint   `maxonlinetime,omitempty`
 }
 
 type NodeOurYAML struct {
@@ -65,10 +89,9 @@ type CfgYAML struct {
        Self  NodeOurYAML
        Neigh map[string]NodeYAML
 
-       Spool    string
-       Log      string
-       Sendmail []string
-       Notify   *NotifyYAML `notify,omitempty`
+       Spool  string
+       Log    string
+       Notify *NotifyYAML `notify,omitempty`
 }
 
 func NewNode(name string, yml NodeYAML) (*Node, error) {
@@ -93,12 +116,15 @@ func NewNode(name string, yml NodeYAML) (*Node, error) {
                return nil, errors.New("Invalid signPub size")
        }
 
-       noisePub, err := FromBase32(yml.NoisePub)
-       if err != nil {
-               return nil, err
-       }
-       if len(noisePub) != 32 {
-               return nil, errors.New("Invalid noisePub size")
+       var noisePub []byte
+       if yml.NoisePub != nil {
+               noisePub, err = FromBase32(*yml.NoisePub)
+               if err != nil {
+                       return nil, err
+               }
+               if len(noisePub) != 32 {
+                       return nil, errors.New("Invalid noisePub size")
+               }
        }
 
        var incoming *string
@@ -119,17 +145,89 @@ func NewNode(name string, yml NodeYAML) (*Node, error) {
                freq = &fr
        }
 
+       defOnlineDeadline := uint(DefaultDeadline)
+       if yml.OnlineDeadline != nil {
+               if *yml.OnlineDeadline <= 0 {
+                       return nil, errors.New("OnlineDeadline must be at least 1 second")
+               }
+               defOnlineDeadline = *yml.OnlineDeadline
+       }
+       var defMaxOnlineTime uint
+       if yml.MaxOnlineTime != nil {
+               defMaxOnlineTime = *yml.MaxOnlineTime
+       }
+
+       var calls []*Call
+       for _, callYml := range yml.Calls {
+               expr, err := cronexpr.Parse(callYml.Cron)
+               if err != nil {
+                       return nil, err
+               }
+               nice := uint8(255)
+               if callYml.Nice != nil {
+                       if *callYml.Nice < 1 || *callYml.Nice > 255 {
+                               return nil, errors.New("Nice must be between 1 and 255")
+                       }
+                       nice = uint8(*callYml.Nice)
+               }
+               var xx TRxTx
+               if callYml.Xx != nil {
+                       switch *callYml.Xx {
+                       case "rx":
+                               xx = TRx
+                       case "tx":
+                               xx = TTx
+                       default:
+                               return nil, errors.New("xx field must be either \"rx\" or \"tx\"")
+                       }
+               }
+               var addr *string
+               if callYml.Addr != nil {
+                       if a, exists := yml.Addrs[*callYml.Addr]; exists {
+                               addr = &a
+                       } else {
+                               addr = callYml.Addr
+                       }
+               }
+               onlineDeadline := defOnlineDeadline
+               if callYml.OnlineDeadline != nil {
+                       if *callYml.OnlineDeadline == 0 {
+                               return nil, errors.New("OnlineDeadline must be at least 1 second")
+                       }
+                       onlineDeadline = *callYml.OnlineDeadline
+               }
+               var maxOnlineTime uint
+               if callYml.MaxOnlineTime != nil {
+                       maxOnlineTime = *callYml.MaxOnlineTime
+               }
+               calls = append(calls, &Call{
+                       Cron:           expr,
+                       Nice:           nice,
+                       Xx:             &xx,
+                       Addr:           addr,
+                       OnlineDeadline: onlineDeadline,
+                       MaxOnlineTime:  maxOnlineTime,
+               })
+       }
+
        node := Node{
-               Name:     name,
-               Id:       nodeId,
-               ExchPub:  new([32]byte),
-               SignPub:  ed25519.PublicKey(signPub),
-               NoisePub: new([32]byte),
-               Incoming: incoming,
-               Freq:     freq,
+               Name:           name,
+               Id:             nodeId,
+               ExchPub:        new([32]byte),
+               SignPub:        ed25519.PublicKey(signPub),
+               Sendmail:       yml.Sendmail,
+               Incoming:       incoming,
+               Freq:           freq,
+               Calls:          calls,
+               Addrs:          yml.Addrs,
+               OnlineDeadline: defOnlineDeadline,
+               MaxOnlineTime:  defMaxOnlineTime,
        }
        copy(node.ExchPub[:], exchPub)
-       copy(node.NoisePub[:], noisePub)
+       if len(noisePub) > 0 {
+               node.NoisePub = new([32]byte)
+               copy(node.NoisePub[:], noisePub)
+       }
        return &node, nil
 }
 
@@ -239,12 +337,11 @@ func CfgParse(data []byte) (*Ctx, error) {
                return nil, errors.New("Log path must be absolute")
        }
        ctx := Ctx{
-               Spool:    spoolPath,
-               LogPath:  logPath,
-               Self:     self,
-               Neigh:    make(map[NodeId]*Node, len(cfgYAML.Neigh)),
-               Alias:    make(map[string]*NodeId),
-               Sendmail: cfgYAML.Sendmail,
+               Spool:   spoolPath,
+               LogPath: logPath,
+               Self:    self,
+               Neigh:   make(map[NodeId]*Node, len(cfgYAML.Neigh)),
+               Alias:   make(map[string]*NodeId),
        }
        if cfgYAML.Notify != nil {
                if cfgYAML.Notify.File != nil {
@@ -281,3 +378,11 @@ func CfgParse(data []byte) (*Ctx, error) {
        }
        return &ctx, nil
 }
+
+func CfgPathFromEnv(cmdlineFlag *string) (p string) {
+       p = os.Getenv(CfgPathEnv)
+       if p == "" {
+               p = *cmdlineFlag
+       }
+       return
+}