]> Cypherpunks.ru repositories - nncp.git/blob - src/cmd/nncp-trns/main.go
Remove huge usage headers, -warranty exists anyway
[nncp.git] / src / cmd / nncp-trns / main.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2023 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 // Wrap existing encrypted packet to transition ones.
19 package main
20
21 import (
22         "flag"
23         "fmt"
24         "io"
25         "log"
26         "os"
27         "path/filepath"
28         "strings"
29
30         "go.cypherpunks.ru/nncp/v8"
31 )
32
33 func usage() {
34         fmt.Fprint(os.Stderr, "nncp-trns -- transit existing encrypted packet\n\n")
35         fmt.Fprintf(os.Stderr, "Usage: %s [options] -via NODEx[,...] NODE:PKT\n", os.Args[0])
36         fmt.Fprintf(os.Stderr, "       (to transit SPOOL/NODE/tx/PKT)\n")
37         fmt.Fprintf(os.Stderr, "       %s [options] -via NODEx[,...] /path/to/PKT\nOptions:\n",
38                 os.Args[0])
39         flag.PrintDefaults()
40 }
41
42 func main() {
43         var (
44                 cfgPath     = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
45                 niceRaw     = flag.String("nice", nncp.NicenessFmt(nncp.DefaultNiceFile), "Outbound packet niceness")
46                 viaOverride = flag.String("via", "", "Override Via path to destination node")
47                 spoolPath   = flag.String("spool", "", "Override path to spool")
48                 logPath     = flag.String("log", "", "Override path to logfile")
49                 quiet       = flag.Bool("quiet", false, "Print only errors")
50                 showPrgrs   = flag.Bool("progress", false, "Force progress showing")
51                 omitPrgrs   = flag.Bool("noprogress", false, "Omit progress showing")
52                 debug       = flag.Bool("debug", false, "Print debug messages")
53                 version     = flag.Bool("version", false, "Print version information")
54                 warranty    = flag.Bool("warranty", false, "Print warranty information")
55         )
56         log.SetFlags(log.Lshortfile)
57         flag.Usage = usage
58         flag.Parse()
59         if *warranty {
60                 fmt.Println(nncp.Warranty)
61                 return
62         }
63         if *version {
64                 fmt.Println(nncp.VersionGet())
65                 return
66         }
67         if flag.NArg() != 1 {
68                 usage()
69                 os.Exit(1)
70         }
71         nice, err := nncp.NicenessParse(*niceRaw)
72         if err != nil {
73                 log.Fatalln(err)
74         }
75
76         ctx, err := nncp.CtxFromCmdline(
77                 *cfgPath,
78                 *spoolPath,
79                 *logPath,
80                 *quiet,
81                 *showPrgrs,
82                 *omitPrgrs,
83                 *debug,
84         )
85         if err != nil {
86                 log.Fatalln("Error during initialization:", err)
87         }
88         if ctx.Self == nil {
89                 log.Fatalln("Config lacks private keys")
90         }
91         ctx.Umask()
92
93         var pktPath string
94         var pktName string
95         if _, err = os.Stat(flag.Arg(0)); err == nil {
96                 pktPath = flag.Arg(0)
97                 pktName = filepath.Base(pktPath)
98         } else {
99                 splitted := strings.Split(flag.Arg(0), ":")
100                 if len(splitted) != 2 {
101                         log.Fatalln("Invalid NODE:PKT specification")
102                 }
103                 node, err := ctx.FindNode(splitted[0])
104                 if err != nil {
105                         log.Fatalln("Invalid NODE specified:", err)
106                 }
107                 pktPath = filepath.Join(
108                         ctx.Spool, node.Id.String(), string(nncp.TTx), splitted[1],
109                 )
110                 pktName = filepath.Base(splitted[1])
111         }
112
113         fd, err := os.Open(pktPath)
114         if err != nil {
115                 log.Fatalln(err)
116         }
117         fi, err := fd.Stat()
118         if err != nil {
119                 log.Fatalln(err)
120         }
121         pktEnc, _, err := ctx.HdrRead(fd)
122         if err != nil {
123                 log.Fatalln(err)
124         }
125         if _, err = fd.Seek(0, io.SeekStart); err != nil {
126                 log.Fatalln(err)
127         }
128
129         node := ctx.Neigh[*pktEnc.Recipient]
130         nncp.ViaOverride(*viaOverride, ctx, node)
131         via := node.Via[:len(node.Via)-1]
132         node = ctx.Neigh[*node.Via[len(node.Via)-1]]
133         node.Via = via
134
135         pktTrns, err := nncp.NewPkt(nncp.PktTypeTrns, 0, pktEnc.Recipient[:])
136         if err != nil {
137                 panic(err)
138         }
139         if _, _, _, err = ctx.Tx(
140                 node,
141                 pktTrns,
142                 nice,
143                 fi.Size(), 0, nncp.MaxFileSize,
144                 fd,
145                 pktName,
146                 nil,
147         ); err != nil {
148                 log.Fatalln(err)
149         }
150 }