]> Cypherpunks.ru repositories - nncp.git/blob - src/cmd/nncp-xfer/main.go
016c441510cda5215a363b06ecec1805616a2ab6
[nncp.git] / src / cmd / nncp-xfer / 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 // Exchange NNCP inbound and outbounds packets with external directory.
19 package main
20
21 import (
22         "bufio"
23         "errors"
24         "flag"
25         "fmt"
26         "io"
27         "io/fs"
28         "log"
29         "os"
30         "path/filepath"
31
32         "github.com/dustin/go-humanize"
33         "go.cypherpunks.ru/nncp/v8"
34 )
35
36 func usage() {
37         fmt.Fprint(os.Stderr, nncp.UsageHeader())
38         fmt.Fprint(os.Stderr, "nncp-xfer -- copy inbound and outbounds packets\n\n")
39         fmt.Fprintf(os.Stderr, "Usage: %s [options] DIR\nOptions:\n", os.Args[0])
40         flag.PrintDefaults()
41 }
42
43 func main() {
44         var (
45                 cfgPath   = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
46                 nodeRaw   = flag.String("node", "", "Process only that node")
47                 niceRaw   = flag.String("nice", nncp.NicenessFmt(255), "Minimal required niceness")
48                 rxOnly    = flag.Bool("rx", false, "Only receive packets")
49                 txOnly    = flag.Bool("tx", false, "Only transfer packets")
50                 mkdir     = flag.Bool("mkdir", false, "Create necessary outbound directories")
51                 keep      = flag.Bool("keep", false, "Do not delete transferred packets")
52                 spoolPath = flag.String("spool", "", "Override path to spool")
53                 logPath   = flag.String("log", "", "Override path to logfile")
54                 quiet     = flag.Bool("quiet", false, "Print only errors")
55                 showPrgrs = flag.Bool("progress", false, "Force progress showing")
56                 omitPrgrs = flag.Bool("noprogress", false, "Omit progress showing")
57                 debug     = flag.Bool("debug", false, "Print debug messages")
58                 version   = flag.Bool("version", false, "Print version information")
59                 warranty  = flag.Bool("warranty", false, "Print warranty information")
60         )
61         log.SetFlags(log.Lshortfile)
62         flag.Usage = usage
63         flag.Parse()
64         if *warranty {
65                 fmt.Println(nncp.Warranty)
66                 return
67         }
68         if *version {
69                 fmt.Println(nncp.VersionGet())
70                 return
71         }
72         if flag.NArg() != 1 {
73                 usage()
74                 os.Exit(1)
75         }
76         nice, err := nncp.NicenessParse(*niceRaw)
77         if err != nil {
78                 log.Fatalln(err)
79         }
80         if *rxOnly && *txOnly {
81                 log.Fatalln("-rx and -tx can not be set simultaneously")
82         }
83
84         ctx, err := nncp.CtxFromCmdline(
85                 *cfgPath,
86                 *spoolPath,
87                 *logPath,
88                 *quiet,
89                 *showPrgrs,
90                 *omitPrgrs,
91                 *debug,
92         )
93         if err != nil {
94                 log.Fatalln("Error during initialization:", err)
95         }
96
97         var nodeOnly *nncp.Node
98         if *nodeRaw != "" {
99                 nodeOnly, err = ctx.FindNode(*nodeRaw)
100                 if err != nil {
101                         log.Fatalln("Invalid -node specified:", err)
102                 }
103         }
104
105         ctx.Umask()
106         selfPath := filepath.Join(flag.Arg(0), ctx.SelfId.String())
107         isBad := false
108         var dir *os.File
109         var fis []os.FileInfo
110         var les nncp.LEs
111         var logMsg func(les nncp.LEs) string
112         if *txOnly {
113                 goto Tx
114         }
115         les = nncp.LEs{
116                 {K: "XX", V: string(nncp.TRx)},
117                 {K: "Dir", V: selfPath},
118         }
119         logMsg = func(les nncp.LEs) string {
120                 return "Packet transfer, received from self"
121         }
122         ctx.LogD("xfer-self", les, logMsg)
123         if _, err = os.Stat(selfPath); err != nil {
124                 if errors.Is(err, fs.ErrNotExist) {
125                         ctx.LogD("xfer-self-no-dir", les, func(les nncp.LEs) string {
126                                 return logMsg(les) + ": no directory"
127                         })
128                         goto Tx
129                 }
130                 ctx.LogE("xfer-self-stat", les, err, func(les nncp.LEs) string {
131                         return logMsg(les) + ": stating"
132                 })
133                 isBad = true
134                 goto Tx
135         }
136         dir, err = os.Open(selfPath)
137         if err != nil {
138                 ctx.LogE("xfer-self-open", les, err, func(les nncp.LEs) string {
139                         return logMsg(les) + ": opening"
140                 })
141                 isBad = true
142                 goto Tx
143         }
144         fis, err = dir.Readdir(0)
145         dir.Close()
146         if err != nil {
147                 ctx.LogE("xfer-self-read", les, err, func(les nncp.LEs) string {
148                         return logMsg(les) + ": reading"
149                 })
150                 isBad = true
151                 goto Tx
152         }
153         for _, fi := range fis {
154                 if !fi.IsDir() {
155                         continue
156                 }
157                 nodeId, err := nncp.NodeIdFromString(fi.Name())
158                 les := append(les, nncp.LE{K: "Node", V: fi.Name()})
159                 logMsg := func(les nncp.LEs) string {
160                         return "Packet transfer, received from " + ctx.NodeName(nodeId)
161                 }
162                 if err != nil {
163                         ctx.LogD("xfer-rx-not-node", les, func(les nncp.LEs) string {
164                                 return logMsg(les) + ": is not NodeId"
165                         })
166                         continue
167                 }
168                 if nodeOnly != nil && *nodeId != *nodeOnly.Id {
169                         ctx.LogD("xfer-rx-skip", les, func(les nncp.LEs) string {
170                                 return logMsg(les) + ": skipping"
171                         })
172                         continue
173                 }
174                 if _, known := ctx.Neigh[*nodeId]; !known {
175                         ctx.LogD("xfer-rx-unknown", les, func(les nncp.LEs) string {
176                                 return logMsg(les) + ": unknown"
177                         })
178                         continue
179                 }
180                 dir, err = os.Open(filepath.Join(selfPath, fi.Name()))
181                 if err != nil {
182                         ctx.LogE("xfer-rx-open", les, err, func(les nncp.LEs) string {
183                                 return logMsg(les) + ": opening"
184                         })
185                         isBad = true
186                         continue
187                 }
188                 fisInt, err := dir.Readdir(0)
189                 dir.Close()
190                 if err != nil {
191                         ctx.LogE("xfer-rx-read", les, err, func(les nncp.LEs) string {
192                                 return logMsg(les) + ": reading"
193                         })
194                         isBad = true
195                         continue
196                 }
197                 for _, fiInt := range fisInt {
198                         if !fiInt.IsDir() {
199                                 continue
200                         }
201                         // Check that it is valid Base32 encoding
202                         if _, err = nncp.NodeIdFromString(fiInt.Name()); err != nil {
203                                 continue
204                         }
205                         filename := filepath.Join(dir.Name(), fiInt.Name())
206                         les := append(les, nncp.LE{K: "File", V: filename})
207                         logMsg := func(les nncp.LEs) string {
208                                 return fmt.Sprintf(
209                                         "Packet transfer, received from %s: %s",
210                                         ctx.NodeName(nodeId), filename,
211                                 )
212                         }
213                         if _, err = os.Stat(filepath.Join(
214                                 ctx.Spool,
215                                 nodeId.String(),
216                                 string(nncp.TRx),
217                                 nncp.SeenDir,
218                                 fiInt.Name(),
219                         )); err == nil || !errors.Is(err, fs.ErrNotExist) {
220                                 ctx.LogI("xfer-rx-seen", les, func(les nncp.LEs) string {
221                                         return logMsg(les) + ": packet already seen"
222                                 })
223                                 if !*keep {
224                                         if err = os.Remove(filename); err != nil {
225                                                 ctx.LogE("xfer-rx-remove", les, err, logMsg)
226                                                 isBad = true
227                                         }
228                                 }
229                                 continue
230                         }
231                         fd, err := os.Open(filename)
232                         if err != nil {
233                                 ctx.LogE("xfer-rx-open", les, err, func(les nncp.LEs) string {
234                                         return logMsg(les) + ": opening"
235                                 })
236                                 isBad = true
237                                 continue
238                         }
239                         pktEnc, pktEncRaw, err := ctx.HdrRead(fd)
240                         if err == nil {
241                                 switch pktEnc.Magic {
242                                 case nncp.MagicNNCPEv1.B:
243                                         err = nncp.MagicNNCPEv1.TooOld()
244                                 case nncp.MagicNNCPEv2.B:
245                                         err = nncp.MagicNNCPEv2.TooOld()
246                                 case nncp.MagicNNCPEv3.B:
247                                         err = nncp.MagicNNCPEv3.TooOld()
248                                 case nncp.MagicNNCPEv4.B:
249                                         err = nncp.MagicNNCPEv4.TooOld()
250                                 case nncp.MagicNNCPEv5.B:
251                                         err = nncp.MagicNNCPEv5.TooOld()
252                                 case nncp.MagicNNCPEv6.B:
253                                 default:
254                                         err = errors.New("is not an encrypted packet")
255                                 }
256                         }
257                         if err != nil {
258                                 ctx.LogD(
259                                         "xfer-rx-not-packet",
260                                         append(les, nncp.LE{K: "Err", V: err}),
261                                         func(les nncp.LEs) string {
262                                                 return logMsg(les) + ": not valid packet: " + err.Error()
263                                         },
264                                 )
265                                 fd.Close()
266                                 continue
267                         }
268                         if pktEnc.Nice > nice {
269                                 ctx.LogD("xfer-rx-too-nice", les, func(les nncp.LEs) string {
270                                         return logMsg(les) + ": too nice"
271                                 })
272                                 fd.Close()
273                                 continue
274                         }
275                         les = append(les, nncp.LE{K: "Size", V: fiInt.Size()})
276                         logMsg = func(les nncp.LEs) string {
277                                 return fmt.Sprintf(
278                                         "Packet transfer, received from %s: %s (%s)",
279                                         ctx.NodeName(nodeId), filename,
280                                         humanize.IBytes(uint64(fiInt.Size())),
281                                 )
282                         }
283                         if !ctx.IsEnoughSpace(fiInt.Size()) {
284                                 ctx.LogE("xfer-rx", les, errors.New("is not enough space"), logMsg)
285                                 fd.Close()
286                                 continue
287                         }
288                         if _, err = fd.Seek(0, io.SeekStart); err != nil {
289                                 log.Fatalln(err)
290                         }
291                         tmp, err := ctx.NewTmpFileWHash()
292                         if err != nil {
293                                 log.Fatalln(err)
294                         }
295                         r, w := io.Pipe()
296                         go func() {
297                                 _, err := io.CopyN(
298                                         w, bufio.NewReaderSize(fd, nncp.MTHBlockSize), fiInt.Size(),
299                                 )
300                                 if err == nil {
301                                         err = w.Close()
302                                 }
303                                 if err != nil {
304                                         ctx.LogE("xfer-rx", les, err, logMsg)
305                                         w.CloseWithError(err)
306                                 }
307                         }()
308                         _, err = nncp.CopyProgressed(
309                                 tmp.W, r, "Rx",
310                                 append(
311                                         les,
312                                         nncp.LE{K: "Pkt", V: filename},
313                                         nncp.LE{K: "FullSize", V: fiInt.Size()},
314                                 ),
315                                 ctx.ShowPrgrs,
316                         )
317                         fd.Close()
318                         if err != nil {
319                                 ctx.LogE("xfer-rx", les, err, logMsg)
320                                 tmp.Cancel()
321                                 isBad = true
322                                 continue
323                         }
324                         if err = tmp.W.Flush(); err != nil {
325                                 ctx.LogE("xfer-rx", les, err, logMsg)
326                                 tmp.Cancel()
327                                 isBad = true
328                                 continue
329                         }
330                         if tmp.Checksum() != fiInt.Name() {
331                                 ctx.LogE("xfer-rx", les, errors.New("checksum mismatch"), logMsg)
332                                 tmp.Cancel()
333                                 isBad = true
334                                 continue
335                         }
336                         if err = tmp.Commit(filepath.Join(
337                                 ctx.Spool,
338                                 nodeId.String(),
339                                 string(nncp.TRx),
340                         )); err != nil {
341                                 log.Fatalln(err)
342                         }
343                         ctx.LogI("xfer-rx", les, logMsg)
344                         if !*keep {
345                                 if err = os.Remove(filename); err != nil {
346                                         ctx.LogE("xfer-rx-remove", les, err, logMsg)
347                                         isBad = true
348                                 }
349                         }
350                         if ctx.HdrUsage {
351                                 ctx.HdrWrite(pktEncRaw, filepath.Join(
352                                         ctx.Spool,
353                                         nodeId.String(),
354                                         string(nncp.TRx),
355                                         tmp.Checksum(),
356                                 ))
357                         }
358                 }
359         }
360
361 Tx:
362         if *rxOnly {
363                 if isBad {
364                         os.Exit(1)
365                 }
366                 return
367         }
368         for nodeId := range ctx.Neigh {
369                 les := nncp.LEs{{K: "XX", V: string(nncp.TTx)}, {K: "Node", V: nodeId}}
370                 logMsg := func(les nncp.LEs) string {
371                         return "Packet transfer, sent to " + ctx.NodeName(&nodeId)
372                 }
373                 if nodeOnly != nil && nodeId != *nodeOnly.Id {
374                         ctx.LogD("xfer-tx-skip", les, func(les nncp.LEs) string {
375                                 return logMsg(les) + ": skipping"
376                         })
377                         continue
378                 }
379                 dirLock, err := ctx.LockDir(&nodeId, string(nncp.TTx))
380                 if err != nil {
381                         continue
382                 }
383                 nodePath := filepath.Join(flag.Arg(0), nodeId.String())
384                 les = append(les, nncp.LE{K: "Dir", V: nodePath})
385                 logMsg = func(les nncp.LEs) string {
386                         return fmt.Sprintf(
387                                 "Packet transfer, sent to %s: directory %s",
388                                 ctx.NodeName(&nodeId), nodePath,
389                         )
390                 }
391                 _, err = os.Stat(nodePath)
392                 if err != nil {
393                         if errors.Is(err, fs.ErrNotExist) {
394                                 ctx.LogD("xfer-tx-not-exist", les, func(les nncp.LEs) string {
395                                         return logMsg(les) + ": does not exist"
396                                 })
397                                 if !*mkdir {
398                                         ctx.UnlockDir(dirLock)
399                                         continue
400                                 }
401                                 if err = os.Mkdir(nodePath, os.FileMode(0777)); err != nil {
402                                         ctx.UnlockDir(dirLock)
403                                         ctx.LogE("xfer-tx-mkdir", les, err, logMsg)
404                                         isBad = true
405                                         continue
406                                 }
407                         } else {
408                                 ctx.UnlockDir(dirLock)
409                                 ctx.LogE("xfer-tx", les, err, logMsg)
410                                 isBad = true
411                                 continue
412                         }
413                 }
414                 dstPath := filepath.Join(nodePath, ctx.SelfId.String())
415                 les[len(les)-1].V = dstPath
416                 logMsg = func(les nncp.LEs) string {
417                         return fmt.Sprintf(
418                                 "Packet transfer, sent to %s: directory %s",
419                                 ctx.NodeName(&nodeId), dstPath,
420                         )
421                 }
422                 _, err = os.Stat(dstPath)
423                 if err != nil {
424                         if errors.Is(err, fs.ErrNotExist) {
425                                 if err = os.Mkdir(dstPath, os.FileMode(0777)); err != nil {
426                                         ctx.UnlockDir(dirLock)
427                                         ctx.LogE("xfer-tx-mkdir", les, err, logMsg)
428                                         isBad = true
429                                         continue
430                                 }
431                         } else {
432                                 ctx.UnlockDir(dirLock)
433                                 ctx.LogE("xfer-tx", les, err, logMsg)
434                                 isBad = true
435                                 continue
436                         }
437                 }
438                 les = les[:len(les)-1]
439                 for job := range ctx.Jobs(&nodeId, nncp.TTx) {
440                         pktName := filepath.Base(job.Path)
441                         les := append(les, nncp.LE{K: "Pkt", V: pktName})
442                         logMsg = func(les nncp.LEs) string {
443                                 return fmt.Sprintf(
444                                         "Packet transfer, sent to %s: %s",
445                                         ctx.NodeName(&nodeId), pktName,
446                                 )
447                         }
448                         if job.PktEnc.Nice > nice {
449                                 ctx.LogD("xfer-tx-too-nice", les, func(les nncp.LEs) string {
450                                         return logMsg(les) + ": too nice"
451                                 })
452                                 continue
453                         }
454                         if _, err = os.Stat(filepath.Join(dstPath, pktName)); err == nil || !errors.Is(err, fs.ErrNotExist) {
455                                 ctx.LogD("xfer-tx-exists", les, func(les nncp.LEs) string {
456                                         return logMsg(les) + ": already exists"
457                                 })
458                                 continue
459                         }
460                         tmp, err := nncp.TempFile(dstPath, "xfer")
461                         if err != nil {
462                                 ctx.LogE("xfer-tx-mktemp", les, err, func(les nncp.LEs) string {
463                                         return logMsg(les) + ": mktemp"
464                                 })
465                                 isBad = true
466                                 break
467                         }
468                         les = append(les, nncp.LE{K: "Tmp", V: tmp.Name()})
469                         ctx.LogD("xfer-tx-tmp-create", les, func(les nncp.LEs) string {
470                                 return fmt.Sprintf("%s: temporary %s created", logMsg(les), tmp.Name())
471                         })
472                         fd, err := os.Open(job.Path)
473                         if err != nil {
474                                 ctx.LogE("xfer-tx-open", les, err, func(les nncp.LEs) string {
475                                         return logMsg(les) + ": opening"
476                                 })
477                                 tmp.Close()
478                                 isBad = true
479                                 continue
480                         }
481                         bufW := bufio.NewWriter(tmp)
482                         copied, err := nncp.CopyProgressed(
483                                 bufW, bufio.NewReaderSize(fd, nncp.MTHBlockSize), "Tx",
484                                 append(les, nncp.LE{K: "FullSize", V: job.Size}),
485                                 ctx.ShowPrgrs,
486                         )
487                         fd.Close()
488                         if err != nil {
489                                 ctx.LogE("xfer-tx-copy", les, err, func(les nncp.LEs) string {
490                                         return logMsg(les) + ": copying"
491                                 })
492                                 tmp.Close()
493                                 isBad = true
494                                 continue
495                         }
496                         if err = bufW.Flush(); err != nil {
497                                 tmp.Close()
498                                 ctx.LogE("xfer-tx-flush", les, err, func(les nncp.LEs) string {
499                                         return logMsg(les) + ": flushing"
500                                 })
501                                 isBad = true
502                                 continue
503                         }
504                         if !nncp.NoSync {
505                                 if err = tmp.Sync(); err != nil {
506                                         tmp.Close()
507                                         ctx.LogE("xfer-tx-sync", les, err, func(les nncp.LEs) string {
508                                                 return logMsg(les) + ": syncing"
509                                         })
510                                         isBad = true
511                                         continue
512                                 }
513                         }
514                         if err = tmp.Close(); err != nil {
515                                 ctx.LogE("xfer-tx-close", les, err, func(les nncp.LEs) string {
516                                         return logMsg(les) + ": closing"
517                                 })
518                         }
519                         if err = os.Rename(tmp.Name(), filepath.Join(dstPath, pktName)); err != nil {
520                                 ctx.LogE("xfer-tx-rename", les, err, func(les nncp.LEs) string {
521                                         return logMsg(les) + ": renaming"
522                                 })
523                                 isBad = true
524                                 continue
525                         }
526                         if err = nncp.DirSync(dstPath); err != nil {
527                                 ctx.LogE("xfer-tx-dirsync", les, err, func(les nncp.LEs) string {
528                                         return logMsg(les) + ": dirsyncing"
529                                 })
530                                 isBad = true
531                                 continue
532                         }
533                         os.Remove(filepath.Join(dstPath, pktName+".part"))
534                         les = les[:len(les)-1]
535                         ctx.LogI(
536                                 "xfer-tx",
537                                 append(les, nncp.LE{K: "Size", V: copied}),
538                                 func(les nncp.LEs) string {
539                                         return fmt.Sprintf(
540                                                 "%s (%s)", logMsg(les), humanize.IBytes(uint64(copied)),
541                                         )
542                                 },
543                         )
544                         if !*keep {
545                                 if err = os.Remove(job.Path); err != nil {
546                                         ctx.LogE("xfer-tx-remove", les, err, func(les nncp.LEs) string {
547                                                 return logMsg(les) + ": removing"
548                                         })
549                                         isBad = true
550                                 } else if ctx.HdrUsage {
551                                         os.Remove(nncp.JobPath2Hdr(job.Path))
552                                 }
553                         }
554                 }
555                 ctx.UnlockDir(dirLock)
556         }
557         if isBad {
558                 os.Exit(1)
559         }
560 }