-
Notifications
You must be signed in to change notification settings - Fork 0
/
udp.go
114 lines (105 loc) · 3 KB
/
udp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
GoVPN -- simple secure free software virtual private network daemon
Copyright (C) 2014-2022 Sergey Matveev <stargrave@stargrave.org>
Copyright (C) 2022 github.com/bytejedi
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package client
import (
"fmt"
"net"
"sync/atomic"
"time"
"github.com/bytejedi/govpn"
)
func (c *Client) startUDP() {
remote, err := net.ResolveUDPAddr("udp", c.config.RemoteAddress)
if err != nil {
c.Error <- fmt.Errorf("Can not resolve remote address: %s", err)
return
}
conn, err := net.DialUDP("udp", nil, remote)
if err != nil {
c.Error <- fmt.Errorf("Can not connect remote address: %s", err)
return
}
govpn.Printf(`[connected remote="%s"]`, c.config.RemoteAddress)
hs := govpn.HandshakeStart(c.config.RemoteAddress, conn, c.config.Peer)
buf := make([]byte, c.config.MTU*2)
var n int
var timeouts int
var peer *govpn.Peer
var terminator chan struct{}
timeout := int(c.config.Peer.Timeout.Seconds())
MainCycle:
for {
select {
case <-c.termination:
break MainCycle
default:
}
if err = conn.SetReadDeadline(time.Now().Add(time.Second)); err != nil {
c.Error <- err
break MainCycle
}
n, err = conn.Read(buf)
if timeouts == timeout {
govpn.Printf(`[connection-timeouted remote="%s"]`, c.config.RemoteAddress)
c.timeouted <- struct{}{}
break
}
if err != nil {
timeouts++
continue
}
if peer != nil {
if peer.PktProcess(buf[:n], c.tap, true) {
timeouts = 0
} else {
govpn.Printf(`[packet-unauthenticated remote="%s"]`, c.config.RemoteAddress)
timeouts++
}
if atomic.LoadUint64(&peer.BytesIn)+atomic.LoadUint64(&peer.BytesOut) > govpn.MaxBytesPerKey {
govpn.Printf(`[rehandshake-required remote="%s"]`, c.config.RemoteAddress)
c.rehandshaking <- struct{}{}
break MainCycle
}
continue
}
if c.idsCache.Find(buf[:n]) == nil {
govpn.Printf(`[identity-invalid remote="%s"]`, c.config.RemoteAddress)
continue
}
timeouts = 0
peer = hs.Client(buf[:n])
if peer == nil {
continue
}
govpn.Printf(`[handshake-completed remote="%s"]`, c.config.RemoteAddress)
c.knownPeers.Store(c.config.RemoteAddress, &peer)
if c.firstUpCall {
go govpn.ScriptCall(c.config.UpPath, c.config.InterfaceName, c.config.RemoteAddress)
c.firstUpCall = false
}
hs.Zero()
terminator = make(chan struct{})
go govpn.PeerTapProcessor(peer, c.tap, terminator)
}
if terminator != nil {
terminator <- struct{}{}
}
if hs != nil {
hs.Zero()
}
if err = conn.Close(); err != nil {
c.Error <- err
}
}