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.
33 remoteAddr = flag.String("remote", "", "Remote server address")
34 ifaceName = flag.String("iface", "tap0", "TAP network interface")
35 IDRaw = flag.String("id", "", "Client identification")
36 keyPath = flag.String("key", "", "Path to authentication key file")
37 upPath = flag.String("up", "", "Path to up-script")
38 downPath = flag.String("down", "", "Path to down-script")
39 stats = flag.String("stats", "", "Enable stats retrieving on host:port")
40 mtu = flag.Int("mtu", 1500, "MTU")
41 nonceDiff = flag.Int("noncediff", 1, "Allow nonce difference")
42 timeoutP = flag.Int("timeout", 60, "Timeout seconds")
49 log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
52 govpn.Timeout = timeout
53 govpn.Noncediff = *nonceDiff
55 id := govpn.IDDecode(*IDRaw)
56 govpn.PeersInitDummy(id)
57 key := govpn.KeyRead(*keyPath)
59 panic("ID is not specified")
62 bind, err := net.ResolveUDPAddr("udp", "0.0.0.0:0")
66 conn, err := net.ListenUDP("udp", bind)
70 remote, err := net.ResolveUDPAddr("udp", *remoteAddr)
75 tap, ethSink, ethReady, _, err := govpn.TAPListen(*ifaceName)
79 udpSink, udpBuf, udpReady := govpn.ConnListen(conn)
85 var udpPkt *govpn.UDPPkt
87 knownPeers := govpn.KnownPeers(map[string]**govpn.Peer{remote.String(): &peer})
89 log.Println(govpn.VersionGet())
91 log.Println("Stats are going to listen on", *stats)
92 statsPort, err := net.Listen("tcp", *stats)
96 go govpn.StatsProcessor(statsPort, &knownPeers)
99 termSignal := make(chan os.Signal, 1)
100 signal.Notify(termSignal, os.Interrupt, os.Kill)
102 log.Println("Starting handshake")
103 handshake := govpn.HandshakeStart(conn, remote, id, key)
107 if peer != nil && (peer.BytesIn+peer.BytesOut) > govpn.MaxBytesPerKey {
110 handshake = govpn.HandshakeStart(conn, remote, id, key)
111 log.Println("Rehandshaking")
116 case ethPkt = <-ethSink:
119 ethReady <- struct{}{}
123 peer.EthProcess(ethPkt, conn, ethReady)
124 case udpPkt = <-udpSink:
126 if timeouts >= timeout {
130 udpReady <- struct{}{}
134 udpPktData = udpBuf[:udpPkt.Size]
136 if udpPkt.Addr.String() != remote.String() {
137 udpReady <- struct{}{}
138 log.Println("Unknown handshake message")
141 if govpn.IDsCache.Find(udpPktData) == nil {
142 log.Println("Invalid identity in handshake packet")
143 udpReady <- struct{}{}
146 if p := handshake.Client(id, conn, key, udpPktData); p != nil {
147 log.Println("Handshake completed")
149 go govpn.ScriptCall(*upPath, *ifaceName)
156 udpReady <- struct{}{}
160 udpReady <- struct{}{}
163 if peer.UDPProcess(udpPktData, tap, udpReady) {
168 govpn.ScriptCall(*downPath, *ifaceName)