-
Notifications
You must be signed in to change notification settings - Fork 14
/
session.go
373 lines (343 loc) · 11.1 KB
/
session.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package server
import (
"encoding/binary"
"sync"
"sync/atomic"
"time"
"github.com/awcullen/opcua/ua"
)
type publishOp struct {
ch *serverSecureChannel
requestId uint32
req *ua.PublishRequest
results []ua.StatusCode
}
type stateChangeOp struct {
subscriptionId uint32
message ua.NotificationMessage
}
type browseCP struct {
data []ua.ReferenceDescription
max int
}
type Session struct {
sync.RWMutex
sessionId ua.NodeID
sessionName string
authenticationToken ua.NodeID
timeout float64
userIdentity any
sessionNonce ua.ByteString
lastAccess time.Time
publishRequests chan *publishOp
stateChanges chan *stateChangeOp
channelId uint32
securityMode ua.MessageSecurityMode
securityPolicyURI string
clientCertificate ua.ByteString
browseCPs map[uint32]browseCP
lastBrowseCP uint32
maxBrowseContinuationPoints int
historyCPs map[uint32]time.Time
maxHistoryContinuationPoints int
clientDescription ua.ApplicationDescription
serverUri string
endpointURL string
maxResponseMessageSize uint32
localeIds []string
timeCreated time.Time
requestCount uint32
errorCount uint32
unauthorizedRequestCount uint32
readCount uint32
readErrorCount uint32
historyReadCount uint32
historyReadErrorCount uint32
writeCount uint32
writeErrorCount uint32
historyUpdateCount uint32
historyUpdateErrorCount uint32
callCount uint32
callErrorCount uint32
createMonitoredItemsCount uint32
createMonitoredItemsErrorCount uint32
modifyMonitoredItemsCount uint32
modifyMonitoredItemsErrorCount uint32
setMonitoringModeCount uint32
setMonitoringModeErrorCount uint32
setTriggeringCount uint32
setTriggeringErrorCount uint32
deleteMonitoredItemsCount uint32
deleteMonitoredItemsErrorCount uint32
createSubscriptionCount uint32
createSubscriptionErrorCount uint32
modifySubscriptionCount uint32
modifySubscriptionErrorCount uint32
setPublishingModeCount uint32
setPublishingModeErrorCount uint32
publishCount uint32
publishErrorCount uint32
republishCount uint32
republishErrorCount uint32
transferSubscriptionsCount uint32
transferSubscriptionsErrorCount uint32
deleteSubscriptionsCount uint32
deleteSubscriptionsErrorCount uint32
addNodesCount uint32
addNodesErrorCount uint32
addReferencesCount uint32
addReferencesErrorCount uint32
deleteNodesCount uint32
deleteNodesErrorCount uint32
deleteReferencesCount uint32
deleteReferencesErrorCount uint32
browseCount uint32
browseErrorCount uint32
browseNextCount uint32
browseNextErrorCount uint32
translateBrowsePathsToNodeIdsCount uint32
translateBrowsePathsToNodeIdsErrorCount uint32
queryFirstCount uint32
queryFirstErrorCount uint32
queryNextCount uint32
queryNextErrorCount uint32
registerNodesCount uint32
registerNodesErrorCount uint32
unregisterNodesCount uint32
unregisterNodesErrorCount uint32
clientUserIdOfSession string
authenticationMechanism string
clientUserIdHistory []string
}
func NewSession(server *Server, sessionId ua.NodeID, sessionName string, authenticationToken ua.NodeID, sessionNonce ua.ByteString, timeout float64, clientDescription ua.ApplicationDescription, serverUri string, endpointUrl string, clientCertificate ua.ByteString, maxResponseMessageSize uint32) *Session {
return &Session{
sessionId: sessionId,
sessionName: sessionName,
authenticationToken: authenticationToken,
timeout: timeout,
sessionNonce: sessionNonce,
securityMode: ua.MessageSecurityModeNone,
securityPolicyURI: ua.SecurityPolicyURINone,
lastAccess: time.Now(),
publishRequests: make(chan *publishOp, 64),
stateChanges: make(chan *stateChangeOp, 64),
browseCPs: make(map[uint32]browseCP, 16),
maxBrowseContinuationPoints: int(server.ServerCapabilities().MaxBrowseContinuationPoints),
historyCPs: make(map[uint32]time.Time, 16),
maxHistoryContinuationPoints: int(server.ServerCapabilities().MaxHistoryContinuationPoints),
clientDescription: clientDescription,
serverUri: serverUri,
endpointURL: endpointUrl,
localeIds: []string{"en-US"},
maxResponseMessageSize: maxResponseMessageSize,
timeCreated: time.Now(),
clientUserIdHistory: []string{},
}
}
func (s *Session) IsExpired() bool {
s.RLock()
defer s.RUnlock()
return time.Since(s.lastAccess).Milliseconds() > int64(s.timeout)
}
func (s *Session) delete() {
s.Lock()
defer s.Unlock()
//s.sessionId = nil // need to keep to look up diagnostics node
s.authenticationToken = nil
s.userIdentity = nil
s.channelId = 0
s.securityMode = ua.MessageSecurityModeNone
s.securityPolicyURI = ua.SecurityPolicyURINone
s.sessionNonce = ua.ByteString("")
s.clientCertificate = ua.ByteString("")
s.publishRequests = nil
for k := range s.browseCPs {
delete(s.browseCPs, k)
}
s.browseCPs = nil
for k := range s.historyCPs {
delete(s.historyCPs, k)
}
s.historyCPs = nil
s.clientUserIdHistory = nil
}
func (s *Session) SessionId() ua.NodeID {
s.RLock()
defer s.RUnlock()
return s.sessionId
}
func (s *Session) SessionName() string {
s.RLock()
defer s.RUnlock()
return s.sessionName
}
func (s *Session) AuthenticationToken() ua.NodeID {
s.RLock()
defer s.RUnlock()
return s.authenticationToken
}
func (s *Session) Timeout() float64 {
s.RLock()
defer s.RUnlock()
return s.timeout
}
func (s *Session) UserIdentity() any {
s.RLock()
defer s.RUnlock()
return s.userIdentity
}
func (s *Session) SetUserIdentity(value any) {
s.Lock()
defer s.Unlock()
s.userIdentity = value
// update diagnostics
switch ui := s.userIdentity.(type) {
case ua.IssuedIdentity:
s.clientUserIdOfSession = "<issued>"
s.authenticationMechanism = "Issued"
case ua.X509Identity:
s.clientUserIdOfSession = "<certificate>"
s.authenticationMechanism = "X509"
case ua.UserNameIdentity:
s.clientUserIdOfSession = ui.UserName
s.authenticationMechanism = "UserName"
default:
s.clientUserIdOfSession = "<anonymous>"
s.authenticationMechanism = "Anonymous"
}
s.clientUserIdHistory = append(s.clientUserIdHistory, s.clientUserIdOfSession)
}
func (s *Session) SessionNonce() ua.ByteString {
s.RLock()
defer s.RUnlock()
return s.sessionNonce
}
func (s *Session) SetSessionNonce(value ua.ByteString) {
s.Lock()
defer s.Unlock()
s.sessionNonce = value
}
func (s *Session) LastAccess() time.Time {
s.RLock()
defer s.RUnlock()
return s.lastAccess
}
func (s *Session) SetLastAccess(value time.Time) {
s.Lock()
defer s.Unlock()
s.lastAccess = value
}
func (s *Session) SecureChannelId() uint32 {
s.RLock()
defer s.RUnlock()
return s.channelId
}
func (s *Session) SetSecureChannelId(value uint32) {
s.Lock()
defer s.Unlock()
s.channelId = value
}
func (s *Session) SecurityMode() ua.MessageSecurityMode {
s.RLock()
defer s.RUnlock()
return s.securityMode
}
func (s *Session) SetSecurityMode(value ua.MessageSecurityMode) {
s.Lock()
defer s.Unlock()
s.securityMode = value
}
func (s *Session) SecurityPolicyURI() string {
s.RLock()
defer s.RUnlock()
return s.securityPolicyURI
}
func (s *Session) SetSecurityPolicyURI(value string) {
s.Lock()
defer s.Unlock()
s.securityPolicyURI = value
}
func (s *Session) ClientCertificate() ua.ByteString {
s.RLock()
defer s.RUnlock()
return s.clientCertificate
}
func (s *Session) addPublishRequest(ch *serverSecureChannel, requestid uint32, req *ua.PublishRequest, results []ua.StatusCode) error {
for {
select {
case s.publishRequests <- &publishOp{ch, requestid, req, results}:
return nil
default:
op := <-s.publishRequests
err := op.ch.Write(
&ua.ServiceFault{
ResponseHeader: ua.ResponseHeader{
Timestamp: time.Now(),
RequestHandle: op.req.RequestHandle,
ServiceResult: ua.BadTooManyPublishRequests,
},
},
op.requestId,
)
if err != nil {
return err
}
}
}
}
func (s *Session) removePublishRequest() (*serverSecureChannel, uint32, *ua.PublishRequest, []ua.StatusCode, bool, error) {
for {
select {
case op := <-s.publishRequests:
ch := op.ch
req := op.req
rid := op.requestId
results := op.results
// check if expired
if time.Now().After(req.RequestHeader.Timestamp.Add(time.Duration(req.RequestHeader.TimeoutHint) * time.Millisecond)) {
err := ch.Write(
&ua.ServiceFault{
ResponseHeader: ua.ResponseHeader{
Timestamp: time.Now(),
RequestHandle: req.RequestHandle,
ServiceResult: ua.BadTimeout,
},
},
rid,
)
if err != nil {
return nil, 0, nil, nil, false, err
}
continue
}
return ch, rid, req, results, true, nil
default:
return nil, 0, nil, nil, false, nil
}
}
}
func (s *Session) addBrowseContinuationPoint(data []ua.ReferenceDescription, max int) ([]byte, error) {
s.Lock()
defer s.Unlock()
if s.maxBrowseContinuationPoints > 0 && len(s.browseCPs) >= s.maxBrowseContinuationPoints {
return nil, ua.BadNoContinuationPoints
}
id := atomic.AddUint32(&s.lastBrowseCP, 1)
s.browseCPs[id] = browseCP{data, max}
cp := make([]byte, 4)
binary.LittleEndian.PutUint32(cp, id)
return cp, nil
}
func (s *Session) removeBrowseContinuationPoint(cp []byte) ([]ua.ReferenceDescription, int, bool) {
if cp == nil {
return nil, 0, false
}
s.Lock()
defer s.Unlock()
id := binary.LittleEndian.Uint32(cp)
if x, ok := s.browseCPs[id]; ok {
delete(s.browseCPs, id)
return x.data, x.max, ok
}
return nil, 0, false
}