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