]> Cypherpunks.ru repositories - govpn.git/blob - cmd/govpn-client/main.go
Zero handshake and peer states after their usage
[govpn.git] / cmd / govpn-client / main.go
1 /*
2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2015 Sergey Matveev <stargrave@stargrave.org>
4
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.
9
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.
14
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/>.
17 */
18
19 // Simple secure free software virtual private network daemon.
20 package main
21
22 import (
23         "flag"
24         "log"
25         "net"
26         "os"
27         "os/signal"
28
29         "govpn"
30 )
31
32 var (
33         remoteAddr = flag.String("remote", "", "Remote server address")
34         ifaceName  = flag.String("iface", "tap0", "TAP network interface")
35         IDRaw      = flag.String("id", "", "Client identification")
36         keyPath    = flag.String("key", "", "Path to authentication key file")
37         upPath     = flag.String("up", "", "Path to up-script")
38         downPath   = flag.String("down", "", "Path to down-script")
39         mtu        = flag.Int("mtu", 1500, "MTU")
40         nonceDiff  = flag.Int("noncediff", 1, "Allow nonce difference")
41         timeoutP   = flag.Int("timeout", 60, "Timeout seconds")
42 )
43
44 func main() {
45         flag.Parse()
46         timeout := *timeoutP
47         var err error
48         log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
49
50         govpn.MTU = *mtu
51         govpn.Timeout = timeout
52         govpn.Noncediff = *nonceDiff
53
54         id := govpn.IDDecode(*IDRaw)
55         key := govpn.KeyRead(*keyPath)
56         if id == nil {
57                 panic("ID is not specified")
58         }
59
60         bind, err := net.ResolveUDPAddr("udp", "0.0.0.0:0")
61         if err != nil {
62                 panic(err)
63         }
64         conn, err := net.ListenUDP("udp", bind)
65         if err != nil {
66                 panic(err)
67         }
68         remote, err := net.ResolveUDPAddr("udp", *remoteAddr)
69         if err != nil {
70                 panic(err)
71         }
72
73         tap, ethSink, ethReady, _, err := govpn.TAPListen(*ifaceName)
74         if err != nil {
75                 panic(err)
76         }
77         udpSink, udpBuf, udpReady := govpn.ConnListen(conn)
78
79         timeouts := 0
80         firstUpCall := true
81         var peer *govpn.Peer
82         var ethPkt []byte
83         var udpPkt *govpn.UDPPkt
84         var udpPktData []byte
85
86         termSignal := make(chan os.Signal, 1)
87         signal.Notify(termSignal, os.Interrupt, os.Kill)
88
89         log.Println("Client version", govpn.Version)
90         log.Println("Starting handshake")
91         handshake := govpn.HandshakeStart(conn, remote, id, key)
92
93 MainCycle:
94         for {
95                 if peer != nil && peer.Bytes > govpn.MaxBytesPerKey {
96                         peer.Zero()
97                         peer = nil
98                         handshake = govpn.HandshakeStart(conn, remote, id, key)
99                         log.Println("Rehandshaking")
100                 }
101                 select {
102                 case <-termSignal:
103                         break MainCycle
104                 case ethPkt = <-ethSink:
105                         if peer == nil {
106                                 if len(ethPkt) > 0 {
107                                         ethReady <- struct{}{}
108                                 }
109                                 continue
110                         }
111                         peer.EthProcess(ethPkt, conn, ethReady)
112                 case udpPkt = <-udpSink:
113                         timeouts++
114                         if timeouts >= timeout {
115                                 break MainCycle
116                         }
117                         if udpPkt == nil {
118                                 udpReady <- struct{}{}
119                                 continue
120                         }
121
122                         udpPktData = udpBuf[:udpPkt.Size]
123                         if govpn.IsValidHandshakePkt(udpPktData) {
124                                 if udpPkt.Addr.String() != remote.String() {
125                                         udpReady <- struct{}{}
126                                         log.Println("Unknown handshake message")
127                                         continue
128                                 }
129                                 if p := handshake.Client(conn, key, udpPktData); p != nil {
130                                         log.Println("Handshake completed")
131                                         if firstUpCall {
132                                                 go govpn.ScriptCall(*upPath, *ifaceName)
133                                                 firstUpCall = false
134                                         }
135                                         peer = p
136                                         handshake.Zero()
137                                         handshake = nil
138                                 }
139                                 udpReady <- struct{}{}
140                                 continue
141                         }
142                         if peer == nil {
143                                 udpReady <- struct{}{}
144                                 continue
145                         }
146                         if peer.UDPProcess(udpPktData, tap, udpReady) {
147                                 timeouts = 0
148                         }
149                 }
150         }
151         govpn.ScriptCall(*downPath, *ifaceName)
152 }