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