forked from mutecomm/mute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
169 lines (155 loc) · 4.62 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright (c) 2015 Mute Communications Ltd.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package configclient implements a configuration fetcher.
package configclient
import (
"bytes"
"encoding/hex"
"errors"
"io"
"io/ioutil"
"net/http"
"sort"
"time"
"github.com/mutecomm/mute/configclient/cahash"
"github.com/mutecomm/mute/configclient/roundrobin"
"github.com/mutecomm/mute/configclient/sortedmap"
"github.com/mutecomm/mute/util/times"
)
var (
// ErrHashWrong is returned if the cacert could not be verified.
ErrHashWrong = errors.New("configclient: CACert hash wrong")
// ErrNoServers is returned if no valid servers were configured.
ErrNoServers = errors.New("configclient: no available servers")
)
// MaxReadBody is the maximum size of the body that is transferred.
const MaxReadBody = 1048576
const skew = 82800
// Config contains configuration data for the config call, and it's result.
type Config struct {
PublicKey []byte // Public key of configd. Decoded/binary
URLList string // The list of the configd urls. "10,www.google.com:3912;20,8.8.8.8:80;10,irl.com:1020"
CACert []byte // The current CACert, if any. Will always be set after config update
Map map[string]string // The configuration map. If set it will be overwritten
LastSignDate uint64 // The last signdate, will be updated
Timeout int64 // Timeout, can be zero (will be set to 30)
servers []string // list of servers generated from URLList
curServer int // current server in servers list
}
// Update a configuration structure.
func (c *Config) Update() error {
var err error
var cert *sortedmap.SignedMap
var myCAhash, certHashb []byte
var hisHash string
var ok bool
if c.Timeout == 0 {
c.Timeout = 30
}
ts := roundrobin.ParseServers(c.URLList)
sort.Sort(ts)
c.servers = ts.Order()
if len(c.servers) < 1 {
return ErrNoServers
}
if c.LastSignDate == 0 {
c.LastSignDate = uint64(times.Now() - skew)
}
GetConfigLoop:
for ; c.curServer < len(c.servers); c.curServer++ {
cert, err = getConfig(c.servers[c.curServer], c.PublicKey, c.LastSignDate, c.Timeout)
if err == nil {
break GetConfigLoop
}
}
if err != nil {
return err
}
// Change server ordering to make working and untested first
c.servers = append(c.servers[c.curServer:], c.servers[:c.curServer]...)
c.curServer = 0
if hisHash, ok = cert.Config["CACertHash"]; !ok {
c.Map = cert.Config
c.LastSignDate = cert.SignDate
return nil
}
if certHashb, err = hex.DecodeString(hisHash); err != nil {
return err
}
if c.CACert != nil {
if myCAhash, err = cahash.Hash(c.CACert); err != nil {
return err
}
}
if !bytes.Equal(myCAhash, certHashb) {
CALoop:
for ; c.curServer < len(c.servers); c.curServer++ {
c.CACert, err = getCACert(c.servers[c.curServer], hisHash, c.Timeout)
if err == nil {
break CALoop
}
}
}
if err != nil {
return err
}
c.Map = cert.Config
c.LastSignDate = cert.SignDate
return nil
}
func readBody(rc io.ReadCloser) ([]byte, error) {
defer rc.Close()
p, err := ioutil.ReadAll(&io.LimitedReader{R: rc, N: MaxReadBody})
if err != nil {
return nil, err
}
return p, nil
}
func fixURL(URL string) string {
if URL[len(URL)-1] == '/' {
return URL
}
return URL + "/"
}
// getConfig reads the config from the server url configURL and verifies it
// with ed25519 publicKey. lastSignDate, if greater than zero, is taken into
// consideration. Timeout is in seconds. Configuration can be accessed via
// cert.Config (map[string]string).
func getConfig(configURL string, publicKey []byte, lastSignDate uint64, timeout int64) (cert *sortedmap.SignedMap, err error) {
c := &http.Client{Timeout: time.Second * time.Duration(timeout)}
resp, err := c.Get(fixURL(configURL) + "config")
if err != nil {
return nil, err
}
p, err := readBody(resp.Body)
if err != nil {
return nil, err
}
return sortedmap.Certify(lastSignDate, publicKey, p)
}
// getCACert returns the ca certificate (verified). certHash is from
// GetConfig().Config["CACertHash"]
func getCACert(configURL string, certHash string, timeout int64) ([]byte, error) {
c := &http.Client{Timeout: time.Second * time.Duration(timeout)}
resp, err := c.Get(fixURL(configURL) + "cacert")
if err != nil {
return nil, err
}
p, err := readBody(resp.Body)
if err != nil {
return nil, err
}
testHash, err := cahash.Hash(p)
if err != nil {
return nil, err
}
certHashb, err := hex.DecodeString(certHash)
if err != nil {
return nil, err
}
if !bytes.Equal(testHash, certHashb) {
return nil, ErrHashWrong
}
return p, nil
}