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(
51 ).Debug("Stats are going to listen")
52 statsPort, err := net.Listen("tcp", stats)
54 logger.WithError(err).WithField(
56 ).Error("Can't listen stats server")
61 conn, err = statsPort.Accept()
63 logger.WithFields(fields).WithError(err).Error("Can't accept connection")
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")
77 for _, peer := range *peers {
78 peersList = append(peersList, *peer)
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")
86 if err = conn.Close(); err != nil {
87 logger.WithFields(fields).WithError(err).Error("Can't close connection")