2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2015 Sergey Matveev <stargrave@stargrave.org>
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.
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.
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/>.
19 // Simple secure free software virtual private network daemon client.
35 remoteAddr = flag.String("remote", "", "Remote server address")
36 proto = flag.String("proto", "udp", "Protocol to use: udp or tcp")
37 ifaceName = flag.String("iface", "tap0", "TAP network interface")
38 IDRaw = flag.String("id", "", "Client identification")
39 keyPath = flag.String("key", "", "Path to passphrase file")
40 upPath = flag.String("up", "", "Path to up-script")
41 downPath = flag.String("down", "", "Path to down-script")
42 stats = flag.String("stats", "", "Enable stats retrieving on host:port")
43 mtu = flag.Int("mtu", 1452, "MTU for outgoing packets")
44 timeoutP = flag.Int("timeout", 60, "Timeout seconds")
45 noisy = flag.Bool("noise", false, "Enable noise appending")
46 cpr = flag.Int("cpr", 0, "Enable constant KiB/sec out traffic rate")
47 egdPath = flag.String("egd", "", "Optional path to EGD socket")
54 log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
58 id, err := govpn.IDDecode(*IDRaw)
64 log.Println("Using", *egdPath, "EGD")
65 govpn.EGDInit(*egdPath)
68 pub, priv := govpn.NewVerifier(id, govpn.StringFromFile(*keyPath))
69 conf := &govpn.PeerConf{
71 Timeout: time.Second * time.Duration(timeout),
77 govpn.PeersInitDummy(id, conf)
81 var ready chan struct{}
84 conn, sink, ready = startUDP()
86 conn, sink, ready = startTCP()
88 log.Fatalln("Unknown protocol specified")
91 tap, ethSink, ethReady, _, err := govpn.TAPListen(
93 time.Second*time.Duration(timeout),
97 log.Fatalln("Can not listen on TAP interface:", err)
105 knownPeers := govpn.KnownPeers(map[string]**govpn.Peer{*remoteAddr: &peer})
107 log.Println(govpn.VersionGet())
108 log.Println("Connected to", *proto, *remoteAddr)
109 log.Println("Max MTU on TAP interface:", govpn.TAPMaxMTU())
111 log.Println("Stats are going to listen on", *stats)
112 statsPort, err := net.Listen("tcp", *stats)
114 log.Fatalln("Can not listen on stats port:", err)
116 go govpn.StatsProcessor(statsPort, &knownPeers)
119 termSignal := make(chan os.Signal, 1)
120 signal.Notify(termSignal, os.Interrupt, os.Kill)
122 log.Println("Starting handshake")
123 handshake := govpn.HandshakeStart(*remoteAddr, conn, conf)
127 if peer != nil && (peer.BytesIn+peer.BytesOut) > govpn.MaxBytesPerKey {
130 handshake = govpn.HandshakeStart(*remoteAddr, conn, conf)
131 log.Println("Rehandshaking")
136 case ethPkt = <-ethSink:
139 ethReady <- struct{}{}
143 peer.EthProcess(ethPkt, ethReady)
146 if timeouts >= timeout {
155 if govpn.IDsCache.Find(pkt) == nil {
156 log.Println("Invalid identity in handshake packet")
160 if p := handshake.Client(pkt); p != nil {
161 log.Println("Handshake completed")
163 go govpn.ScriptCall(*upPath, *ifaceName)
177 if peer.PktProcess(pkt, tap, ready) {
182 govpn.ScriptCall(*downPath, *ifaceName)