]> Cypherpunks.ru repositories - govpn.git/blob - cmd/govpn-client/main.go
6b7fac1307e8ba576c08a8aa35b90d8b4ea77017
[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         stats      = flag.String("stats", "", "Enable stats retrieving on host:port")
40         mtu        = flag.Int("mtu", 1452, "MTU for outgoing packets")
41         nonceDiff  = flag.Int("noncediff", 1, "Allow nonce difference")
42         timeoutP   = flag.Int("timeout", 60, "Timeout seconds")
43 )
44
45 func main() {
46         flag.Parse()
47         timeout := *timeoutP
48         var err error
49         log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
50
51         govpn.MTU = *mtu
52         govpn.Timeout = timeout
53         govpn.Noncediff = *nonceDiff
54
55         id := govpn.IDDecode(*IDRaw)
56         govpn.PeersInitDummy(id)
57         key := govpn.KeyRead(*keyPath)
58         if id == nil {
59                 panic("ID is not specified")
60         }
61
62         bind, err := net.ResolveUDPAddr("udp", "0.0.0.0:0")
63         if err != nil {
64                 panic(err)
65         }
66         conn, err := net.ListenUDP("udp", bind)
67         if err != nil {
68                 panic(err)
69         }
70         remote, err := net.ResolveUDPAddr("udp", *remoteAddr)
71         if err != nil {
72                 panic(err)
73         }
74
75         tap, ethSink, ethReady, _, err := govpn.TAPListen(*ifaceName)
76         if err != nil {
77                 panic(err)
78         }
79         udpSink, udpBuf, udpReady := govpn.ConnListen(conn)
80
81         timeouts := 0
82         firstUpCall := true
83         var peer *govpn.Peer
84         var ethPkt []byte
85         var udpPkt govpn.UDPPkt
86         var udpPktData []byte
87         knownPeers := govpn.KnownPeers(map[string]**govpn.Peer{remote.String(): &peer})
88
89         log.Println(govpn.VersionGet())
90         log.Println("Max MTU on TAP interface:", govpn.TAPMaxMTU())
91         if *stats != "" {
92                 log.Println("Stats are going to listen on", *stats)
93                 statsPort, err := net.Listen("tcp", *stats)
94                 if err != nil {
95                         panic(err)
96                 }
97                 go govpn.StatsProcessor(statsPort, &knownPeers)
98         }
99
100         termSignal := make(chan os.Signal, 1)
101         signal.Notify(termSignal, os.Interrupt, os.Kill)
102
103         log.Println("Starting handshake")
104         handshake := govpn.HandshakeStart(conn, remote, id, key)
105
106 MainCycle:
107         for {
108                 if peer != nil && (peer.BytesIn+peer.BytesOut) > govpn.MaxBytesPerKey {
109                         peer.Zero()
110                         peer = nil
111                         handshake = govpn.HandshakeStart(conn, remote, id, key)
112                         log.Println("Rehandshaking")
113                 }
114                 select {
115                 case <-termSignal:
116                         break MainCycle
117                 case ethPkt = <-ethSink:
118                         if peer == nil {
119                                 if len(ethPkt) > 0 {
120                                         ethReady <- struct{}{}
121                                 }
122                                 continue
123                         }
124                         peer.EthProcess(ethPkt, conn, ethReady)
125                 case udpPkt = <-udpSink:
126                         timeouts++
127                         if timeouts >= timeout {
128                                 break MainCycle
129                         }
130                         if udpPkt.Addr == nil {
131                                 udpReady <- struct{}{}
132                                 continue
133                         }
134
135                         udpPktData = udpBuf[:udpPkt.Size]
136                         if peer == nil {
137                                 if udpPkt.Addr.String() != remote.String() {
138                                         udpReady <- struct{}{}
139                                         log.Println("Unknown handshake message")
140                                         continue
141                                 }
142                                 if govpn.IDsCache.Find(udpPktData) == nil {
143                                         log.Println("Invalid identity in handshake packet")
144                                         udpReady <- struct{}{}
145                                         continue
146                                 }
147                                 if p := handshake.Client(id, conn, key, udpPktData); p != nil {
148                                         log.Println("Handshake completed")
149                                         if firstUpCall {
150                                                 go govpn.ScriptCall(*upPath, *ifaceName)
151                                                 firstUpCall = false
152                                         }
153                                         peer = p
154                                         handshake.Zero()
155                                         handshake = nil
156                                 }
157                                 udpReady <- struct{}{}
158                                 continue
159                         }
160                         if peer == nil {
161                                 udpReady <- struct{}{}
162                                 continue
163                         }
164                         if peer.UDPProcess(udpPktData, tap, udpReady) {
165                                 timeouts = 0
166                         }
167                 }
168         }
169         govpn.ScriptCall(*downPath, *ifaceName)
170 }