]> Cypherpunks.ru repositories - govpn.git/blob - src/cypherpunks.ru/govpn/cmd/govpn-server/conf.go
Forbid any later GNU GPL versions autousage
[govpn.git] / src / cypherpunks.ru / govpn / cmd / govpn-server / conf.go
1 /*
2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2019 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, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 package main
19
20 import (
21         "errors"
22         "io/ioutil"
23         "log"
24         "time"
25
26         "gopkg.in/yaml.v2"
27
28         "cypherpunks.ru/govpn"
29 )
30
31 const (
32         RefreshRate = time.Minute
33 )
34
35 var (
36         confs    map[govpn.PeerID]*govpn.PeerConf
37         idsCache *govpn.MACCache
38 )
39
40 func confRead() (*map[govpn.PeerID]*govpn.PeerConf, error) {
41         data, err := ioutil.ReadFile(*confPath)
42         if err != nil {
43                 return nil, err
44         }
45         confsRaw := new(map[string]govpn.PeerConf)
46         err = yaml.Unmarshal(data, confsRaw)
47         if err != nil {
48                 return nil, err
49         }
50
51         confs := make(map[govpn.PeerID]*govpn.PeerConf, len(*confsRaw))
52         for name, pc := range *confsRaw {
53                 verifier, err := govpn.VerifierFromString(pc.VerifierRaw)
54                 if err != nil {
55                         return nil, errors.New("Unable to decode verifier: " + err.Error())
56                 }
57                 if pc.Encless {
58                         pc.Noise = true
59                 }
60                 if pc.MTU == 0 {
61                         pc.MTU = govpn.MTUDefault
62                 }
63                 if pc.MTU > govpn.MTUMax {
64                         govpn.Printf(`[mtu-high bind="%s" value="%d" overriden="%d"]`, *bindAddr, pc.MTU, govpn.MTUMax)
65                         pc.MTU = govpn.MTUMax
66                 }
67                 conf := govpn.PeerConf{
68                         Verifier: verifier,
69                         ID:       verifier.ID,
70                         Name:     name,
71                         Iface:    pc.Iface,
72                         MTU:      pc.MTU,
73                         Up:       pc.Up,
74                         Down:     pc.Down,
75                         Noise:    pc.Noise,
76                         CPR:      pc.CPR,
77                         Encless:  pc.Encless,
78                         TimeSync: pc.TimeSync,
79                 }
80                 if pc.TimeoutInt <= 0 {
81                         pc.TimeoutInt = govpn.TimeoutDefault
82                 }
83                 conf.Timeout = time.Second * time.Duration(pc.TimeoutInt)
84                 confs[*verifier.ID] = &conf
85         }
86         return &confs, nil
87 }
88
89 func confRefresh() error {
90         newConfs, err := confRead()
91         if err != nil {
92                 govpn.Printf(`[conf-parse-failed bind="%s" err="%s"]`, *bindAddr, err)
93                 return err
94         }
95         confs = *newConfs
96         idsCache.Update(newConfs)
97         return nil
98 }
99
100 func confInit() {
101         idsCache = govpn.NewMACCache()
102         if err := confRefresh(); err != nil {
103                 log.Fatalln(err)
104         }
105         go func() {
106                 for {
107                         time.Sleep(RefreshRate)
108                         confRefresh()
109                 }
110         }()
111 }