]> Cypherpunks.ru repositories - govpn.git/blob - src/cypherpunks.ru/govpn/cmd/govpn-server/main.go
Merge branch 'develop'
[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         if *syslog {
64                 govpn.SyslogEnable()
65         }
66
67         switch *proto {
68         case "udp":
69                 startUDP()
70         case "tcp":
71                 startTCP()
72         case "all":
73                 startUDP()
74                 startTCP()
75         default:
76                 log.Fatalln("Unknown protocol specified")
77         }
78
79         termSignal := make(chan os.Signal, 1)
80         signal.Notify(termSignal, os.Interrupt, os.Kill)
81
82         hsHeartbeat := time.Tick(timeout)
83         go func() { <-hsHeartbeat }()
84
85         if *stats != "" {
86                 log.Println("Stats are going to listen on", *stats)
87                 statsPort, err := net.Listen("tcp", *stats)
88                 if err != nil {
89                         log.Fatalln("Can not listen on stats port:", err)
90                 }
91                 go govpn.StatsProcessor(statsPort, &knownPeers)
92         }
93         if *proxy != "" {
94                 go proxyStart()
95         }
96         govpn.BothPrintf(`[started bind="%s"]`, *bindAddr)
97
98         var needsDeletion bool
99 MainCycle:
100         for {
101                 select {
102                 case <-termSignal:
103                         govpn.BothPrintf(`[terminating bind="%s"]`, *bindAddr)
104                         for _, ps := range peers {
105                                 govpn.ScriptCall(
106                                         confs[*ps.peer.Id].Down,
107                                         ps.tap.Name,
108                                         ps.peer.Addr,
109                                 )
110                         }
111                         break MainCycle
112                 case <-hsHeartbeat:
113                         now := time.Now()
114                         hsLock.Lock()
115                         for addr, hs := range handshakes {
116                                 if hs.LastPing.Add(timeout).Before(now) {
117                                         govpn.Printf(`[handshake-delete bind="%s" addr="%s"]`, *bindAddr, addr)
118                                         hs.Zero()
119                                         delete(handshakes, addr)
120                                 }
121                         }
122                         peersLock.Lock()
123                         peersByIdLock.Lock()
124                         kpLock.Lock()
125                         for addr, ps := range peers {
126                                 ps.peer.BusyR.Lock()
127                                 needsDeletion = ps.peer.LastPing.Add(timeout).Before(now)
128                                 ps.peer.BusyR.Unlock()
129                                 if needsDeletion {
130                                         govpn.Printf(`[peer-delete bind="%s" peer="%s"]`, *bindAddr, ps.peer)
131                                         delete(peers, addr)
132                                         delete(knownPeers, addr)
133                                         delete(peersById, *ps.peer.Id)
134                                         go govpn.ScriptCall(
135                                                 confs[*ps.peer.Id].Down,
136                                                 ps.tap.Name,
137                                                 ps.peer.Addr,
138                                         )
139                                         ps.terminator <- struct{}{}
140                                 }
141                         }
142                         hsLock.Unlock()
143                         peersLock.Unlock()
144                         peersByIdLock.Unlock()
145                         kpLock.Unlock()
146                 }
147         }
148 }