]> Cypherpunks.ru repositories - nncp.git/blob - src/cmd/nncp-xfer/main.go
Merge branch 'develop'
[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-2021 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         "log"
28         "os"
29         "path/filepath"
30
31         xdr "github.com/davecgh/go-xdr/xdr2"
32         "go.cypherpunks.ru/nncp/v5"
33 )
34
35 func usage() {
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])
39         flag.PrintDefaults()
40 }
41
42 func main() {
43         var (
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")
59         )
60         flag.Usage = usage
61         flag.Parse()
62         if *warranty {
63                 fmt.Println(nncp.Warranty)
64                 return
65         }
66         if *version {
67                 fmt.Println(nncp.VersionGet())
68                 return
69         }
70         if flag.NArg() != 1 {
71                 usage()
72                 os.Exit(1)
73         }
74         nice, err := nncp.NicenessParse(*niceRaw)
75         if err != nil {
76                 log.Fatalln(err)
77         }
78         if *rxOnly && *txOnly {
79                 log.Fatalln("-rx and -tx can not be set simultaneously")
80         }
81
82         ctx, err := nncp.CtxFromCmdline(
83                 *cfgPath,
84                 *spoolPath,
85                 *logPath,
86                 *quiet,
87                 *showPrgrs,
88                 *omitPrgrs,
89                 *debug,
90         )
91         if err != nil {
92                 log.Fatalln("Error during initialization:", err)
93         }
94
95         var nodeOnly *nncp.Node
96         if *nodeRaw != "" {
97                 nodeOnly, err = ctx.FindNode(*nodeRaw)
98                 if err != nil {
99                         log.Fatalln("Invalid -node specified:", err)
100                 }
101         }
102
103         ctx.Umask()
104         selfPath := filepath.Join(flag.Arg(0), ctx.SelfId.String())
105         isBad := false
106         var dir *os.File
107         var fis []os.FileInfo
108         var les nncp.LEs
109         if *txOnly {
110                 goto Tx
111         }
112         les = nncp.LEs{
113                 {K: "XX", V: string(nncp.TRx)},
114                 {K: "Dir", V: selfPath},
115         }
116         ctx.LogD("nncp-xfer", les, "self")
117         if _, err = os.Stat(selfPath); err != nil {
118                 if os.IsNotExist(err) {
119                         ctx.LogD("nncp-xfer", les, "no dir")
120                         goto Tx
121                 }
122                 ctx.LogE("nncp-xfer", les, err, "stat")
123                 isBad = true
124                 goto Tx
125         }
126         dir, err = os.Open(selfPath)
127         if err != nil {
128                 ctx.LogE("nncp-xfer", les, err, "open")
129                 isBad = true
130                 goto Tx
131         }
132         fis, err = dir.Readdir(0)
133         dir.Close() // #nosec G104
134         if err != nil {
135                 ctx.LogE("nncp-xfer", les, err, "read")
136                 isBad = true
137                 goto Tx
138         }
139         for _, fi := range fis {
140                 if !fi.IsDir() {
141                         continue
142                 }
143                 nodeId, err := nncp.NodeIdFromString(fi.Name())
144                 les := append(les, nncp.LE{K: "Node", V: fi.Name()})
145                 if err != nil {
146                         ctx.LogD("nncp-xfer", les, "is not NodeId")
147                         continue
148                 }
149                 if nodeOnly != nil && *nodeId != *nodeOnly.Id {
150                         ctx.LogD("nncp-xfer", les, "skip")
151                         continue
152                 }
153                 if _, known := ctx.Neigh[*nodeId]; !known {
154                         ctx.LogD("nncp-xfer", les, "unknown")
155                         continue
156                 }
157                 dir, err = os.Open(filepath.Join(selfPath, fi.Name()))
158                 if err != nil {
159                         ctx.LogE("nncp-xfer", les, err, "open")
160                         isBad = true
161                         continue
162                 }
163                 fisInt, err := dir.Readdir(0)
164                 dir.Close() // #nosec G104
165                 if err != nil {
166                         ctx.LogE("nncp-xfer", les, err, "read")
167                         isBad = true
168                         continue
169                 }
170                 for _, fiInt := range fisInt {
171                         if !fi.IsDir() {
172                                 continue
173                         }
174                         // Check that it is valid Base32 encoding
175                         if _, err = nncp.NodeIdFromString(fiInt.Name()); err != nil {
176                                 continue
177                         }
178                         filename := filepath.Join(dir.Name(), fiInt.Name())
179                         les := append(les, nncp.LE{K: "File", V: filename})
180                         fd, err := os.Open(filename)
181                         if err != nil {
182                                 ctx.LogE("nncp-xfer", les, err, "open")
183                                 isBad = true
184                                 continue
185                         }
186                         var pktEnc nncp.PktEnc
187                         _, err = xdr.Unmarshal(fd, &pktEnc)
188                         if err != nil || pktEnc.Magic != nncp.MagicNNCPEv4 {
189                                 ctx.LogD("nncp-xfer", les, "is not a packet")
190                                 fd.Close() // #nosec G104
191                                 continue
192                         }
193                         if pktEnc.Nice > nice {
194                                 ctx.LogD("nncp-xfer", les, "too nice")
195                                 fd.Close() // #nosec G104
196                                 continue
197                         }
198                         les = append(les, nncp.LE{K: "Size", V: fiInt.Size()})
199                         if !ctx.IsEnoughSpace(fiInt.Size()) {
200                                 ctx.LogE("nncp-xfer", les, errors.New("is not enough space"), "")
201                                 fd.Close() // #nosec G104
202                                 continue
203                         }
204                         if _, err = fd.Seek(0, 0); err != nil {
205                                 log.Fatalln(err)
206                         }
207                         tmp, err := ctx.NewTmpFileWHash()
208                         if err != nil {
209                                 log.Fatalln(err)
210                         }
211                         r, w := io.Pipe()
212                         go func() {
213                                 _, err := io.CopyN(w, bufio.NewReader(fd), fiInt.Size())
214                                 if err == nil {
215                                         err = w.Close()
216                                 }
217                                 if err != nil {
218                                         ctx.LogE("nncp-xfer", les, err, "copy")
219                                         w.CloseWithError(err) // #nosec G104
220                                 }
221                         }()
222                         if _, err = nncp.CopyProgressed(
223                                 tmp.W, r, "Rx",
224                                 append(les, nncp.LEs{
225                                         {K: "Pkt", V: filename},
226                                         {K: "FullSize", V: fiInt.Size()},
227                                 }...),
228                                 ctx.ShowPrgrs,
229                         ); err != nil {
230                                 ctx.LogE("nncp-xfer", les, err, "copy")
231                                 isBad = true
232                         }
233                         fd.Close() // #nosec G104
234                         if isBad {
235                                 tmp.Cancel()
236                                 continue
237                         }
238                         if err = tmp.Commit(filepath.Join(
239                                 ctx.Spool,
240                                 nodeId.String(),
241                                 string(nncp.TRx),
242                         )); err != nil {
243                                 log.Fatalln(err)
244                         }
245                         ctx.LogI("nncp-xfer", les, "")
246                         if !*keep {
247                                 if err = os.Remove(filename); err != nil {
248                                         ctx.LogE("nncp-xfer", les, err, "remove")
249                                         isBad = true
250                                 }
251                         }
252                 }
253         }
254
255 Tx:
256         if *rxOnly {
257                 if isBad {
258                         os.Exit(1)
259                 }
260                 return
261         }
262         for nodeId := range ctx.Neigh {
263                 les := nncp.LEs{
264                         {K: "XX", V: string(nncp.TTx)},
265                         {K: "Node", V: nodeId},
266                 }
267                 if nodeOnly != nil && nodeId != *nodeOnly.Id {
268                         ctx.LogD("nncp-xfer", les, "skip")
269                         continue
270                 }
271                 dirLock, err := ctx.LockDir(&nodeId, string(nncp.TTx))
272                 if err != nil {
273                         continue
274                 }
275                 nodePath := filepath.Join(flag.Arg(0), nodeId.String())
276                 les = append(les, nncp.LE{K: "Dir", V: nodePath})
277                 _, err = os.Stat(nodePath)
278                 if err != nil {
279                         if os.IsNotExist(err) {
280                                 ctx.LogD("nncp-xfer", les, "does not exist")
281                                 if !*mkdir {
282                                         ctx.UnlockDir(dirLock)
283                                         continue
284                                 }
285                                 if err = os.Mkdir(nodePath, os.FileMode(0777)); err != nil {
286                                         ctx.UnlockDir(dirLock)
287                                         ctx.LogE("nncp-xfer", les, err, "mkdir")
288                                         isBad = true
289                                         continue
290                                 }
291                         } else {
292                                 ctx.UnlockDir(dirLock)
293                                 ctx.LogE("nncp-xfer", les, err, "stat")
294                                 isBad = true
295                                 continue
296                         }
297                 }
298                 dstPath := filepath.Join(nodePath, ctx.SelfId.String())
299                 les[len(les)-1].V = dstPath
300                 _, err = os.Stat(dstPath)
301                 if err != nil {
302                         if os.IsNotExist(err) {
303                                 if err = os.Mkdir(dstPath, os.FileMode(0777)); err != nil {
304                                         ctx.UnlockDir(dirLock)
305                                         ctx.LogE("nncp-xfer", les, err, "mkdir")
306                                         isBad = true
307                                         continue
308                                 }
309                         } else {
310                                 ctx.UnlockDir(dirLock)
311                                 ctx.LogE("nncp-xfer", les, err, "stat")
312                                 isBad = true
313                                 continue
314                         }
315                 }
316                 les = les[:len(les)-1]
317                 for job := range ctx.Jobs(&nodeId, nncp.TTx) {
318                         pktName := filepath.Base(job.Fd.Name())
319                         les := append(les, nncp.LE{K: "Pkt", V: pktName})
320                         if job.PktEnc.Nice > nice {
321                                 ctx.LogD("nncp-xfer", les, "too nice")
322                                 job.Fd.Close() // #nosec G104
323                                 continue
324                         }
325                         if _, err = os.Stat(filepath.Join(dstPath, pktName)); err == nil || !os.IsNotExist(err) {
326                                 ctx.LogD("nncp-xfer", les, "already exists")
327                                 job.Fd.Close() // #nosec G104
328                                 continue
329                         }
330                         if _, err = os.Stat(filepath.Join(dstPath, pktName+nncp.SeenSuffix)); err == nil || !os.IsNotExist(err) {
331                                 ctx.LogD("nncp-xfer", les, "already exists")
332                                 job.Fd.Close() // #nosec G104
333                                 continue
334                         }
335                         tmp, err := nncp.TempFile(dstPath, "xfer")
336                         if err != nil {
337                                 ctx.LogE("nncp-xfer", les, err, "mktemp")
338                                 job.Fd.Close() // #nosec G104
339                                 isBad = true
340                                 break
341                         }
342                         les = append(les, nncp.LE{K: "Tmp", V: tmp.Name()})
343                         ctx.LogD("nncp-xfer", les, "created")
344                         bufW := bufio.NewWriter(tmp)
345                         copied, err := nncp.CopyProgressed(
346                                 bufW, bufio.NewReader(job.Fd), "Tx",
347                                 append(les, nncp.LE{K: "FullSize", V: job.Size}),
348                                 ctx.ShowPrgrs,
349                         )
350                         job.Fd.Close() // #nosec G104
351                         if err != nil {
352                                 ctx.LogE("nncp-xfer", les, err, "copy")
353                                 tmp.Close() // #nosec G104
354                                 isBad = true
355                                 continue
356                         }
357                         if err = bufW.Flush(); err != nil {
358                                 tmp.Close() // #nosec G104
359                                 ctx.LogE("nncp-xfer", les, err, "flush")
360                                 isBad = true
361                                 continue
362                         }
363                         if err = tmp.Sync(); err != nil {
364                                 tmp.Close() // #nosec G104
365                                 ctx.LogE("nncp-xfer", les, err, "sync")
366                                 isBad = true
367                                 continue
368                         }
369                         if err = tmp.Close(); err != nil {
370                                 ctx.LogE("nncp-xfer", les, err, "sync")
371                         }
372                         if err = os.Rename(tmp.Name(), filepath.Join(dstPath, pktName)); err != nil {
373                                 ctx.LogE("nncp-xfer", les, err, "rename")
374                                 isBad = true
375                                 continue
376                         }
377                         if err = nncp.DirSync(dstPath); err != nil {
378                                 ctx.LogE("nncp-xfer", les, err, "sync")
379                                 isBad = true
380                                 continue
381                         }
382                         os.Remove(filepath.Join(dstPath, pktName+".part")) // #nosec G104
383                         les = les[:len(les)-1]
384                         ctx.LogI("nncp-xfer", append(les, nncp.LE{K: "Size", V: copied}), "")
385                         if !*keep {
386                                 if err = os.Remove(job.Fd.Name()); err != nil {
387                                         ctx.LogE("nncp-xfer", les, err, "remove")
388                                         isBad = true
389                                 }
390                         }
391                 }
392                 ctx.UnlockDir(dirLock)
393         }
394         if isBad {
395                 os.Exit(1)
396         }
397 }