]> Cypherpunks.ru repositories - govpn.git/blob - src/govpn/cmd/govpn-client/main.go
Replace -noncediff with the hash keeping up to 256 seen nonces
[govpn.git] / src / govpn / 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 client.
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 passphrase 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         timeoutP   = flag.Int("timeout", 60, "Timeout seconds")
43         noisy      = flag.Bool("noise", false, "Enable noise appending")
44         cpr        = flag.Int("cpr", 0, "Enable constant KiB/sec out traffic rate")
45         egdPath    = flag.String("egd", "", "Optional path to EGD socket")
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
56         id, err := govpn.IDDecode(*IDRaw)
57         if err != nil {
58                 log.Fatalln(err)
59         }
60
61         if *egdPath != "" {
62                 log.Println("Using", *egdPath, "EGD")
63                 govpn.EGDInit(*egdPath)
64         }
65
66         pub, priv := govpn.NewVerifier(id, govpn.StringFromFile(*keyPath))
67         conf := &govpn.PeerConf{
68                 Id:          id,
69                 Timeout:     time.Second * time.Duration(timeout),
70                 NoiseEnable: *noisy,
71                 CPR:         *cpr,
72                 DSAPub:      pub,
73                 DSAPriv:     priv,
74         }
75         govpn.PeersInitDummy(id, conf)
76
77         bind, err := net.ResolveUDPAddr("udp", "0.0.0.0:0")
78         if err != nil {
79                 log.Fatalln("Can not resolve address:", err)
80         }
81         conn, err := net.ListenUDP("udp", bind)
82         if err != nil {
83                 log.Fatalln("Can not listen on UDP:", err)
84         }
85         remote, err := net.ResolveUDPAddr("udp", *remoteAddr)
86         if err != nil {
87                 log.Fatalln("Can not resolve remote address:", err)
88         }
89
90         tap, ethSink, ethReady, _, err := govpn.TAPListen(
91                 *ifaceName,
92                 time.Second*time.Duration(timeout),
93                 *cpr,
94         )
95         if err != nil {
96                 log.Fatalln("Can not listen on TAP interface:", err)
97         }
98         udpSink, udpBuf, udpReady := govpn.ConnListen(conn)
99
100         timeouts := 0
101         firstUpCall := true
102         var peer *govpn.Peer
103         var ethPkt []byte
104         var udpPkt govpn.UDPPkt
105         var udpPktData []byte
106         knownPeers := govpn.KnownPeers(map[string]**govpn.Peer{remote.String(): &peer})
107
108         log.Println(govpn.VersionGet())
109         log.Println("Max MTU on TAP interface:", govpn.TAPMaxMTU())
110         if *stats != "" {
111                 log.Println("Stats are going to listen on", *stats)
112                 statsPort, err := net.Listen("tcp", *stats)
113                 if err != nil {
114                         log.Fatalln("Can not listen on stats port:", err)
115                 }
116                 go govpn.StatsProcessor(statsPort, &knownPeers)
117         }
118
119         termSignal := make(chan os.Signal, 1)
120         signal.Notify(termSignal, os.Interrupt, os.Kill)
121
122         log.Println("Starting handshake")
123         handshake := govpn.HandshakeStart(conf, conn, remote)
124
125 MainCycle:
126         for {
127                 if peer != nil && (peer.BytesIn+peer.BytesOut) > govpn.MaxBytesPerKey {
128                         peer.Zero()
129                         peer = nil
130                         handshake = govpn.HandshakeStart(conf, conn, remote)
131                         log.Println("Rehandshaking")
132                 }
133                 select {
134                 case <-termSignal:
135                         break MainCycle
136                 case ethPkt = <-ethSink:
137                         if peer == nil {
138                                 if len(ethPkt) > 0 {
139                                         ethReady <- struct{}{}
140                                 }
141                                 continue
142                         }
143                         peer.EthProcess(ethPkt, conn, ethReady)
144                 case udpPkt = <-udpSink:
145                         timeouts++
146                         if timeouts >= timeout {
147                                 break MainCycle
148                         }
149                         if udpPkt.Addr == nil {
150                                 udpReady <- struct{}{}
151                                 continue
152                         }
153
154                         udpPktData = udpBuf[:udpPkt.Size]
155                         if peer == nil {
156                                 if udpPkt.Addr.String() != remote.String() {
157                                         udpReady <- struct{}{}
158                                         log.Println("Unknown handshake message")
159                                         continue
160                                 }
161                                 if govpn.IDsCache.Find(udpPktData) == nil {
162                                         log.Println("Invalid identity in handshake packet")
163                                         udpReady <- struct{}{}
164                                         continue
165                                 }
166                                 if p := handshake.Client(conn, udpPktData); p != nil {
167                                         log.Println("Handshake completed")
168                                         if firstUpCall {
169                                                 go govpn.ScriptCall(*upPath, *ifaceName)
170                                                 firstUpCall = false
171                                         }
172                                         peer = p
173                                         handshake.Zero()
174                                         handshake = nil
175                                 }
176                                 udpReady <- struct{}{}
177                                 continue
178                         }
179                         if peer == nil {
180                                 udpReady <- struct{}{}
181                                 continue
182                         }
183                         if peer.UDPProcess(udpPktData, tap, udpReady) {
184                                 timeouts = 0
185                         }
186                 }
187         }
188         govpn.ScriptCall(*downPath, *ifaceName)
189 }