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 proxyAddr = flag.String("proxy", "", "Use HTTP proxy on host:port")
44 proxyAuth = flag.String("proxy-auth", "", "user:password Basic proxy auth")
45 mtu = flag.Int("mtu", 1452, "MTU for outgoing packets")
46 timeoutP = flag.Int("timeout", 60, "Timeout seconds")
47 noisy = flag.Bool("noise", false, "Enable noise appending")
48 cpr = flag.Int("cpr", 0, "Enable constant KiB/sec out traffic rate")
49 egdPath = flag.String("egd", "", "Optional path to EGD socket")
56 log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
60 id, err := govpn.IDDecode(*IDRaw)
66 log.Println("Using", *egdPath, "EGD")
67 govpn.EGDInit(*egdPath)
70 pub, priv := govpn.NewVerifier(id, govpn.StringFromFile(*keyPath))
71 conf := &govpn.PeerConf{
73 Timeout: time.Second * time.Duration(timeout),
79 govpn.PeersInitDummy(id, conf)
83 var ready chan struct{}
86 conn, sink, ready = startUDP()
89 conn, sink, ready = proxyTCP()
91 conn, sink, ready = startTCP()
94 log.Fatalln("Unknown protocol specified")
97 tap, ethSink, ethReady, _, err := govpn.TAPListen(
99 time.Second*time.Duration(timeout),
103 log.Fatalln("Can not listen on TAP interface:", err)
111 knownPeers := govpn.KnownPeers(map[string]**govpn.Peer{*remoteAddr: &peer})
113 log.Println(govpn.VersionGet())
114 log.Println("Connected to", *proto, *remoteAddr)
115 log.Println("Max MTU on TAP interface:", govpn.TAPMaxMTU())
117 log.Println("Stats are going to listen on", *stats)
118 statsPort, err := net.Listen("tcp", *stats)
120 log.Fatalln("Can not listen on stats port:", err)
122 go govpn.StatsProcessor(statsPort, &knownPeers)
125 termSignal := make(chan os.Signal, 1)
126 signal.Notify(termSignal, os.Interrupt, os.Kill)
128 log.Println("Starting handshake")
129 handshake := govpn.HandshakeStart(*remoteAddr, conn, conf)
133 if peer != nil && (peer.BytesIn+peer.BytesOut) > govpn.MaxBytesPerKey {
136 handshake = govpn.HandshakeStart(*remoteAddr, conn, conf)
137 log.Println("Rehandshaking")
142 case ethPkt = <-ethSink:
145 ethReady <- struct{}{}
149 peer.EthProcess(ethPkt, ethReady)
152 if timeouts >= timeout {
161 if govpn.IDsCache.Find(pkt) == nil {
162 log.Println("Invalid identity in handshake packet")
166 if p := handshake.Client(pkt); p != nil {
167 log.Println("Handshake completed")
169 go govpn.ScriptCall(*upPath, *ifaceName)
183 if peer.PktProcess(pkt, tap, ready) {
188 govpn.ScriptCall(*downPath, *ifaceName)