Skip to content

Commit

Permalink
feat: 提高SVN-PROXY超时时间 TencentBlueKing#1535 (TencentBlueKing#1536)
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlkl authored Dec 7, 2023
1 parent 0636476 commit 1cf8cb9
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.tencent.bkrepo.common.api.util.BasicAuthUtils
import com.tencent.bkrepo.common.service.util.okhttp.HttpClientBuilderFactory
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import okhttp3.Response
Expand All @@ -15,10 +16,10 @@ import org.slf4j.LoggerFactory
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

object HttpProxyUtil {
private val logger = LoggerFactory.getLogger(HttpProxyUtil::class.java)
private val client = HttpClientBuilderFactory.create().build()
private val defaultProxyCallHandler = DefaultProxyCallHandler()
class HttpProxyUtil(
private val client: OkHttpClient = HttpClientBuilderFactory.create().build(),
private val defaultProxyCallHandler: ProxyCallHandler = DefaultProxyCallHandler()
) {
fun proxy(
proxyRequest: HttpServletRequest,
proxyResponse: HttpServletResponse,
Expand Down Expand Up @@ -46,37 +47,6 @@ object HttpProxyUtil {
proxyCallHandler.after(proxyRequest, proxyResponse, newResponse)
}

fun HttpServletRequest.headers(): Map<String, String> {
val headers = mutableMapOf<String, String>()
val headerNames = this.headerNames
while (headerNames.hasMoreElements()) {
val headerName = headerNames.nextElement()
headers[headerName] = this.getHeader(headerName)
}
return headers
}

fun HttpServletRequest.body(): RequestBody? {
val isChunked = headers()[HttpHeaders.TRANSFER_ENCODING] == "chunked"
if (this.contentLengthLong <= 0 && !isChunked) {
return null
}
val mediaType = this.contentType?.toMediaTypeOrNull()
val inputStream = this.inputStream
val contentLength = this.contentLengthLong
return object : RequestBody() {
override fun contentType(): MediaType? = mediaType

override fun contentLength(): Long = contentLength

override fun writeTo(sink: BufferedSink) {
inputStream.source().use {
sink.writeAll(it)
}
}
}
}

private fun HttpServletRequest.accessLog(upRes: Response) {
var user = "-"
if (getHeader(HttpHeaders.AUTHORIZATION).orEmpty().startsWith(BASIC_AUTH_PREFIX)) {
Expand All @@ -92,4 +62,39 @@ object HttpProxyUtil {
"user:$user up_status: ${upRes.code} ms:$requestTime up:$url agent:$httpUserAgent $requestBodyBytes",
)
}

companion object {
private val logger = LoggerFactory.getLogger(HttpProxyUtil::class.java)

fun HttpServletRequest.headers(): Map<String, String> {
val headers = mutableMapOf<String, String>()
val headerNames = this.headerNames
while (headerNames.hasMoreElements()) {
val headerName = headerNames.nextElement()
headers[headerName] = this.getHeader(headerName)
}
return headers
}

fun HttpServletRequest.body(): RequestBody? {
val isChunked = headers()[HttpHeaders.TRANSFER_ENCODING] == "chunked"
if (this.contentLengthLong <= 0 && !isChunked) {
return null
}
val mediaType = this.contentType?.toMediaTypeOrNull()
val inputStream = this.inputStream
val contentLength = this.contentLengthLong
return object : RequestBody() {
override fun contentType(): MediaType? = mediaType

override fun contentLength(): Long = contentLength

override fun writeTo(sink: BufferedSink) {
inputStream.source().use {
sink.writeAll(it)
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ import com.tencent.bkrepo.common.artifact.pojo.configuration.proxy.ProxyConfigur
import com.tencent.bkrepo.common.artifact.repository.context.ArtifactContextHolder
import com.tencent.bkrepo.common.service.util.proxy.DefaultProxyCallHandler
import com.tencent.bkrepo.common.service.util.proxy.HttpProxyUtil
import okhttp3.Response
import org.slf4j.LoggerFactory
import org.springframework.web.servlet.HandlerInterceptor
import org.springframework.web.servlet.HandlerMapping
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import okhttp3.Response
import org.slf4j.LoggerFactory

class ProxyInterceptor : HandlerInterceptor {
private val lfsProxyHandler = LfsProxyHandler()
private val httpProxyUtil = HttpProxyUtil()

override fun preHandle(request: HttpServletRequest, response: HttpServletResponse, handler: Any): Boolean {
request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE) ?: return false
val repo = ArtifactContextHolder.getRepoDetail()!!
Expand All @@ -31,9 +33,9 @@ class ProxyInterceptor : HandlerInterceptor {
val configuration = repo.configuration as ProxyConfiguration
val proxyUrl = configuration.proxy.url
if (request.requestURI.endsWith(LFS_BATCH_URI)) {
HttpProxyUtil.proxy(request, response, proxyUrl, gitRepoKey, lfsProxyHandler)
httpProxyUtil.proxy(request, response, proxyUrl, gitRepoKey, lfsProxyHandler)
} else {
HttpProxyUtil.proxy(request, response, proxyUrl, gitRepoKey)
httpProxyUtil.proxy(request, response, proxyUrl, gitRepoKey)
}

return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import com.tencent.bkrepo.common.security.util.SecurityUtils
import com.tencent.bkrepo.common.service.util.HeaderUtils
import com.tencent.bkrepo.common.service.util.HttpContextHolder
import com.tencent.bkrepo.common.service.util.okhttp.HttpClientBuilderFactory
import com.tencent.bkrepo.common.service.util.proxy.HttpProxyUtil.headers
import com.tencent.bkrepo.common.service.util.proxy.HttpProxyUtil.Companion.headers
import com.tencent.bkrepo.common.storage.innercos.http.toRequestBody
import com.tencent.bkrepo.lfs.artifact.LfsArtifactInfo
import com.tencent.bkrepo.lfs.artifact.LfsProperties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ package com.tencent.bkrepo.svn.interceptor
import com.tencent.bkrepo.common.api.constant.HttpHeaders
import com.tencent.bkrepo.common.api.constant.ensurePrefix
import com.tencent.bkrepo.common.service.util.proxy.DefaultProxyCallHandler
import com.tencent.bkrepo.common.service.util.proxy.HttpProxyUtil.headers
import com.tencent.bkrepo.common.service.util.proxy.HttpProxyUtil.Companion.headers
import com.tencent.bkrepo.svn.config.SvnProperties
import okhttp3.HttpUrl
import okhttp3.MediaType.Companion.toMediaType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,22 @@ import com.tencent.bkrepo.common.artifact.pojo.RepositoryCategory
import com.tencent.bkrepo.common.artifact.pojo.configuration.proxy.ProxyConfiguration
import com.tencent.bkrepo.common.artifact.repository.context.ArtifactContextHolder
import com.tencent.bkrepo.common.service.util.HttpContextHolder
import com.tencent.bkrepo.common.service.util.okhttp.HttpClientBuilderFactory
import com.tencent.bkrepo.common.service.util.proxy.HttpProxyUtil
import com.tencent.bkrepo.common.service.util.proxy.ProxyCallHandler
import com.tencent.bkrepo.svn.utils.SvnProxyHelper.getRepoId
import org.springframework.web.servlet.HandlerInterceptor
import java.util.concurrent.TimeUnit
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

class ProxyInterceptor(private val proxyHandler: ProxyCallHandler) : HandlerInterceptor {
private val client = HttpClientBuilderFactory.create()
.readTimeout(15, TimeUnit.MINUTES)
.writeTimeout(15, TimeUnit.MINUTES)
.build()
private val httpProxyUtil = HttpProxyUtil(client)

override fun preHandle(request: HttpServletRequest, response: HttpServletResponse, handler: Any): Boolean {
val repositoryId = getRepoId(request) ?: return false
val repo = ArtifactContextHolder.getRepoDetail(repositoryId)
Expand All @@ -50,7 +58,7 @@ class ProxyInterceptor(private val proxyHandler: ProxyCallHandler) : HandlerInte
val configuration = repo.configuration as ProxyConfiguration
val proxyUrl = configuration.proxy.url
val prefix = "/${repo.projectId}/${repo.name}"
HttpProxyUtil.proxy(
httpProxyUtil.proxy(
HttpContextHolder.getRequest(),
HttpContextHolder.getResponse(),
proxyUrl,
Expand Down

0 comments on commit 1cf8cb9

Please sign in to comment.