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