]> Cypherpunks.ru repositories - govpn.git/blob - cmd/govpn-client/main.go
Merge branch 'develop'
[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 = nil
97                         handshake = govpn.HandshakeStart(conn, remote, id, key)
98                         log.Println("Rehandshaking")
99                 }
100                 select {
101                 case <-termSignal:
102                         break MainCycle
103                 case ethPkt = <-ethSink:
104                         if peer == nil {
105                                 if len(ethPkt) > 0 {
106                                         ethReady <- struct{}{}
107                                 }
108                                 continue
109                         }
110                         peer.EthProcess(ethPkt, conn, ethReady)
111                 case udpPkt = <-udpSink:
112                         timeouts++
113                         if timeouts >= timeout {
114                                 break MainCycle
115                         }
116                         if udpPkt == nil {
117                                 udpReady <- struct{}{}
118                                 continue
119                         }
120
121                         udpPktData = udpBuf[:udpPkt.Size]
122                         if govpn.IsValidHandshakePkt(udpPktData) {
123                                 if udpPkt.Addr.String() != remote.String() {
124                                         udpReady <- struct{}{}
125                                         log.Println("Unknown handshake message")
126                                         continue
127                                 }
128                                 if p := handshake.Client(conn, key, udpPktData); p != nil {
129                                         log.Println("Handshake completed")
130                                         if firstUpCall {
131                                                 go govpn.ScriptCall(*upPath, *ifaceName)
132                                                 firstUpCall = false
133                                         }
134                                         peer = p
135                                         handshake = nil
136                                 }
137                                 udpReady <- struct{}{}
138                                 continue
139                         }
140                         if peer == nil {
141                                 udpReady <- struct{}{}
142                                 continue
143                         }
144                         if peer.UDPProcess(udpPktData, tap, udpReady) {
145                                 timeouts = 0
146                         }
147                 }
148         }
149         govpn.ScriptCall(*downPath, *ifaceName)
150 }