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