]> Cypherpunks.ru repositories - govpn.git/blob - src/govpn/cmd/govpn-server/conf.go
6d78d8fc3414f9886a869e1f9b01ebd3430961d5
[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                 if pc.MTU == 0 {
60                         pc.MTU = govpn.MTUDefault
61                 }
62                 conf := govpn.PeerConf{
63                         Verifier: verifier,
64                         Id:       verifier.Id,
65                         Name:     name,
66                         Iface:    pc.Iface,
67                         MTU:      pc.MTU,
68                         Up:       pc.Up,
69                         Down:     pc.Down,
70                         Noise:    pc.Noise,
71                         CPR:      pc.CPR,
72                         Encless:  pc.Encless,
73                 }
74                 if pc.TimeoutInt <= 0 {
75                         pc.TimeoutInt = govpn.TimeoutDefault
76                 }
77                 conf.Timeout = time.Second * time.Duration(pc.TimeoutInt)
78                 confs[*verifier.Id] = &conf
79         }
80         return confs
81 }
82
83 func confRefresh() {
84         confs = confRead()
85         ids := make([]govpn.PeerId, 0, len(confs))
86         for peerId, _ := range confs {
87                 ids = append(ids, peerId)
88         }
89         idsCache.Update(ids)
90 }
91
92 func confInit() {
93         idsCache = govpn.NewCipherCache(nil)
94         confRefresh()
95         go func() {
96                 for {
97                         time.Sleep(RefreshRate)
98                         confRefresh()
99                 }
100         }()
101 }