]> Cypherpunks.ru repositories - govpn.git/blob - src/cypherpunks.ru/govpn/cmd/govpn-server/main.go
Go language does not like underscores in 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-2017 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
26         "github.com/Sirupsen/logrus"
27
28         "cypherpunks.ru/govpn"
29         "cypherpunks.ru/govpn/server"
30 )
31
32 var (
33         bindAddr = flag.String("bind", "[::]:1194", "Bind to address")
34         proto    = flag.String("proto", "udp", "Protocol to use: udp, tcp or all")
35         confPath = flag.String("conf", "peers.yaml", "Path to configuration YAML")
36         stats    = flag.String("stats", "", "Enable stats retrieving on host:port")
37         proxy    = flag.String("proxy", "", "Enable HTTP proxy on host:port")
38         egdPath  = flag.String("egd", "", "Optional path to EGD socket")
39         syslog   = flag.Bool("syslog", false, "Enable logging to syslog")
40         version  = flag.Bool("version", false, "Print version information")
41         warranty = flag.Bool("warranty", false, "Print warranty information")
42         logLevel = flag.String("loglevel", "warning", "Logging level")
43 )
44
45 func main() {
46         var err error
47         fields := logrus.Fields{"func": "main"}
48
49         flag.Parse()
50         if *warranty {
51                 fmt.Println(govpn.Warranty)
52                 return
53         }
54         if *version {
55                 fmt.Println(govpn.VersionGet())
56                 return
57         }
58
59         logger, err = govpn.NewLogger(*logLevel, *syslog)
60         if err != nil {
61                 logrus.WithFields(
62                         fields,
63                 ).WithError(err).Fatal("Can not initialize logging")
64         }
65         govpn.SetLogger(logger)
66
67         if *egdPath != "" {
68                 logger.WithField(
69                         "egd_path", *egdPath,
70                 ).WithFields(
71                         fields,
72                 ).Debug("Init EGD")
73                 govpn.EGDInit(*egdPath)
74         }
75
76         confInit()
77
78         serverConfig := server.Configuration{
79                 BindAddress:  *bindAddr,
80                 ProxyAddress: *proxy,
81                 Timeout:      govpn.TimeoutDefault,
82         }
83         if serverConfig.Protocol, err = govpn.NewProtocolFromString(*proto); err != nil {
84                 logger.WithError(err).WithFields(
85                         fields,
86                 ).WithField(
87                         "proto", *proto,
88                 ).Fatal("Invalid protocol")
89         }
90         if err = serverConfig.Validate(); err != nil {
91                 logger.WithError(err).WithFields(fields).Fatal("Invalid configuration")
92         }
93
94         srv := server.NewServer(
95                 serverConfig,
96                 confs,
97                 idsCache,
98                 logger,
99                 govpn.CatchSignalShutdown(),
100         )
101
102         if *stats != "" {
103                 go govpn.StatsProcessor(*stats, srv.KnownPeers())
104         }
105
106         go srv.MainCycle()
107         if err = <-srv.Error; err != nil {
108                 logger.WithError(err).Fatal("Fatal error")
109         }
110 }