]> Cypherpunks.ru repositories - nncp.git/blob - src/cmd/nncp-daemon/main.go
Intermediate .nock packets step
[nncp.git] / src / cmd / nncp-daemon / 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 // NNCP TCP daemon.
19 package main
20
21 import (
22         "flag"
23         "fmt"
24         "log"
25         "net"
26         "os"
27         "time"
28
29         "go.cypherpunks.ru/nncp/v5"
30         "golang.org/x/net/netutil"
31 )
32
33 func usage() {
34         fmt.Fprintf(os.Stderr, nncp.UsageHeader())
35         fmt.Fprintf(os.Stderr, "nncp-daemon -- TCP daemon\n\n")
36         fmt.Fprintf(os.Stderr, "Usage: %s [options]\nOptions:\n", os.Args[0])
37         flag.PrintDefaults()
38 }
39
40 type InetdConn struct {
41         r *os.File
42         w *os.File
43 }
44
45 func (c InetdConn) Read(p []byte) (n int, err error) {
46         return c.r.Read(p)
47 }
48
49 func (c InetdConn) Write(p []byte) (n int, err error) {
50         return c.w.Write(p)
51 }
52
53 func (c InetdConn) SetReadDeadline(t time.Time) error {
54         return c.r.SetReadDeadline(t)
55 }
56
57 func (c InetdConn) SetWriteDeadline(t time.Time) error {
58         return c.w.SetWriteDeadline(t)
59 }
60
61 func (c InetdConn) Close() error {
62         if err := c.r.Close(); err != nil {
63                 c.w.Close() // #nosec G104
64                 return err
65         }
66         return c.w.Close()
67 }
68
69 func performSP(
70         ctx *nncp.Ctx,
71         conn nncp.ConnDeadlined,
72         nice uint8,
73         noCK bool,
74         nodeIdC chan *nncp.NodeId,
75 ) {
76         state := nncp.SPState{
77                 Ctx:  ctx,
78                 Nice: nice,
79                 NoCK: noCK,
80         }
81         if err := state.StartR(conn); err == nil {
82                 ctx.LogI("call-start", nncp.LEs{{K: "Node", V: state.Node.Id}}, "connected")
83                 nodeIdC <- state.Node.Id
84                 state.Wait()
85                 ctx.LogI("call-finish", nncp.LEs{
86                         {K: "Node", V: state.Node.Id},
87                         {K: "Duration", V: int64(state.Duration.Seconds())},
88                         {K: "RxBytes", V: state.RxBytes},
89                         {K: "TxBytes", V: state.TxBytes},
90                         {K: "RxSpeed", V: state.RxSpeed},
91                         {K: "TxSpeed", V: state.TxSpeed},
92                 }, "")
93         } else {
94                 nodeId := "unknown"
95                 if state.Node == nil {
96                         nodeIdC <- nil
97                 } else {
98                         nodeIdC <- state.Node.Id
99                         nodeId = state.Node.Id.String()
100                 }
101                 ctx.LogI("call-start", nncp.LEs{{K: "Node", V: nodeId}}, "connected")
102         }
103         close(nodeIdC)
104 }
105
106 func main() {
107         var (
108                 cfgPath   = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
109                 niceRaw   = flag.String("nice", nncp.NicenessFmt(255), "Minimal required niceness")
110                 bind      = flag.String("bind", "[::]:5400", "Address to bind to")
111                 inetd     = flag.Bool("inetd", false, "Is it started as inetd service")
112                 maxConn   = flag.Int("maxconn", 128, "Maximal number of simultaneous connections")
113                 noCK      = flag.Bool("nock", false, "Do no checksum checking")
114                 spoolPath = flag.String("spool", "", "Override path to spool")
115                 logPath   = flag.String("log", "", "Override path to logfile")
116                 quiet     = flag.Bool("quiet", false, "Print only errors")
117                 showPrgrs = flag.Bool("progress", false, "Force progress showing")
118                 omitPrgrs = flag.Bool("noprogress", false, "Omit progress showing")
119                 debug     = flag.Bool("debug", false, "Print debug messages")
120                 version   = flag.Bool("version", false, "Print version information")
121                 warranty  = flag.Bool("warranty", false, "Print warranty information")
122
123                 autoToss       = flag.Bool("autotoss", false, "Toss after call is finished")
124                 autoTossDoSeen = flag.Bool("autotoss-seen", false, "Create .seen files during tossing")
125                 autoTossNoFile = flag.Bool("autotoss-nofile", false, "Do not process \"file\" packets during tossing")
126                 autoTossNoFreq = flag.Bool("autotoss-nofreq", false, "Do not process \"freq\" packets during tossing")
127                 autoTossNoExec = flag.Bool("autotoss-noexec", false, "Do not process \"exec\" packets during tossing")
128                 autoTossNoTrns = flag.Bool("autotoss-notrns", false, "Do not process \"trns\" packets during tossing")
129         )
130         flag.Usage = usage
131         flag.Parse()
132         if *warranty {
133                 fmt.Println(nncp.Warranty)
134                 return
135         }
136         if *version {
137                 fmt.Println(nncp.VersionGet())
138                 return
139         }
140         nice, err := nncp.NicenessParse(*niceRaw)
141         if err != nil {
142                 log.Fatalln(err)
143         }
144
145         ctx, err := nncp.CtxFromCmdline(
146                 *cfgPath,
147                 *spoolPath,
148                 *logPath,
149                 *quiet,
150                 *showPrgrs,
151                 *omitPrgrs,
152                 *debug,
153         )
154         if err != nil {
155                 log.Fatalln("Error during initialization:", err)
156         }
157         if ctx.Self == nil {
158                 log.Fatalln("Config lacks private keys")
159         }
160         ctx.Umask()
161
162         if *inetd {
163                 os.Stderr.Close() // #nosec G104
164                 conn := &InetdConn{os.Stdin, os.Stdout}
165                 nodeIdC := make(chan *nncp.NodeId)
166                 go performSP(ctx, conn, nice, *noCK, nodeIdC)
167                 nodeId := <-nodeIdC
168                 var autoTossFinish chan struct{}
169                 var autoTossBadCode chan bool
170                 if *autoToss && nodeId != nil {
171                         autoTossFinish, autoTossBadCode = ctx.AutoToss(
172                                 nodeId,
173                                 nice,
174                                 *autoTossDoSeen,
175                                 *autoTossNoFile,
176                                 *autoTossNoFreq,
177                                 *autoTossNoExec,
178                                 *autoTossNoTrns,
179                         )
180                 }
181                 <-nodeIdC // call completion
182                 if *autoToss {
183                         close(autoTossFinish)
184                         <-autoTossBadCode
185                 }
186                 conn.Close() // #nosec G104
187                 return
188         }
189
190         ln, err := net.Listen("tcp", *bind)
191         if err != nil {
192                 log.Fatalln("Can not listen:", err)
193         }
194         ln = netutil.LimitListener(ln, *maxConn)
195         for {
196                 conn, err := ln.Accept()
197                 if err != nil {
198                         log.Fatalln("Can not accept connection:", err)
199                 }
200                 ctx.LogD("daemon", nncp.LEs{{K: "Addr", V: conn.RemoteAddr()}}, "accepted")
201                 go func(conn net.Conn) {
202                         nodeIdC := make(chan *nncp.NodeId)
203                         go performSP(ctx, conn, nice, *noCK, nodeIdC)
204                         nodeId := <-nodeIdC
205                         var autoTossFinish chan struct{}
206                         var autoTossBadCode chan bool
207                         if *autoToss && nodeId != nil {
208                                 autoTossFinish, autoTossBadCode = ctx.AutoToss(
209                                         nodeId,
210                                         nice,
211                                         *autoTossDoSeen,
212                                         *autoTossNoFile,
213                                         *autoTossNoFreq,
214                                         *autoTossNoExec,
215                                         *autoTossNoTrns,
216                                 )
217                         }
218                         <-nodeIdC // call completion
219                         if *autoToss {
220                                 close(autoTossFinish)
221                                 <-autoTossBadCode
222                         }
223                         conn.Close() // #nosec G104
224                 }(conn)
225         }
226 }