refactor(launch): 统一代理的使用.

之前的版本中, 如果未指定 Maven 仓库的独立代理配置, 同时 Bot 拥有独立代理配置的情况下, Aether 将不会使用 Bot
的独立代理配置, 这样弄比较乱, 因此统一代理配置的使用顺序:
- 如果配置中包括了代理配置, 则优先使用独立代理配置;
- 如果不包括独立代理配置, 则使用关联 Bot 的独立代理配置;
- 如果关联 Bot 没有独立代理配置, 则使用 AppConfig 中的全局配置(如无配置则直连访问).
This commit is contained in:
LamGC 2022-07-16 20:46:15 +08:00
parent a8a0a9576f
commit 93b9c6b727
Signed by: LamGC
GPG Key ID: 6C5AE2A913941E1D
2 changed files with 36 additions and 20 deletions

View File

@ -40,13 +40,28 @@ internal fun ProxyConfig.toAetherProxy(): Proxy? {
return Proxy(typeStr, host, port)
}
internal fun MavenRepositoryConfig.toRemoteRepository(proxyConfig: ProxyConfig): RemoteRepository {
val builder =
RemoteRepository.Builder(id ?: createDefaultRepositoryId(), checkRepositoryLayout(layout), url.toString())
internal fun MavenRepositoryConfig.toRemoteRepository(proxyConfig: ProxyConfig? = null): RemoteRepository {
val repositoryId = if (id == null) {
val generatedRepoId = createDefaultRepositoryId()
log.debug { "仓库 Url `$url` 未设置仓库 Id, 已分配缺省 Id: $generatedRepoId" }
generatedRepoId
} else {
id
}
val builder = RemoteRepository.Builder(repositoryId, checkRepositoryLayout(layout), url.toString())
if (proxy != null) {
builder.setProxy(proxy)
} else if (proxyConfig.type == ProxyType.HTTP) {
val selfProxy = proxy!!
builder.setProxy(selfProxy)
log.debug { "仓库 $repositoryId 已使用独立的代理配置: ${selfProxy.type}://${selfProxy.host}:${selfProxy.port}" }
} else if (proxyConfig != null) {
if (proxyConfig.type in (ProxyType.HTTP..ProxyType.HTTPS)) {
builder.setProxy(proxyConfig.toAetherProxy())
log.debug { "仓库 $repositoryId 已使用 全局/Bot 代理配置: $proxyConfig" }
} else {
log.debug { "仓库 $repositoryId 不支持 全局/Bot 的代理配置: `$proxyConfig` (仅支持 HTTP 和 HTTPS)" }
}
} else {
log.debug { "仓库 $repositoryId 不使用代理." }
}
builder.setReleasePolicy(

View File

@ -4,10 +4,7 @@ import com.google.gson.JsonParseException
import io.prometheus.client.exporter.HTTPServer
import kotlinx.coroutines.runBlocking
import mu.KotlinLogging
import net.lamgc.scalabot.config.AppConfig
import net.lamgc.scalabot.config.BotConfig
import net.lamgc.scalabot.config.MetricsConfig
import net.lamgc.scalabot.config.ProxyType
import net.lamgc.scalabot.config.*
import net.lamgc.scalabot.util.registerShutdownHook
import org.eclipse.aether.repository.LocalRepository
import org.telegram.telegrambots.bots.DefaultBotOptions
@ -154,16 +151,20 @@ internal class Launcher(private val config: AppConfig = Const.config) : AutoClos
return
}
log.info { "正在启动机器人 `${botConfig.account.name}`..." }
val botOption = DefaultBotOptions().apply {
val proxyConfig =
if (botConfig.proxy.type != ProxyType.NO_PROXY) {
log.debug { "[Bot ${botConfig.account.name}] 使用独立代理: ${botConfig.proxy.type}" }
botConfig.proxy
} else if (config.proxy.type != ProxyType.NO_PROXY) {
log.debug { "[Bot ${botConfig.account.name}] 使用全局代理: ${botConfig.proxy.type}" }
config.proxy
} else {
null
log.debug { "[Bot ${botConfig.account.name}] 不使用代理." }
ProxyConfig(type = ProxyType.NO_PROXY)
}
if (proxyConfig != null) {
val botOption = DefaultBotOptions().apply {
if (proxyConfig.type != ProxyType.NO_PROXY) {
proxyType = proxyConfig.type.toTelegramBotsType()
proxyHost = config.proxy.host
proxyPort = config.proxy.port
@ -175,7 +176,7 @@ internal class Launcher(private val config: AppConfig = Const.config) : AutoClos
val account = botConfig.account
val remoteRepositories = config.mavenRepositories
.map { it.toRemoteRepository(config.proxy) }
.map { it.toRemoteRepository(proxyConfig) }
.toMutableList().apply {
if (this.none {
it.url == MavenRepositoryExtensionFinder.MAVEN_CENTRAL_URL