]> Cypherpunks.ru repositories - govpn.git/blob - src/govpn/cmd/govpn-server/conf.go
284f4f9dc5a2047b0cefed8c5f6d6e1728f15ad1
[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         "io/ioutil"
24         "log"
25         "time"
26
27         "govpn"
28 )
29
30 const (
31         RefreshRate = time.Minute
32 )
33
34 var (
35         confs    map[govpn.PeerId]*govpn.PeerConf
36         idsCache govpn.CipherCache
37 )
38
39 func confRead() map[govpn.PeerId]*govpn.PeerConf {
40         data, err := ioutil.ReadFile(*confPath)
41         if err != nil {
42                 log.Fatalln("Unable to read configuration:", err)
43         }
44         confsRaw := new(map[string]govpn.PeerConf)
45         err = json.Unmarshal(data, confsRaw)
46         if err != nil {
47                 log.Fatalln("Unable to parse configuration:", err)
48         }
49
50         confs := make(map[govpn.PeerId]*govpn.PeerConf, len(*confsRaw))
51         for name, pc := range *confsRaw {
52                 verifier, err := govpn.VerifierFromString(pc.VerifierRaw)
53                 if err != nil {
54                         log.Fatalln("Unable to decode the key:", err.Error(), pc.VerifierRaw)
55                 }
56                 if pc.EncLess {
57                         pc.Noise = true
58                 }
59                 conf := govpn.PeerConf{
60                         Verifier: verifier,
61                         Id:       verifier.Id,
62                         Name:     name,
63                         Iface:    pc.Iface,
64                         Up:       pc.Up,
65                         Down:     pc.Down,
66                         Noise:    pc.Noise,
67                         CPR:      pc.CPR,
68                         EncLess:  pc.EncLess,
69                 }
70                 if pc.TimeoutInt <= 0 {
71                         pc.TimeoutInt = govpn.TimeoutDefault
72                 }
73                 conf.Timeout = time.Second * time.Duration(pc.TimeoutInt)
74                 confs[*verifier.Id] = &conf
75         }
76         return confs
77 }
78
79 func confRefresh() {
80         confs = confRead()
81         ids := make([]govpn.PeerId, 0, len(confs))
82         for peerId, _ := range confs {
83                 ids = append(ids, peerId)
84         }
85         idsCache.Update(ids)
86 }
87
88 func confInit() {
89         idsCache = govpn.NewCipherCache(nil)
90         confRefresh()
91         go func() {
92                 for {
93                         time.Sleep(RefreshRate)
94                         confRefresh()
95                 }
96         }()
97 }