remove duplicated TtyWebSocketFrameHandler

This commit is contained in:
gongdewei
2020-05-20 11:36:26 +08:00
parent 922b7eaa46
commit 03eb909a21
4 changed files with 7 additions and 122 deletions
@@ -44,14 +44,12 @@ public class HttpRequestHandler extends SimpleChannelInboundHandler<FullHttpRequ
private File dir;
private HttpApiHandler httpApiHandler;
// private HttpWebUIHandler webUIHandler;
public HttpRequestHandler(String wsUri, File dir) {
this.wsUri = wsUri;
this.dir = dir;
dir.mkdirs();
this.httpApiHandler = HttpApiHandler.getInstance();
// this.webUIHandler = HttpWebUIHandler.getInstance();
}
@Override
@@ -77,10 +75,7 @@ public class HttpRequestHandler extends SimpleChannelInboundHandler<FullHttpRequ
isHttpApiResponse = true;
}
//handler webui requests
// if (path.startsWith(HttpWebUIHandler.WEB_UI_PATH)) {
// response = webUIHandler.handle(request);
// }
//handle webui requests
if (path.equals("/ui")){
response = createRedirectResponse(request, "/ui/");
}
@@ -1,7 +1,5 @@
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;
@@ -12,8 +10,11 @@ import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.util.concurrent.EventExecutorGroup;
import io.termd.core.function.Consumer;
import io.termd.core.http.netty.TtyWebSocketFrameHandler;
import io.termd.core.tty.TtyConnection;
import java.io.File;
/**
* @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
@@ -39,6 +40,6 @@ public class TtyServerInitializer extends ChannelInitializer<SocketChannel> {
pipeline.addLast(new HttpObjectAggregator(64 * 1024));
pipeline.addLast(workerGroup, "HttpRequestHandler", new HttpRequestHandler("/ws", new File("arthas-output")));
pipeline.addLast(new WebSocketServerProtocolHandler("/ws"));
pipeline.addLast(new TtyWebSocketFrameHandler(group, handler));
pipeline.addLast(new TtyWebSocketFrameHandler(group, handler, HttpRequestHandler.class));
}
}
@@ -1,111 +0,0 @@
/*
* Copyright 2015 Julien Viet
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.taobao.arthas.core.shell.term.impl.http;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.group.ChannelGroup;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.termd.core.function.Consumer;
import io.termd.core.http.HttpTtyConnection;
import io.termd.core.tty.TtyConnection;
import java.util.concurrent.TimeUnit;
/**
* @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
*/
public class TtyWebSocketFrameHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {
private final ChannelGroup group;
private final Consumer<TtyConnection> handler;
private ChannelHandlerContext context;
private HttpTtyConnection conn;
public TtyWebSocketFrameHandler(ChannelGroup group, Consumer<TtyConnection> handler) {
this.group = group;
this.handler = handler;
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx);
context = ctx;
}
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if (evt == WebSocketServerProtocolHandler.ServerHandshakeStateEvent.HANDSHAKE_COMPLETE) {
ctx.pipeline().remove(HttpRequestHandler.class);
group.add(ctx.channel());
conn = new HttpTtyConnection() {
@Override
protected void write(byte[] buffer) {
ByteBuf byteBuf = Unpooled.buffer();
byteBuf.writeBytes(buffer);
if (context != null) {
context.writeAndFlush(new TextWebSocketFrame(byteBuf));
}
}
@Override
public void schedule(Runnable task, long delay, TimeUnit unit) {
if (context != null) {
context.executor().schedule(task, delay, unit);
}
}
@Override
public void execute(Runnable task) {
if (context != null) {
context.executor().execute(task);
}
}
@Override
public void close() {
if (context != null) {
context.close();
}
}
};
handler.accept(conn);
} else {
super.userEventTriggered(ctx, evt);
}
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
HttpTtyConnection tmp = conn;
context = null;
conn = null;
if (tmp != null) {
Consumer<Void> closeHandler = tmp.getCloseHandler();
if (closeHandler != null) {
closeHandler.accept(null);
}
}
}
public void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) throws Exception {
conn.writeToDecoder(msg.text());
}
}
@@ -4,7 +4,6 @@ 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;
@@ -19,6 +18,7 @@ import io.netty.util.concurrent.EventExecutorGroup;
import io.netty.util.concurrent.ScheduledFuture;
import io.termd.core.function.Consumer;
import io.termd.core.function.Supplier;
import io.termd.core.http.netty.TtyWebSocketFrameHandler;
import io.termd.core.telnet.TelnetHandler;
import io.termd.core.telnet.netty.TelnetChannelHandler;
import io.termd.core.tty.TtyConnection;
@@ -87,7 +87,7 @@ public class ProtocolDetectHandler extends ChannelInboundHandlerAdapter {
pipeline.addLast(new HttpObjectAggregator(64 * 1024));
pipeline.addLast(workerGroup, "HttpRequestHandler", new HttpRequestHandler("/ws", new File("arthas-output")));
pipeline.addLast(new WebSocketServerProtocolHandler("/ws"));
pipeline.addLast(new TtyWebSocketFrameHandler(channelGroup, ttyConnectionFactory));
pipeline.addLast(new TtyWebSocketFrameHandler(channelGroup, ttyConnectionFactory, HttpRequestHandler.class));
ctx.fireChannelActive();
}
pipeline.remove(this);