-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: 实现代理仓库 #1122 * feat: 增加云研发源ip限制 #1122 * feat: 处理ip转换 #1122 * feat: 修复大仓库下的推送报错 #1122 * feat: 支持查询代理仓库接口 #1122 * feat: 支持git lfs #1122 * feat: merge master #1122 * feat: 修复单元测试 #1122
- Loading branch information
1 parent
ad0ca30
commit 3bc6c37
Showing
24 changed files
with
524 additions
and
42 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
27 changes: 27 additions & 0 deletions
27
.../kotlin/com/tencent/bkrepo/common/artifact/pojo/configuration/proxy/ProxyConfiguration.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,27 @@ | ||
package com.tencent.bkrepo.common.artifact.pojo.configuration.proxy | ||
|
||
import com.tencent.bkrepo.common.artifact.pojo.configuration.composite.ProxyChannelSetting | ||
import com.tencent.bkrepo.common.artifact.pojo.configuration.local.LocalConfiguration | ||
import io.swagger.annotations.ApiModel | ||
import io.swagger.annotations.ApiModelProperty | ||
|
||
/** | ||
* 代理仓库配置 | ||
* */ | ||
@ApiModel("代理仓库配置") | ||
class ProxyConfiguration( | ||
/** | ||
* 代理服务器配置 | ||
* */ | ||
@ApiModelProperty("代理服务器配置") | ||
val proxy: ProxyChannelSetting = ProxyChannelSetting(false, "", ""), | ||
/** | ||
* 客户端访问的代理地址 | ||
* */ | ||
@ApiModelProperty("访问url", required = false) | ||
var url: String? = null, | ||
) : LocalConfiguration() { | ||
companion object { | ||
const val type = "proxy" | ||
} | ||
} |
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
12 changes: 12 additions & 0 deletions
12
...ce/src/main/kotlin/com/tencent/bkrepo/common/artifact/repository/proxy/ProxyRepository.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,12 @@ | ||
package com.tencent.bkrepo.common.artifact.repository.proxy | ||
|
||
import com.tencent.bkrepo.common.artifact.repository.core.AbstractArtifactRepository | ||
import org.springframework.stereotype.Service | ||
|
||
/** | ||
* 代理仓库 | ||
* | ||
* 暂时不做任务业务处理,仅用作区分仓库类型 | ||
* */ | ||
@Service | ||
class ProxyRepository : AbstractArtifactRepository() |
19 changes: 19 additions & 0 deletions
19
...e/src/main/kotlin/com/tencent/bkrepo/common/service/util/proxy/DefaultProxyCallHandler.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,19 @@ | ||
package com.tencent.bkrepo.common.service.util.proxy | ||
|
||
import javax.servlet.http.HttpServletRequest | ||
import javax.servlet.http.HttpServletResponse | ||
import okhttp3.Response | ||
|
||
open class DefaultProxyCallHandler : ProxyCallHandler { | ||
override fun after(proxyRequest: HttpServletRequest, proxyResponse: HttpServletResponse, response: Response) { | ||
// 转发状态码 | ||
proxyResponse.status = response.code | ||
// 转发头 | ||
response.headers.forEach { (key, value) -> proxyResponse.addHeader(key, value) } | ||
|
||
// 转发body | ||
response.body?.byteStream()?.use { | ||
it.copyTo(proxyResponse.outputStream) | ||
} | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
...mon-service/src/main/kotlin/com/tencent/bkrepo/common/service/util/proxy/HttpProxyUtil.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,90 @@ | ||
package com.tencent.bkrepo.common.service.util.proxy | ||
|
||
import com.tencent.bkrepo.common.api.constant.BASIC_AUTH_PREFIX | ||
import com.tencent.bkrepo.common.api.constant.HttpHeaders | ||
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.Request | ||
import okhttp3.RequestBody | ||
import okhttp3.Response | ||
import okio.BufferedSink | ||
import okio.source | ||
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() | ||
fun proxy( | ||
proxyRequest: HttpServletRequest, | ||
proxyResponse: HttpServletResponse, | ||
targetUrl: String, | ||
prefix: String? = null, | ||
proxyCallHandler: ProxyCallHandler = defaultProxyCallHandler, | ||
) { | ||
val newUrl = "$targetUrl${proxyRequest.requestURI.removePrefix(prefix.orEmpty())}?${proxyRequest.queryString}" | ||
val newRequest = Request.Builder() | ||
.url(newUrl) | ||
.apply { | ||
proxyRequest.headers().forEach { (key, value) -> this.header(key, value) } | ||
} | ||
.method(proxyRequest.method, proxyRequest.body()) | ||
.build() | ||
val newResponse = client | ||
.newCall(newRequest) | ||
.execute() | ||
proxyRequest.accessLog(newResponse) | ||
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)) { | ||
val authorizationHeader = getHeader(HttpHeaders.AUTHORIZATION).orEmpty() | ||
user = BasicAuthUtils.decode(authorizationHeader).first | ||
} | ||
val requestTime = System.currentTimeMillis() - upRes.sentRequestAtMillis | ||
val httpUserAgent = getHeader(HttpHeaders.USER_AGENT) | ||
val url = upRes.request.url.host | ||
val requestBodyBytes = contentLengthLong | ||
logger.info( | ||
"\"$method $requestURI $protocol\" - " + | ||
"user:$user up_status: ${upRes.code} ms:$requestTime up:$url agent:$httpUserAgent $requestBodyBytes", | ||
) | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...-service/src/main/kotlin/com/tencent/bkrepo/common/service/util/proxy/ProxyCallHandler.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,9 @@ | ||
package com.tencent.bkrepo.common.service.util.proxy | ||
|
||
import javax.servlet.http.HttpServletRequest | ||
import javax.servlet.http.HttpServletResponse | ||
import okhttp3.Response | ||
|
||
interface ProxyCallHandler { | ||
fun after(proxyRequest: HttpServletRequest, proxyResponse: HttpServletResponse, response: Response) | ||
} |
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
29 changes: 29 additions & 0 deletions
29
...backend/git/biz-git/src/main/kotlin/com/tencent/bkrepo/git/controller/GitLfsController.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,29 @@ | ||
package com.tencent.bkrepo.git.controller | ||
|
||
import com.tencent.bkrepo.common.api.exception.MethodNotAllowedException | ||
import com.tencent.bkrepo.common.api.pojo.Response | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.PathVariable | ||
import org.springframework.web.bind.annotation.PostMapping | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.RequestMethod | ||
import org.springframework.web.bind.annotation.RestController | ||
|
||
@RequestMapping("{projectId}/{repoName}.git") | ||
@RestController | ||
class GitLfsController { | ||
|
||
@PostMapping("/info/lfs/objects/batch") | ||
fun batch( | ||
@PathVariable projectId: String, | ||
@PathVariable repoName: String, | ||
): Response<Void> { | ||
throw MethodNotAllowedException() | ||
} | ||
|
||
@GetMapping() | ||
@RequestMapping("/content/lfs/objects/{oid}", method = [RequestMethod.GET, RequestMethod.PUT]) | ||
fun get(@PathVariable oid: String): Response<Void> { | ||
throw MethodNotAllowedException() | ||
} | ||
} |
Oops, something went wrong.