2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2019 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, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 // Simple secure, DPI/censorship-resistant free software VPN daemon.
30 "go.cypherpunks.ru/govpn/v7"
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.yaml", "Path to configuration YAML")
37 stats = flag.String("stats", "", "Enable stats retrieving on host:port")
38 proxy = flag.String("proxy", "", "Enable HTTP proxy on host:port")
39 egdPath = flag.String("egd", "", "Optional path to EGD socket")
40 syslog = flag.Bool("syslog", false, "Enable logging to syslog")
41 version = flag.Bool("version", false, "Print version information")
42 warranty = flag.Bool("warranty", false, "Print warranty information")
48 fmt.Println(govpn.Warranty)
52 fmt.Println(govpn.VersionGet())
55 timeout := time.Second * time.Duration(govpn.TimeoutDefault)
56 log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
57 log.Println(govpn.VersionGet())
62 log.Println("Using", *egdPath, "EGD")
63 govpn.EGDInit(*egdPath)
79 log.Fatalln("Unknown protocol specified")
82 termSignal := make(chan os.Signal, 1)
83 signal.Notify(termSignal, os.Interrupt, os.Kill)
85 hsHeartbeat := time.Tick(timeout)
86 go func() { <-hsHeartbeat }()
89 log.Println("Stats are going to listen on", *stats)
90 statsPort, err := net.Listen("tcp", *stats)
92 log.Fatalln("Can not listen on stats port:", err)
94 go govpn.StatsProcessor(statsPort, &knownPeers)
99 govpn.BothPrintf(`[started bind="%s"]`, *bindAddr)
101 var needsDeletion bool
106 govpn.BothPrintf(`[terminating bind="%s"]`, *bindAddr)
107 peers.Range(func(_, psI interface{}) bool {
108 ps := psI.(*PeerState)
110 confs[*ps.peer.ID].Down,
120 handshakes.Range(func(addrI, hsI interface{}) bool {
121 addr := addrI.(string)
122 hs := hsI.(*govpn.Handshake)
123 if hs.LastPing.Add(timeout).Before(now) {
124 govpn.Printf(`[handshake-delete bind="%s" addr="%s"]`, *bindAddr, addr)
126 handshakes.Delete(addr)
131 peers.Range(func(addrI, psI interface{}) bool {
132 addr := addrI.(string)
133 ps := psI.(*PeerState)
135 needsDeletion = ps.peer.LastPing.Add(timeout).Before(now)
136 ps.peer.BusyR.Unlock()
139 `[peer-delete bind="%s" peer="%s"]`,
144 knownPeers.Delete(addr)
145 peersByID.Delete(*ps.peer.ID)
147 confs[*ps.peer.ID].Down,
151 ps.terminator <- struct{}{}