]> Cypherpunks.ru repositories - govpn.git/blob - src/cypherpunks.ru/govpn/cmd/govpn-server/main.go
Add common cypherpunks.ru prefix for govpn Go package names
[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                         break MainCycle
93                 case <-hsHeartbeat:
94                         now := time.Now()
95                         hsLock.Lock()
96                         for addr, hs := range handshakes {
97                                 if hs.LastPing.Add(timeout).Before(now) {
98                                         log.Println("Deleting handshake state", addr)
99                                         hs.Zero()
100                                         delete(handshakes, addr)
101                                 }
102                         }
103                         peersLock.Lock()
104                         peersByIdLock.Lock()
105                         kpLock.Lock()
106                         for addr, ps := range peers {
107                                 ps.peer.BusyR.Lock()
108                                 needsDeletion = ps.peer.LastPing.Add(timeout).Before(now)
109                                 ps.peer.BusyR.Unlock()
110                                 if needsDeletion {
111                                         log.Println("Deleting peer", ps.peer)
112                                         delete(peers, addr)
113                                         delete(knownPeers, addr)
114                                         delete(peersById, *ps.peer.Id)
115                                         go govpn.ScriptCall(
116                                                 confs[*ps.peer.Id].Down,
117                                                 ps.tap.Name,
118                                         )
119                                         ps.terminator <- struct{}{}
120                                 }
121                         }
122                         hsLock.Unlock()
123                         peersLock.Unlock()
124                         peersByIdLock.Unlock()
125                         kpLock.Unlock()
126                 }
127         }
128 }