2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2021 Sergey Matveev <stargrave@stargrave.org>
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.
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.
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/>.
18 // Exchange NNCP inbound and outbounds packets with external directory.
31 "github.com/dustin/go-humanize"
32 "go.cypherpunks.ru/nncp/v7"
36 fmt.Fprintf(os.Stderr, nncp.UsageHeader())
37 fmt.Fprintf(os.Stderr, "nncp-xfer -- copy inbound and outbounds packets\n\n")
38 fmt.Fprintf(os.Stderr, "Usage: %s [options] DIR\nOptions:\n", os.Args[0])
44 cfgPath = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
45 nodeRaw = flag.String("node", "", "Process only that node")
46 niceRaw = flag.String("nice", nncp.NicenessFmt(255), "Minimal required niceness")
47 rxOnly = flag.Bool("rx", false, "Only receive packets")
48 txOnly = flag.Bool("tx", false, "Only transfer packets")
49 mkdir = flag.Bool("mkdir", false, "Create necessary outbound directories")
50 keep = flag.Bool("keep", false, "Do not delete transferred packets")
51 spoolPath = flag.String("spool", "", "Override path to spool")
52 logPath = flag.String("log", "", "Override path to logfile")
53 quiet = flag.Bool("quiet", false, "Print only errors")
54 showPrgrs = flag.Bool("progress", false, "Force progress showing")
55 omitPrgrs = flag.Bool("noprogress", false, "Omit progress showing")
56 debug = flag.Bool("debug", false, "Print debug messages")
57 version = flag.Bool("version", false, "Print version information")
58 warranty = flag.Bool("warranty", false, "Print warranty information")
60 log.SetFlags(log.Lshortfile)
64 fmt.Println(nncp.Warranty)
68 fmt.Println(nncp.VersionGet())
75 nice, err := nncp.NicenessParse(*niceRaw)
79 if *rxOnly && *txOnly {
80 log.Fatalln("-rx and -tx can not be set simultaneously")
83 ctx, err := nncp.CtxFromCmdline(
93 log.Fatalln("Error during initialization:", err)
96 var nodeOnly *nncp.Node
98 nodeOnly, err = ctx.FindNode(*nodeRaw)
100 log.Fatalln("Invalid -node specified:", err)
105 selfPath := filepath.Join(flag.Arg(0), ctx.SelfId.String())
108 var fis []os.FileInfo
110 var logMsg func(les nncp.LEs) string
115 {K: "XX", V: string(nncp.TRx)},
116 {K: "Dir", V: selfPath},
118 logMsg = func(les nncp.LEs) string {
119 return "Packet transfer, received from self"
121 ctx.LogD("xfer-self", les, logMsg)
122 if _, err = os.Stat(selfPath); err != nil {
123 if os.IsNotExist(err) {
124 ctx.LogD("xfer-self-no-dir", les, func(les nncp.LEs) string {
125 return logMsg(les) + ": no directory"
129 ctx.LogE("xfer-self-stat", les, err, func(les nncp.LEs) string {
130 return logMsg(les) + ": stating"
135 dir, err = os.Open(selfPath)
137 ctx.LogE("xfer-self-open", les, err, func(les nncp.LEs) string {
138 return logMsg(les) + ": opening"
143 fis, err = dir.Readdir(0)
144 dir.Close() // #nosec G104
146 ctx.LogE("xfer-self-read", les, err, func(les nncp.LEs) string {
147 return logMsg(les) + ": reading"
152 for _, fi := range fis {
156 nodeId, err := nncp.NodeIdFromString(fi.Name())
157 les := append(les, nncp.LE{K: "Node", V: fi.Name()})
158 logMsg := func(les nncp.LEs) string {
159 return "Packet transfer, received from " + ctx.NodeName(nodeId)
162 ctx.LogD("xfer-rx-not-node", les, func(les nncp.LEs) string {
163 return logMsg(les) + ": is not NodeId"
167 if nodeOnly != nil && *nodeId != *nodeOnly.Id {
168 ctx.LogD("xfer-rx-skip", les, func(les nncp.LEs) string {
169 return logMsg(les) + ": skipping"
173 if _, known := ctx.Neigh[*nodeId]; !known {
174 ctx.LogD("xfer-rx-unknown", les, func(les nncp.LEs) string {
175 return logMsg(les) + ": unknown"
179 dir, err = os.Open(filepath.Join(selfPath, fi.Name()))
181 ctx.LogE("xfer-rx-open", les, err, func(les nncp.LEs) string {
182 return logMsg(les) + ": opening"
187 fisInt, err := dir.Readdir(0)
188 dir.Close() // #nosec G104
190 ctx.LogE("xfer-rx-read", les, err, func(les nncp.LEs) string {
191 return logMsg(les) + ": reading"
196 for _, fiInt := range fisInt {
200 // Check that it is valid Base32 encoding
201 if _, err = nncp.NodeIdFromString(fiInt.Name()); err != nil {
204 filename := filepath.Join(dir.Name(), fiInt.Name())
205 les := append(les, nncp.LE{K: "File", V: filename})
206 logMsg := func(les nncp.LEs) string {
208 "Packet transfer, received from %s: %s",
209 ctx.NodeName(nodeId), filename,
212 fd, err := os.Open(filename)
214 ctx.LogE("xfer-rx-open", les, err, func(les nncp.LEs) string {
215 return logMsg(les) + ": opening"
220 pktEnc, pktEncRaw, err := ctx.HdrRead(fd)
222 switch pktEnc.Magic {
223 case nncp.MagicNNCPEv1.B:
224 err = nncp.MagicNNCPEv1.TooOld()
225 case nncp.MagicNNCPEv2.B:
226 err = nncp.MagicNNCPEv2.TooOld()
227 case nncp.MagicNNCPEv3.B:
228 err = nncp.MagicNNCPEv3.TooOld()
229 case nncp.MagicNNCPEv4.B:
230 err = nncp.MagicNNCPEv4.TooOld()
231 case nncp.MagicNNCPEv5.B:
233 err = errors.New("is not an encrypted packet")
238 "xfer-rx-not-packet",
239 append(les, nncp.LE{K: "Err", V: err}),
240 func(les nncp.LEs) string {
241 return logMsg(les) + ": not valid packet: " + err.Error()
244 fd.Close() // #nosec G104
247 if pktEnc.Nice > nice {
248 ctx.LogD("xfer-rx-too-nice", les, func(les nncp.LEs) string {
249 return logMsg(les) + ": too nice"
251 fd.Close() // #nosec G104
254 les = append(les, nncp.LE{K: "Size", V: fiInt.Size()})
255 logMsg = func(les nncp.LEs) string {
257 "Packet transfer, received from %s: %s (%s)",
258 ctx.NodeName(nodeId), filename,
259 humanize.IBytes(uint64(fiInt.Size())),
262 if !ctx.IsEnoughSpace(fiInt.Size()) {
263 ctx.LogE("xfer-rx", les, errors.New("is not enough space"), logMsg)
264 fd.Close() // #nosec G104
267 if _, err = fd.Seek(0, 0); err != nil {
270 tmp, err := ctx.NewTmpFileWHash()
276 _, err := io.CopyN(w, bufio.NewReader(fd), fiInt.Size())
281 ctx.LogE("xfer-rx", les, err, logMsg)
282 w.CloseWithError(err) // #nosec G104
285 if _, err = nncp.CopyProgressed(
289 nncp.LE{K: "Pkt", V: filename},
290 nncp.LE{K: "FullSize", V: fiInt.Size()},
294 ctx.LogE("xfer-rx", les, err, logMsg)
297 fd.Close() // #nosec G104
302 if err = tmp.Commit(filepath.Join(
309 ctx.LogI("xfer-rx", les, logMsg)
311 if err = os.Remove(filename); err != nil {
312 ctx.LogE("xfer-rx-remove", les, err, logMsg)
317 ctx.HdrWrite(pktEncRaw, filepath.Join(
334 for nodeId := range ctx.Neigh {
335 les := nncp.LEs{{K: "XX", V: string(nncp.TTx)}, {K: "Node", V: nodeId}}
336 logMsg := func(les nncp.LEs) string {
337 return "Packet transfer, sent to " + ctx.NodeName(&nodeId)
339 if nodeOnly != nil && nodeId != *nodeOnly.Id {
340 ctx.LogD("xfer-tx-skip", les, func(les nncp.LEs) string {
341 return logMsg(les) + ": skipping"
345 dirLock, err := ctx.LockDir(&nodeId, string(nncp.TTx))
349 nodePath := filepath.Join(flag.Arg(0), nodeId.String())
350 les = append(les, nncp.LE{K: "Dir", V: nodePath})
351 logMsg = func(les nncp.LEs) string {
353 "Packet transfer, sent to %s: directory %s",
354 ctx.NodeName(&nodeId), nodePath,
357 _, err = os.Stat(nodePath)
359 if os.IsNotExist(err) {
360 ctx.LogD("xfer-tx-not-exist", les, func(les nncp.LEs) string {
361 return logMsg(les) + ": does not exist"
364 ctx.UnlockDir(dirLock)
367 if err = os.Mkdir(nodePath, os.FileMode(0777)); err != nil {
368 ctx.UnlockDir(dirLock)
369 ctx.LogE("xfer-tx-mkdir", les, err, logMsg)
374 ctx.UnlockDir(dirLock)
375 ctx.LogE("xfer-tx", les, err, logMsg)
380 dstPath := filepath.Join(nodePath, ctx.SelfId.String())
381 les[len(les)-1].V = dstPath
382 logMsg = func(les nncp.LEs) string {
384 "Packet transfer, sent to %s: directory %s",
385 ctx.NodeName(&nodeId), dstPath,
388 _, err = os.Stat(dstPath)
390 if os.IsNotExist(err) {
391 if err = os.Mkdir(dstPath, os.FileMode(0777)); err != nil {
392 ctx.UnlockDir(dirLock)
393 ctx.LogE("xfer-tx-mkdir", les, err, logMsg)
398 ctx.UnlockDir(dirLock)
399 ctx.LogE("xfer-tx", les, err, logMsg)
404 les = les[:len(les)-1]
405 for job := range ctx.Jobs(&nodeId, nncp.TTx) {
406 pktName := filepath.Base(job.Path)
407 les := append(les, nncp.LE{K: "Pkt", V: pktName})
408 logMsg = func(les nncp.LEs) string {
410 "Packet transfer, sent to %s: %s",
411 ctx.NodeName(&nodeId), pktName,
414 if job.PktEnc.Nice > nice {
415 ctx.LogD("xfer-tx-too-nice", les, func(les nncp.LEs) string {
416 return logMsg(les) + ": too nice"
420 if _, err = os.Stat(filepath.Join(dstPath, pktName)); err == nil || !os.IsNotExist(err) {
421 ctx.LogD("xfer-tx-exists", les, func(les nncp.LEs) string {
422 return logMsg(les) + ": already exists"
426 if _, err = os.Stat(filepath.Join(dstPath, pktName+nncp.SeenSuffix)); err == nil || !os.IsNotExist(err) {
427 ctx.LogD("xfer-tx-seen", les, func(les nncp.LEs) string {
428 return logMsg(les) + ": already seen"
432 tmp, err := nncp.TempFile(dstPath, "xfer")
434 ctx.LogE("xfer-tx-mktemp", les, err, func(les nncp.LEs) string {
435 return logMsg(les) + ": mktemp"
440 les = append(les, nncp.LE{K: "Tmp", V: tmp.Name()})
441 ctx.LogD("xfer-tx-tmp-create", les, func(les nncp.LEs) string {
442 return fmt.Sprintf("%s: temporary %s created", logMsg(les), tmp.Name())
444 fd, err := os.Open(job.Path)
446 ctx.LogE("xfer-tx-open", les, err, func(les nncp.LEs) string {
447 return logMsg(les) + ": opening"
449 tmp.Close() // #nosec G104
453 bufW := bufio.NewWriter(tmp)
454 copied, err := nncp.CopyProgressed(
455 bufW, bufio.NewReader(fd), "Tx",
456 append(les, nncp.LE{K: "FullSize", V: job.Size}),
459 fd.Close() // #nosec G104
461 ctx.LogE("xfer-tx-copy", les, err, func(les nncp.LEs) string {
462 return logMsg(les) + ": copying"
464 tmp.Close() // #nosec G104
468 if err = bufW.Flush(); err != nil {
469 tmp.Close() // #nosec G104
470 ctx.LogE("xfer-tx-flush", les, err, func(les nncp.LEs) string {
471 return logMsg(les) + ": flushing"
476 if err = tmp.Sync(); err != nil {
477 tmp.Close() // #nosec G104
478 ctx.LogE("xfer-tx-sync", les, err, func(les nncp.LEs) string {
479 return logMsg(les) + ": syncing"
484 if err = tmp.Close(); err != nil {
485 ctx.LogE("xfer-tx-close", les, err, func(les nncp.LEs) string {
486 return logMsg(les) + ": closing"
489 if err = os.Rename(tmp.Name(), filepath.Join(dstPath, pktName)); err != nil {
490 ctx.LogE("xfer-tx-rename", les, err, func(les nncp.LEs) string {
491 return logMsg(les) + ": renaming"
496 if err = nncp.DirSync(dstPath); err != nil {
497 ctx.LogE("xfer-tx-dirsync", les, err, func(les nncp.LEs) string {
498 return logMsg(les) + ": dirsyncing"
503 os.Remove(filepath.Join(dstPath, pktName+".part")) // #nosec G104
504 les = les[:len(les)-1]
507 append(les, nncp.LE{K: "Size", V: copied}),
508 func(les nncp.LEs) string {
510 "%s (%s)", logMsg(les), humanize.IBytes(uint64(copied)),
515 if err = os.Remove(job.Path); err != nil {
516 ctx.LogE("xfer-tx-remove", les, err, func(les nncp.LEs) string {
517 return logMsg(les) + ": removing"
520 } else if ctx.HdrUsage {
521 os.Remove(job.Path + nncp.HdrSuffix)
525 ctx.UnlockDir(dirLock)