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 mtu = flag.Int("mtu", 1500, "MTU")
40 nonceDiff = flag.Int("noncediff", 1, "Allow nonce difference")
41 timeoutP = flag.Int("timeout", 60, "Timeout seconds")
48 log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
51 govpn.Timeout = timeout
52 govpn.Noncediff = *nonceDiff
54 id := govpn.IDDecode(*IDRaw)
55 key := govpn.KeyRead(*keyPath)
57 panic("ID is not specified")
60 bind, err := net.ResolveUDPAddr("udp", "0.0.0.0:0")
64 conn, err := net.ListenUDP("udp", bind)
68 remote, err := net.ResolveUDPAddr("udp", *remoteAddr)
73 tap, ethSink, ethReady, _, err := govpn.TAPListen(*ifaceName)
77 udpSink, udpBuf, udpReady := govpn.ConnListen(conn)
83 var udpPkt *govpn.UDPPkt
86 termSignal := make(chan os.Signal, 1)
87 signal.Notify(termSignal, os.Interrupt, os.Kill)
89 log.Println(govpn.VersionGet())
90 log.Println("Starting handshake")
91 handshake := govpn.HandshakeStart(conn, remote, id, key)
95 if peer != nil && peer.Bytes > govpn.MaxBytesPerKey {
98 handshake = govpn.HandshakeStart(conn, remote, id, key)
99 log.Println("Rehandshaking")
104 case ethPkt = <-ethSink:
107 ethReady <- struct{}{}
111 peer.EthProcess(ethPkt, conn, ethReady)
112 case udpPkt = <-udpSink:
114 if timeouts >= timeout {
118 udpReady <- struct{}{}
122 udpPktData = udpBuf[:udpPkt.Size]
123 if govpn.IsValidHandshakePkt(udpPktData) {
124 if udpPkt.Addr.String() != remote.String() {
125 udpReady <- struct{}{}
126 log.Println("Unknown handshake message")
129 if p := handshake.Client(conn, key, udpPktData); p != nil {
130 log.Println("Handshake completed")
132 go govpn.ScriptCall(*upPath, *ifaceName)
139 udpReady <- struct{}{}
143 udpReady <- struct{}{}
146 if peer.UDPProcess(udpPktData, tap, udpReady) {
151 govpn.ScriptCall(*downPath, *ifaceName)