]> Cypherpunks.ru repositories - nncp.git/blob - src/cypherpunks.ru/nncp/cmd/nncp-daemon/main.go
3f2897206eb745600025f80e3f98205bc1746def
[nncp.git] / src / cypherpunks.ru / nncp / cmd / nncp-daemon / main.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2019 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, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 // NNCP TCP daemon.
20 package main
21
22 import (
23         "flag"
24         "fmt"
25         "log"
26         "net"
27         "os"
28         "strconv"
29         "time"
30
31         "cypherpunks.ru/nncp"
32         "golang.org/x/net/netutil"
33 )
34
35 func usage() {
36         fmt.Fprintf(os.Stderr, nncp.UsageHeader())
37         fmt.Fprintf(os.Stderr, "nncp-daemon -- TCP daemon\n\n")
38         fmt.Fprintf(os.Stderr, "Usage: %s [options]\nOptions:\n", os.Args[0])
39         flag.PrintDefaults()
40 }
41
42 type InetdConn struct {
43         r *os.File
44         w *os.File
45 }
46
47 func (ic *InetdConn) Read(p []byte) (n int, err error) {
48         return ic.r.Read(p)
49 }
50
51 func (ic *InetdConn) Write(p []byte) (n int, err error) {
52         return ic.w.Write(p)
53 }
54
55 func (ic *InetdConn) SetReadDeadline(t time.Time) error {
56         return ic.r.SetReadDeadline(t)
57 }
58
59 func (ic *InetdConn) SetWriteDeadline(t time.Time) error {
60         return ic.w.SetWriteDeadline(t)
61 }
62
63 func performSP(ctx *nncp.Ctx, conn nncp.ConnDeadlined, nice uint8) {
64         state := nncp.SPState{
65                 Ctx:  ctx,
66                 Nice: nice,
67         }
68         if err := state.StartR(conn); err == nil {
69                 ctx.LogI("call-start", nncp.SDS{"node": state.Node.Id}, "connected")
70                 state.Wait()
71                 ctx.LogI("call-finish", nncp.SDS{
72                         "node":     state.Node.Id,
73                         "duration": strconv.FormatInt(int64(state.Duration.Seconds()), 10),
74                         "rxbytes":  strconv.FormatInt(state.RxBytes, 10),
75                         "txbytes":  strconv.FormatInt(state.TxBytes, 10),
76                         "rxspeed":  strconv.FormatInt(state.RxSpeed, 10),
77                         "txspeed":  strconv.FormatInt(state.TxSpeed, 10),
78                 }, "")
79         } else {
80                 nodeId := "unknown"
81                 if state.Node != nil {
82                         nodeId = state.Node.Id.String()
83                 }
84                 ctx.LogE("call-start", nncp.SDS{"node": nodeId, "err": err}, "")
85         }
86 }
87
88 func main() {
89         var (
90                 cfgPath   = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
91                 niceRaw   = flag.String("nice", nncp.NicenessFmt(255), "Minimal required niceness")
92                 bind      = flag.String("bind", "[::]:5400", "Address to bind to")
93                 inetd     = flag.Bool("inetd", false, "Is it started as inetd service")
94                 maxConn   = flag.Int("maxconn", 128, "Maximal number of simultaneous connections")
95                 spoolPath = flag.String("spool", "", "Override path to spool")
96                 logPath   = flag.String("log", "", "Override path to logfile")
97                 quiet     = flag.Bool("quiet", false, "Print only errors")
98                 debug     = flag.Bool("debug", false, "Print debug messages")
99                 version   = flag.Bool("version", false, "Print version information")
100                 warranty  = flag.Bool("warranty", false, "Print warranty information")
101         )
102         flag.Usage = usage
103         flag.Parse()
104         if *warranty {
105                 fmt.Println(nncp.Warranty)
106                 return
107         }
108         if *version {
109                 fmt.Println(nncp.VersionGet())
110                 return
111         }
112         nice, err := nncp.NicenessParse(*niceRaw)
113         if err != nil {
114                 log.Fatalln(err)
115         }
116
117         ctx, err := nncp.CtxFromCmdline(*cfgPath, *spoolPath, *logPath, *quiet, *debug)
118         if err != nil {
119                 log.Fatalln("Error during initialization:", err)
120         }
121         if ctx.Self == nil {
122                 log.Fatalln("Config lacks private keys")
123         }
124
125         if *inetd {
126                 os.Stderr.Close()
127                 conn := &InetdConn{os.Stdin, os.Stdout}
128                 performSP(ctx, conn, nice)
129                 return
130         }
131
132         ln, err := net.Listen("tcp", *bind)
133         if err != nil {
134                 log.Fatalln("Can not listen:", err)
135         }
136         ln = netutil.LimitListener(ln, *maxConn)
137         for {
138                 conn, err := ln.Accept()
139                 if err != nil {
140                         log.Fatalln("Can not accept connection:", err)
141                 }
142                 ctx.LogD("daemon", nncp.SDS{"addr": conn.RemoteAddr()}, "accepted")
143                 go func(conn net.Conn) {
144                         performSP(ctx, conn, nice)
145                         conn.Close()
146                 }(conn)
147         }
148 }