]> Cypherpunks.ru repositories - govpn.git/blob - src/cypherpunks.ru/govpn/stats.go
Could -> can, for consistency
[govpn.git] / src / cypherpunks.ru / govpn / stats.go
1 /*
2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2017 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 package govpn
20
21 import (
22         "encoding/json"
23         "net"
24         "time"
25
26         "github.com/Sirupsen/logrus"
27 )
28
29 const rwTimeout = 10 * time.Second
30
31 // KnownPeers map of all connected GoVPN peers
32 type KnownPeers map[string]**Peer
33
34 // StatsProcessor is assumed to be run in background. It accepts
35 // connection on statsPort, reads anything one send to them and show
36 // information about known peers in serialized JSON format. peers
37 // argument is a reference to the map with references to the peers as
38 // values. Map is used here because of ease of adding and removing
39 // elements in it.
40 func StatsProcessor(stats string, peers *KnownPeers) {
41         var conn net.Conn
42         buf := make([]byte, 2<<8)
43         fields := logrus.Fields{
44                 "func":    logFuncPrefix + "StatsProcessor",
45                 "bufsize": len(buf),
46                 "port":    stats,
47         }
48
49         logger.WithFields(fields).WithField(
50                 "port", stats,
51         ).Debug("Stats are going to listen")
52         statsPort, err := net.Listen("tcp", stats)
53         if err != nil {
54                 logger.WithError(err).WithField(
55                         "stats", stats,
56                 ).Error("Can't listen stats server")
57                 return
58         }
59
60         for {
61                 conn, err = statsPort.Accept()
62                 if err != nil {
63                         logger.WithFields(fields).WithError(err).Error("Can't accept connection")
64                         continue
65                 }
66                 deadLine := time.Now().Add(rwTimeout)
67                 if err = conn.SetDeadline(deadLine); err != nil {
68                         logger.WithFields(fields).WithField(
69                                 "deadline", deadLine.String(),
70                         ).WithError(err).Error("Can't set deadline")
71                 } else if _, err = conn.Read(buf); err != nil {
72                         logger.WithFields(fields).WithError(err).Error("Can't read buffer")
73                 } else if _, err = conn.Write([]byte("HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n")); err != nil {
74                         logger.WithFields(fields).WithError(err).Error("Can't write HTTP headers")
75                 } else {
76                         var peersList []*Peer
77                         for _, peer := range *peers {
78                                 peersList = append(peersList, *peer)
79                         }
80                         if err = json.NewEncoder(conn).Encode(peersList); err != nil {
81                                 logger.WithFields(fields).WithField(
82                                         "peers", len(peersList),
83                                 ).WithError(err).Error("Can't encode to JSON")
84                         }
85                 }
86                 if err = conn.Close(); err != nil {
87                         logger.WithFields(fields).WithError(err).Error("Can't close connection")
88                 }
89         }
90 }