]> Cypherpunks.ru repositories - govpn.git/blob - src/govpn/cmd/govpn-server/conf.go
Do not exit if JSON configuration failed during runtime
[govpn.git] / src / govpn / cmd / govpn-server / conf.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 package main
20
21 import (
22         "encoding/json"
23         "errors"
24         "io/ioutil"
25         "log"
26         "time"
27
28         "govpn"
29 )
30
31 const (
32         RefreshRate = time.Minute
33 )
34
35 var (
36         confs    map[govpn.PeerId]*govpn.PeerConf
37         idsCache *govpn.CipherCache
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 = json.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                         log.Println("MTU value", pc.MTU, "is too high, overriding to", 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                 }
79                 if pc.TimeoutInt <= 0 {
80                         pc.TimeoutInt = govpn.TimeoutDefault
81                 }
82                 conf.Timeout = time.Second * time.Duration(pc.TimeoutInt)
83                 confs[*verifier.Id] = &conf
84         }
85         return &confs, nil
86 }
87
88 func confRefresh() error {
89         newConfs, err := confRead()
90         if err != nil {
91                 log.Println("Unable to parse peers configuration:", err)
92                 return err
93         }
94         confs = *newConfs
95         ids := make([]govpn.PeerId, 0, len(confs))
96         for peerId, _ := range confs {
97                 ids = append(ids, peerId)
98         }
99         idsCache.Update(ids)
100         return nil
101 }
102
103 func confInit() {
104         idsCache = govpn.NewCipherCache(nil)
105         if err := confRefresh(); err != nil {
106                 log.Fatalln(err)
107         }
108         go func() {
109                 for {
110                         time.Sleep(RefreshRate)
111                         confRefresh()
112                 }
113         }()
114 }