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, DPI/censorship-resistant free software VPN daemon.
34 bindAddr = flag.String("bind", "[::]:1194", "Bind to address")
35 proto = flag.String("proto", "udp", "Protocol to use: udp, tcp or all")
36 confPath = flag.String("conf", "peers.json", "Path to configuration JSON")
37 stats = flag.String("stats", "", "Enable stats retrieving on host:port")
38 proxy = flag.String("proxy", "", "Enable HTTP proxy on host:port")
39 mtu = flag.Int("mtu", 1452, "MTU for outgoing packets")
40 egdPath = flag.String("egd", "", "Optional path to EGD socket")
45 timeout := time.Second * time.Duration(govpn.TimeoutDefault)
46 log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
47 log.Println(govpn.VersionGet())
51 knownPeers = govpn.KnownPeers(make(map[string]**govpn.Peer))
54 log.Println("Using", *egdPath, "EGD")
55 govpn.EGDInit(*egdPath)
67 log.Fatalln("Unknown protocol specified")
70 termSignal := make(chan os.Signal, 1)
71 signal.Notify(termSignal, os.Interrupt, os.Kill)
73 hsHeartbeat := time.Tick(timeout)
74 go func() { <-hsHeartbeat }()
76 log.Println("Max MTU on TAP interface:", govpn.TAPMaxMTU())
78 log.Println("Stats are going to listen on", *stats)
79 statsPort, err := net.Listen("tcp", *stats)
81 log.Fatalln("Can not listen on stats port:", err)
83 go govpn.StatsProcessor(statsPort, &knownPeers)
88 log.Println("Server started")
90 var needsDeletion bool
99 for addr, hs := range handshakes {
100 if hs.LastPing.Add(timeout).Before(now) {
101 log.Println("Deleting handshake state", addr)
103 delete(handshakes, addr)
109 for addr, ps := range peers {
111 needsDeletion = ps.peer.LastPing.Add(timeout).Before(now)
112 ps.peer.BusyR.Unlock()
114 log.Println("Deleting peer", ps.peer)
116 delete(knownPeers, addr)
117 delete(peersById, *ps.peer.Id)
119 confs[*ps.peer.Id].Down,
122 ps.terminator <- struct{}{}
127 peersByIdLock.Unlock()