2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2017 Sergey Matveev <stargrave@stargrave.org>
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.
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.
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/>.
25 "github.com/Sirupsen/logrus"
26 "github.com/pkg/errors"
29 "cypherpunks.ru/govpn"
32 const refreshRate = time.Minute
35 confs peerConfigurations
36 idsCache *govpn.MACCache
40 type peerConfigurations map[govpn.PeerID]*govpn.PeerConf
42 func (peerConfs peerConfigurations) Get(peerID govpn.PeerID) *govpn.PeerConf {
43 pc, exists := peerConfs[peerID]
50 type peerConf struct {
51 Name string `yaml:"name"`
52 Iface string `yaml:"iface"`
55 Down string `yaml:"down"`
56 TimeoutInt int `yaml:"timeout"`
57 Noise bool `yaml:"noise"`
59 Encless bool `yaml:"encless"`
60 TimeSync int `yaml:"timesync"`
61 VerifierRaw string `yaml:"verifier"`
64 func confRead() (*map[govpn.PeerID]*govpn.PeerConf, error) {
65 data, err := ioutil.ReadFile(*confPath)
67 return nil, errors.Wrap(err, "ioutil.ReadFile")
69 confsRaw := new(map[string]peerConf)
70 err = yaml.Unmarshal(data, confsRaw)
72 return nil, errors.Wrap(err, "yaml.Unmarshal")
75 confs := make(map[govpn.PeerID]*govpn.PeerConf, len(*confsRaw))
76 for name, pc := range *confsRaw {
77 verifier, err := govpn.VerifierFromString(pc.VerifierRaw)
79 return nil, errors.Wrap(err, "govpn.VerifierFromString")
85 pc.MTU = govpn.MTUDefault
87 if pc.MTU > govpn.MTUMax {
88 logger.WithFields(logrus.Fields{
90 "previous_mtu": pc.MTU,
91 "new_mtu": govpn.MTUMax,
92 }).Warning("Overriden MTU")
95 conf := govpn.PeerConf{
101 PreUp: preUpAction(pc.Up),
102 Down: govpn.RunScriptAction(&pc.Down),
106 TimeSync: pc.TimeSync,
108 if pc.TimeoutInt <= 0 {
109 conf.Timeout = govpn.TimeoutDefault
111 conf.Timeout = time.Second * time.Duration(pc.TimeoutInt)
113 confs[*verifier.ID] = &conf
118 func confRefresh() error {
119 fields := logrus.Fields{
120 "func": "confRefresh",
122 logger.WithFields(fields).Debug("Check configuration file")
123 newConfs, err := confRead()
125 return errors.Wrap(err, "confRead")
128 logger.WithFields(fields).WithField("newConfs", len(confs)).Debug("idsCache.Update")
129 if err = idsCache.Update(newConfs); err != nil {
130 return errors.Wrap(err, "idsCache.Update")
132 logger.WithFields(fields).Debug("Done")
137 idsCache = govpn.NewMACCache()
139 fields := logrus.Fields{"func": "confInit"}
141 logger.WithError(err).WithFields(fields).Fatal("Couldn't perform initial configuration read")
145 time.Sleep(refreshRate)
146 if err = confRefresh(); err != nil {
147 logger.WithError(err).WithFields(fields).Error("Couldn't refresh configuration")