2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2019 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, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 "go.cypherpunks.ru/govpn/v7"
30 func (c *Client) startTCP() {
31 remote, err := net.ResolveTCPAddr("tcp", c.config.RemoteAddress)
33 c.Error <- fmt.Errorf("Can not resolve remote address: %s", err)
36 conn, err := net.DialTCP("tcp", nil, remote)
38 c.Error <- fmt.Errorf("Can not connect to address: %s", err)
41 govpn.Printf(`[connected remote="%s"]`, c.config.RemoteAddress)
45 func (c *Client) handleTCP(conn *net.TCPConn) {
46 hs := govpn.HandshakeStart(c.config.RemoteAddress, conn, c.config.Peer)
47 buf := make([]byte, 2*(govpn.EnclessEnlargeSize+c.config.MTU)+c.config.MTU)
52 var terminator chan struct{}
61 govpn.Printf(`[packet-timeouted remote="%s"]`, c.config.RemoteAddress)
62 c.timeouted <- struct{}{}
66 if err = conn.SetReadDeadline(time.Now().Add(c.config.Peer.Timeout)); err != nil {
70 n, err = conn.Read(buf[prev:])
72 govpn.Printf(`[connection-timeouted remote="%s"]`, c.config.RemoteAddress)
73 c.timeouted <- struct{}{}
78 peerID := c.idsCache.Find(buf[:prev])
82 peer = hs.Client(buf[:prev])
87 govpn.Printf(`[handshake-completed remote="%s"]`, c.config.RemoteAddress)
88 c.knownPeers.Store(c.config.RemoteAddress, &peer)
90 go govpn.ScriptCall(c.config.UpPath, c.config.InterfaceName, c.config.RemoteAddress)
94 terminator = make(chan struct{})
95 go govpn.PeerTapProcessor(peer, c.tap, terminator)
110 case <-c.termination:
114 if prev == len(buf) {
115 govpn.Printf(`[packet-timeouted remote="%s"]`, c.config.RemoteAddress)
116 c.timeouted <- struct{}{}
119 if err = conn.SetReadDeadline(time.Now().Add(c.config.Peer.Timeout)); err != nil {
123 n, err = conn.Read(buf[prev:])
125 govpn.Printf(`[connection-timeouted remote="%s"]`, c.config.RemoteAddress)
126 c.timeouted <- struct{}{}
131 if prev < govpn.MinPktLength {
134 i = bytes.Index(buf[:prev], peer.NonceExpect)
138 if !peer.PktProcess(buf[:i+govpn.NonceSize], c.tap, false) {
139 govpn.Printf(`[packet-unauthenticated remote="%s"]`, c.config.RemoteAddress)
140 c.timeouted <- struct{}{}
143 if atomic.LoadUint64(&peer.BytesIn)+atomic.LoadUint64(&peer.BytesOut) > govpn.MaxBytesPerKey {
144 govpn.Printf(`[rehandshake-required remote="%s"]`, c.config.RemoteAddress)
145 c.rehandshaking <- struct{}{}
148 copy(buf, buf[i+govpn.NonceSize:prev])
149 prev = prev - i - govpn.NonceSize
152 if terminator != nil {
153 terminator <- struct{}{}
156 if err = conn.Close(); err != nil {