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 govpn.PeersInitDummy(id)
56 key := govpn.KeyRead(*keyPath)
58 panic("ID is not specified")
61 bind, err := net.ResolveUDPAddr("udp", "0.0.0.0:0")
65 conn, err := net.ListenUDP("udp", bind)
69 remote, err := net.ResolveUDPAddr("udp", *remoteAddr)
74 tap, ethSink, ethReady, _, err := govpn.TAPListen(*ifaceName)
78 udpSink, udpBuf, udpReady := govpn.ConnListen(conn)
84 var udpPkt *govpn.UDPPkt
87 termSignal := make(chan os.Signal, 1)
88 signal.Notify(termSignal, os.Interrupt, os.Kill)
90 log.Println(govpn.VersionGet())
91 log.Println("Starting handshake")
92 handshake := govpn.HandshakeStart(conn, remote, id, key)
96 if peer != nil && (peer.BytesIn+peer.BytesOut) > govpn.MaxBytesPerKey {
99 handshake = govpn.HandshakeStart(conn, remote, id, key)
100 log.Println("Rehandshaking")
105 case ethPkt = <-ethSink:
108 ethReady <- struct{}{}
112 peer.EthProcess(ethPkt, conn, ethReady)
113 case udpPkt = <-udpSink:
115 if timeouts >= timeout {
119 udpReady <- struct{}{}
123 udpPktData = udpBuf[:udpPkt.Size]
125 if udpPkt.Addr.String() != remote.String() {
126 udpReady <- struct{}{}
127 log.Println("Unknown handshake message")
130 if govpn.IDsCache.Find(udpPktData) == nil {
131 log.Println("Invalid identity in handshake packet")
132 udpReady <- struct{}{}
135 if p := handshake.Client(id, conn, key, udpPktData); p != nil {
136 log.Println("Handshake completed")
138 go govpn.ScriptCall(*upPath, *ifaceName)
145 udpReady <- struct{}{}
149 udpReady <- struct{}{}
152 if peer.UDPProcess(udpPktData, tap, udpReady) {
157 govpn.ScriptCall(*downPath, *ifaceName)