-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient_channel.go
254 lines (208 loc) · 6.98 KB
/
client_channel.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
package lime
import (
"context"
"fmt"
)
// ClientChannel implements the client-side communication channel in a Lime session.
type ClientChannel struct {
*channel
}
func NewClientChannel(t Transport, bufferSize int) *ClientChannel {
c := newChannel(t, bufferSize)
c.client = true
return &ClientChannel{channel: c}
}
// receiveSessionFromServer receives a session from the remote node.
func (c *ClientChannel) receiveSessionFromServer(ctx context.Context) (*Session, error) {
ses, err := c.receiveSession(ctx)
if err != nil {
return nil, fmt.Errorf("receive session: %w", err)
}
if ses.State == SessionStateEstablished {
c.localNode = ses.To
c.remoteNode = ses.From
}
c.sessionID = ses.ID
c.setState(ses.State)
if ses.State == SessionStateFinished || ses.State == SessionStateFailed {
if err := c.transport.Close(); err != nil {
return nil, fmt.Errorf("closing the transport failed: %w", err)
}
}
return ses, nil
}
// startNewSession sends a new session envelope to the server and awaits for the response.
func (c *ClientChannel) startNewSession(ctx context.Context) (*Session, error) {
if err := c.ensureState(SessionStateNew, "start new session"); err != nil {
return nil, err
}
if err := c.sendSession(ctx, &Session{State: SessionStateNew}); err != nil {
return nil, fmt.Errorf("sending new session failed: %w", err)
}
ses, err := c.receiveSessionFromServer(ctx)
if err != nil {
return nil, fmt.Errorf("receiving on new session failed: %w", err)
}
return ses, nil
}
// negotiateSession sends a "negotiate" session envelope to accept the session negotiation options and awaits for the server confirmation.
func (c *ClientChannel) negotiateSession(ctx context.Context, comp SessionCompression, encrypt SessionEncryption) (*Session, error) {
if err := c.ensureState(SessionStateNegotiating, "negotiate session"); err != nil {
return nil, err
}
negSes := Session{
Envelope: Envelope{
ID: c.sessionID,
},
State: SessionStateNegotiating,
Compression: comp,
Encryption: encrypt,
}
if err := c.sendSession(ctx, &negSes); err != nil {
return nil, fmt.Errorf("sending negotiating session failed: %w", err)
}
ses, err := c.receiveSessionFromServer(ctx)
if err != nil {
return nil, fmt.Errorf("receiving on session negotiation failed: %w", err)
}
return ses, nil
}
// authenticateSession send an "authenticate" session envelope to the server to establish an authenticated session and awaits for the response.
func (c *ClientChannel) authenticateSession(ctx context.Context, identity Identity, auth Authentication, instance string) (*Session, error) {
if err := c.ensureState(SessionStateAuthenticating, "authenticate session"); err != nil {
return nil, err
}
authSes := Session{
Envelope: Envelope{
ID: c.sessionID,
From: Node{
identity,
instance,
},
},
State: SessionStateAuthenticating,
}
authSes.SetAuthentication(auth)
if err := c.sendSession(ctx, &authSes); err != nil {
return nil, fmt.Errorf("sending authenticating session failed: %w", err)
}
ses, err := c.receiveSessionFromServer(ctx)
if err != nil {
return nil, fmt.Errorf("receiving on session authentication failed: %w", err)
}
return ses, nil
}
func (c *ClientChannel) sendFinishingSession(ctx context.Context) error {
if err := c.ensureState(SessionStateEstablished, "finish the session"); err != nil {
return err
}
ses := Session{
Envelope: Envelope{
ID: c.sessionID,
},
State: SessionStateFinishing,
}
return c.sendSession(ctx, &ses)
}
// CompressionSelector defines a function for selecting the compression for a session.
type CompressionSelector func(options []SessionCompression) SessionCompression
var NoneCompressionSelector CompressionSelector = func(options []SessionCompression) SessionCompression {
return SessionCompressionNone
}
// EncryptionSelector defines a function for selecting the encryption for a session.
type EncryptionSelector func(options []SessionEncryption) SessionEncryption
var NoneEncryptionSelector EncryptionSelector = func(options []SessionEncryption) SessionEncryption {
return SessionEncryptionNone
}
var TLSEncryptionSelector EncryptionSelector = func(options []SessionEncryption) SessionEncryption {
return SessionEncryptionTLS
}
type Authenticator func(schemes []AuthenticationScheme, roundTrip Authentication) Authentication
var GuestAuthenticator Authenticator = func(schemes []AuthenticationScheme, roundTrip Authentication) Authentication {
return &GuestAuthentication{}
}
var TransportAuthenticator Authenticator = func(schemes []AuthenticationScheme, roundTrip Authentication) Authentication {
return &TransportAuthentication{}
}
// EstablishSession performs the client session negotiation and authentication handshake.
func (c *ClientChannel) EstablishSession(
ctx context.Context,
compSelector CompressionSelector,
encryptSelector EncryptionSelector,
identity Identity,
authenticator Authenticator,
instance string,
) (*Session, error) {
if authenticator == nil {
panic("the authenticator should not be nil")
}
if c.state != SessionStateNew {
panic("channel state is not new")
}
ses, err := c.startNewSession(ctx)
if err != nil {
return nil, fmt.Errorf("establish session: %w", err)
}
// Session negotiation
if ses.State == SessionStateNegotiating {
if compSelector == nil {
panic("nil compression selector")
}
if encryptSelector == nil {
panic("nil encrypt selector")
}
// Select options
ses, err = c.negotiateSession(
ctx,
compSelector(ses.CompressionOptions),
encryptSelector(ses.EncryptionOptions))
if err != nil {
return nil, fmt.Errorf("establish session: %w", err)
}
if ses.State == SessionStateNegotiating {
if ses.Compression != "" && ses.Compression != c.transport.Compression() {
err = c.transport.SetCompression(ctx, ses.Compression)
if err != nil {
return nil, fmt.Errorf("establish session: set compression: %w", err)
}
}
if ses.Encryption != "" && ses.Encryption != c.transport.Encryption() {
err = c.transport.SetEncryption(ctx, ses.Encryption)
if err != nil {
return nil, fmt.Errorf("establish session: set encryption: %w", err)
}
}
}
// Await for authentication options
ses, err = c.receiveSessionFromServer(ctx)
if err != nil {
return nil, fmt.Errorf("establish session: %w", err)
}
}
// Session authentication
var roundTrip Authentication
for ses.State == SessionStateAuthenticating {
ses, err = c.authenticateSession(
ctx,
identity,
authenticator(ses.SchemeOptions, roundTrip),
instance,
)
if err != nil {
return nil, fmt.Errorf("establish session: %w", err)
}
roundTrip = ses.Authentication
}
return ses, nil
}
// FinishSession performs the session finishing handshake.
func (c *ClientChannel) FinishSession(ctx context.Context) (*Session, error) {
if err := c.sendFinishingSession(ctx); err != nil {
return nil, fmt.Errorf("finish session: %w", err)
}
ses, err := c.receiveSessionFromServer(ctx)
if err != nil {
return nil, fmt.Errorf("finish session: %w", err)
}
return ses, nil
}