Skip to content

Commit

Permalink
More diagnostics in Ziti feedback (#203)
Browse files Browse the repository at this point in the history
* add '/current-identity/edge-routers' api
* get current edge routers and report in ZitiContext.dump()
* include contents of Ziti DNS in feedback
  • Loading branch information
ekoby authored Jun 29, 2021
1 parent 92f07e2 commit 66493c0
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 28 deletions.
4 changes: 4 additions & 0 deletions ziti-android/src/main/java/org/openziti/android/Ziti.kt
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,10 @@ object Ziti: CoroutineScope, Logged by ZitiLog() {
writer.flush()
}

zip.putNextEntry(ZipEntry("ziti_dns.info"))
getDnsResolver().dump(writer)
writer.flush()

zip.putNextEntry(ZipEntry("ziti.log"))
writer.appendLine("logcat result: ${logrc.get()}")
writer.write(log.get())
Expand Down
5 changes: 5 additions & 0 deletions ziti/src/main/kotlin/org/openziti/api/Controller.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ internal class Controller(endpoint: URL, sslContext: SSLContext, trustManager: X
@DELETE("current-api-session")
fun logout(): Deferred<Unit>

@GET("/current-identity/edge-routers")
fun getEdgeRouters(): Deferred<Response<Collection<EdgeRouter>>>

@GET("/current-identity/mfa")
fun getMFA(): Deferred<Response<MFAEnrollment>>

Expand Down Expand Up @@ -303,6 +306,8 @@ internal class Controller(endpoint: URL, sslContext: SSLContext, trustManager: X
api.sendPosture(pr).await()
}

internal suspend fun getEdgeRouters() = api.getEdgeRouters().await().data ?: emptyList()

private fun convertError(t: Throwable): Nothing {
val errCode = when (t) {
is HttpException -> getZitiError(getError(t.response()))
Expand Down
6 changes: 5 additions & 1 deletion ziti/src/main/kotlin/org/openziti/api/types.kt
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,11 @@ class PostureResponse (val id: String, val typeId: PostureQueryType, val data: D
}
}

internal data class EdgeRouter(val name: String, val hostname: String, val urls: Map<String, String>)
internal data class EdgeRouter(
val name: String,
val hostname: String,
val supportedProtocols: Map<String, String>,
@Deprecated("use supportedProtocols") val urls: Map<String, String>)

internal data class Session(val id: String, val token: String, val service: Id, val type: SessionType,
var edgeRouters: Array<EdgeRouter>?) {
Expand Down
14 changes: 13 additions & 1 deletion ziti/src/main/kotlin/org/openziti/impl/ZitiContextImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ internal class ZitiContextImpl(internal val id: Identity, enabled: Boolean) : Zi
get() = Dispatchers.IO + supervisor

private val apiSession = MutableStateFlow<ApiSession?>(null)
private val currentEdgeRouters = MutableStateFlow<Collection<EdgeRouter>>(emptyList())

private val controller: Controller = Controller(URI.create(id.controller()).toURL(), sslContext(), trustManager())
private val postureService = PostureService()
Expand Down Expand Up @@ -313,6 +314,12 @@ internal class ZitiContextImpl(internal val id: Identity, enabled: Boolean) : Zi
}
}

controller.runCatching { getEdgeRouters() }
.onSuccess {
i{"current edge routers = $it"}
currentEdgeRouters.value = it }
.onFailure { w("failed to get current edge routers: $it") }

oneUpdate.join()
oneUpdate.invokeOnCompletion {
when (it) {
Expand Down Expand Up @@ -585,9 +592,14 @@ internal class ZitiContextImpl(internal val id: Identity, enabled: Boolean) : Zi
servicesByName.forEach { (name, s) ->
writer.appendLine("name: $name id: ${s.id} permissions: ${s.permissions.joinToString()} intercept: ${s.interceptConfig}")
}
writer.flush()

writer.appendLine()
writer.appendLine("=== Channels ===")
writer.appendLine("=== Available Edge Routers[${currentEdgeRouters.value.size}] ===")
currentEdgeRouters.value.forEach {
writer.appendLine(it.toString())
}
writer.appendLine("=== Channels[${channels.size}] ===")
channels.forEach { (name, ch) ->
writer.appendLine("ER: $name status: ${ch.state}")
}
Expand Down
29 changes: 7 additions & 22 deletions ziti/src/main/kotlin/org/openziti/net/Transport.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,12 @@ package org.openziti.net
import org.openziti.net.nio.AsyncTLSChannel
import org.openziti.net.nio.connectSuspend
import org.openziti.net.nio.readSuspend
import org.openziti.net.nio.writeSuspend
import org.openziti.util.Logged
import org.openziti.util.ZitiLog
import org.openziti.net.nio.writeCompletely
import java.io.Closeable
import java.net.InetAddress
import java.net.InetSocketAddress
import java.net.URI
import java.nio.ByteBuffer
import java.nio.channels.AsynchronousSocketChannel
import javax.net.ssl.SSLContext

internal interface Transport : Closeable {
Expand All @@ -47,22 +44,14 @@ internal interface Transport : Closeable {
suspend fun write(buf: ByteBuffer)
suspend fun read(buf: ByteBuffer, full: Boolean = true): Int

class TLS(host: String, port: Int, sslContext: SSLContext) : Transport, Logged by ZitiLog("ziti-tls") {
val socket: AsynchronousSocketChannel
class TLS(host: String, port: Int, sslContext: SSLContext) : Transport {
val socket = AsyncTLSChannel(sslContext)
val addr = InetSocketAddress(InetAddress.getByName(host), port)
init {
v { "connecting to $host:$port on t[${Thread.currentThread().name}" }
socket = AsyncTLSChannel(sslContext)
}

override suspend fun connect(timeout: Long) {
socket.connectSuspend(addr, timeout)
}
override suspend fun connect(timeout: Long) = socket.connectSuspend(addr, timeout)

override suspend fun write(buf: ByteBuffer) {
while(buf.hasRemaining()) {
socket.writeSuspend(buf)
}
socket.writeCompletely(buf)
}

override suspend fun read(buf: ByteBuffer, full: Boolean): Int {
Expand All @@ -77,14 +66,10 @@ internal interface Transport : Closeable {
return res
}

override fun close() {
socket.close()
}
override fun close() = socket.close()

override fun isClosed(): Boolean = !socket.isOpen

override fun toString(): String {
return "TLS:${socket.remoteAddress}"
}
override fun toString(): String = "TLS:${socket.remoteAddress}"
}
}
5 changes: 4 additions & 1 deletion ziti/src/main/kotlin/org/openziti/net/dns/DNSResolver.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018-2020 NetFoundry, Inc.
* Copyright (c) 2018-2021 NetFoundry, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,7 @@

package org.openziti.net.dns

import java.io.Writer
import java.net.InetAddress
import java.util.function.Consumer

Expand All @@ -25,4 +26,6 @@ interface DNSResolver {
data class DNSEvent(val hostname: String?, val ip: InetAddress, val removed: Boolean)
fun subscribe(sub: (DNSEvent) -> Unit)
fun subscribe(sub: Consumer<DNSEvent>)

fun dump(writer: Writer)
}
7 changes: 7 additions & 0 deletions ziti/src/main/kotlin/org/openziti/net/dns/ZitiDNSManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import org.bouncycastle.util.IPAddress
import java.io.Writer
import java.net.Inet4Address
import java.net.Inet6Address
import java.net.InetAddress
Expand Down Expand Up @@ -82,4 +83,10 @@ internal object ZitiDNSManager : DNSResolver, CoroutineScope {
host2Ip.clear()
postfix.set(startPostfix)
}

override fun dump(writer: Writer) {
for ((h,ip) in host2Ip) {
writer.appendLine("$h -> $ip")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ class AsyncTLSChannel(
} }

return async {
println("connecting on ${Thread.currentThread()}")
transport.connectSuspend(remote)
state = State.connecting
null
Expand Down
3 changes: 1 addition & 2 deletions ziti/src/main/kotlin/org/openziti/net/nio/NetUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ suspend fun AsynchronousSocketChannel.readSuspend(b: ByteBuffer, timeout: Long,
suspend fun AsynchronousSocketChannel.connectSuspend(addr: SocketAddress) = connectSuspend(addr, Long.MAX_VALUE)

suspend fun AsynchronousSocketChannel.connectSuspend(addr: SocketAddress, timeout: Long) {
println(coroutineContext)
val ch = this

return withContext(coroutineContext) {
Expand All @@ -74,7 +73,7 @@ suspend fun AsynchronousSocketChannel.connectSuspend(addr: SocketAddress, timeou
val timeoutDelay = launch {
delay(timeout)
if (!result.isCompleted) {
val ex = SocketTimeoutException("failed to connect in $timeout millis")
val ex = SocketTimeoutException("failed to connect to $addr in $timeout millis")
if (result.completeExceptionally(ex)) {
ch.runCatching { close() }
}
Expand Down

0 comments on commit 66493c0

Please sign in to comment.