@@ -21,7 +21,6 @@ import kotlinx.coroutines.launch
2121import kotlinx.coroutines.runBlocking
2222import org.openziti.*
2323import org.openziti.api.Service
24- import org.openziti.identity.Enroller
2524import org.openziti.identity.KeyStoreIdentity
2625import org.openziti.identity.findIdentityAlias
2726import org.openziti.identity.loadKeystore
@@ -38,7 +37,7 @@ import java.net.URI
3837import java.security.KeyStore
3938
4039internal object ZitiImpl : Logged by ZitiLog() {
41- internal val contexts = mutableListOf< ZitiContextImpl >( )
40+ internal val contexts = MutableStateFlow < Collection < ZitiContextImpl >>(emptyList() )
4241 internal var appId = " "
4342 internal var appVersion = " "
4443
@@ -48,7 +47,7 @@ internal object ZitiImpl : Logged by ZitiLog() {
4847 try {
4948 Class .forName(" android.util.Log" )
5049 true
51- } catch (cnf : ClassNotFoundException ) {
50+ } catch (_ : ClassNotFoundException ) {
5251 false
5352 }
5453 }
@@ -59,7 +58,7 @@ internal object ZitiImpl : Logged by ZitiLog() {
5958
6059 fun loadContext (cfg : IdentityConfig , enabled : Boolean ) =
6160 ZitiContextImpl (cfg, enabled).also { ztx ->
62- contexts.add( ztx)
61+ contexts.value + = ztx
6362 ztx.launch {
6463 ztxEvents.emit(Ziti .IdentityEvent (Ziti .IdentityEventType .Loaded , ztx))
6564 ztx.serviceUpdates().collect {
@@ -73,7 +72,7 @@ internal object ZitiImpl : Logged by ZitiLog() {
7372 val idName = alias ? : findIdentityAlias(ks)
7473 val id = KeyStoreIdentity (ks, idName)
7574 return ZitiContextImpl (id, true ).also { ctx ->
76- contexts.add( ctx)
75+ contexts.value + = ctx
7776 ctx.launch {
7877 ztxEvents.emit(Ziti .IdentityEvent (Ziti .IdentityEventType .Loaded , ctx))
7978 ctx.serviceUpdates().collect {
@@ -95,10 +94,11 @@ internal object ZitiImpl : Logged by ZitiLog() {
9594 return loadContext(ks, alias)
9695 }
9796
98- internal fun loadContext (id : ByteArray ): ZitiContextImpl {
99- val ks = loadKeystore(id)
100- return loadContext(ks, null )
101- }
97+ internal fun loadContext (id : ByteArray ): ZitiContextImpl =
98+ id.inputStream().use {
99+ val ks = loadKeystore(it, charArrayOf())
100+ loadContext(ks, null )
101+ }
102102
103103 fun init (c : ByteArray , seamless : Boolean ) {
104104 initInternalNetworking(seamless)
@@ -115,14 +115,16 @@ internal object ZitiImpl : Logged by ZitiLog() {
115115 }
116116
117117 fun removeContext (ctx : ZitiContext ) {
118- contexts.remove(ctx)
119- if (ctx is ZitiContextImpl ) {
118+
119+ if (ctx is ZitiContextImpl && contexts.value.contains(ctx)) {
120+ val ctxs = contexts.value - ctx
121+ contexts.value = ctxs
120122 runBlocking { ztxEvents.emit(Ziti .IdentityEvent (Ziti .IdentityEventType .Removed , ctx)) }
121123 ctx.destroy()
122124 }
123125 }
124126
125- fun init (ks : KeyStore , seamless : Boolean ): List <ZitiContext > {
127+ fun init (ks : KeyStore , seamless : Boolean ): Collection <ZitiContext > {
126128 initInternalNetworking(seamless)
127129
128130 for (a in ks.aliases()) {
@@ -131,7 +133,7 @@ internal object ZitiImpl : Logged by ZitiLog() {
131133 }
132134 }
133135
134- return contexts
136+ return contexts.value
135137 }
136138
137139 private fun isZitiIdentity (ks : KeyStore , alias : String ): Boolean =
@@ -155,9 +157,9 @@ internal object ZitiImpl : Logged by ZitiLog() {
155157 return ZitiDNSManager .lookup(addr.address)?.let { getServiceFor(it, addr.port) }
156158 }
157159
158- fun getServiceFor (host : String , port : Int ): Pair <ZitiContext , Service >? = contexts.map { c ->
159- c.getServiceForAddress(host, port)?.let { Pair (c, it) }
160- }.filterNotNull().firstOrNull()
160+ fun getServiceFor (host : String , port : Int ): Pair <ZitiContext , Service >? = contexts.value.firstNotNullOfOrNull { c ->
161+ c.getServiceForAddress(host, port)?.let { Pair (c, it) }
162+ }
161163
162164 fun connect (addr : SocketAddress ): ZitiConnection {
163165 when (addr) {
@@ -166,7 +168,7 @@ internal object ZitiImpl : Logged by ZitiLog() {
166168 return ztx.dial(svc.name)
167169 }
168170 is ZitiAddress .Dial -> {
169- for (c in contexts) {
171+ for (c in contexts.value ) {
170172 c.getService(addr.service)?.let {
171173 return c.dial(addr)
172174 }
@@ -189,14 +191,14 @@ internal object ZitiImpl : Logged by ZitiLog() {
189191
190192 private val ztxEvents = MutableSharedFlow <Ziti .IdentityEvent >()
191193 internal fun getEvents (): Flow <Ziti .IdentityEvent > = flow {
192- contexts.forEach {
194+ contexts.value. forEach {
193195 emit(Ziti .IdentityEvent (Ziti .IdentityEventType .Loaded , it))
194196 }
195197 emitAll(ztxEvents)
196198 }
197199
198200 fun findDialInfo (addr : InetSocketAddress ): Pair <ZitiContext , SocketAddress >? {
199- for (c in contexts) {
201+ for (c in contexts.value ) {
200202 val dial = c.getDialAddress(addr)
201203 if (dial != null ) {
202204 return c to dial
0 commit comments