2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2016 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.
31 "cypherpunks.ru/govpn"
35 bindAddr = flag.String("bind", "[::]:1194", "Bind to address")
36 proto = flag.String("proto", "udp", "Protocol to use: udp, tcp or all")
37 confPath = flag.String("conf", "peers.yaml", "Path to configuration YAML")
38 stats = flag.String("stats", "", "Enable stats retrieving on host:port")
39 proxy = flag.String("proxy", "", "Enable HTTP proxy on host:port")
40 egdPath = flag.String("egd", "", "Optional path to EGD socket")
41 warranty = flag.Bool("warranty", false, "Print warranty information")
47 fmt.Println(govpn.Warranty)
50 timeout := time.Second * time.Duration(govpn.TimeoutDefault)
51 log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
52 log.Println(govpn.VersionGet())
55 knownPeers = govpn.KnownPeers(make(map[string]**govpn.Peer))
58 log.Println("Using", *egdPath, "EGD")
59 govpn.EGDInit(*egdPath)
71 log.Fatalln("Unknown protocol specified")
74 termSignal := make(chan os.Signal, 1)
75 signal.Notify(termSignal, os.Interrupt, os.Kill)
77 hsHeartbeat := time.Tick(timeout)
78 go func() { <-hsHeartbeat }()
81 log.Println("Stats are going to listen on", *stats)
82 statsPort, err := net.Listen("tcp", *stats)
84 log.Fatalln("Can not listen on stats port:", err)
86 go govpn.StatsProcessor(statsPort, &knownPeers)
91 log.Println("Server started")
93 var needsDeletion bool
98 log.Println("Terminating")
99 for _, ps := range peers {
101 confs[*ps.peer.Id].Down,
110 for addr, hs := range handshakes {
111 if hs.LastPing.Add(timeout).Before(now) {
112 log.Println("Deleting handshake state", addr)
114 delete(handshakes, addr)
120 for addr, ps := range peers {
122 needsDeletion = ps.peer.LastPing.Add(timeout).Before(now)
123 ps.peer.BusyR.Unlock()
125 log.Println("Deleting peer", ps.peer)
127 delete(knownPeers, addr)
128 delete(peersById, *ps.peer.Id)
130 confs[*ps.peer.Id].Down,
134 ps.terminator <- struct{}{}
139 peersByIdLock.Unlock()