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/sec out traffic rate")
52 log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
56 id := govpn.IDDecode(*IDRaw)
57 govpn.PeersInitDummy(id, govpn.PeerConf{
59 Timeout: time.Second * time.Duration(timeout),
60 Noncediff: *nonceDiff,
64 key := govpn.KeyRead(*keyPath)
66 panic("ID is not specified")
69 bind, err := net.ResolveUDPAddr("udp", "0.0.0.0:0")
73 conn, err := net.ListenUDP("udp", bind)
77 remote, err := net.ResolveUDPAddr("udp", *remoteAddr)
82 tap, ethSink, ethReady, _, err := govpn.TAPListen(
84 time.Second*time.Duration(timeout),
90 udpSink, udpBuf, udpReady := govpn.ConnListen(conn)
96 var udpPkt govpn.UDPPkt
98 knownPeers := govpn.KnownPeers(map[string]**govpn.Peer{remote.String(): &peer})
100 log.Println(govpn.VersionGet())
101 log.Println("Max MTU on TAP interface:", govpn.TAPMaxMTU())
103 log.Println("Stats are going to listen on", *stats)
104 statsPort, err := net.Listen("tcp", *stats)
108 go govpn.StatsProcessor(statsPort, &knownPeers)
111 termSignal := make(chan os.Signal, 1)
112 signal.Notify(termSignal, os.Interrupt, os.Kill)
114 log.Println("Starting handshake")
115 handshake := govpn.HandshakeStart(conn, remote, id, key)
119 if peer != nil && (peer.BytesIn+peer.BytesOut) > govpn.MaxBytesPerKey {
122 handshake = govpn.HandshakeStart(conn, remote, id, key)
123 log.Println("Rehandshaking")
128 case ethPkt = <-ethSink:
131 ethReady <- struct{}{}
135 peer.EthProcess(ethPkt, conn, ethReady)
136 case udpPkt = <-udpSink:
138 if timeouts >= timeout {
141 if udpPkt.Addr == nil {
142 udpReady <- struct{}{}
146 udpPktData = udpBuf[:udpPkt.Size]
148 if udpPkt.Addr.String() != remote.String() {
149 udpReady <- struct{}{}
150 log.Println("Unknown handshake message")
153 if govpn.IDsCache.Find(udpPktData) == nil {
154 log.Println("Invalid identity in handshake packet")
155 udpReady <- struct{}{}
158 if p := handshake.Client(id, conn, key, udpPktData); p != nil {
159 log.Println("Handshake completed")
161 go govpn.ScriptCall(*upPath, *ifaceName)
168 udpReady <- struct{}{}
172 udpReady <- struct{}{}
175 if peer.UDPProcess(udpPktData, tap, udpReady) {
180 govpn.ScriptCall(*downPath, *ifaceName)