提交 1c3a47bc 编写于 作者: K kohsuke

added port forwarding support

git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@19393 71c3de6d-444a-0410-be80-ed276b4c234a
上级 d6677bcb
package hudson.remoting.forward;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Copies a stream and close them at EOF.
*
* @author Kohsuke Kawaguchi
*/
final class CopyThread extends Thread {
private final InputStream in;
private final OutputStream out;
public CopyThread(String threadName, InputStream in, OutputStream out) {
super(threadName);
this.in = in;
this.out = out;
}
public void run() {
try {
try {
byte[] buf = new byte[8192];
int len;
while ((len = in.read(buf)) > 0)
out.write(buf, 0, len);
} finally {
in.close();
out.close();
}
} catch (IOException e) {
// TODO: what to do?
}
}
}
/*
* The MIT License
*
* Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.remoting.forward;
import java.io.Serializable;
import java.io.OutputStream;
import java.io.IOException;
/**
* Abstracts away how the forwarding is set up.
*
* @author Kohsuke Kawaguchi
*/
public interface Forwarder extends Serializable {
/**
* Establishes a port forwarding connection and returns
* the writer end.
*
* @param out
* The writer end to the initiator. The callee will
* start a thread that writes to this.
*/
OutputStream connect(OutputStream out) throws IOException;
}
/*
* The MIT License
*
* Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.remoting.forward;
import hudson.remoting.VirtualChannel;
import hudson.remoting.Callable;
import hudson.remoting.SocketInputStream;
import hudson.remoting.RemoteOutputStream;
import hudson.remoting.SocketOutputStream;
import hudson.remoting.Channel;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
/**
* Creates {@link Forwarder}.
*
* @author Kohsuke Kawaguchi
*/
public class ForwarderFactory {
/**
* Creates a connector on the remote side that connects to the speicied host and port.
*/
public static Forwarder create(VirtualChannel channel, String remoteHost, int remotePort) throws IOException, InterruptedException {
return channel.call(new Exporter(remoteHost, remotePort));
}
/**
* Creates a remote {@link Forwarder} instance and returns it.
*/
private static final class Exporter implements Callable<Forwarder,IOException> {
private final String remoteHost;
private final int remotePort;
private Exporter(String remoteHost, int remotePort) {
this.remoteHost = remoteHost;
this.remotePort = remotePort;
}
public Forwarder call() throws IOException {
return new Forwarder() {
public OutputStream connect(OutputStream out) throws IOException {
Socket s = new Socket(remoteHost, remotePort);
new CopyThread(String.format("Copier to %s:%d",remoteHost,remotePort),
new SocketInputStream(s), out).start();
return new RemoteOutputStream(new SocketOutputStream(s));
}
/**
* When sent to the remote node, send a proxy.
*/
private Object writeReplace() {
return Channel.current().export(Forwarder.class, this);
}
};
}
private static final long serialVersionUID = 1L;
}
}
/*
* The MIT License
*
* Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.remoting.forward;
import hudson.remoting.RemoteOutputStream;
import hudson.remoting.SocketOutputStream;
import hudson.remoting.SocketInputStream;
import hudson.remoting.VirtualChannel;
import hudson.remoting.Callable;
import hudson.remoting.Channel;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Closeable;
import static java.util.logging.Level.FINE;
import java.util.logging.Logger;
/**
* Port forwarder over a remote channel.
*
* @author Kohsuke Kawaguchi
* @since 1.315
*/
public class PortForwarder extends Thread implements Closeable {
private final Forwarder forwarder;
private final ServerSocket socket;
public PortForwarder(int localPort, Forwarder forwarder) throws IOException {
super(String.format("Port forwarder %d",localPort));
this.forwarder = forwarder;
this.socket = new ServerSocket(localPort);
}
@Override
public void run() {
try {
try {
while(true) {
final Socket s = socket.accept();
new Thread("Port forwarding session from "+s.getRemoteSocketAddress()) {
public void run() {
try {
final OutputStream out = forwarder.connect(new RemoteOutputStream(new SocketOutputStream(s)));
new CopyThread("Copier for "+s.getRemoteSocketAddress(),
new SocketInputStream(s), out).start();
} catch (IOException e) {
// this happens if the socket connection is terminated abruptly.
LOGGER.log(FINE,"Port forwarding session was shut down abnormally",e);
}
}
}.start();
}
} finally {
socket.close();
}
} catch (IOException e) {
LOGGER.log(FINE,"Port forwarding was shut down abnormally",e);
}
}
/**
* Shuts down this port forwarder.
*/
public void close() throws IOException {
interrupt();
socket.close();
}
/**
* Starts a {@link PortForwarder} accepting remotely at the given channel,
* which connects by using the given connector.
*
* @return
* A {@link Closeable} that can be used to shut the port forwarding down.
*/
public static Closeable create(VirtualChannel ch, final int acceptingPort, Forwarder forwarder) throws IOException, InterruptedException {
// need a remotable reference
final Forwarder proxy = ch.export(Forwarder.class, forwarder);
return ch.call(new Callable<Closeable,IOException>() {
public Closeable call() throws IOException {
PortForwarder t = new PortForwarder(acceptingPort, proxy);
t.start();
return Channel.current().export(Closeable.class,t);
}
});
}
private static final Logger LOGGER = Logger.getLogger(PortForwarder.class.getName());
}
/**
* TCP port forwarding over {@code hudson.remoting}.
*/
package hudson.remoting.forward;
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册