]> Cypherpunks.ru repositories - govpn.git/blob - src/govpn/cmd/govpn-server/conf.go
4136fabbdec4a2472af4ee80405d625344750f49
[govpn.git] / src / govpn / cmd / govpn-server / conf.go
1 /*
2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2015 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/hex"
23         "encoding/json"
24         "io/ioutil"
25         "log"
26         "time"
27
28         "github.com/agl/ed25519"
29
30         "govpn"
31 )
32
33 const (
34         RefreshRate = time.Minute
35 )
36
37 var (
38         confs    map[govpn.PeerId]*govpn.PeerConf
39         idsCache govpn.CipherCache
40 )
41
42 func confRead() map[govpn.PeerId]*govpn.PeerConf {
43         data, err := ioutil.ReadFile(*confPath)
44         if err != nil {
45                 log.Fatalln("Unable to read configuration:", err)
46         }
47         confsRaw := new(map[string]govpn.PeerConf)
48         err = json.Unmarshal(data, confsRaw)
49         if err != nil {
50                 log.Fatalln("Unable to parse configuration:", err)
51         }
52
53         confs := make(map[govpn.PeerId]*govpn.PeerConf, len(*confsRaw))
54         for peerIdRaw, pc := range *confsRaw {
55                 peerId, err := govpn.IDDecode(peerIdRaw)
56                 if err != nil {
57                         log.Fatalln("Invalid peer ID:", peerIdRaw, err)
58                 }
59                 conf := govpn.PeerConf{
60                         Id:    peerId,
61                         Name:  pc.Name,
62                         Up:    pc.Up,
63                         Down:  pc.Down,
64                         Noise: pc.Noise,
65                         CPR:   pc.CPR,
66                 }
67                 if pc.TimeoutInt <= 0 {
68                         pc.TimeoutInt = govpn.TimeoutDefault
69                 }
70                 conf.Timeout = time.Second * time.Duration(pc.TimeoutInt)
71
72                 if len(pc.Verifier) != ed25519.PublicKeySize*2 {
73                         log.Fatalln("Verifier must be 64 hex characters long")
74                 }
75                 keyDecoded, err := hex.DecodeString(string(pc.Verifier))
76                 if err != nil {
77                         log.Fatalln("Unable to decode the key:", err.Error(), pc.Verifier)
78                 }
79                 conf.DSAPub = new([ed25519.PublicKeySize]byte)
80                 copy(conf.DSAPub[:], keyDecoded)
81
82                 confs[*peerId] = &conf
83         }
84         return confs
85 }
86
87 func confRefresh() {
88         confs = confRead()
89         ids := make([]govpn.PeerId, 0, len(confs))
90         for peerId, _ := range confs {
91                 ids = append(ids, peerId)
92         }
93         idsCache.Update(ids)
94 }
95
96 func confInit() {
97         idsCache = govpn.NewCipherCache(nil)
98         confRefresh()
99         go func() {
100                 for {
101                         time.Sleep(RefreshRate)
102                         confRefresh()
103                 }
104         }()
105 }