]> Cypherpunks.ru repositories - nncp.git/blob - src/cypherpunks.ru/nncp/cmd/nncp-daemon/main.go
Leading dots in packages description
[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-2018 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, err := ctx.StartR(conn, nice, "")
65         if err == nil {
66                 ctx.LogI("call-start", nncp.SDS{"node": state.Node.Id}, "connected")
67                 state.Wait()
68                 ctx.LogI("call-finish", nncp.SDS{
69                         "node":     state.Node.Id,
70                         "duration": strconv.FormatInt(int64(state.Duration.Seconds()), 10),
71                         "rxbytes":  strconv.FormatInt(state.RxBytes, 10),
72                         "txbytes":  strconv.FormatInt(state.TxBytes, 10),
73                         "rxspeed":  strconv.FormatInt(state.RxSpeed, 10),
74                         "txspeed":  strconv.FormatInt(state.TxSpeed, 10),
75                 }, "")
76         } else {
77                 nodeId := "unknown"
78                 if state != nil && state.Node != nil {
79                         nodeId = state.Node.Id.String()
80                 }
81                 ctx.LogE("call-start", nncp.SDS{"node": nodeId, "err": err}, "")
82         }
83 }
84
85 func main() {
86         var (
87                 cfgPath   = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
88                 niceRaw   = flag.String("nice", nncp.NicenessFmt(255), "Minimal required niceness")
89                 bind      = flag.String("bind", "[::]:5400", "Address to bind to")
90                 inetd     = flag.Bool("inetd", false, "Is it started as inetd service")
91                 maxConn   = flag.Int("maxconn", 128, "Maximal number of simultaneous connections")
92                 spoolPath = flag.String("spool", "", "Override path to spool")
93                 logPath   = flag.String("log", "", "Override path to logfile")
94                 quiet     = flag.Bool("quiet", false, "Print only errors")
95                 debug     = flag.Bool("debug", false, "Print debug messages")
96                 version   = flag.Bool("version", false, "Print version information")
97                 warranty  = flag.Bool("warranty", false, "Print warranty information")
98         )
99         flag.Usage = usage
100         flag.Parse()
101         if *warranty {
102                 fmt.Println(nncp.Warranty)
103                 return
104         }
105         if *version {
106                 fmt.Println(nncp.VersionGet())
107                 return
108         }
109         nice, err := nncp.NicenessParse(*niceRaw)
110         if err != nil {
111                 log.Fatalln(err)
112         }
113
114         ctx, err := nncp.CtxFromCmdline(*cfgPath, *spoolPath, *logPath, *quiet, *debug)
115         if err != nil {
116                 log.Fatalln("Error during initialization:", err)
117         }
118         if ctx.Self == nil {
119                 log.Fatalln("Config lacks private keys")
120         }
121
122         if *inetd {
123                 os.Stderr.Close()
124                 conn := &InetdConn{os.Stdin, os.Stdout}
125                 performSP(ctx, conn, nice)
126                 return
127         }
128
129         ln, err := net.Listen("tcp", *bind)
130         if err != nil {
131                 log.Fatalln("Can not listen:", err)
132         }
133         ln = netutil.LimitListener(ln, *maxConn)
134         for {
135                 conn, err := ln.Accept()
136                 if err != nil {
137                         log.Fatalln("Can not accept connection:", err)
138                 }
139                 ctx.LogD("daemon", nncp.SDS{"addr": conn.RemoteAddr()}, "accepted")
140                 go func(conn net.Conn) {
141                         performSP(ctx, conn, nice)
142                         conn.Close()
143                 }(conn)
144         }
145 }