2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2017 Sergey Matveev <stargrave@stargrave.org>
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.
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.
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/>.
26 "github.com/Sirupsen/logrus"
29 const rwTimeout = 10 * time.Second
31 // KnownPeers map of all connected GoVPN peers
32 type KnownPeers map[string]**Peer
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
40 func StatsProcessor(stats string, peers *KnownPeers) {
42 buf := make([]byte, 2<<8)
43 fields := logrus.Fields{
44 "func": logFuncPrefix + "StatsProcessor",
49 logger.WithFields(fields).WithField("port", stats).Debug("Stats are going to listen")
50 statsPort, err := net.Listen("tcp", stats)
52 logger.WithError(err).WithField("stats", stats).Error("Can't listen stats server")
57 conn, err = statsPort.Accept()
59 logger.WithFields(fields).WithError(err).Error("Couldn't accept connection")
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")
71 for _, peer := range *peers {
72 peersList = append(peersList, *peer)
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")
78 if err = conn.Close(); err != nil {
79 logger.WithFields(fields).WithError(err).Error("Couldn't close connection")