Skip to content

Commit

Permalink
populate ZitiAddress.Dial.identity field if requested by intercept.v1…
Browse files Browse the repository at this point in the history
… config (#621)
  • Loading branch information
ekoby authored Sep 4, 2024
1 parent 97375d6 commit f1f9d39
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
1 change: 1 addition & 0 deletions samples/sample-okhttp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
14 changes: 0 additions & 14 deletions ziti/src/main/kotlin/org/openziti/api/intercept.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<InterceptAddress> {
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)
Expand All @@ -57,7 +44,6 @@ fun String.asInterceptAddr(): InterceptAddress {
}
}

// @JsonAdapter(InterceptAddressDeserializer::class)
sealed class InterceptAddress {
abstract fun matches(addr: Any): Boolean
companion object {
Expand Down
31 changes: 26 additions & 5 deletions ziti/src/main/kotlin/org/openziti/impl/ZitiContextImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit f1f9d39

Please sign in to comment.