mirror of
https://github.com/LamGC/ScalaBot.git
synced 2025-04-30 06:37:29 +00:00
refactor(launch): 统一代理的使用.
之前的版本中, 如果未指定 Maven 仓库的独立代理配置, 同时 Bot 拥有独立代理配置的情况下, Aether 将不会使用 Bot 的独立代理配置, 这样弄比较乱, 因此统一代理配置的使用顺序: - 如果配置中包括了代理配置, 则优先使用独立代理配置; - 如果不包括独立代理配置, 则使用关联 Bot 的独立代理配置; - 如果关联 Bot 没有独立代理配置, 则使用 AppConfig 中的全局配置(如无配置则直连访问).
This commit is contained in:
parent
a8a0a9576f
commit
93b9c6b727
@ -40,13 +40,28 @@ internal fun ProxyConfig.toAetherProxy(): Proxy? {
|
|||||||
return Proxy(typeStr, host, port)
|
return Proxy(typeStr, host, port)
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun MavenRepositoryConfig.toRemoteRepository(proxyConfig: ProxyConfig): RemoteRepository {
|
internal fun MavenRepositoryConfig.toRemoteRepository(proxyConfig: ProxyConfig? = null): RemoteRepository {
|
||||||
val builder =
|
val repositoryId = if (id == null) {
|
||||||
RemoteRepository.Builder(id ?: createDefaultRepositoryId(), checkRepositoryLayout(layout), url.toString())
|
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) {
|
if (proxy != null) {
|
||||||
builder.setProxy(proxy)
|
val selfProxy = proxy!!
|
||||||
} else if (proxyConfig.type == ProxyType.HTTP) {
|
builder.setProxy(selfProxy)
|
||||||
builder.setProxy(proxyConfig.toAetherProxy())
|
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(
|
builder.setReleasePolicy(
|
||||||
|
@ -4,10 +4,7 @@ import com.google.gson.JsonParseException
|
|||||||
import io.prometheus.client.exporter.HTTPServer
|
import io.prometheus.client.exporter.HTTPServer
|
||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.runBlocking
|
||||||
import mu.KotlinLogging
|
import mu.KotlinLogging
|
||||||
import net.lamgc.scalabot.config.AppConfig
|
import net.lamgc.scalabot.config.*
|
||||||
import net.lamgc.scalabot.config.BotConfig
|
|
||||||
import net.lamgc.scalabot.config.MetricsConfig
|
|
||||||
import net.lamgc.scalabot.config.ProxyType
|
|
||||||
import net.lamgc.scalabot.util.registerShutdownHook
|
import net.lamgc.scalabot.util.registerShutdownHook
|
||||||
import org.eclipse.aether.repository.LocalRepository
|
import org.eclipse.aether.repository.LocalRepository
|
||||||
import org.telegram.telegrambots.bots.DefaultBotOptions
|
import org.telegram.telegrambots.bots.DefaultBotOptions
|
||||||
@ -154,16 +151,20 @@ internal class Launcher(private val config: AppConfig = Const.config) : AutoClos
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.info { "正在启动机器人 `${botConfig.account.name}`..." }
|
log.info { "正在启动机器人 `${botConfig.account.name}`..." }
|
||||||
|
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 {
|
||||||
|
log.debug { "[Bot ${botConfig.account.name}] 不使用代理." }
|
||||||
|
ProxyConfig(type = ProxyType.NO_PROXY)
|
||||||
|
}
|
||||||
|
|
||||||
val botOption = DefaultBotOptions().apply {
|
val botOption = DefaultBotOptions().apply {
|
||||||
val proxyConfig =
|
if (proxyConfig.type != ProxyType.NO_PROXY) {
|
||||||
if (botConfig.proxy.type != ProxyType.NO_PROXY) {
|
|
||||||
botConfig.proxy
|
|
||||||
} else if (config.proxy.type != ProxyType.NO_PROXY) {
|
|
||||||
config.proxy
|
|
||||||
} else {
|
|
||||||
null
|
|
||||||
}
|
|
||||||
if (proxyConfig != null) {
|
|
||||||
proxyType = proxyConfig.type.toTelegramBotsType()
|
proxyType = proxyConfig.type.toTelegramBotsType()
|
||||||
proxyHost = config.proxy.host
|
proxyHost = config.proxy.host
|
||||||
proxyPort = config.proxy.port
|
proxyPort = config.proxy.port
|
||||||
@ -175,7 +176,7 @@ internal class Launcher(private val config: AppConfig = Const.config) : AutoClos
|
|||||||
val account = botConfig.account
|
val account = botConfig.account
|
||||||
|
|
||||||
val remoteRepositories = config.mavenRepositories
|
val remoteRepositories = config.mavenRepositories
|
||||||
.map { it.toRemoteRepository(config.proxy) }
|
.map { it.toRemoteRepository(proxyConfig) }
|
||||||
.toMutableList().apply {
|
.toMutableList().apply {
|
||||||
if (this.none {
|
if (this.none {
|
||||||
it.url == MavenRepositoryExtensionFinder.MAVEN_CENTRAL_URL
|
it.url == MavenRepositoryExtensionFinder.MAVEN_CENTRAL_URL
|
||||||
|
Loading…
Reference in New Issue
Block a user