-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathZitiTunnelEvent.swift
More file actions
257 lines (206 loc) · 8.41 KB
/
ZitiTunnelEvent.swift
File metadata and controls
257 lines (206 loc) · 8.41 KB
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
/*
Copyright NetFoundry Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import Foundation
import CZitiPrivate
/// Class to encapsulate a Ziti Tunneler SDK event
@objc public class ZitiTunnelEvent : NSObject {
let log = ZitiLog(ZitiTunnelEvent.self)
/// Weak reference to Ziti instnace associated with this event
public weak var ziti:Ziti?
init(_ ziti:Ziti) {
self.ziti = ziti
}
func toStr(_ cStr:UnsafePointer<CChar>?) -> String {
if let cStr = cStr { return String(cString: cStr) }
return ""
}
/// Provide a debug description of the event
/// - returns: String containing the debug description
public override var debugDescription: String {
return "ZitiTunnelEvent: \(String(describing: self))\n" +
" identity: \(ziti?.id.name ?? ""):\"\(ziti?.id.id ?? "")\""
}
}
/// Class encapsulating Ziti Tunnel SDK C Context Event
@objc public class ZitiTunnelContextEvent : ZitiTunnelEvent {
/// Controller status
public var status:String = ""
/// Controller name
public var name:String = ""
/// Controller version
public var version:String = ""
/// Controller address
public var controller:String = ""
/// Controller event code
public var code:Int64
init(_ ziti:Ziti, _ evt:UnsafePointer<ziti_ctx_event>) {
self.code = evt.pointee.code
super.init(ziti)
self.status = toStr(evt.pointee.status)
self.name = toStr(evt.pointee.name)
self.version = toStr(evt.pointee.version)
self.controller = toStr(evt.pointee.controller)
}
/// Debug description of event
/// - returns: String containing the debug description
public override var debugDescription: String {
return super.debugDescription + "\n" +
" status: \(status)\n" +
" name: \(name)\n" +
" version: \(version)\n" +
" controller: \(controller)\n" +
" code: \(code)"
}
}
/// Class encapsulating Ziti Tunnel SDK C MFA Event
@objc public class ZitiTunnelMfaEvent : ZitiTunnelEvent {
/// Enumeration of MFA Status
public enum MfaStatus {
/// MFA Authentication Status
case AuthStatus
/// MFA Authentication Challenge
case AuthChallenge
/// MFA Enrollment Verification
case EnrollmentVerification
/// MFA Removal
case EnrollmentRemove
/// MFA Enrollment Challenge
case EnrollmentChallenge
/// Unregognized status
case Uknown
init(_ mfaStatus:mfa_status) {
switch mfaStatus {
case mfa_status_mfa_auth_status: self = .AuthStatus
case mfa_status_auth_challenge: self = .AuthChallenge
case mfa_status_enrollment_verification: self = .EnrollmentVerification
case mfa_status_enrollment_remove: self = .EnrollmentRemove
case mfa_status_enrollment_challenge: self = .EnrollmentChallenge
default: self = .Uknown
}
}
var mfaStatus : mfa_status {
switch self {
case .AuthStatus: return mfa_status_mfa_auth_status
case .AuthChallenge: return mfa_status_auth_challenge
case .EnrollmentVerification: return mfa_status_enrollment_verification
case .EnrollmentRemove: return mfa_status_enrollment_remove
case .EnrollmentChallenge: return mfa_status_enrollment_challenge
case .Uknown: return mfa_status_Unknown
}
}
}
/// MFA provider
public var provider:String = ""
/// MFA status
public var status:String = ""
/// MFA operation
public var operation:String = ""
/// MFA operation type
public var operationType:MfaStatus
/// MFA provisioning URL
public var provisioningUrl:String = ""
/// MFA recovery codes
public var recovery_codes:[String]
/// MFA authentication code
public var code:Int64
init(_ ziti:Ziti, _ evt:UnsafePointer<mfa_event>) {
self.operationType = MfaStatus(evt.pointee.operation_type)
self.code = evt.pointee.code
self.recovery_codes = []
if var ptr = evt.pointee.recovery_codes {
while let s = ptr.pointee {
self.recovery_codes.append(String(cString:s))
ptr += 1
}
}
super.init(ziti)
self.provider = toStr(evt.pointee.provider)
self.status = toStr(evt.pointee.status)
self.operation = toStr(evt.pointee.operation)
self.provisioningUrl = toStr(evt.pointee.provisioning_url)
}
/// Debug description
/// - returns: String containing debug description of this event
public override var debugDescription: String {
return super.debugDescription + "\n" +
" provider: \(provider)\n" +
" status: \(status)\n" +
" operation: \(operation)\n" +
" operationType: \(operationType)\n" +
" provisioningUrl: \(provisioningUrl)\n" +
" code: \(code)"
}
}
/// Class encapsulating Ziti Tunnel SDK C Service Event
@objc public class ZitiTunnelServiceEvent : ZitiTunnelEvent {
/// Event status
public var status:String = ""
/// Listing of services removed
public var removed:[ZitiService] = []
/// Listing of services added
public var added:[ZitiService] = []
init(_ ziti:Ziti, _ evt:UnsafePointer<service_event>) {
ZitiEvent.ServiceEvent.convert(evt.pointee.removed_services, &removed)
ZitiEvent.ServiceEvent.convert(evt.pointee.added_services, &added)
super.init(ziti)
self.status = toStr(evt.pointee.status)
}
/// Debug description
/// - returns: String containing debug description of this event
public override var debugDescription: String {
return super.debugDescription + "\n" +
" status: \(status)\n" +
" removed: (\(removed.count))\n\(ZitiEvent.svcArrToStr(removed))" +
" added: (\(added.count))\n\(ZitiEvent.svcArrToStr(added))"
}
}
/// Class encapsulating Ziti Tunnel SDK C Config Event
@objc public class ZitiTunnelConfigEvent : ZitiTunnelEvent {
/// Controller address (legacy)
public var controllerUrl:String = ""
/// Controller addresses
public var controllers:[String] = []
/// CA bundle
public var caBundle:String = ""
/// Certificte PEM (possibly multiple certificates)
public var certPEM:String = ""
init(_ ziti:Ziti, _ evt:UnsafePointer<config_event>) {
super.init(ziti)
var ziti_cfg_ptr:UnsafeMutablePointer<ziti_config>?
parse_ziti_config_ptr(&ziti_cfg_ptr, evt.pointee.config_json, strlen(evt.pointee.config_json))
self.controllerUrl = toStr(ziti_cfg_ptr?.pointee.controller_url)
var ctrlList = ziti_cfg_ptr!.pointee.controllers
withUnsafeMutablePointer(to: &ctrlList) { ctrlListPtr in
var i = model_list_iterator(ctrlListPtr)
while i != nil {
let ctrlPtr = model_list_it_element(i)
if let ctrl = UnsafeMutablePointer<CChar>(OpaquePointer(ctrlPtr)) {
let ctrlStr = toStr(ctrl)
controllers.append(ctrlStr)
}
i = model_list_it_next(i)
}
}
self.caBundle = toStr(ziti_cfg_ptr?.pointee.id.ca)
self.certPEM = toStr(ziti_cfg_ptr?.pointee.id.cert)
}
/// Debug description
/// - returns: String containing debug description of this event
public override var debugDescription: String {
return super.debugDescription + "\n" +
" controller_url: \(controllerUrl)\n" +
" contrlollers: \(controllers)\n" +
" caBundle: \(caBundle)\n" +
" cert: \(certPEM)"
}
}