Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Host IP address in the overview and sharable #1180

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ internal class HttpTransaction(
@ColumnInfo(name = "protocol") var protocol: String?,
@ColumnInfo(name = "method") var method: String?,
@ColumnInfo(name = "url") var url: String?,
@ColumnInfo(name = "hostIp") var hostIp: String?,
@ColumnInfo(name = "host") var host: String?,
@ColumnInfo(name = "path") var path: String?,
@ColumnInfo(name = "scheme") var scheme: String?,
Expand Down Expand Up @@ -70,6 +71,7 @@ internal class HttpTransaction(
protocol = null,
method = null,
url = null,
hostIp = null,
host = null,
path = null,
scheme = null,
Expand Down Expand Up @@ -317,6 +319,7 @@ internal class HttpTransaction(
(protocol == other.protocol) &&
(method == other.method) &&
(url == other.url) &&
(hostIp == other.hostIp) &&
(host == other.host) &&
(path == other.path) &&
(scheme == other.scheme) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import androidx.room.Room
import androidx.room.RoomDatabase
import com.chuckerteam.chucker.internal.data.entity.HttpTransaction

@Database(entities = [HttpTransaction::class], version = 9, exportSchema = false)
@Database(entities = [HttpTransaction::class], version = 10, exportSchema = false)
internal abstract class ChuckerDatabase : RoomDatabase() {
abstract fun transactionDao(): HttpTransactionDao

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.chuckerteam.chucker.internal.support

import okio.BufferedSource
import java.util.regex.Matcher
import java.util.regex.Pattern

private const val IP_REGEX = "(?:\\d{1,3}\\.){3}\\d{1,3}"

public fun BufferedSource.getHostIp(): String? {
val body = readUtf8()
Copy link
Contributor

@MiSikora MiSikora Mar 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reads the whole body of the source to the memory and assumes that it is UTF-8 encoded.

val pattern: Pattern = Pattern.compile(IP_REGEX)
val matcher: Matcher? = body.let { pattern.matcher(it) }
if (matcher?.find() == true) {
return matcher.group()
}
return null
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ internal class ResponseProcessor(
requestDate = response.sentRequestAtMillis
responseDate = response.receivedResponseAtMillis
protocol = response.protocol.toString()
hostIp = response.body?.source()?.getHostIp()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm having trouble understanding this. Why is it assumed that the response body contains IP?

responseCode = response.code
responseMessage = response.message

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ internal class TransactionDetailsSharable(
override fun toSharableContent(context: Context): Source =
Buffer().apply {
writeUtf8("${context.getString(R.string.chucker_url)}: ${transaction.getFormattedUrl(encodeUrls)}\n")
writeUtf8("${context.getString(R.string.chucker_host_ip)}: ${transaction.hostIp}\n")
writeUtf8("${context.getString(R.string.chucker_method)}: ${transaction.method}\n")
writeUtf8("${context.getString(R.string.chucker_protocol)}: ${transaction.protocol}\n")
writeUtf8("${context.getString(R.string.chucker_status)}: ${transaction.status}\n")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ internal class TransactionOverviewFragment : Fragment() {
) {
with(overviewBinding) {
url.text = transaction?.getFormattedUrl(encodeUrl)
hostIp.text = transaction?.hostIp
method.text = transaction?.method
protocol.text = transaction?.protocol
status.text = transaction?.status.toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,35 @@
app:layout_constraintTop_toTopOf="parent"
tools:text="https://example.com/path/to/resource?here=might_be_really_long" />

<TextView
style="@style/Chucker.TextAppearance.Label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="start"
android:text="@string/chucker_host_ip"
app:layout_constraintEnd_toStartOf="@id/overviewGuideline"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/url" />

<TextView
android:id="@+id/host_ip"
style="@style/Chucker.TextAppearance.Value"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="start"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@id/overviewGuideline"
app:layout_constraintTop_toBottomOf="@+id/url"
tools:text="192.168.1.1" />

<TextView
style="@style/Chucker.TextAppearance.Label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/chucker_method"
app:layout_constraintEnd_toStartOf="@id/overviewGuideline"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/url" />
app:layout_constraintTop_toBottomOf="@id/host_ip" />

<TextView
android:id="@+id/method"
Expand All @@ -54,7 +75,7 @@
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@id/overviewGuideline"
app:layout_constraintTop_toBottomOf="@+id/url"
app:layout_constraintTop_toBottomOf="@+id/host_ip"
tools:text="GET" />

<TextView
Expand Down
1 change: 1 addition & 0 deletions library/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<string name="chucker_request">Request</string>
<string name="chucker_response">Response</string>
<string name="chucker_url">URL</string>
<string name="chucker_host_ip">Host IP</string>
<string name="chucker_method">Method</string>
<string name="chucker_protocol">Protocol</string>
<string name="chucker_status">Status</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ internal fun HttpTransaction.withResponseData(): HttpTransaction =
responseCode = 418 // I'm a teapot
responseDate = 321L
tookMs = 21L
hostIp = "192.168.1.1"
responseTlsVersion = randomString()
responseCipherSuite = randomString()
responsePayloadSize = 0L
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.chuckerteam.chucker.internal.data.har

import com.chuckerteam.chucker.internal.data.har.log.entry.response.Content
import com.chuckerteam.chucker.internal.support.getHostIp
import com.chuckerteam.chucker.util.HarTestUtils
import com.google.common.truth.Truth.assertThat
import okhttp3.mockwebserver.MockResponse
import org.junit.Test

internal class ResponseTest {
Expand Down Expand Up @@ -48,4 +50,24 @@ internal class ResponseTest {

assertThat(response?.bodySize).isEqualTo(1000)
}

@Test
fun `host ip address from response successfully`() {
val body =
"{\"args\": {},\"origin\": \"192.168.1.1\", \"url\": \"https://httpbin.org/get\"}"
val response = MockResponse().setBody(body).setResponseCode(200)
val buffer = response.getBody()
val hostIp = buffer?.getHostIp()
assertThat(hostIp).isEqualTo("192.168.1.1")
}

@Test
fun `host ip address from response doesn't exist`() {
val body =
"{\"args\": {}, \"url\": \"https://httpbin.org/get\"}"
val response = MockResponse().setBody(body).setResponseCode(200)
val buffer = response.getBody()
val hostIp = buffer?.getHostIp()
assertThat(hostIp).isEqualTo(null)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ internal class HttpTransactionDaoTest {
assertThat(stringValue("responseHeaders")).isEqualTo(data.responseHeaders)
assertThat(stringValue("method")).isEqualTo(data.method)
assertThat(stringValue("url")).isEqualTo(data.url)
assertThat(stringValue("hostIp")).isEqualTo(data.hostIp)
assertThat(stringValue("host")).isEqualTo(data.host)
assertThat(stringValue("path")).isEqualTo(data.path)
assertThat(stringValue("scheme")).isEqualTo(data.scheme)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ internal object HarTestUtils {
protocol = "HTTP",
method = method,
url = "http://localhost:80/getUsers",
hostIp = "192.168.1.1",
host = "localhost",
path = "/getUsers",
scheme = "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ internal object TestTransactionFactory {
protocol = "HTTP",
method = method,
url = "http://localhost:80/getUsers",
hostIp = "192.168.1.1",
host = "localhost",
path = "/getUsers",
scheme = "",
Expand Down
Loading