X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=src%2Fcypherpunks.ru%2Fnncp%2Fcfg.go;h=d948a274086438719aa6e09ae36c08f27405c93a;hb=535d386941ae38abbaa8e1a6df69a5e739058011;hp=10dd46b917a430174b17ac3bb8801d705917bd4f;hpb=919345b4dbaba2fc8f543efb8e0ec99330a775fc;p=nncp.git diff --git a/src/cypherpunks.ru/nncp/cfg.go b/src/cypherpunks.ru/nncp/cfg.go index 10dd46b..d948a27 100644 --- a/src/cypherpunks.ru/nncp/cfg.go +++ b/src/cypherpunks.ru/nncp/cfg.go @@ -19,46 +19,57 @@ along with this program. If not, see . package nncp import ( + "bytes" "errors" + "log" "os" "path" "github.com/gorhill/cronexpr" "golang.org/x/crypto/ed25519" + "golang.org/x/crypto/ssh/terminal" "gopkg.in/yaml.v2" ) const ( - CfgPathEnv = "NNCPCFG" + CfgPathEnv = "NNCPCFG" + CfgSpoolEnv = "NNCPSPOOL" + CfgLogEnv = "NNCPLOG" ) 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 `noisepub,omitempty` - Sendmail []string - Incoming *string `incoming,omitempty` - Freq *string `freq,omitempty` - Via []string `via,omitempty` - Calls []CallYAML `calls,omitempty` + Id string + ExchPub string + SignPub string + NoisePub *string `noisepub,omitempty` + Sendmail []string `sendmail,omitempty` + Incoming *string `incoming,omitempty` + Freq *string `freq,omitempty` + FreqChunked *uint64 `freqchunked,omitempty` + FreqMinSize *uint64 `freqminsize,omitempty` + Via []string `via,omitempty` + Calls []CallYAML `calls,omitempty` Addrs map[string]string `addrs,omitempty` - OnlineDeadline *int `onlinedeadline,omitempty` + OnlineDeadline *uint `onlinedeadline,omitempty` + MaxOnlineTime *uint `maxonlinetime,omitempty` } type CallYAML struct { Cron string Nice *int `nice,omitempty` - Xx *string `xx,omitempty` + Xx string `xx,omitempty` Addr *string `addr,omitempty` - OnlineDeadline *int `onlinedeadline,omitempty` + OnlineDeadline *uint `onlinedeadline,omitempty` + MaxOnlineTime *uint `maxonlinetime,omitempty` } type NodeOurYAML struct { @@ -82,7 +93,7 @@ type NotifyYAML struct { } type CfgYAML struct { - Self NodeOurYAML + Self *NodeOurYAML `self,omitempty` Neigh map[string]NodeYAML Spool string @@ -140,14 +151,29 @@ func NewNode(name string, yml NodeYAML) (*Node, error) { } freq = &fr } + var freqChunked int64 + if yml.FreqChunked != nil { + if *yml.FreqChunked == 0 { + return nil, errors.New("freqchunked value must be greater than zero") + } + freqChunked = int64(*yml.FreqChunked) * 1024 + } + var freqMinSize int64 + if yml.FreqMinSize != nil { + freqMinSize = int64(*yml.FreqMinSize) * 1024 + } - defOnlineDeadline := int(DefaultDeadline) + 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 { @@ -163,15 +189,14 @@ func NewNode(name string, yml NodeYAML) (*Node, error) { 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\"") - } + switch callYml.Xx { + case "rx": + xx = TRx + case "tx": + xx = TTx + case "": + default: + return nil, errors.New("xx field must be either \"rx\" or \"tx\"") } var addr *string if callYml.Addr != nil { @@ -183,17 +208,22 @@ func NewNode(name string, yml NodeYAML) (*Node, error) { } onlineDeadline := defOnlineDeadline if callYml.OnlineDeadline != nil { - if *yml.OnlineDeadline <= 0 { + 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, + Xx: xx, Addr: addr, OnlineDeadline: onlineDeadline, + MaxOnlineTime: maxOnlineTime, }) } @@ -205,9 +235,12 @@ func NewNode(name string, yml NodeYAML) (*Node, error) { Sendmail: yml.Sendmail, Incoming: incoming, Freq: freq, + FreqChunked: freqChunked, + FreqMinSize: freqMinSize, Calls: calls, Addrs: yml.Addrs, OnlineDeadline: defOnlineDeadline, + MaxOnlineTime: defMaxOnlineTime, } copy(node.ExchPub[:], exchPub) if len(noisePub) > 0 { @@ -217,7 +250,7 @@ func NewNode(name string, yml NodeYAML) (*Node, error) { return &node, nil } -func NewNodeOur(yml NodeOurYAML) (*NodeOur, error) { +func NewNodeOur(yml *NodeOurYAML) (*NodeOur, error) { id, err := NodeIdFromString(yml.Id) if err != nil { return nil, err @@ -305,14 +338,32 @@ func (nodeOur *NodeOur) ToYAML() string { } func CfgParse(data []byte) (*Ctx, error) { + var err error + if bytes.Compare(data[:8], MagicNNCPBv2[:]) == 0 { + os.Stderr.WriteString("Passphrase:") + password, err := terminal.ReadPassword(0) + if err != nil { + log.Fatalln(err) + } + os.Stderr.WriteString("\n") + data, err = DeEBlob(data, password) + if err != nil { + return nil, err + } + } var cfgYAML CfgYAML - err := yaml.Unmarshal(data, &cfgYAML) - if err != nil { + if err = yaml.Unmarshal(data, &cfgYAML); err != nil { return nil, err } - self, err := NewNodeOur(cfgYAML.Self) - if err != nil { - return nil, err + if _, exists := cfgYAML.Neigh["self"]; !exists { + return nil, errors.New("self neighbour missing") + } + var self *NodeOur + if cfgYAML.Self != nil { + self, err = NewNodeOur(cfgYAML.Self) + if err != nil { + return nil, err + } } spoolPath := path.Clean(cfgYAML.Spool) if !path.IsAbs(spoolPath) { @@ -350,6 +401,7 @@ func CfgParse(data []byte) (*Ctx, error) { ctx.Alias[name] = neigh.Id vias[*neigh.Id] = neighYAML.Via } + ctx.SelfId = ctx.Alias["self"] for neighId, viasRaw := range vias { for _, viaRaw := range viasRaw { foundNodeId, err := ctx.FindNode(viaRaw) @@ -364,11 +416,3 @@ func CfgParse(data []byte) (*Ctx, error) { } return &ctx, nil } - -func CfgPathFromEnv(cmdlineFlag *string) (p string) { - p = os.Getenv(CfgPathEnv) - if p == "" { - p = *cmdlineFlag - } - return -}