]> Cypherpunks.ru repositories - govpn.git/blob - src/cypherpunks.ru/govpn/cmd/govpn-server/conf.go
131eeba502201fc46f56d077e65113af69a6c8e4
[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-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         "errors"
23         "io/ioutil"
24         "log"
25         "time"
26
27         "github.com/go-yaml/yaml"
28
29         "cypherpunks.ru/govpn"
30 )
31
32 const (
33         RefreshRate = time.Minute
34 )
35
36 var (
37         confs    map[govpn.PeerId]*govpn.PeerConf
38         idsCache *govpn.CipherCache
39 )
40
41 func confRead() (*map[govpn.PeerId]*govpn.PeerConf, error) {
42         data, err := ioutil.ReadFile(*confPath)
43         if err != nil {
44                 return nil, err
45         }
46         confsRaw := new(map[string]govpn.PeerConf)
47         err = yaml.Unmarshal(data, confsRaw)
48         if err != nil {
49                 return nil, err
50         }
51
52         confs := make(map[govpn.PeerId]*govpn.PeerConf, len(*confsRaw))
53         for name, pc := range *confsRaw {
54                 verifier, err := govpn.VerifierFromString(pc.VerifierRaw)
55                 if err != nil {
56                         return nil, errors.New("Unable to decode verifier: " + err.Error())
57                 }
58                 if pc.Encless {
59                         pc.Noise = true
60                 }
61                 if pc.MTU == 0 {
62                         pc.MTU = govpn.MTUDefault
63                 }
64                 if pc.MTU > govpn.MTUMax {
65                         log.Println("MTU value", pc.MTU, "is too high, overriding to", govpn.MTUMax)
66                         pc.MTU = govpn.MTUMax
67                 }
68                 conf := govpn.PeerConf{
69                         Verifier: verifier,
70                         Id:       verifier.Id,
71                         Name:     name,
72                         Iface:    pc.Iface,
73                         MTU:      pc.MTU,
74                         Up:       pc.Up,
75                         Down:     pc.Down,
76                         Noise:    pc.Noise,
77                         CPR:      pc.CPR,
78                         Encless:  pc.Encless,
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                 log.Println("Unable to parse peers configuration:", err)
93                 return err
94         }
95         confs = *newConfs
96         ids := make([]govpn.PeerId, 0, len(confs))
97         for peerId, _ := range confs {
98                 ids = append(ids, peerId)
99         }
100         idsCache.Update(ids)
101         return nil
102 }
103
104 func confInit() {
105         idsCache = govpn.NewCipherCache(nil)
106         if err := confRefresh(); err != nil {
107                 log.Fatalln(err)
108         }
109         go func() {
110                 for {
111                         time.Sleep(RefreshRate)
112                         confRefresh()
113                 }
114         }()
115 }