2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2016 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"
27 "github.com/pkg/errors"
29 "cypherpunks.ru/govpn"
32 func (s *Server) startTCP() {
33 bind, err := net.ResolveTCPAddr("tcp", s.configuration.BindAddress)
35 s.Error <- errors.Wrap(err, "net.ResolveTCPAddr")
38 listener, err := net.ListenTCP("tcp", bind)
40 s.Error <- errors.Wrapf(err, "net.ListenTCP %q", bind.String())
43 fields := logrus.Fields{
44 "func": logFuncPrefix + "Server.startTCP",
45 "bind": bind.String(),
47 s.logger.WithFields(fields).WithFields(s.LogFields()).WithFields(s.configuration.LogFields()).Info("Listen")
50 conn, err := listener.AcceptTCP()
52 s.logger.WithError(err).WithFields(fields).WithFields(s.LogFields()).Error("Failed to accept TCP connection")
60 func (s *Server) handleTCP(conn net.Conn) {
61 addr := conn.RemoteAddr().String()
62 buf := make([]byte, govpn.EnclessEnlargeSize+2*govpn.MTUMax)
66 var hs *govpn.Handshake
69 var deadLine time.Time
71 var conf *govpn.PeerConf
72 fields := logrus.Fields{
73 "func": logFuncPrefix + "Server.handleTCP",
82 deadLine = time.Now().Add(govpn.TimeoutDefault)
83 if err = conn.SetReadDeadline(deadLine); err != nil {
84 s.Error <- errors.Wrapf(err, "conn.SetReadDeadline %s", deadLine.String())
87 n, err = conn.Read(buf[prev:])
89 s.logger.WithFields(fields).WithFields(s.LogFields()).WithError(err).Debug("Can't read connection: either EOFed or timeouted")
93 peerID, err := s.idsCache.Find(buf[:prev])
95 s.logger.WithFields(fields).WithFields(s.LogFields()).WithError(err).Debug("Couldn't lookup for peer in ids")
99 s.logger.WithFields(fields).WithFields(s.LogFields()).Debug("Couldn't find peer")
103 conf = s.confs.Get(*peerID)
105 s.logger.WithFields(fields).WithFields(s.LogFields()).WithFields(s.configuration.LogFields()).Error("Configuration get failed")
108 hs = govpn.NewHandshake(addr, conn, conf)
110 peer, err = hs.Server(buf[:prev])
112 s.logger.WithFields(fields).WithError(err).WithFields(s.LogFields()).Error("Can't create new peer")
120 s.logger.WithFields(fields).WithFields(s.LogFields()).WithFields(peer.LogFields()).Info("Handshake completed")
123 s.peersByIDLock.RLock()
124 addrPrev, exists := s.peersByID[*peer.ID]
125 s.peersByIDLock.RUnlock()
129 s.peers[addrPrev].terminator <- struct{}{}
130 tap = s.peers[addrPrev].tap
134 terminator: make(chan struct{}),
136 peer.Protocol = govpn.ProtocolTCP
137 go govpn.PeerTapProcessor(ps.peer, ps.tap, ps.terminator)
138 s.peersByIDLock.Lock()
140 delete(s.peers, addrPrev)
141 delete(s.knownPeers, addrPrev)
143 s.knownPeers[addr] = &peer
144 s.peersByID[*peer.ID] = addr
146 s.peersByIDLock.Unlock()
148 s.logger.WithFields(fields).WithFields(s.LogFields()).WithFields(peer.LogFields()).Debug("Rehandshake completed")
150 tap, err = s.callUp(peer, govpn.ProtocolTCP)
152 s.logger.WithFields(fields).WithFields(s.LogFields()).WithFields(peer.LogFields()).WithError(err).Error("TAP failed")
159 terminator: make(chan struct{}, 1),
161 peer.Protocol = govpn.ProtocolTCP
162 go govpn.PeerTapProcessor(ps.peer, ps.tap, ps.terminator)
164 s.peersByIDLock.Lock()
167 s.peersByID[*peer.ID] = addr
168 s.knownPeers[addr] = &peer
170 s.peersByIDLock.Unlock()
172 s.logger.WithFields(fields).WithFields(s.LogFields()).WithFields(peer.LogFields()).Info("Peer created")
186 if prev == len(buf) {
189 deadLine = time.Now().Add(conf.Timeout)
190 if err = conn.SetReadDeadline(deadLine); err != nil {
191 s.Error <- errors.Wrapf(err, "conn.SetReadDeadline %s", deadLine.String())
194 n, err = conn.Read(buf[prev:])
196 s.logger.WithFields(fields).WithFields(s.LogFields()).WithError(err).Debug("Can't read connection: either EOFed or timeouted")
201 if prev < govpn.MinPktLength {
204 i = bytes.Index(buf[:prev], peer.NonceExpect)
208 if !peer.PktProcess(buf[:i+govpn.NonceSize], tap, false) {
209 s.logger.WithFields(fields).WithFields(s.LogFields()).WithFields(peer.LogFields()).Warn("Packet unauthenticated")
212 copy(buf, buf[i+govpn.NonceSize:prev])
213 prev = prev - i - govpn.NonceSize