fix http port 8563 support. #955

This commit is contained in:
hengyunabc
2019-12-04 00:40:34 +08:00
parent cb2700370f
commit be13519032
10 changed files with 158 additions and 5 deletions
@@ -4,10 +4,10 @@ import com.taobao.arthas.core.shell.future.Future;
import com.taobao.arthas.core.shell.handlers.Handler;
import com.taobao.arthas.core.shell.term.Term;
import com.taobao.arthas.core.shell.term.TermServer;
import com.taobao.arthas.core.shell.term.impl.http.NettyWebsocketTtyBootstrap;
import com.taobao.arthas.core.util.LogUtil;
import com.taobao.middleware.logger.Logger;
import io.termd.core.function.Consumer;
import io.termd.core.http.netty.NettyWebsocketTtyBootstrap;
import io.termd.core.tty.TtyConnection;
import java.util.concurrent.TimeUnit;
@@ -1,4 +1,4 @@
package com.taobao.arthas.core.shell.term.impl.httptelnet;
package com.taobao.arthas.core.shell.term.impl.http;
import java.io.File;
import java.io.FileInputStream;
@@ -1,4 +1,4 @@
package com.taobao.arthas.core.shell.term.impl.httptelnet;
package com.taobao.arthas.core.shell.term.impl.http;
import java.io.File;
import java.io.InputStream;
@@ -0,0 +1,106 @@
package com.taobao.arthas.core.shell.term.impl.http;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import io.netty.util.concurrent.ImmediateEventExecutor;
import io.termd.core.function.Consumer;
import io.termd.core.tty.TtyConnection;
import io.termd.core.util.CompletableFuture;
import io.termd.core.util.Helper;
/**
* Convenience class for quickly starting a Netty Tty server.
*
* @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
*/
public class NettyWebsocketTtyBootstrap {
private final ChannelGroup channelGroup = new DefaultChannelGroup(ImmediateEventExecutor.INSTANCE);
private String host;
private int port;
private EventLoopGroup group;
private Channel channel;
public NettyWebsocketTtyBootstrap() {
this.host = "localhost";
this.port = 8080;
}
public String getHost() {
return host;
}
public NettyWebsocketTtyBootstrap setHost(String host) {
this.host = host;
return this;
}
public int getPort() {
return port;
}
public NettyWebsocketTtyBootstrap setPort(int port) {
this.port = port;
return this;
}
public void start(Consumer<TtyConnection> handler, final Consumer<Throwable> doneHandler) {
group = new NioEventLoopGroup();
ServerBootstrap b = new ServerBootstrap();
b.group(group).channel(NioServerSocketChannel.class).handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new TtyServerInitializer(channelGroup, handler));
final ChannelFuture f = b.bind(host, port);
f.addListener(new GenericFutureListener<Future<? super Void>>() {
@Override
public void operationComplete(Future<? super Void> future) throws Exception {
if (future.isSuccess()) {
channel = f.channel();
doneHandler.accept(null);
} else {
doneHandler.accept(future.cause());
}
}
});
}
public CompletableFuture<Void> start(Consumer<TtyConnection> handler) {
CompletableFuture<Void> fut = new CompletableFuture<Void>();
start(handler, Helper.startedHandler(fut));
return fut;
}
public void stop(final Consumer<Throwable> doneHandler) {
if (channel != null) {
channel.close();
}
channelGroup.close().addListener(new GenericFutureListener<Future<? super Void>>() {
@Override
public void operationComplete(Future<? super Void> future) throws Exception {
try {
doneHandler.accept(future.cause());
} finally {
group.shutdownGracefully();
}
}
});
}
public CompletableFuture<Void> stop() {
CompletableFuture<Void> fut = new CompletableFuture<Void>();
stop(Helper.stoppedHandler(fut));
return fut;
}
}
@@ -0,0 +1,40 @@
package com.taobao.arthas.core.shell.term.impl.http;
import java.io.File;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.termd.core.function.Consumer;
import io.termd.core.tty.TtyConnection;
/**
* @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
*/
public class TtyServerInitializer extends ChannelInitializer<SocketChannel> {
private final ChannelGroup group;
private final Consumer<TtyConnection> handler;
public TtyServerInitializer(ChannelGroup group, Consumer<TtyConnection> handler) {
this.group = group;
this.handler = handler;
}
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new HttpServerCodec());
pipeline.addLast(new ChunkedWriteHandler());
pipeline.addLast(new HttpObjectAggregator(64 * 1024));
pipeline.addLast(new HttpRequestHandler("/ws", new File("arthas-output")));
pipeline.addLast(new WebSocketServerProtocolHandler("/ws"));
pipeline.addLast(new TtyWebSocketFrameHandler(group, handler));
}
}
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package com.taobao.arthas.core.shell.term.impl.httptelnet;
package com.taobao.arthas.core.shell.term.impl.http;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
@@ -0,0 +1 @@
package com.taobao.arthas.core.shell.term.impl.http;
@@ -3,6 +3,9 @@ package com.taobao.arthas.core.shell.term.impl.httptelnet;
import java.io.File;
import java.util.concurrent.TimeUnit;
import com.taobao.arthas.core.shell.term.impl.http.HttpRequestHandler;
import com.taobao.arthas.core.shell.term.impl.http.TtyWebSocketFrameHandler;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;