Skip to content

Commit

Permalink
handle IP client intercepts on Ziti services (#64)
Browse files Browse the repository at this point in the history
* handle IP client intercepts on Ziti services
* update dependencies
* prevent gradle OOM
  • Loading branch information
ekoby authored Oct 13, 2020
1 parent 267f310 commit 8429fe2
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 17 deletions.
2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

kotlin.code.style=official

buildForAndroid=true
Expand Down
1 change: 0 additions & 1 deletion samples/http-sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ plugins {
dependencies {
implementation project(':ziti')
implementation("org.slf4j:slf4j-simple:1.7.30")
testImplementation 'junit:junit:4.12'
}

application {
Expand Down
1 change: 0 additions & 1 deletion samples/netty-http-sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ dependencies {
implementation "io.netty:netty-all:4.1.52.Final"
implementation "com.github.ajalt:clikt:2.7.0"
implementation "org.slf4j:slf4j-simple:1.7.30"
testImplementation "junit:junit:4.12"
}

application {
Expand Down
1 change: 0 additions & 1 deletion samples/sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ plugins {

dependencies {
implementation project(":ziti")
testImplementation "junit:junit:4.12"
}

compileJava {
Expand Down
4 changes: 2 additions & 2 deletions ziti-android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ dependencies {
embed("com.goterl.lazycode:lazysodium-android:4.1.1@aar")
embed('net.java.dev.jna:jna:5.6.0@aar')

implementation "org.jetbrains.kotlin:kotlin-stdlib:1.3.72"
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.4.10"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.8'

testImplementation 'junit:junit:4.13'
testImplementation 'junit:junit:4.13.1'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.support:support-compat:28.0.0'
Expand Down
4 changes: 2 additions & 2 deletions ziti-netty/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ repositories {
dependencies {
api(project(':ziti'))

implementation "org.jetbrains.kotlin:kotlin-stdlib:1.3.72"
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.4.10"
implementation "io.netty:netty-all:4.1.52.Final"

testImplementation "org.jetbrains.kotlin:kotlin-test-junit:1.3.72"
testImplementation "org.jetbrains.kotlin:kotlin-test-junit:1.4.10"
testImplementation "com.google.code.gson:gson:2.8.6"
}

Expand Down
2 changes: 1 addition & 1 deletion ziti/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ plugins {
}

dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib:1.3.72")
implementation("org.jetbrains.kotlin:kotlin-stdlib:1.4.10")
implementation('org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.8')

implementation("org.slf4j:slf4j-api:1.7.30")
Expand Down
6 changes: 5 additions & 1 deletion ziti/src/main/kotlin/org/openziti/net/dns/DNSResolver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@
package org.openziti.net.dns

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

@FunctionalInterface
interface DNSResolver {
fun resolve(hostname: String): InetAddress?

data class DNSEvent(val hostname: String?, val ip: InetAddress, val removed: Boolean)
fun subscribe(sub: (DNSEvent) -> Unit)
fun subscribe(sub: Consumer<DNSEvent>)
}
42 changes: 38 additions & 4 deletions ziti/src/main/kotlin/org/openziti/net/dns/ZitiDNSManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,49 @@

package org.openziti.net.dns

import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.channels.BroadcastChannel
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.bouncycastle.util.IPAddress
import org.openziti.api.Service
import java.net.Inet4Address
import java.net.Inet6Address
import java.net.InetAddress
import java.net.InetSocketAddress
import java.util.*
import java.util.concurrent.atomic.AtomicInteger
import java.util.function.Consumer


@ExperimentalCoroutinesApi
internal object ZitiDNSManager : DNSResolver, ServiceMapper {

private val TAG = this::class.java.simpleName

internal val PREFIX = byteArrayOf(0xa9.toByte(), 0xfe.toByte())

internal val postfix = AtomicInteger(0x0101) // start with 1.1 postfix

internal val host2Ip = mutableMapOf<String, InetAddress>()
internal val addr2serviceId = mutableMapOf<InetSocketAddress, String>()
internal val serviceId2addr = mutableMapOf<String, InetSocketAddress>()
internal val dnsBroadCast = BroadcastChannel<DNSResolver.DNSEvent>(Channel.BUFFERED)

internal fun registerService(service: Service): InetSocketAddress? {

service.dns?.hostname?.toLowerCase(Locale.getDefault())?.let { hostname ->
val ip = host2Ip.getOrPut(hostname) {
nextAddr(hostname)

val ip = when {
IPAddress.isValidIPv4(hostname) -> Inet4Address.getByName(hostname)
IPAddress.isValidIPv6(hostname) -> Inet6Address.getByName(hostname)
else -> host2Ip.getOrPut(hostname) { nextAddr(hostname) }
}

runBlocking {
dnsBroadCast.send(DNSResolver.DNSEvent(hostname, ip, false))
}

service.dns?.port?.let { port ->
Expand All @@ -59,11 +78,26 @@ internal object ZitiDNSManager : DNSResolver, ServiceMapper {
val addr = serviceId2addr.get(service.id)
if (addr != null) {
addr2serviceId.remove(addr)
runBlocking {
GlobalScope.launch {
dnsBroadCast.send(DNSResolver.DNSEvent(service.dns?.hostname, addr.address, true))
}
}
}
}

override fun resolve(hostname: String): InetAddress? = host2Ip.get(hostname.toLowerCase(Locale.getDefault()))

override fun subscribe(sub: (DNSResolver.DNSEvent) -> Unit) {
runBlocking {
GlobalScope.launch {
dnsBroadCast.asFlow().collect { sub(it) }
}
}
}

override fun subscribe(sub: Consumer<DNSResolver.DNSEvent>) = subscribe{sub.accept(it)}

override fun getServiceIdByAddr(addr: InetSocketAddress): String? = addr2serviceId.get(addr)

internal fun nextAddr(dnsname: String): InetAddress {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ class AsyncTLSSocketFactory(val ssl: SSLContext): SSLSocketFactory() {
private val implField: Field?

init {
val sockMethods = Socket::class.java.methods
println("methods: $sockMethods")
println("declMethods: ${Socket::class.java.declaredMethods}")

var m: Method? = null
try {
m = Socket::class.java.getDeclaredMethod("getImpl").apply {
Expand Down

0 comments on commit 8429fe2

Please sign in to comment.