]> Cypherpunks.ru repositories - govpn.git/blob - cmd/govpn-client/main.go
Replace handshake NULLs with an IDtag
[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         govpn.PeersInitDummy(id)
56         key := govpn.KeyRead(*keyPath)
57         if id == nil {
58                 panic("ID is not specified")
59         }
60
61         bind, err := net.ResolveUDPAddr("udp", "0.0.0.0:0")
62         if err != nil {
63                 panic(err)
64         }
65         conn, err := net.ListenUDP("udp", bind)
66         if err != nil {
67                 panic(err)
68         }
69         remote, err := net.ResolveUDPAddr("udp", *remoteAddr)
70         if err != nil {
71                 panic(err)
72         }
73
74         tap, ethSink, ethReady, _, err := govpn.TAPListen(*ifaceName)
75         if err != nil {
76                 panic(err)
77         }
78         udpSink, udpBuf, udpReady := govpn.ConnListen(conn)
79
80         timeouts := 0
81         firstUpCall := true
82         var peer *govpn.Peer
83         var ethPkt []byte
84         var udpPkt *govpn.UDPPkt
85         var udpPktData []byte
86
87         termSignal := make(chan os.Signal, 1)
88         signal.Notify(termSignal, os.Interrupt, os.Kill)
89
90         log.Println(govpn.VersionGet())
91         log.Println("Starting handshake")
92         handshake := govpn.HandshakeStart(conn, remote, id, key)
93
94 MainCycle:
95         for {
96                 if peer != nil && peer.Bytes > govpn.MaxBytesPerKey {
97                         peer.Zero()
98                         peer = nil
99                         handshake = govpn.HandshakeStart(conn, remote, id, key)
100                         log.Println("Rehandshaking")
101                 }
102                 select {
103                 case <-termSignal:
104                         break MainCycle
105                 case ethPkt = <-ethSink:
106                         if peer == nil {
107                                 if len(ethPkt) > 0 {
108                                         ethReady <- struct{}{}
109                                 }
110                                 continue
111                         }
112                         peer.EthProcess(ethPkt, conn, ethReady)
113                 case udpPkt = <-udpSink:
114                         timeouts++
115                         if timeouts >= timeout {
116                                 break MainCycle
117                         }
118                         if udpPkt == nil {
119                                 udpReady <- struct{}{}
120                                 continue
121                         }
122
123                         udpPktData = udpBuf[:udpPkt.Size]
124                         if peer == nil {
125                                 if udpPkt.Addr.String() != remote.String() {
126                                         udpReady <- struct{}{}
127                                         log.Println("Unknown handshake message")
128                                         continue
129                                 }
130                                 if govpn.IDsCache.Find(udpPktData) == nil {
131                                         log.Println("Invalid identity in handshake packet")
132                                         udpReady <- struct{}{}
133                                         continue
134                                 }
135                                 if p := handshake.Client(id, conn, key, udpPktData); p != nil {
136                                         log.Println("Handshake completed")
137                                         if firstUpCall {
138                                                 go govpn.ScriptCall(*upPath, *ifaceName)
139                                                 firstUpCall = false
140                                         }
141                                         peer = p
142                                         handshake.Zero()
143                                         handshake = nil
144                                 }
145                                 udpReady <- struct{}{}
146                                 continue
147                         }
148                         if peer == nil {
149                                 udpReady <- struct{}{}
150                                 continue
151                         }
152                         if peer.UDPProcess(udpPktData, tap, udpReady) {
153                                 timeouts = 0
154                         }
155                 }
156         }
157         govpn.ScriptCall(*downPath, *ifaceName)
158 }