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