]> Cypherpunks.ru repositories - nncp.git/blob - src/cmd/nncp-cfgnew/main.go
Full go modules usage
[nncp.git] / src / cmd / nncp-cfgnew / main.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2019 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 // Generate new NNCP node keys and configuration file
19 package main
20
21 import (
22         "flag"
23         "fmt"
24         "os"
25
26         "go.cypherpunks.ru/nncp/v4"
27         "gopkg.in/yaml.v2"
28 )
29
30 func usage() {
31         fmt.Fprintf(os.Stderr, nncp.UsageHeader())
32         fmt.Fprintln(os.Stderr, "nncp-cfgnew -- generate new configuration and keys\nOptions:")
33         flag.PrintDefaults()
34 }
35
36 func main() {
37         var (
38                 version  = flag.Bool("version", false, "Print version information")
39                 warranty = flag.Bool("warranty", false, "Print warranty information")
40         )
41         flag.Usage = usage
42         flag.Parse()
43         if *warranty {
44                 fmt.Println(nncp.Warranty)
45                 return
46         }
47         if *version {
48                 fmt.Println(nncp.VersionGet())
49                 return
50         }
51         nodeOur, err := nncp.NewNodeGenerate()
52         if err != nil {
53                 panic(err)
54         }
55         noisePub := nncp.ToBase32(nodeOur.NoisePub[:])
56         cfg := nncp.CfgYAML{
57                 Self: &nncp.NodeOurYAML{
58                         Id:       nodeOur.Id.String(),
59                         ExchPub:  nncp.ToBase32(nodeOur.ExchPub[:]),
60                         ExchPrv:  nncp.ToBase32(nodeOur.ExchPrv[:]),
61                         SignPub:  nncp.ToBase32(nodeOur.SignPub[:]),
62                         SignPrv:  nncp.ToBase32(nodeOur.SignPrv[:]),
63                         NoisePub: nncp.ToBase32(nodeOur.NoisePub[:]),
64                         NoisePrv: nncp.ToBase32(nodeOur.NoisePrv[:]),
65                 },
66                 Neigh: map[string]nncp.NodeYAML{
67                         "self": nncp.NodeYAML{
68                                 Id:       nodeOur.Id.String(),
69                                 ExchPub:  nncp.ToBase32(nodeOur.ExchPub[:]),
70                                 SignPub:  nncp.ToBase32(nodeOur.SignPub[:]),
71                                 NoisePub: &noisePub,
72                                 Exec: map[string][]string{
73                                         "sendmail": []string{nncp.DefaultSendmailPath},
74                                 },
75                         },
76                 },
77                 Spool: nncp.DefaultSpoolPath,
78                 Log:   nncp.DefaultLogPath,
79         }
80         raw, err := yaml.Marshal(&cfg)
81         if err != nil {
82                 panic(err)
83         }
84         fmt.Print(string(raw))
85 }