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