]> Cypherpunks.ru repositories - govpn.git/blob - src/cypherpunks.ru/govpn/stats.go
Fix copyright years
[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("port", stats).Debug("Stats are going to listen")
50         statsPort, err := net.Listen("tcp", stats)
51         if err != nil {
52                 logger.WithError(err).WithField("stats", stats).Error("Can't listen stats server")
53                 return
54         }
55
56         for {
57                 conn, err = statsPort.Accept()
58                 if err != nil {
59                         logger.WithFields(fields).WithError(err).Error("Couldn't accept connection")
60                         continue
61                 }
62                 deadLine := time.Now().Add(rwTimeout)
63                 if err = conn.SetDeadline(deadLine); err != nil {
64                         logger.WithFields(fields).WithField("deadline", deadLine.String()).WithError(err).Error("Couldn't set deadline")
65                 } else if _, err = conn.Read(buf); err != nil {
66                         logger.WithFields(fields).WithError(err).Error("Couldn't read buffer")
67                 } else if _, err = conn.Write([]byte("HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n")); err != nil {
68                         logger.WithFields(fields).WithError(err).Error("Couldn't write HTTP headers")
69                 } else {
70                         var peersList []*Peer
71                         for _, peer := range *peers {
72                                 peersList = append(peersList, *peer)
73                         }
74                         if err = json.NewEncoder(conn).Encode(peersList); err != nil {
75                                 logger.WithFields(fields).WithField("peers", len(peersList)).WithError(err).Error("Couldn't encode to JSON")
76                         }
77                 }
78                 if err = conn.Close(); err != nil {
79                         logger.WithFields(fields).WithError(err).Error("Couldn't close connection")
80                 }
81         }
82 }