提交 297c2b9f 编写于 作者: K kohsuke

[FIXED HUDSON-1833] Update center accepts HTTP proxy username/password.

In 1.234.

git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@10737 71c3de6d-444a-0410-be80-ed276b4c234a
上级 0963c863
...@@ -218,7 +218,12 @@ public final class PluginManager extends AbstractModelObject { ...@@ -218,7 +218,12 @@ public final class PluginManager extends AbstractModelObject {
rsp.sendRedirect("../updateCenter/"); rsp.sendRedirect("../updateCenter/");
} }
public void doProxyConfigure(@QueryParameter("proxy.server") String server, @QueryParameter("proxy.port") String port, StaplerResponse rsp) throws IOException { public void doProxyConfigure(
@QueryParameter("proxy.server") String server,
@QueryParameter("proxy.port") String port,
@QueryParameter("proxy.userName") String userName,
@QueryParameter("proxy.password") String password,
StaplerResponse rsp) throws IOException {
Hudson.getInstance().checkPermission(Hudson.ADMINISTER); Hudson.getInstance().checkPermission(Hudson.ADMINISTER);
Hudson hudson = Hudson.getInstance(); Hudson hudson = Hudson.getInstance();
...@@ -227,7 +232,8 @@ public final class PluginManager extends AbstractModelObject { ...@@ -227,7 +232,8 @@ public final class PluginManager extends AbstractModelObject {
hudson.proxy = null; hudson.proxy = null;
ProxyConfiguration.getXmlFile().delete(); ProxyConfiguration.getXmlFile().delete();
} else { } else {
hudson.proxy = new ProxyConfiguration(server,Integer.parseInt(Util.fixEmptyAndTrim(port))); hudson.proxy = new ProxyConfiguration(server,Integer.parseInt(Util.fixEmptyAndTrim(port)),
Util.fixEmptyAndTrim(userName),Util.fixEmptyAndTrim(password));
hudson.proxy.save(); hudson.proxy.save();
} }
rsp.sendRedirect("./advanced"); rsp.sendRedirect("./advanced");
......
...@@ -3,24 +3,50 @@ package hudson; ...@@ -3,24 +3,50 @@ package hudson;
import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.XStream;
import hudson.model.Hudson; import hudson.model.Hudson;
import hudson.util.XStream2; import hudson.util.XStream2;
import hudson.util.Scrambler;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.Proxy; import java.net.Proxy;
import java.net.URLConnection;
import java.net.URL;
/** /**
* HTTP proxy configuration. * HTTP proxy configuration.
* *
* <p>
* Use {@link #open(URL)} to open a connection with the proxy setting.
*
* @see Hudson#proxy * @see Hudson#proxy
*/ */
public final class ProxyConfiguration { public final class ProxyConfiguration {
public final String name; public final String name;
public final int port; public final int port;
/**
* Possibly null proxy user name and password.
* Password is base64 scrambled since this is persisted to disk.
*/
private final String userName,password;
public ProxyConfiguration(String name, int port) { public ProxyConfiguration(String name, int port) {
this(name,port,null,null);
}
public ProxyConfiguration(String name, int port, String userName, String password) {
this.name = name; this.name = name;
this.port = port; this.port = port;
this.userName = userName;
this.password = Scrambler.scramble(password);
}
public String getUserName() {
return userName;
}
public String getPassword() {
return Scrambler.descramble(password);
} }
public Proxy createProxy() { public Proxy createProxy() {
...@@ -43,6 +69,19 @@ public final class ProxyConfiguration { ...@@ -43,6 +69,19 @@ public final class ProxyConfiguration {
return null; return null;
} }
public static URLConnection open(URL url) throws IOException {
ProxyConfiguration p = Hudson.getInstance().proxy;
if(p==null)
return url.openConnection();
URLConnection con = url.openConnection(p.createProxy());
if(p.getUserName()!=null) {
con.setRequestProperty("Proxy-Authorization","Basic "+
Scrambler.scramble(p.getUserName()+':'+p.getPassword()));
}
return con;
}
private static final XStream XSTREAM = new XStream2(); private static final XStream XSTREAM = new XStream2();
static { static {
......
...@@ -383,16 +383,6 @@ public final class Hudson extends View implements ItemGroup<TopLevelItem>, Node, ...@@ -383,16 +383,6 @@ public final class Hudson extends View implements ItemGroup<TopLevelItem>, Node,
return slaveAgentPort; return slaveAgentPort;
} }
/**
* Obtains the {@link Proxy} instance to talk to other HTTP servers.
*/
public Proxy createProxy() {
if(proxy==null)
return Proxy.NO_PROXY;
else
return proxy.createProxy();
}
/** /**
* If you are calling this on Hudson something is wrong. * If you are calling this on Hudson something is wrong.
* *
......
...@@ -4,6 +4,7 @@ import hudson.Functions; ...@@ -4,6 +4,7 @@ import hudson.Functions;
import hudson.PluginManager; import hudson.PluginManager;
import hudson.PluginWrapper; import hudson.PluginWrapper;
import hudson.Util; import hudson.Util;
import hudson.ProxyConfiguration;
import hudson.util.DaemonThreadFactory; import hudson.util.DaemonThreadFactory;
import hudson.util.TextFile; import hudson.util.TextFile;
import static hudson.util.TimeUnit2.DAYS; import static hudson.util.TimeUnit2.DAYS;
...@@ -386,7 +387,7 @@ public class UpdateCenter implements ModelObject { ...@@ -386,7 +387,7 @@ public class UpdateCenter implements ModelObject {
} }
private void testConnection(URL url) throws IOException { private void testConnection(URL url) throws IOException {
InputStream in = url.openConnection(Hudson.getInstance().createProxy()).getInputStream(); InputStream in = ProxyConfiguration.open(url).getInputStream();
IOUtils.copy(in,new ByteArrayOutputStream()); IOUtils.copy(in,new ByteArrayOutputStream());
in.close(); in.close();
} }
...@@ -426,7 +427,7 @@ public class UpdateCenter implements ModelObject { ...@@ -426,7 +427,7 @@ public class UpdateCenter implements ModelObject {
// In the future if we are to open up update center to 3rd party, we need more elaborate scheme // In the future if we are to open up update center to 3rd party, we need more elaborate scheme
// like signing to ensure the safety of the bits. // like signing to ensure the safety of the bits.
URLConnection con = new URL(plugin.url).openConnection(Hudson.getInstance().createProxy()); URLConnection con = ProxyConfiguration.open(new URL(plugin.url));
int total = con.getContentLength(); int total = con.getContentLength();
CountingInputStream in = new CountingInputStream(con.getInputStream()); CountingInputStream in = new CountingInputStream(con.getInputStream());
byte[] buf = new byte[8192]; byte[] buf = new byte[8192];
......
...@@ -14,13 +14,19 @@ ...@@ -14,13 +14,19 @@
<h1>${%HTTP Proxy Configuration}</h1> <h1>${%HTTP Proxy Configuration}</h1>
<f:form method="post" action="proxyConfigure"> <f:form method="post" action="proxyConfigure">
<f:block> <f:block>
<f:entry title="HTTP proxy server" help="/help/update-center/proxy-server.html"> <f:entry title="Server" help="/help/update-center/proxy-server.html">
<f:textbox name="proxy.server" value="${app.proxy.name}" /> <f:textbox name="proxy.server" value="${app.proxy.name}" />
</f:entry> </f:entry>
<f:entry title="HTTP proxy port" help="/help/update-center/proxy-port.html"> <f:entry title="Port" help="/help/update-center/proxy-port.html">
<f:textbox name="proxy.port" value="${app.proxy.port}" /> <f:textbox name="proxy.port" value="${app.proxy.port}" />
</f:entry> </f:entry>
</f:block> </f:block>
<f:entry title="User name">
<f:textbox name="proxy.userName" value="${app.proxy.userName}" />
</f:entry>
<f:entry title="Password">
<f:password name="proxy.password" value="${app.proxy.password}" />
</f:entry>
<f:block> <f:block>
<f:submit value="${%Submit}" /> <f:submit value="${%Submit}" />
</f:block> </f:block>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册