]> Cypherpunks.ru repositories - govpn.git/blob - src/cypherpunks.ru/govpn/cmd/govpn-server/main.go
Execute all down script when server is terminated
[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-2016 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         "log"
25         "net"
26         "os"
27         "os/signal"
28         "time"
29
30         "cypherpunks.ru/govpn"
31 )
32
33 var (
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 )
41
42 func main() {
43         flag.Parse()
44         timeout := time.Second * time.Duration(govpn.TimeoutDefault)
45         log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
46         log.Println(govpn.VersionGet())
47
48         confInit()
49         knownPeers = govpn.KnownPeers(make(map[string]**govpn.Peer))
50
51         if *egdPath != "" {
52                 log.Println("Using", *egdPath, "EGD")
53                 govpn.EGDInit(*egdPath)
54         }
55
56         switch *proto {
57         case "udp":
58                 startUDP()
59         case "tcp":
60                 startTCP()
61         case "all":
62                 startUDP()
63                 startTCP()
64         default:
65                 log.Fatalln("Unknown protocol specified")
66         }
67
68         termSignal := make(chan os.Signal, 1)
69         signal.Notify(termSignal, os.Interrupt, os.Kill)
70
71         hsHeartbeat := time.Tick(timeout)
72         go func() { <-hsHeartbeat }()
73
74         if *stats != "" {
75                 log.Println("Stats are going to listen on", *stats)
76                 statsPort, err := net.Listen("tcp", *stats)
77                 if err != nil {
78                         log.Fatalln("Can not listen on stats port:", err)
79                 }
80                 go govpn.StatsProcessor(statsPort, &knownPeers)
81         }
82         if *proxy != "" {
83                 go proxyStart()
84         }
85         log.Println("Server started")
86
87         var needsDeletion bool
88 MainCycle:
89         for {
90                 select {
91                 case <-termSignal:
92                         log.Println("Terminating")
93                         for _, ps := range peers {
94                                 govpn.ScriptCall(
95                                         confs[*ps.peer.Id].Down,
96                                         ps.tap.Name,
97                                         ps.peer.Addr,
98                                 )
99                         }
100                         break MainCycle
101                 case <-hsHeartbeat:
102                         now := time.Now()
103                         hsLock.Lock()
104                         for addr, hs := range handshakes {
105                                 if hs.LastPing.Add(timeout).Before(now) {
106                                         log.Println("Deleting handshake state", addr)
107                                         hs.Zero()
108                                         delete(handshakes, addr)
109                                 }
110                         }
111                         peersLock.Lock()
112                         peersByIdLock.Lock()
113                         kpLock.Lock()
114                         for addr, ps := range peers {
115                                 ps.peer.BusyR.Lock()
116                                 needsDeletion = ps.peer.LastPing.Add(timeout).Before(now)
117                                 ps.peer.BusyR.Unlock()
118                                 if needsDeletion {
119                                         log.Println("Deleting peer", ps.peer)
120                                         delete(peers, addr)
121                                         delete(knownPeers, addr)
122                                         delete(peersById, *ps.peer.Id)
123                                         go govpn.ScriptCall(
124                                                 confs[*ps.peer.Id].Down,
125                                                 ps.tap.Name,
126                                                 ps.peer.Addr,
127                                         )
128                                         ps.terminator <- struct{}{}
129                                 }
130                         }
131                         hsLock.Unlock()
132                         peersLock.Unlock()
133                         peersByIdLock.Unlock()
134                         kpLock.Unlock()
135                 }
136         }
137 }