]> Cypherpunks.ru repositories - govpn.git/blob - src/govpn/cmd/govpn-client/main.go
Do not check nonce against buckets in TCP mode
[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         proto      = flag.String("proto", "udp", "Protocol to use: udp or tcp")
36         ifaceName  = flag.String("iface", "tap0", "TAP network interface")
37         IDRaw      = flag.String("id", "", "Client identification")
38         keyPath    = flag.String("key", "", "Path to passphrase file")
39         upPath     = flag.String("up", "", "Path to up-script")
40         downPath   = flag.String("down", "", "Path to down-script")
41         stats      = flag.String("stats", "", "Enable stats retrieving on host:port")
42         proxyAddr  = flag.String("proxy", "", "Use HTTP proxy on host:port")
43         proxyAuth  = flag.String("proxy-auth", "", "user:password Basic proxy auth")
44         mtu        = flag.Int("mtu", 1452, "MTU for outgoing packets")
45         timeoutP   = flag.Int("timeout", 60, "Timeout seconds")
46         noisy      = flag.Bool("noise", false, "Enable noise appending")
47         cpr        = flag.Int("cpr", 0, "Enable constant KiB/sec out traffic rate")
48         egdPath    = flag.String("egd", "", "Optional path to EGD socket")
49 )
50
51 func main() {
52         flag.Parse()
53         timeout := *timeoutP
54         var err error
55         log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
56
57         govpn.MTU = *mtu
58
59         id, err := govpn.IDDecode(*IDRaw)
60         if err != nil {
61                 log.Fatalln(err)
62         }
63
64         if *egdPath != "" {
65                 log.Println("Using", *egdPath, "EGD")
66                 govpn.EGDInit(*egdPath)
67         }
68
69         pub, priv := govpn.NewVerifier(id, govpn.StringFromFile(*keyPath))
70         conf := &govpn.PeerConf{
71                 Id:          id,
72                 Timeout:     time.Second * time.Duration(timeout),
73                 NoiseEnable: *noisy,
74                 CPR:         *cpr,
75                 DSAPub:      pub,
76                 DSAPriv:     priv,
77         }
78         govpn.PeersInitDummy(id, conf)
79
80         var conn govpn.RemoteConn
81         var sink chan []byte
82         var ready chan struct{}
83         switch *proto {
84         case "udp":
85                 conn, sink, ready = startUDP()
86         case "tcp":
87                 if *proxyAddr != "" {
88                         conn, sink, ready = proxyTCP()
89                 } else {
90                         conn, sink, ready = startTCP()
91                 }
92         default:
93                 log.Fatalln("Unknown protocol specified")
94         }
95
96         tap, ethSink, ethReady, _, err := govpn.TAPListen(
97                 *ifaceName,
98                 time.Second*time.Duration(timeout),
99                 *cpr,
100         )
101         if err != nil {
102                 log.Fatalln("Can not listen on TAP interface:", err)
103         }
104
105         timeouts := 0
106         firstUpCall := true
107         var peer *govpn.Peer
108         var ethPkt []byte
109         var pkt []byte
110         knownPeers := govpn.KnownPeers(map[string]**govpn.Peer{*remoteAddr: &peer})
111
112         log.Println(govpn.VersionGet())
113         log.Println("Connected to", *proto, *remoteAddr)
114         log.Println("Max MTU on TAP interface:", govpn.TAPMaxMTU())
115         if *stats != "" {
116                 log.Println("Stats are going to listen on", *stats)
117                 statsPort, err := net.Listen("tcp", *stats)
118                 if err != nil {
119                         log.Fatalln("Can not listen on stats port:", err)
120                 }
121                 go govpn.StatsProcessor(statsPort, &knownPeers)
122         }
123
124         termSignal := make(chan os.Signal, 1)
125         signal.Notify(termSignal, os.Interrupt, os.Kill)
126
127         log.Println("Starting handshake")
128         handshake := govpn.HandshakeStart(*remoteAddr, conn, conf)
129
130 MainCycle:
131         for {
132                 if peer != nil && (peer.BytesIn+peer.BytesOut) > govpn.MaxBytesPerKey {
133                         peer.Zero()
134                         peer = nil
135                         handshake = govpn.HandshakeStart(*remoteAddr, conn, conf)
136                         log.Println("Rehandshaking")
137                 }
138                 select {
139                 case <-termSignal:
140                         break MainCycle
141                 case ethPkt = <-ethSink:
142                         if peer == nil {
143                                 if len(ethPkt) > 0 {
144                                         ethReady <- struct{}{}
145                                 }
146                                 continue
147                         }
148                         peer.EthProcess(ethPkt, ethReady)
149                 case pkt = <-sink:
150                         timeouts++
151                         if timeouts >= timeout {
152                                 break MainCycle
153                         }
154                         if pkt == nil {
155                                 ready <- struct{}{}
156                                 continue
157                         }
158
159                         if peer == nil {
160                                 if govpn.IDsCache.Find(pkt) == nil {
161                                         log.Println("Invalid identity in handshake packet")
162                                         ready <- struct{}{}
163                                         continue
164                                 }
165                                 if p := handshake.Client(pkt); p != nil {
166                                         log.Println("Handshake completed")
167                                         if firstUpCall {
168                                                 go govpn.ScriptCall(*upPath, *ifaceName)
169                                                 firstUpCall = false
170                                         }
171                                         peer = p
172                                         handshake.Zero()
173                                         handshake = nil
174                                 }
175                                 ready <- struct{}{}
176                                 continue
177                         }
178                         if peer == nil {
179                                 ready <- struct{}{}
180                                 continue
181                         }
182                         if peer.PktProcess(pkt, tap, ready) {
183                                 timeouts = 0
184                         }
185                 }
186         }
187         govpn.ScriptCall(*downPath, *ifaceName)
188 }