diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/BuildScript.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/BuildScript.kt index d02433cc..ec38dab1 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/BuildScript.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/BuildScript.kt @@ -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()) diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/Kobalt.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/Kobalt.kt index 0018b7a2..493fa845 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/Kobalt.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/Kobalt.kt @@ -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 diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/KobaltSettingsXml.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/KobaltSettingsXml.kt index 89b49a58..9f88b388 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/KobaltSettingsXml.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/KobaltSettingsXml.kt @@ -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 = 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 diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/Http.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/Http.kt index fc8b023e..2a4b4799 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/Http.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/Http.kt @@ -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 { diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/aether/Aether.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/aether/Aether.kt index 21070d6c..7ab04ee1 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/aether/Aether.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/aether/Aether.kt @@ -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 get() = Kobalt.repos.map { RemoteRepository.Builder("maven", "default", it.url) + .setProxy(Kobalt.proxyConfig.toAetherProxy()) // .setSnapshotPolicy(RepositoryPolicy(false, null, null)) .build() } diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/CountingFileRequestBody.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/CountingFileRequestBody.kt index b9dc6d8a..ca64d3d6 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/CountingFileRequestBody.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/CountingFileRequestBody.kt @@ -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 { diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/GithubApi2.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/GithubApi2.kt index 8fd571e3..3386ce74 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/GithubApi2.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/GithubApi2.kt @@ -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()