]> Cypherpunks.ru repositories - govpn.git/blob - src/cypherpunks.ru/govpn/cmd/govpn-server/main.go
Fix copyright years
[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("log_level", "warning", "Log 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(fields).WithError(err).Fatal("Couldn't initialize logging")
62         }
63         govpn.SetLogger(logger)
64
65         if *egdPath != "" {
66                 logger.WithField("egd_path", *egdPath).WithFields(fields).Debug("Init EGD")
67                 govpn.EGDInit(*egdPath)
68         }
69
70         confInit()
71
72         serverConfig := server.Configuration{
73                 BindAddress:  *bindAddr,
74                 ProxyAddress: *proxy,
75                 Timeout:      govpn.TimeoutDefault,
76         }
77         if serverConfig.Protocol, err = govpn.NewProtocolFromString(*proto); err != nil {
78                 logger.WithError(err).WithFields(fields).WithField("proto", *proto).Fatal("Invalid protocol")
79         }
80         if err = serverConfig.Validate(); err != nil {
81                 logger.WithError(err).WithFields(fields).Fatal("Invalid configuration")
82         }
83
84         srv := server.NewServer(serverConfig, confs, idsCache, logger, govpn.CatchSignalShutdown())
85
86         if *stats != "" {
87                 go govpn.StatsProcessor(*stats, srv.KnownPeers())
88         }
89
90         go srv.MainCycle()
91         if err = <-srv.Error; err != nil {
92                 logger.WithError(err).Fatal("Fatal error")
93         }
94 }