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