diff --git a/samples/sample-okhttp/build.gradle b/samples/sample-okhttp/build.gradle index 99d9eea8..c70ac00b 100644 --- a/samples/sample-okhttp/build.gradle +++ b/samples/sample-okhttp/build.gradle @@ -10,6 +10,7 @@ repositories { dependencies { implementation deps.ziti + implementation deps.slf4jSimple implementation 'com.squareup.okhttp3:okhttp:3.14.9' testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.1' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.1' diff --git a/ziti/src/main/kotlin/org/openziti/api/intercept.kt b/ziti/src/main/kotlin/org/openziti/api/intercept.kt index 7cc75391..af201cc2 100644 --- a/ziti/src/main/kotlin/org/openziti/api/intercept.kt +++ b/ziti/src/main/kotlin/org/openziti/api/intercept.kt @@ -17,25 +17,12 @@ package org.openziti.api import com.fasterxml.jackson.annotation.JsonCreator -import com.google.gson.JsonDeserializationContext -import com.google.gson.JsonDeserializer -import com.google.gson.JsonElement -import com.google.gson.annotations.JsonAdapter import org.openziti.util.IPUtil -import java.lang.reflect.Type import java.net.Inet4Address import java.net.Inet6Address import java.net.InetAddress -class InterceptAddressDeserializer: JsonDeserializer { - override fun deserialize( - json: JsonElement, - typeOfT: Type, - context: JsonDeserializationContext? - ): InterceptAddress = json.asString.asInterceptAddr() -} - fun String.asInterceptAddr(): InterceptAddress { val addr = this if (addr[0] == '*') return DomainName(addr) @@ -57,7 +44,6 @@ fun String.asInterceptAddr(): InterceptAddress { } } -// @JsonAdapter(InterceptAddressDeserializer::class) sealed class InterceptAddress { abstract fun matches(addr: Any): Boolean companion object { diff --git a/ziti/src/main/kotlin/org/openziti/impl/ZitiContextImpl.kt b/ziti/src/main/kotlin/org/openziti/impl/ZitiContextImpl.kt index 3b185a62..7e2be364 100644 --- a/ziti/src/main/kotlin/org/openziti/impl/ZitiContextImpl.kt +++ b/ziti/src/main/kotlin/org/openziti/impl/ZitiContextImpl.kt @@ -409,29 +409,50 @@ internal class ZitiContextImpl(internal val id: Identity, enabled: Boolean) : Zi internal fun getDialAddress(addr: InetSocketAddress, proto: Protocol = Protocol.TCP): ZitiAddress.Dial? { isEnabled() || return null - val targetAddr = getDnsTarget(addr) ?: getIPtarget(addr) ?: return null + val targetIP = getIPtarget(addr) + val targetAddr = getDnsTarget(addr) + + val matchAddr = targetAddr ?: targetIP ?: return null val service = servicesById.values.firstOrNull { s -> s.permissions.contains(SessionType.DIAL) && s.interceptConfig()?.let { cfg -> cfg.protocols.contains(proto) && cfg.portRanges.any { it.contains(addr.port) } && - cfg.addresses.any { it.matches(targetAddr) } + cfg.addresses.any { it.matches(matchAddr) } } ?: false } ?: return null + val identity = service.interceptConfig()?.dialOptions + ?.get("identity")?.toString()?.run { + replace("\$dst_protocol", proto.name) + replace("\$dst_port", addr.port.toString()) + + if (targetAddr != null) { + replace("\$dst_hostname", targetAddr) + } else { + replace("\$dst_ip", targetIP.toString()) + } + } + return ZitiAddress.Dial( service = service.name, callerId = name(), + identity = identity, appData = DialData( dstProtocol = proto, - dstHostname = if (targetAddr is String) targetAddr else null, - dstIp = if (targetAddr is InetAddress) targetAddr.hostAddress else null, + dstHostname = targetAddr, + dstIp = targetIP?.hostAddress, dstPort = addr.port.toString() )) } - override fun getService(addr: InetSocketAddress): Service? = getServiceForAddress(addr.hostString, addr.port) + override fun getService(addr: InetSocketAddress): Service? { + runBlocking { + serviceUpdates().first() + } + return getServiceForAddress(addr.hostString, addr.port) + } override fun getService(name: String): Service? { return servicesByName.get(name)