Add proxy support for plugin installation

This commit is contained in:
Naoki Takezoe
2018-07-15 12:52:22 +09:00
parent 3ebc4e8e23
commit 31345cc739
5 changed files with 94 additions and 12 deletions

View File

@@ -94,9 +94,16 @@ trait SystemSettingsControllerBase extends AccountManagementControllerBase {
),
"skinName" -> trim(label("AdminLTE skin name", text(required))),
"showMailAddress" -> trim(label("Show mail address", boolean())),
"pluginNetworkInstall" -> new SingleValueType[Boolean] {
override def convert(value: String, messages: Messages): Boolean = context.settings.pluginNetworkInstall
}
"pluginNetworkInstall" -> trim(label("Network plugin installation", boolean())),
"proxy" -> optionalIfNotChecked(
"useProxy",
mapping(
"host" -> trim(label("Proxy host", text(required))),
"port" -> trim(label("Proxy port", number())),
"user" -> trim(label("Keystore", optional(text()))),
"password" -> trim(label("Keystore", optional(text())))
)(Proxy.apply)
)
)(SystemSettings.apply).verifying { settings =>
Vector(
if (settings.ssh.enabled && settings.baseUrl.isEmpty) {
@@ -380,11 +387,6 @@ trait SystemSettingsControllerBase extends AccountManagementControllerBase {
})
post("/admin/plugins/_reload")(adminOnly {
// Update configuration
val pluginNetworkInstall = params.get("pluginNetworkInstall").map(_.toBoolean).getOrElse(false)
saveSystemSettings(context.settings.copy(pluginNetworkInstall = pluginNetworkInstall))
// Reload plugins
PluginRegistry.reload(request.getServletContext(), loadSystemSettings(), request2Session(request).conn)
flash += "info" -> "All plugins were reloaded."
redirect("/admin/plugins")

View File

@@ -1,7 +1,12 @@
package gitbucket.core.plugin
import java.net.{InetSocketAddress, PasswordAuthentication}
import java.net.{Proxy => JProxy}
import gitbucket.core.controller.Context
import org.json4s._
import org.apache.commons.io.IOUtils
import java.net.Authenticator
object PluginRepository {
implicit val formats = DefaultFormats
@@ -10,9 +15,27 @@ object PluginRepository {
org.json4s.jackson.JsonMethods.parse(json).extract[Seq[PluginMetadata]]
}
def getPlugins(): Seq[PluginMetadata] = {
def getPlugins()(implicit context: Context): Seq[PluginMetadata] = {
val url = new java.net.URL("https://plugins.gitbucket-community.org/releases/plugins.json")
val str = IOUtils.toString(url, "UTF-8")
val in = 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)
}
url
.openConnection(new JProxy(JProxy.Type.HTTP, new InetSocketAddress(proxy.host, proxy.port)))
.getInputStream
case None => url.openStream()
}
val str = IOUtils.toString(in, "UTF-8")
parsePluginJson(str)
}

View File

@@ -3,8 +3,6 @@
@gitbucket.core.admin.html.menu("plugins") {
@gitbucket.core.helper.html.information(info)
<form action="@context.path/admin/plugins/_reload" method="POST" class="pull-right">
<input type="checkbox" name="pluginNetworkInstall" id="pluginNetworkInstall" value="true" @if(context.settings.pluginNetworkInstall){checked}>
<label for="pluginNetworkInstall">Install plugin from <a href="https://plugins.gitbucket-community.org" target="_blank">plugins.gitbucket-community.org</a></label>
<input type="submit" value="Reload plugins" class="btn btn-default">
</form>
<h1 class="system-settings-title">Plugins</h1>

View File

@@ -8,6 +8,7 @@
<ul class="nav nav-tabs fill-width" id="pullreq-tab">
<li><a href="#system">System settings</a></li>
<li><a href="#authentication">Authentication</a></li>
<li><a href="#plugins">Plugins</a></li>
</ul>
<div class="tab-content fill-width" style="padding-top: 20px;">
<div class="tab-pane" id="system">
@@ -16,6 +17,9 @@
<div class="tab-pane" id="authentication">
@settings_authentication(info)
</div>
<div class="tab-pane" id="plugins">
@settings_plugins(info)
</div>
</div>
<hr>
<div class="align-right" style="margin-top: 20px;">
@@ -30,6 +34,9 @@ $(function(){
if(location.hash == '#authentication'){
$('li:has(a[href="#authentication"])').addClass('active');
$('div#authentication').addClass('active');
} else if(location.hash == '#plugins'){
$('li:has(a[href="#plugins"])').addClass('active');
$('div#plugins').addClass('active');
} else {
$('li:has(a[href="#system"])').addClass('active');
$('div#system').addClass('active');

View File

@@ -0,0 +1,52 @@
@(info: Option[Any])(implicit context: gitbucket.core.controller.Context)
<label class="strong">Plugin repositories</label>
<fieldset>
<label class="checkbox">
<input type="checkbox" id="pluginNetworkInstall" name="pluginNetworkInstall"@if(context.settings.pluginNetworkInstall){ checked} />
<a href="https://plugins.gitbucket-community.org" target="_blank">plugins.gitbucket-community.org</a>
</label>
</fieldset>
<hr>
<fieldset>
<label class="checkbox">
<input type="checkbox" id="useProxy" name="useProxy"@if(context.settings.proxy.isDefined){ checked} />
Use proxy
</label>
</fieldset>
<div class="proxy">
<div class="form-group">
<label class="control-label col-md-2" for="proxyHost">Proxy host</label>
<div class="col-md-10">
<input type="text" id="proxyHost" name="proxy.host" class="form-control" value="@context.settings.proxy.map(_.host)"/>
<span id="error-proxy_host" class="error"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2" for="proxyPort">Proxy port</label>
<div class="col-md-10">
<input type="text" id="proxyPort" name="proxy.port" class="form-control input-mini" value="@context.settings.proxy.map(_.port)"/>
<span id="error-proxy_port" class="error"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2" for="proxyUser">Proxy user</label>
<div class="col-md-10">
<input type="text" id="proxyUser" name="proxy.user" class="form-control" value="@context.settings.proxy.map(_.user)"/>
<span id="error-proxy_user" class="error"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2" for="proxyPassword">Proxy password</label>
<div class="col-md-10">
<input type="text" id="proxyPassword" name="proxy.password" class="form-control" value="@context.settings.proxy.map(_.password)"/>
<span id="error-proxy_password" class="error"></span>
</div>
</div>
</div>
<script>
$(function(){
$('#useProxy').change(function(){
$('.proxy input').prop('disabled', !$(this).prop('checked'));
}).change();
});
</script>