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.
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 authentication key 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/s out traffic rate")
52 log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
55 govpn.Timeout = time.Second * time.Duration(timeout)
56 govpn.Noncediff = *nonceDiff
57 govpn.NoiseEnable = *noisy
60 id := govpn.IDDecode(*IDRaw)
61 govpn.PeersInitDummy(id)
62 key := govpn.KeyRead(*keyPath)
64 panic("ID is not specified")
67 bind, err := net.ResolveUDPAddr("udp", "0.0.0.0:0")
71 conn, err := net.ListenUDP("udp", bind)
75 remote, err := net.ResolveUDPAddr("udp", *remoteAddr)
80 tap, ethSink, ethReady, _, err := govpn.TAPListen(*ifaceName)
84 udpSink, udpBuf, udpReady := govpn.ConnListen(conn)
90 var udpPkt govpn.UDPPkt
92 knownPeers := govpn.KnownPeers(map[string]**govpn.Peer{remote.String(): &peer})
94 log.Println(govpn.VersionGet())
95 log.Println("Max MTU on TAP interface:", govpn.TAPMaxMTU())
97 log.Println("Stats are going to listen on", *stats)
98 statsPort, err := net.Listen("tcp", *stats)
102 go govpn.StatsProcessor(statsPort, &knownPeers)
105 termSignal := make(chan os.Signal, 1)
106 signal.Notify(termSignal, os.Interrupt, os.Kill)
108 log.Println("Starting handshake")
109 handshake := govpn.HandshakeStart(conn, remote, id, key)
113 if peer != nil && (peer.BytesIn+peer.BytesOut) > govpn.MaxBytesPerKey {
116 handshake = govpn.HandshakeStart(conn, remote, id, key)
117 log.Println("Rehandshaking")
122 case ethPkt = <-ethSink:
125 ethReady <- struct{}{}
129 peer.EthProcess(ethPkt, conn, ethReady)
130 case udpPkt = <-udpSink:
132 if timeouts >= timeout {
135 if udpPkt.Addr == nil {
136 udpReady <- struct{}{}
140 udpPktData = udpBuf[:udpPkt.Size]
142 if udpPkt.Addr.String() != remote.String() {
143 udpReady <- struct{}{}
144 log.Println("Unknown handshake message")
147 if govpn.IDsCache.Find(udpPktData) == nil {
148 log.Println("Invalid identity in handshake packet")
149 udpReady <- struct{}{}
152 if p := handshake.Client(id, conn, key, udpPktData); p != nil {
153 log.Println("Handshake completed")
155 go govpn.ScriptCall(*upPath, *ifaceName)
162 udpReady <- struct{}{}
166 udpReady <- struct{}{}
169 if peer.UDPProcess(udpPktData, tap, udpReady) {
174 govpn.ScriptCall(*downPath, *ifaceName)