-
Notifications
You must be signed in to change notification settings - Fork 7
/
client.go
268 lines (213 loc) · 7.06 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Copyright 2016 David Lazar. All rights reserved.
// Use of this source code is governed by the GNU AGPL
// license that can be found in the LICENSE file.
// Package alpenhorn implements an Alpenhorn client.
package alpenhorn
import (
"crypto/ed25519"
"fmt"
"sync"
"vuvuzela.io/alpenhorn/config"
"vuvuzela.io/alpenhorn/edhttp"
"vuvuzela.io/alpenhorn/errors"
"vuvuzela.io/alpenhorn/keywheel"
"vuvuzela.io/alpenhorn/pkg"
"vuvuzela.io/alpenhorn/typesocket"
)
// Use github.com/davidlazar/easyjson:
//go:generate easyjson -output_filename client_json.go .
// An EventHandler specifies how an application should react to
// events in the Alpenhorn client.
type EventHandler interface {
// Error is called when the Alpenhorn client experiences an error.
Error(error)
// ConfirmedFriend is called when the add-friend protocol is completed
// between two friends, resulting in a new Friend object.
ConfirmedFriend(*Friend)
// SentFriendRequest is called when an OutgoingFriendRequest is sent
// to the entry server.
SentFriendRequest(*OutgoingFriendRequest)
// ReceivedFriendRequest is called when the client receives a friend request.
// The application should eventually call .Approve() or .Remove() on the
// IncomingFriendRequest.
ReceivedFriendRequest(*IncomingFriendRequest)
// UnexpectedSigningKey is called when an incoming friend request corresponds
// to a friend request the user sent but has a different long term key than
// what the user specified.
UnexpectedSigningKey(*IncomingFriendRequest, *OutgoingFriendRequest)
// SendingCall is called when an OutgoingCall is about to be sent to the
// entry server. The application can finalize the call to get its session key.
SendingCall(*OutgoingCall)
// ReceivedCall is called when the client receives a call from a friend.
ReceivedCall(*IncomingCall)
// NewConfig is called when the configuration for the add-friend or dialing
// protocol changes. The chain starts with the new config and ends with the
// client's previous config.
NewConfig(chain []*config.SignedConfig)
}
type Client struct {
Username string
LongTermPublicKey ed25519.PublicKey
LongTermPrivateKey ed25519.PrivateKey
PKGLoginKey ed25519.PrivateKey
ConfigClient *config.Client
Handler EventHandler
// ClientPersistPath is where the client writes its state when it changes.
// If empty, the client does not persist state.
ClientPersistPath string
// KeywheelPersistPath is the path where the client's keywheel is stored.
// This field is not persisted along with the rest of the client's state,
// so it must be set before calling Connect.
//
// The client state and keywheel are persisted in separate files for
// forward secrecy. The client state is long-term and should be backed
// up regularly. The keywheel is ephemeral and should not be backed up
// (doing so hurts forward secrecy, and the keywheel can be recreated
// from the client state).
KeywheelPersistPath string
// wheel is the Alpenhorn keywheel. It is persisted to the KeywheelPersistPath.
wheel keywheel.Wheel
initOnce sync.Once
edhttpClient *edhttp.Client
lastDialingRound uint32 // updated atomically
// mu protects everything up to the end of the struct.
mu sync.Mutex
addFriendRounds map[uint32]*addFriendRoundState
addFriendConfigHash string
addFriendConfig *config.SignedConfig
dialingRounds map[uint32]*dialingRoundState
dialingConfigHash string
dialingConfig *config.SignedConfig
friends map[string]*Friend
incomingFriendRequests []*IncomingFriendRequest
outgoingFriendRequests []*OutgoingFriendRequest
sentFriendRequests []*sentFriendRequest
outgoingCalls []*OutgoingCall
addFriendConn typesocket.Conn
dialingConn typesocket.Conn
}
func (c *Client) init() {
c.initOnce.Do(func() {
c.edhttpClient = new(edhttp.Client)
if c.friends == nil {
c.friends = make(map[string]*Friend)
}
c.addFriendRounds = make(map[uint32]*addFriendRoundState)
c.dialingRounds = make(map[uint32]*dialingRoundState)
})
}
// Register registers the username with the given PKG.
func (c *Client) Register(server pkg.PublicServerConfig, token string) error {
c.init()
pkgc := &pkg.Client{
Username: c.Username,
LoginKey: c.PKGLoginKey,
UserLongTermKey: c.LongTermPublicKey,
HTTPClient: c.edhttpClient,
}
err := pkgc.Register(server, token)
if err != nil {
return err
}
return err
}
type PKGStatus struct {
Server pkg.PublicServerConfig
Error error
}
func (c *Client) PKGStatus() []PKGStatus {
c.init()
pkgc := &pkg.Client{
Username: c.Username,
LoginKey: c.PKGLoginKey,
UserLongTermKey: c.LongTermPublicKey,
HTTPClient: c.edhttpClient,
}
c.mu.Lock()
conf := c.addFriendConfig
c.mu.Unlock()
addFriendConfig := conf.Inner.(*config.AddFriendConfig)
statuses := make([]PKGStatus, len(addFriendConfig.PKGServers))
for i, pkgServer := range addFriendConfig.PKGServers {
statuses[i].Server = pkgServer
statuses[i].Error = pkgc.CheckStatus(pkgServer)
}
return statuses
}
func (c *Client) ConnectAddFriend() (chan error, error) {
c.init()
if c.ConfigClient == nil {
return nil, errors.New("no config client")
}
c.mu.Lock()
if c.addFriendConfig == nil {
c.mu.Unlock()
return nil, errors.New("no addfriend config")
}
c.mu.Unlock()
// Fetch the current config to get the coordinator's key and address.
addFriendConfig, err := c.ConfigClient.CurrentConfig("AddFriend")
if err != nil {
return nil, errors.Wrap(err, "fetching addfriend config")
}
addFriendInner := addFriendConfig.Inner.(*config.AddFriendConfig)
afwsAddr := fmt.Sprintf("wss://%s/addfriend/ws", addFriendInner.Coordinator.Address)
addFriendConn, err := typesocket.Dial(afwsAddr, addFriendInner.Coordinator.Key)
if err != nil {
return nil, err
}
c.mu.Lock()
c.addFriendConn = addFriendConn
c.mu.Unlock()
disconnect := make(chan error, 1)
go func() {
disconnect <- addFriendConn.Serve(c.addFriendMux())
}()
return disconnect, nil
}
func (c *Client) ConnectDialing() (chan error, error) {
c.init()
if c.ConfigClient == nil {
return nil, errors.New("no config client")
}
c.mu.Lock()
if c.dialingConfig == nil {
c.mu.Unlock()
return nil, errors.New("no dialing config")
}
c.mu.Unlock()
dialingConfig, err := c.ConfigClient.CurrentConfig("Dialing")
if err != nil {
return nil, errors.Wrap(err, "fetching dialing config")
}
dialingInner := dialingConfig.Inner.(*config.DialingConfig)
dwsAddr := fmt.Sprintf("wss://%s/dialing/ws", dialingInner.Coordinator.Address)
dialingConn, err := typesocket.Dial(dwsAddr, dialingInner.Coordinator.Key)
if err != nil {
return nil, err
}
c.mu.Lock()
c.dialingConn = dialingConn
c.mu.Unlock()
disconnect := make(chan error, 1)
go func() {
disconnect <- dialingConn.Serve(c.dialingMux())
}()
return disconnect, nil
}
func (c *Client) CloseAddFriend() error {
c.mu.Lock()
defer c.mu.Unlock()
if c.addFriendConn != nil {
return c.addFriendConn.Close()
}
return nil
}
func (c *Client) CloseDialing() error {
c.mu.Lock()
defer c.mu.Unlock()
if c.dialingConn != nil {
return c.dialingConn.Close()
}
return nil
}