]> Cypherpunks.ru repositories - govpn.git/blob - src/cypherpunks.ru/govpn/cmd/govpn-server/main.go
Use convenient simpler Go 1.9's sync.Map
[govpn.git] / src / cypherpunks.ru / govpn / cmd / govpn-server / main.go
1 /*
2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2017 Sergey Matveev <stargrave@stargrave.org>
4
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.
9
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.
14
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/>.
17 */
18
19 // Simple secure, DPI/censorship-resistant free software VPN daemon.
20 package main
21
22 import (
23         "flag"
24         "fmt"
25         "log"
26         "net"
27         "os"
28         "os/signal"
29         "time"
30
31         "cypherpunks.ru/govpn"
32 )
33
34 var (
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         syslog   = flag.Bool("syslog", false, "Enable logging to syslog")
42         version  = flag.Bool("version", false, "Print version information")
43         warranty = flag.Bool("warranty", false, "Print warranty information")
44 )
45
46 func main() {
47         flag.Parse()
48         if *warranty {
49                 fmt.Println(govpn.Warranty)
50                 return
51         }
52         if *version {
53                 fmt.Println(govpn.VersionGet())
54                 return
55         }
56         timeout := time.Second * time.Duration(govpn.TimeoutDefault)
57         log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
58         log.Println(govpn.VersionGet())
59
60         confInit()
61
62         if *egdPath != "" {
63                 log.Println("Using", *egdPath, "EGD")
64                 govpn.EGDInit(*egdPath)
65         }
66
67         if *syslog {
68                 govpn.SyslogEnable()
69         }
70
71         switch *proto {
72         case "udp":
73                 startUDP()
74         case "tcp":
75                 startTCP()
76         case "all":
77                 startUDP()
78                 startTCP()
79         default:
80                 log.Fatalln("Unknown protocol specified")
81         }
82
83         termSignal := make(chan os.Signal, 1)
84         signal.Notify(termSignal, os.Interrupt, os.Kill)
85
86         hsHeartbeat := time.Tick(timeout)
87         go func() { <-hsHeartbeat }()
88
89         if *stats != "" {
90                 log.Println("Stats are going to listen on", *stats)
91                 statsPort, err := net.Listen("tcp", *stats)
92                 if err != nil {
93                         log.Fatalln("Can not listen on stats port:", err)
94                 }
95                 go govpn.StatsProcessor(statsPort, &knownPeers)
96         }
97         if *proxy != "" {
98                 go proxyStart()
99         }
100         govpn.BothPrintf(`[started bind="%s"]`, *bindAddr)
101
102         var needsDeletion bool
103 MainCycle:
104         for {
105                 select {
106                 case <-termSignal:
107                         govpn.BothPrintf(`[terminating bind="%s"]`, *bindAddr)
108                         peers.Range(func(_, psI interface{}) bool {
109                                 ps := psI.(*PeerState)
110                                 govpn.ScriptCall(
111                                         confs[*ps.peer.ID].Down,
112                                         ps.tap.Name,
113                                         ps.peer.Addr,
114                                 )
115                                 return true
116                         })
117                         break MainCycle
118                 case <-hsHeartbeat:
119                         now := time.Now()
120
121                         handshakes.Range(func(addrI, hsI interface{}) bool {
122                                 addr := addrI.(string)
123                                 hs := hsI.(*govpn.Handshake)
124                                 if hs.LastPing.Add(timeout).Before(now) {
125                                         govpn.Printf(`[handshake-delete bind="%s" addr="%s"]`, *bindAddr, addr)
126                                         hs.Zero()
127                                         handshakes.Delete(addr)
128                                 }
129                                 return true
130                         })
131
132                         peers.Range(func(addrI, psI interface{}) bool {
133                                 addr := addrI.(string)
134                                 ps := psI.(*PeerState)
135                                 ps.peer.BusyR.Lock()
136                                 needsDeletion = ps.peer.LastPing.Add(timeout).Before(now)
137                                 ps.peer.BusyR.Unlock()
138                                 if needsDeletion {
139                                         govpn.Printf(
140                                                 `[peer-delete bind="%s" peer="%s"]`,
141                                                 *bindAddr,
142                                                 ps.peer.ID.String(),
143                                         )
144                                         peers.Delete(addr)
145                                         knownPeers.Delete(addr)
146                                         peersByID.Delete(*ps.peer.ID)
147                                         go govpn.ScriptCall(
148                                                 confs[*ps.peer.ID].Down,
149                                                 ps.tap.Name,
150                                                 ps.peer.Addr,
151                                         )
152                                         ps.terminator <- struct{}{}
153                                 }
154                                 return true
155                         })
156                 }
157         }
158 }