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/>.
25 "github.com/Sirupsen/logrus"
26 "github.com/pkg/errors"
28 "cypherpunks.ru/govpn"
31 // PeerConfigurer is used by a GoVPN server to figure the configuration
33 type PeerConfigurer interface {
34 Get(govpn.PeerID) *govpn.PeerConf
37 // MACPeerFinder is used by GoVPN server to figure the PeerID from
39 type MACPeerFinder interface {
40 Find([]byte) (*govpn.PeerID, error)
43 // PeerState hold server side state of a single connecting/connected peer
44 type PeerState struct {
46 terminator chan struct{}
50 // Configuration hold GoVPN server configuration
51 type Configuration struct {
54 Protocol govpn.Protocol
58 // Validate return an error if a configuration is invalid
59 func (c *Configuration) Validate() error {
60 if len(c.BindAddress) == 0 {
61 return errors.New("Missing BindAddress")
66 // LogFields return a logrus compatible logging context
67 func (c *Configuration) LogFields() logrus.Fields {
68 const prefix = "srv_conf_"
70 prefix + "bind": c.BindAddress,
71 prefix + "protocol": c.Protocol.String(),
72 prefix + "timeout": c.Timeout.String(),
74 if len(c.ProxyAddress) > 0 {
75 f[prefix+"proxy"] = c.ProxyAddress
80 // Server is a GoVPN server instance
82 configuration Configuration
83 termSignal chan interface{}
85 idsCache MACPeerFinder
88 handshakes map[string]*govpn.Handshake
91 peers map[string]*PeerState
92 peersLock sync.RWMutex
94 peersByID map[govpn.PeerID]string
95 peersByIDLock sync.RWMutex
97 knownPeers govpn.KnownPeers
100 logger *logrus.Logger
102 // Error channel receives any kind of routine errors
106 // LogFields return a logrus compatible logging context
107 func (s *Server) LogFields() logrus.Fields {
108 const prefix = "srv_"
109 return logrus.Fields{
110 prefix + "hs": len(s.handshakes),
111 prefix + "peers": len(s.peers),
112 prefix + "known": len(s.knownPeers),
116 // KnownPeers return GoVPN peers.
117 // used to get client statistics.
118 func (s *Server) KnownPeers() *govpn.KnownPeers {
122 // NewServer return a configured GoVPN server, to listen network
123 // connection MainCycle must be executed
124 func NewServer(serverConf Configuration, peerConfs PeerConfigurer, idsCache MACPeerFinder, logger *logrus.Logger, termSignal chan interface{}) *Server {
125 govpn.SetLogger(logger)
127 configuration: serverConf,
129 termSignal: termSignal,
131 handshakes: make(map[string]*govpn.Handshake),
132 peers: make(map[string]*PeerState),
133 peersByID: make(map[govpn.PeerID]string),
134 knownPeers: govpn.KnownPeers(make(map[string]**govpn.Peer)),
135 Error: make(chan error, 1),
140 // MainCycle main loop that handle connecting/connected client
141 func (s *Server) MainCycle() {
142 switch s.configuration.Protocol {
143 case govpn.ProtocolUDP:
145 case govpn.ProtocolTCP:
147 case govpn.ProtocolALL:
151 s.Error <- errors.New("Unknown protocol specified")
155 if len(s.configuration.ProxyAddress) > 0 {
158 fields := logrus.Fields{"func": logFuncPrefix + "Server.MainCycle"}
165 s.configuration.LogFields(),
166 ).Info("Starting...")
168 var needsDeletion bool
170 hsHeartbeat := time.Tick(s.configuration.Timeout)
171 go func() { <-hsHeartbeat }()
182 s.configuration.LogFields(),
183 ).Info("Terminating")
184 for _, ps := range s.peers {
185 if err = s.callDown(ps); err != nil {
188 ).WithError(err).WithFields(
190 ).Error("Failed to run callDown")
192 if err = ps.tap.Close(); err != nil {
193 logrus.WithError(err).WithFields(
197 ).Error("Couldn't close TAP")
200 // empty value signals that everything is fine
204 logrus.WithFields(fields).Debug("Heartbeat")
207 for addr, hs := range s.handshakes {
208 if hs.LastPing.Add(s.configuration.Timeout).Before(now) {
213 ).Debug("handshake is expired, delete")
215 delete(s.handshakes, addr)
219 s.peersByIDLock.Lock()
221 for addr, ps := range s.peers {
223 needsDeletion = ps.peer.LastPing.Add(
224 s.configuration.Timeout,
226 ps.peer.BusyR.Unlock()
232 ).Info("Delete peer")
233 delete(s.peers, addr)
234 delete(s.knownPeers, addr)
235 delete(s.peersByID, *ps.peer.ID)
236 if err = s.callDown(ps); err != nil {
237 logrus.WithError(err).WithFields(
241 ).Error("Couldn't execute callDown")
243 if err = ps.tap.Close(); err != nil {
244 logrus.WithError(err).WithFields(
248 ).Error("Couldn't close TAP")
250 ps.terminator <- struct{}{}
255 s.peersByIDLock.Unlock()