From cec0c620f6b8d7b5e30130481391192254a03b92 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Fri, 27 Oct 2023 11:16:40 +0900 Subject: [PATCH 1/4] =?UTF-8?q?feat:=20=E3=83=95=E3=82=A9=E3=83=AD?= =?UTF-8?q?=E3=83=BC=E3=83=AA=E3=82=AF=E3=82=A8=E3=82=B9=E3=83=88=E3=81=8C?= =?UTF-8?q?=E9=A3=9B=E3=82=93=E3=81=A7=E3=81=8D=E3=81=9F=E3=81=A8=E3=81=8D?= =?UTF-8?q?=E3=81=AE=E3=83=AD=E3=82=B0=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hideout/service/ap/APReceiveFollowService.kt | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/dev/usbharu/hideout/service/ap/APReceiveFollowService.kt b/src/main/kotlin/dev/usbharu/hideout/service/ap/APReceiveFollowService.kt index 6d9321cd..d6d237e8 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/ap/APReceiveFollowService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/ap/APReceiveFollowService.kt @@ -13,6 +13,7 @@ import dev.usbharu.hideout.service.job.JobQueueParentService import dev.usbharu.hideout.service.user.UserService import io.ktor.http.* import kjob.core.job.JobProps +import org.slf4j.LoggerFactory import org.springframework.beans.factory.annotation.Qualifier import org.springframework.stereotype.Service @@ -32,7 +33,7 @@ class APReceiveFollowServiceImpl( private val apRequestService: APRequestService ) : APReceiveFollowService { override suspend fun receiveFollow(follow: Follow): ActivityPubResponse { - // TODO: Verify HTTP Signature + logger.info("FOLLOW from: {} to: {}", follow.actor, follow.`object`) jobQueueParentService.schedule(ReceiveFollowJob) { props[ReceiveFollowJob.actor] = follow.actor props[ReceiveFollowJob.follow] = objectMapper.writeValueAsString(follow) @@ -42,12 +43,12 @@ class APReceiveFollowServiceImpl( } override suspend fun receiveFollowJob(props: JobProps) { -// throw Exception() transaction.transaction { val actor = props[ReceiveFollowJob.actor] val targetActor = props[ReceiveFollowJob.targetActor] val person = apUserService.fetchPerson(actor, targetActor) val follow = objectMapper.readValue(props[ReceiveFollowJob.follow]) + logger.info("START Follow from: {} to: {}", targetActor, actor) val signer = userQueryService.findByUrl(targetActor) @@ -68,6 +69,11 @@ class APReceiveFollowServiceImpl( userQueryService.findByUrl(follow.actor ?: throw java.lang.IllegalArgumentException("Actor is null")) userService.followRequest(targetEntity.id, followActorEntity.id) + logger.info("SUCCESS Follow from: {} to: {}", targetActor, actor) } } + + companion object { + private val logger = LoggerFactory.getLogger(APReceiveFollowServiceImpl::class.java) + } } From 94900cc32c5e783ac1c18709a1641f01fe92df32 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Fri, 27 Oct 2023 11:17:05 +0900 Subject: [PATCH 2/4] =?UTF-8?q?feat:=20ActivityPub=E3=83=AA=E3=82=AF?= =?UTF-8?q?=E3=82=A8=E3=82=B9=E3=83=88=E3=81=AE=E3=83=AD=E3=82=B0=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/ap/APRequestServiceImpl.kt | 58 ++++++++++++++++--- 1 file changed, 49 insertions(+), 9 deletions(-) diff --git a/src/main/kotlin/dev/usbharu/hideout/service/ap/APRequestServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/ap/APRequestServiceImpl.kt index 3106f131..b8074037 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/ap/APRequestServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/ap/APRequestServiceImpl.kt @@ -34,6 +34,7 @@ class APRequestServiceImpl( ) : APRequestService { override suspend fun apGet(url: String, signer: User?, responseClass: Class): R { + logger.debug("START ActivityPub Request GET url: {}, signer: {}", url, signer?.url) val date = dateTimeFormatter.format(ZonedDateTime.now(ZoneId.of("GMT"))) val u = URL(url) if (signer?.privateKey == null) { @@ -63,7 +64,7 @@ class APRequestServiceImpl( signHeaders = listOf("(request-target)", "date", "host", "accept") ) - val bodyAsText = httpClient.get(url) { + val httpResponse = httpClient.get(url) { headers { headers { appendAll(headers) @@ -72,8 +73,24 @@ class APRequestServiceImpl( } } contentType(ContentType.Application.Activity) - }.bodyAsText() - return objectMapper.readValue(bodyAsText, responseClass) + } + val bodyAsText = httpResponse.bodyAsText() + val readValue = objectMapper.readValue(bodyAsText, responseClass) + logger.debug( + "SUCCESS ActivityPub Request GET status: {} url: {}", + httpResponse.status, + httpResponse.request.url + ) + logger.trace( + """ + |***** BEGIN HTTP Response Trace url: {} ***** + | + |$bodyAsText + | + |***** END HTTP Response TRACE url: {} ***** + """.trimMargin(), url, url + ) + return readValue } override suspend fun apPost( @@ -87,6 +104,7 @@ class APRequestServiceImpl( } override suspend fun apPost(url: String, body: T?, signer: User?): String { + logger.debug("START ActivityPub Request POST url: {}, signer: {}", url, signer?.url) if (body != null) { val mutableListOf = mutableListOf() mutableListOf.add("https://www.w3.org/ns/activitystreams") @@ -96,6 +114,16 @@ class APRequestServiceImpl( val requestBody = objectMapper.writeValueAsString(body) + logger.trace( + """ + |***** BEGIN HTTP Request Trace url: {} ***** + | + |$requestBody + | + |***** END HTTP Request Trace url: {} ***** + """.trimMargin(), url, url + ) + val sha256 = MessageDigest.getInstance("SHA-256") val digest = Base64Util.encode(sha256.digest(requestBody.toByteArray())) @@ -103,8 +131,6 @@ class APRequestServiceImpl( val date = dateTimeFormatter.format(ZonedDateTime.now(ZoneId.of("GMT"))) val u = URL(url) if (signer?.privateKey == null) { - logger.debug("NOT SIGN Request: {}", url) - logger.trace("{}", signer) return httpClient.post(url) { header("Accept", ContentType.Application.Activity) header("Date", date) @@ -114,8 +140,6 @@ class APRequestServiceImpl( }.bodyAsText() } - logger.debug("SIGN Request: {}", url) - val headers = headers { append("Accept", ContentType.Application.Activity) append("Date", date) @@ -136,7 +160,7 @@ class APRequestServiceImpl( signHeaders = listOf("(request-target)", "date", "host", "digest") ) - return httpClient.post(url) { + val httpResponse = httpClient.post(url) { headers { headers { appendAll(headers) @@ -146,7 +170,23 @@ class APRequestServiceImpl( } setBody(requestBody) contentType(ContentType.Application.Activity) - }.bodyAsText() + } + val bodyAsText = httpResponse.bodyAsText() + logger.debug( + "SUCCESS ActivityPub Request POST status: {} url: {}", + httpResponse.status, + httpResponse.request.url + ) + logger.trace( + """ + |***** BEGIN HTTP Response Trace url: {} ***** + | + |$bodyAsText + | + |***** END HTTP Response TRACE url: {} ***** + """.trimMargin(), url, url + ) + return bodyAsText } companion object { From c1dc1011b3bb9257c4002d9de0ebd40acecb8c43 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Fri, 27 Oct 2023 11:28:35 +0900 Subject: [PATCH 3/4] =?UTF-8?q?feat:=20=E7=BD=B2=E5=90=8D=E3=81=95?= =?UTF-8?q?=E3=82=8C=E3=81=AA=E3=81=84=E3=83=AA=E3=82=AF=E3=82=A8=E3=82=B9?= =?UTF-8?q?=E3=83=88=E3=82=82=E3=83=AD=E3=82=B0=E3=82=92=E3=81=A8=E3=82=8B?= =?UTF-8?q?=E3=82=88=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/ap/APRequestServiceImpl.kt | 37 ++++++++++--------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/src/main/kotlin/dev/usbharu/hideout/service/ap/APRequestServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/ap/APRequestServiceImpl.kt index b8074037..75eb4fa4 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/ap/APRequestServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/ap/APRequestServiceImpl.kt @@ -42,6 +42,7 @@ class APRequestServiceImpl( header("Accept", ContentType.Application.Activity) header("Date", date) }.bodyAsText() + logBody(bodyAsText, url) return objectMapper.readValue(bodyAsText, responseClass) } @@ -81,15 +82,7 @@ class APRequestServiceImpl( httpResponse.status, httpResponse.request.url ) - logger.trace( - """ - |***** BEGIN HTTP Response Trace url: {} ***** - | - |$bodyAsText - | - |***** END HTTP Response TRACE url: {} ***** - """.trimMargin(), url, url - ) + logBody(bodyAsText, url) return readValue } @@ -116,11 +109,13 @@ class APRequestServiceImpl( logger.trace( """ + | |***** BEGIN HTTP Request Trace url: {} ***** | |$requestBody | |***** END HTTP Request Trace url: {} ***** + | """.trimMargin(), url, url ) @@ -131,13 +126,15 @@ class APRequestServiceImpl( val date = dateTimeFormatter.format(ZonedDateTime.now(ZoneId.of("GMT"))) val u = URL(url) if (signer?.privateKey == null) { - return httpClient.post(url) { + val bodyAsText = httpClient.post(url) { header("Accept", ContentType.Application.Activity) header("Date", date) header("Digest", "sha-256=$digest") setBody(requestBody) contentType(ContentType.Application.Activity) }.bodyAsText() + logBody(bodyAsText, url) + return bodyAsText } val headers = headers { @@ -177,16 +174,22 @@ class APRequestServiceImpl( httpResponse.status, httpResponse.request.url ) + logBody(bodyAsText, url) + return bodyAsText + } + + private fun logBody(bodyAsText: String, url: String) { logger.trace( """ - |***** BEGIN HTTP Response Trace url: {} ***** - | - |$bodyAsText - | - |***** END HTTP Response TRACE url: {} ***** - """.trimMargin(), url, url + | + |***** BEGIN HTTP Response Trace url: {} ***** + | + |$bodyAsText + | + |***** END HTTP Response TRACE url: {} ***** + | + """.trimMargin(), url, url ) - return bodyAsText } companion object { From eebcc09c5a6bb1559a44d3579b1265c45609fee2 Mon Sep 17 00:00:00 2001 From: usbharu Date: Fri, 27 Oct 2023 11:38:21 +0900 Subject: [PATCH 4/4] Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .../usbharu/hideout/service/ap/APRequestServiceImpl.kt | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/dev/usbharu/hideout/service/ap/APRequestServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/ap/APRequestServiceImpl.kt index 75eb4fa4..37a76333 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/ap/APRequestServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/ap/APRequestServiceImpl.kt @@ -116,7 +116,9 @@ class APRequestServiceImpl( | |***** END HTTP Request Trace url: {} ***** | - """.trimMargin(), url, url + """.trimMargin(), + url, + url ) val sha256 = MessageDigest.getInstance("SHA-256") @@ -188,7 +190,9 @@ class APRequestServiceImpl( | |***** END HTTP Response TRACE url: {} ***** | - """.trimMargin(), url, url + """.trimMargin(), + url, + url ) }