mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-26 08:27:12 -07:00
+ Proxy support in kobalt-settings:
<proxy> <host>somehost</host> <port>3128</port> <type>http</type> <!--<type>https</type>--> </proxy>
This commit is contained in:
parent
d08fe21e24
commit
68d3e5c995
7 changed files with 50 additions and 4 deletions
|
@ -30,6 +30,8 @@ fun plugins(vararg dependencies : String) {
|
|||
}
|
||||
}
|
||||
|
||||
data class ProxyConfig(var host: String = "", var port: Int = 0, val type: String = "")
|
||||
|
||||
data class HostConfig(var url: String = "", var username: String? = null, var password: String? = null) {
|
||||
fun hasAuth() : Boolean {
|
||||
return (! username.isNullOrBlank()) && (! password.isNullOrBlank())
|
||||
|
|
|
@ -3,8 +3,11 @@ package com.beust.kobalt.api
|
|||
import com.beust.kobalt.Constants
|
||||
import com.beust.kobalt.HostConfig
|
||||
import com.beust.kobalt.Plugins
|
||||
import com.beust.kobalt.ProxyConfig
|
||||
import com.google.inject.Injector
|
||||
import org.eclipse.aether.repository.Proxy
|
||||
import java.io.InputStream
|
||||
import java.net.InetSocketAddress
|
||||
import java.time.Duration
|
||||
import java.util.*
|
||||
|
||||
|
@ -14,6 +17,18 @@ class Kobalt {
|
|||
|
||||
var context: KobaltContext? = null
|
||||
|
||||
val proxyConfig = with(Kobalt.context?.settings?.proxy) {
|
||||
if (this != null) {
|
||||
ProxyConfig(host, port.toIntOr(0), type)
|
||||
} else null
|
||||
}
|
||||
|
||||
fun String.toIntOr(defaultValue: Int) = try { //TODO can be extracted to some global Utils
|
||||
toInt()
|
||||
} catch(e: NumberFormatException) {
|
||||
defaultValue
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the repos calculated from the following places:
|
||||
* - Either repos specified in settings.xml or from Constants.DEFAULT_REPOS
|
||||
|
@ -99,3 +114,10 @@ class Kobalt {
|
|||
fun findPlugin(name: String) = Plugins.findPlugin(name)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun ProxyConfig?.toProxy() = if (this != null) {
|
||||
java.net.Proxy(java.net.Proxy.Type.HTTP, InetSocketAddress(host, port))
|
||||
} else null
|
||||
|
||||
fun ProxyConfig?.toAetherProxy() = if (this != null) Proxy(type, host, port) else null //TODO make support for proxy auth
|
||||
|
|
|
@ -22,6 +22,9 @@ class KobaltSettingsXml {
|
|||
@XmlElement(name = "default-repos") @JvmField
|
||||
var defaultRepos: DefaultReposXml? = null
|
||||
|
||||
@XmlElement(name = "proxy") @JvmField
|
||||
var proxy: ProxyXml? = null
|
||||
|
||||
@XmlElement(name = "kobalt-compiler-version") @JvmField
|
||||
var kobaltCompilerVersion: String = "1.0.0"
|
||||
|
||||
|
@ -29,6 +32,17 @@ class KobaltSettingsXml {
|
|||
var kobaltCompilerRepo: String? = null
|
||||
}
|
||||
|
||||
class ProxyXml {
|
||||
@XmlElement @JvmField
|
||||
var host: String = ""
|
||||
|
||||
@XmlElement @JvmField
|
||||
var port: String = ""
|
||||
|
||||
@XmlElement @JvmField
|
||||
var type: String = ""
|
||||
}
|
||||
|
||||
class DefaultReposXml {
|
||||
@XmlElement @JvmField
|
||||
var repo: List<String> = arrayListOf()
|
||||
|
@ -46,6 +60,8 @@ class KobaltSettings @Inject constructor(val xmlFile: KobaltSettingsXml) {
|
|||
|
||||
val defaultRepos = xmlFile.defaultRepos?.repo
|
||||
|
||||
val proxy = xmlFile.proxy
|
||||
|
||||
var kobaltCompilerVersion = xmlFile.kobaltCompilerVersion
|
||||
var kobaltCompilerRepo = xmlFile.kobaltCompilerRepo
|
||||
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package com.beust.kobalt.maven
|
||||
|
||||
import com.beust.kobalt.KobaltException
|
||||
import com.beust.kobalt.api.Kobalt
|
||||
import com.beust.kobalt.api.toProxy
|
||||
import com.beust.kobalt.misc.CountingFileRequestBody
|
||||
import com.beust.kobalt.misc.log
|
||||
import okhttp3.*
|
||||
|
@ -19,7 +21,7 @@ class Http {
|
|||
}
|
||||
|
||||
fun get(user: String?, password: String?, url: String) : Response {
|
||||
val client = OkHttpClient()
|
||||
val client = OkHttpClient.Builder().proxy(Kobalt.proxyConfig.toProxy()).build()
|
||||
val request = Request.Builder().url(url)
|
||||
if (user != null) {
|
||||
request.header("Authorization", Credentials.basic(user, password))
|
||||
|
@ -73,7 +75,7 @@ class Http {
|
|||
.build()
|
||||
|
||||
log(2, "Uploading $file to $url")
|
||||
val response = OkHttpClient().newCall(request).execute()
|
||||
val response = OkHttpClient.Builder().proxy(Kobalt.proxyConfig.toProxy()).build().newCall(request).execute()
|
||||
if (! response.isSuccessful) {
|
||||
error(response)
|
||||
} else {
|
||||
|
|
|
@ -3,6 +3,7 @@ package com.beust.kobalt.maven.aether
|
|||
import com.beust.kobalt.KobaltException
|
||||
import com.beust.kobalt.api.IClasspathDependency
|
||||
import com.beust.kobalt.api.Kobalt
|
||||
import com.beust.kobalt.api.toAetherProxy
|
||||
import com.beust.kobalt.homeDir
|
||||
import com.beust.kobalt.internal.KobaltSettings
|
||||
import com.beust.kobalt.internal.KobaltSettingsXml
|
||||
|
@ -80,6 +81,7 @@ class Aether(val localRepo: File) {
|
|||
private val kobaltRepositories : List<RemoteRepository>
|
||||
get() = Kobalt.repos.map {
|
||||
RemoteRepository.Builder("maven", "default", it.url)
|
||||
.setProxy(Kobalt.proxyConfig.toAetherProxy())
|
||||
// .setSnapshotPolicy(RepositoryPolicy(false, null, null))
|
||||
.build()
|
||||
}
|
||||
|
|
|
@ -54,7 +54,7 @@ class CountingFileRequestBody(val file: File, val contentType: String,
|
|||
// // .post(requestBody)
|
||||
// .build();
|
||||
//
|
||||
// val response = OkHttpClient().newCall(request).execute()
|
||||
// val response = OkHttpClient.Builder().proxy(Kobalt.proxyConfig.toProxy()).build().newCall(request).execute()
|
||||
// if (! response.isSuccessful) {
|
||||
// println("ERROR")
|
||||
// } else {
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package com.beust.kobalt.misc
|
||||
|
||||
import com.beust.kobalt.KobaltException
|
||||
import com.beust.kobalt.api.Kobalt
|
||||
import com.beust.kobalt.api.toProxy
|
||||
import com.beust.kobalt.internal.DocUrl
|
||||
import com.beust.kobalt.maven.Http
|
||||
import com.google.gson.Gson
|
||||
|
@ -58,7 +60,7 @@ class GithubApi2 @Inject constructor(
|
|||
// Read only Api
|
||||
//
|
||||
private val service = Retrofit.Builder()
|
||||
.client(OkHttpClient())
|
||||
.client(OkHttpClient.Builder().proxy(Kobalt.proxyConfig.toProxy()).build())
|
||||
.baseUrl("https://api.github.com")
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.build()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue