]> Cypherpunks.ru repositories - nncp.git/blob - src/cmd/nncp-trns/main.go
Unify copyright comment format
[nncp.git] / src / cmd / nncp-trns / main.go
1 // NNCP -- Node to Node copy, utilities for store-and-forward data exchange
2 // Copyright (C) 2016-2024 Sergey Matveev <stargrave@stargrave.org>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, version 3 of the License.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 // Wrap existing encrypted packet to transition ones.
17 package main
18
19 import (
20         "flag"
21         "fmt"
22         "io"
23         "log"
24         "os"
25         "path/filepath"
26         "strings"
27
28         "go.cypherpunks.ru/nncp/v8"
29 )
30
31 func usage() {
32         fmt.Fprint(os.Stderr, "nncp-trns -- transit existing encrypted packet\n\n")
33         fmt.Fprintf(os.Stderr, "Usage: %s [options] -via NODEx[,...] NODE:PKT\n", os.Args[0])
34         fmt.Fprintf(os.Stderr, "       (to transit SPOOL/NODE/tx/PKT)\n")
35         fmt.Fprintf(os.Stderr, "       %s [options] -via NODEx[,...] /path/to/PKT\nOptions:\n",
36                 os.Args[0])
37         flag.PrintDefaults()
38 }
39
40 func main() {
41         var (
42                 cfgPath = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
43                 niceRaw = flag.String("nice", nncp.NicenessFmt(nncp.DefaultNiceFile),
44                         "Outbound packet niceness")
45                 viaOverride = flag.String("via", "", "Override Via path to destination node")
46                 spoolPath   = flag.String("spool", "", "Override path to spool")
47                 logPath     = flag.String("log", "", "Override path to logfile")
48                 quiet       = flag.Bool("quiet", false, "Print only errors")
49                 showPrgrs   = flag.Bool("progress", false, "Force progress showing")
50                 omitPrgrs   = flag.Bool("noprogress", false, "Omit progress showing")
51                 debug       = flag.Bool("debug", false, "Print debug messages")
52                 version     = flag.Bool("version", false, "Print version information")
53                 warranty    = flag.Bool("warranty", false, "Print warranty information")
54         )
55         log.SetFlags(log.Lshortfile)
56         flag.Usage = usage
57         flag.Parse()
58         if *warranty {
59                 fmt.Println(nncp.Warranty)
60                 return
61         }
62         if *version {
63                 fmt.Println(nncp.VersionGet())
64                 return
65         }
66         if flag.NArg() != 1 {
67                 usage()
68                 os.Exit(1)
69         }
70         nice, err := nncp.NicenessParse(*niceRaw)
71         if err != nil {
72                 log.Fatalln(err)
73         }
74
75         ctx, err := nncp.CtxFromCmdline(
76                 *cfgPath,
77                 *spoolPath,
78                 *logPath,
79                 *quiet,
80                 *showPrgrs,
81                 *omitPrgrs,
82                 *debug,
83         )
84         if err != nil {
85                 log.Fatalln("Error during initialization:", err)
86         }
87         if ctx.Self == nil {
88                 log.Fatalln("Config lacks private keys")
89         }
90         ctx.Umask()
91
92         var pktPath string
93         var pktName string
94         if _, err = os.Stat(flag.Arg(0)); err == nil {
95                 pktPath = flag.Arg(0)
96                 pktName = filepath.Base(pktPath)
97         } else {
98                 splitted := strings.Split(flag.Arg(0), ":")
99                 if len(splitted) != 2 {
100                         log.Fatalln("Invalid NODE:PKT specification")
101                 }
102                 node, err := ctx.FindNode(splitted[0])
103                 if err != nil {
104                         log.Fatalln("Invalid NODE specified:", err)
105                 }
106                 pktPath = filepath.Join(
107                         ctx.Spool, node.Id.String(), string(nncp.TTx), splitted[1],
108                 )
109                 pktName = filepath.Base(splitted[1])
110         }
111
112         fd, err := os.Open(pktPath)
113         if err != nil {
114                 log.Fatalln(err)
115         }
116         fi, err := fd.Stat()
117         if err != nil {
118                 log.Fatalln(err)
119         }
120         pktEnc, _, err := ctx.HdrRead(fd)
121         if err != nil {
122                 log.Fatalln(err)
123         }
124         if _, err = fd.Seek(0, io.SeekStart); err != nil {
125                 log.Fatalln(err)
126         }
127
128         node := ctx.Neigh[*pktEnc.Recipient]
129         nncp.ViaOverride(*viaOverride, ctx, node)
130         via := node.Via[:len(node.Via)-1]
131         node = ctx.Neigh[*node.Via[len(node.Via)-1]]
132         node.Via = via
133
134         pktTrns, err := nncp.NewPkt(nncp.PktTypeTrns, 0, pktEnc.Recipient[:])
135         if err != nil {
136                 panic(err)
137         }
138         if _, _, _, err = ctx.Tx(
139                 node,
140                 pktTrns,
141                 nice,
142                 fi.Size(), 0, nncp.MaxFileSize,
143                 fd,
144                 pktName,
145                 nil,
146         ); err != nil {
147                 log.Fatalln(err)
148         }
149 }