-
-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(kobo): better handling of missing port from Kobo Sync requests
- Loading branch information
Showing
10 changed files
with
216 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
komga/src/main/kotlin/org/gotson/komga/infrastructure/web/KoboMissingPortFilter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package org.gotson.komga.infrastructure.web | ||
|
||
import jakarta.servlet.FilterChain | ||
import jakarta.servlet.http.HttpServletRequest | ||
import jakarta.servlet.http.HttpServletRequestWrapper | ||
import jakarta.servlet.http.HttpServletResponse | ||
import org.springframework.web.filter.OncePerRequestFilter | ||
|
||
class KoboMissingPortFilter(private val koboPortSupplier: () -> Int?) : OncePerRequestFilter() { | ||
companion object { | ||
private val FORWARDED_HEADER_NAMES = | ||
setOf( | ||
"Forwarded", | ||
"X-Forwarded-Host", | ||
"X-Forwarded-Port", | ||
"X-Forwarded-Proto", | ||
"X-Forwarded-Prefix", | ||
"X-Forwarded-Ssl", | ||
"X-Forwarded-For", | ||
) | ||
} | ||
|
||
override fun shouldNotFilter(request: HttpServletRequest): Boolean { | ||
// ignore this filter if forwarded headers are present | ||
FORWARDED_HEADER_NAMES.forEach { if (request.getHeader(it) != null) return true } | ||
return false | ||
} | ||
|
||
override fun shouldNotFilterAsyncDispatch(): Boolean = false | ||
|
||
override fun shouldNotFilterErrorDispatch(): Boolean = false | ||
|
||
override fun doFilterInternal( | ||
request: HttpServletRequest, | ||
response: HttpServletResponse, | ||
filterChain: FilterChain, | ||
) { | ||
val wrappedRequest = | ||
try { | ||
KoboMissingPortRequest(request, koboPortSupplier) | ||
} catch (ex: Throwable) { | ||
if (logger.isDebugEnabled) logger.debug("Failed to apply missing port to " + formatRequest(request), ex) | ||
response.sendError(HttpServletResponse.SC_BAD_REQUEST) | ||
return | ||
} | ||
filterChain.doFilter(wrappedRequest, response) | ||
} | ||
|
||
override fun doFilterNestedErrorDispatch( | ||
request: HttpServletRequest, | ||
response: HttpServletResponse, | ||
filterChain: FilterChain, | ||
) { | ||
doFilterInternal(request, response, filterChain) | ||
} | ||
|
||
private fun formatRequest(request: HttpServletRequest) = "HTTP ${request.method} \"${request.requestURI}\"" | ||
|
||
private class KoboMissingPortRequest(request: HttpServletRequest, val port: () -> Int?) : HttpServletRequestWrapper(request) { | ||
override fun getServerPort() = port() ?: request.serverPort | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...src/main/kotlin/org/gotson/komga/infrastructure/web/KoboMissingPortFilterConfiguration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.gotson.komga.infrastructure.web | ||
|
||
import jakarta.annotation.PostConstruct | ||
import org.gotson.komga.infrastructure.configuration.KomgaSettingsProvider | ||
import org.springframework.boot.web.servlet.FilterRegistrationBean | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.core.Ordered | ||
import org.springframework.web.filter.ForwardedHeaderFilter | ||
|
||
@Configuration | ||
class KoboMissingPortFilterConfiguration( | ||
private val komgaSettingsProvider: KomgaSettingsProvider, | ||
private val serverSettings: WebServerEffectiveSettings, | ||
private val forwardedHeaderFilter: FilterRegistrationBean<ForwardedHeaderFilter>, | ||
) { | ||
@Bean | ||
fun koboMissingPortFilter(): FilterRegistrationBean<out KoboMissingPortFilter> = | ||
FilterRegistrationBean( | ||
KoboMissingPortFilter { komgaSettingsProvider.koboPort ?: serverSettings.effectiveServerPort }, | ||
).apply { | ||
addUrlPatterns( | ||
"/kobo/*", | ||
) | ||
setName("koboMissingPortFilter") | ||
order = Ordered.HIGHEST_PRECEDENCE | ||
} | ||
|
||
@PostConstruct | ||
fun adjustForwardHeaderFilterOrder() { | ||
// the ForwardHeaderFilter must be after the KoboMissingPortFilter, as the latter's detection is based on forwarded headers | ||
// that the former will remove | ||
forwardedHeaderFilter.order = Ordered.HIGHEST_PRECEDENCE + 1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters