Repository: square/okhttp
File: samples/guide/src/main/java/okhttp3/recipes/kt/WiresharkExample.kt (Kotlin)

Code before:
      logger = Logger.getLogger("javax.net.ssl")
          .apply {
            level = Level.FINEST
          }
    }
  }
}

val eventListenerFactory = WireSharkKeyLoggerListener.Factory(File("/tmp/key.log"), verbose = true)

@SuppressSignatureCheck
class WiresharkExample(private val tlsVersions: List<TlsVersion>) {
  val connectionSpec =
    Builder(ConnectionSpec.RESTRICTED_TLS)
        .tlsVersions(*tlsVersions.toTypedArray())
        .build()

  var bootstrapClient = OkHttpClient.Builder()
      .connectionSpecs(listOf(connectionSpec))
      .addInterceptor(BrotliInterceptor)
      .eventListenerFactory(eventListenerFactory)
      .build()

  val dns = DnsOverHttps.Builder()
      .client(bootstrapClient)
      .url("https://1.1.1.1/dns-query".toHttpUrl())
      .includeIPv6(false)
      .build()

  val client = OkHttpClient.Builder()
      .connectionSpecs(listOf(connectionSpec))
      .eventListenerFactory(eventListenerFactory)
      .addInterceptor(BrotliInterceptor)
      .dns(dns)
      .build()

  fun run() {
    if (tlsVersions.contains(TLS_1_2)) {
      println("TLSv1.2 traffic will be logged automatically")
    }

    if (tlsVersions.contains(TLS_1_3)) {
      println("TLSv1.3 requires an external command run before first traffic is sent")
      println("Follow instructions at https://github.com/neykov/extract-tls-secrets for TLSv1.3")
      println(
          "Pid: ${ProcessHandle.current()
              .pid()}"
      )

      Thread.sleep(10000)
    }

Review comment (refactor):
The agent is quite interesting. I wonder if we could detect it automatically so you don’t need to do all the work to extract the PID and log

Code after:
    }
  }
}

val eventListenerFactory = WireSharkKeyLoggerListener.Factory(File("/tmp/key.log"), verbose = true)

@SuppressSignatureCheck
class WiresharkExample(private val tlsVersions: List<TlsVersion>) {
  val connectionSpec =
    ConnectionSpec.Builder(ConnectionSpec.RESTRICTED_TLS)
        .tlsVersions(*tlsVersions.toTypedArray())
        .build()

  val client = OkHttpClient.Builder()
      .connectionSpecs(listOf(connectionSpec))
      .eventListenerFactory(eventListenerFactory)
      .build()

  fun run() {
    if (tlsVersions.contains(TLS_1_2)) {
      println("TLSv1.2 traffic will be logged automatically")
    }

    if (tlsVersions.contains(TLS_1_3)) {
      println("TLSv1.3 requires an external command run before first traffic is sent")
      println("Follow instructions at https://github.com/neykov/extract-tls-secrets for TLSv1.3")
      println("Pid: ${ProcessHandle.current().pid()}")

      Thread.sleep(10000)
    }

    val fbRequest = Request.Builder()
        .url("https://graph.facebook.com/robots.txt?s=fb")
        .build()
    val twitterRequest = Request.Builder()
        .url("https://api.twitter.com/robots.txt?s=tw")
        .build()
    val googleRequest = Request.Builder()
        .url("https://www.google.com/robots.txt?s=g")
        .build()

    for (i in 1..2) {
      // Space out traffic to make it easier to demarcate.
      sendTestRequest(fbRequest)
      Thread.sleep(1000)
      sendTestRequest(twitterRequest)
      Thread.sleep(1000)
      sendTestRequest(googleRequest)
      Thread.sleep(2000)
    }
  }