diff --git a/src/main/scala/gitbucket/core/plugin/PluginRegistry.scala b/src/main/scala/gitbucket/core/plugin/PluginRegistry.scala index fb0d39e..6ed9dbc 100644 --- a/src/main/scala/gitbucket/core/plugin/PluginRegistry.scala +++ b/src/main/scala/gitbucket/core/plugin/PluginRegistry.scala @@ -6,8 +6,8 @@ import java.util.Base64 import java.util.concurrent.ConcurrentLinkedQueue import java.util.concurrent.ConcurrentHashMap -import javax.servlet.ServletContext +import javax.servlet.ServletContext import com.github.zafarkhaja.semver.Version import gitbucket.core.controller.{Context, ControllerBase} import gitbucket.core.model.{Account, Issue} @@ -18,10 +18,12 @@ import gitbucket.core.util.SyntaxSugars._ import gitbucket.core.util.DatabaseConfig import gitbucket.core.util.Directory._ +import gitbucket.core.util.HttpClientUtil._ import io.github.gitbucket.solidbase.Solidbase import io.github.gitbucket.solidbase.manager.JDBCVersionManager import io.github.gitbucket.solidbase.model.Module import org.apache.commons.io.FileUtils +import org.apache.http.client.methods.HttpGet import org.apache.sshd.server.Command import org.slf4j.LoggerFactory import play.twirl.api.Html @@ -253,8 +255,17 @@ }) .foreach(_.delete()) - val in = url.openStream() - FileUtils.copyToFile(in, new File(PluginHome, new File(url.getFile).getName)) + withHttpClient(settings.proxy) { httpClient => + val httpGet = new HttpGet(url.toString) + try { + val response = httpClient.execute(httpGet) + val in = response.getEntity.getContent + FileUtils.copyToFile(in, new File(PluginHome, new File(url.getFile).getName)) + } finally { + httpGet.releaseConnection() + } + } + instance = new PluginRegistry() initialize(context, settings, conn) } diff --git a/src/main/scala/gitbucket/core/plugin/PluginRepository.scala b/src/main/scala/gitbucket/core/plugin/PluginRepository.scala index 8c8ac5d..56a49f6 100644 --- a/src/main/scala/gitbucket/core/plugin/PluginRepository.scala +++ b/src/main/scala/gitbucket/core/plugin/PluginRepository.scala @@ -1,14 +1,12 @@ package gitbucket.core.plugin -import java.net.{InetSocketAddress, PasswordAuthentication} -import java.net.{Proxy => JProxy} - import gitbucket.core.controller.Context import gitbucket.core.util.SyntaxSugars.using +import gitbucket.core.util.HttpClientUtil._ import org.json4s._ import org.apache.commons.io.IOUtils -import java.net.Authenticator +import org.apache.http.client.methods.HttpGet import org.slf4j.LoggerFactory object PluginRepository { @@ -22,23 +20,18 @@ def getPlugins()(implicit context: Context): Seq[PluginMetadata] = { try { val url = new java.net.URL("https://plugins.gitbucket-community.org/releases/plugins.json") - using(context.settings.proxy match { - case Some(proxy) => - (proxy.user, proxy.password) match { - case (Some(user), Some(password)) => - Authenticator.setDefault(new Authenticator() { - override def getPasswordAuthentication = new PasswordAuthentication(user, password.toCharArray) - }) - case _ => - Authenticator.setDefault(null) + + withHttpClient(context.settings.proxy) { httpClient => + val httpGet = new HttpGet(url.toString) + try { + val response = httpClient.execute(httpGet) + using(response.getEntity.getContent) { in => + val str = IOUtils.toString(in, "UTF-8") + parsePluginJson(str) } - url - .openConnection(new JProxy(JProxy.Type.HTTP, new InetSocketAddress(proxy.host, proxy.port))) - .getInputStream - case None => url.openStream() - }) { in => - val str = IOUtils.toString(in, "UTF-8") - parsePluginJson(str) + } finally { + httpGet.releaseConnection() + } } } catch { case t: Throwable => diff --git a/src/main/scala/gitbucket/core/util/HttpClientUtil.scala b/src/main/scala/gitbucket/core/util/HttpClientUtil.scala new file mode 100644 index 0000000..3e89400 --- /dev/null +++ b/src/main/scala/gitbucket/core/util/HttpClientUtil.scala @@ -0,0 +1,34 @@ +package gitbucket.core.util + +import gitbucket.core.service.SystemSettingsService +import org.apache.http.HttpHost +import org.apache.http.auth.{AuthScope, UsernamePasswordCredentials} +import org.apache.http.impl.client.{BasicCredentialsProvider, CloseableHttpClient, HttpClientBuilder} + +object HttpClientUtil { + + def withHttpClient[T](proxy: Option[SystemSettingsService.Proxy])(f: CloseableHttpClient => T): T = { + val builder = HttpClientBuilder.create.useSystemProperties + + proxy.foreach { proxy => + for (user <- proxy.user; password <- proxy.password) { + builder.setProxy(new HttpHost(proxy.host, proxy.port)) + val credential = new BasicCredentialsProvider() + credential.setCredentials( + new AuthScope(proxy.host, proxy.port), + new UsernamePasswordCredentials(user, password) + ) + builder.setDefaultCredentialsProvider(credential) + } + } + + val httpClient = builder.build() + + try { + f(httpClient) + } finally { + httpClient.close() + } + } + +}