mirror of
https://github.com/alibaba/arthas.git
synced 2024-04-21 10:21:39 +00:00
support listen at netty LocalAddress, and tunnel client connect to LocalAddress. #1467
This commit is contained in:
@@ -62,6 +62,7 @@ import io.netty.util.concurrent.EventExecutorGroup;
|
||||
|
||||
/**
|
||||
* @author vlinux on 15/5/2.
|
||||
* @author hengyunabc
|
||||
*/
|
||||
public class ArthasBootstrap {
|
||||
private static final String ARTHAS_SPY_JAR = "arthas-spy.jar";
|
||||
@@ -289,17 +290,10 @@ public class ArthasBootstrap {
|
||||
|
||||
String agentId = null;
|
||||
try {
|
||||
if (configure.getTunnelServer() != null && configure.getHttpPort() > 0) {
|
||||
if (configure.getTunnelServer() != null) {
|
||||
tunnelClient = new TunnelClient();
|
||||
tunnelClient.setId(configure.getAgentId());
|
||||
tunnelClient.setTunnelServerUrl(configure.getTunnelServer());
|
||||
// ws://127.0.0.1:8563/ws
|
||||
String host = "127.0.0.1";
|
||||
if(configure.getIp() != null) {
|
||||
host = configure.getIp();
|
||||
}
|
||||
URI uri = new URI("ws", null, host, configure.getHttpPort(), "/ws", null, null);
|
||||
tunnelClient.setLocalServerUrl(uri.toString());
|
||||
ChannelFuture channelFuture = tunnelClient.start();
|
||||
channelFuture.await(10, TimeUnit.SECONDS);
|
||||
if(channelFuture.isSuccess()) {
|
||||
@@ -341,6 +335,11 @@ public class ArthasBootstrap {
|
||||
shellServer.registerTermServer(new HttpTermServer(configure.getIp(), configure.getHttpPort(),
|
||||
options.getConnectionTimeout(), workerGroup));
|
||||
} else {
|
||||
// listen local address in VM communication
|
||||
if (configure.getTunnelServer() != null) {
|
||||
shellServer.registerTermServer(new HttpTermServer(configure.getIp(), configure.getHttpPort(),
|
||||
options.getConnectionTimeout(), workerGroup));
|
||||
}
|
||||
logger().info("http port is {}, skip bind http server.", configure.getHttpPort());
|
||||
}
|
||||
|
||||
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package com.taobao.arthas.core.shell.term.impl.http;
|
||||
|
||||
import io.netty.channel.ChannelInitializer;
|
||||
import io.netty.channel.ChannelPipeline;
|
||||
import io.netty.channel.group.ChannelGroup;
|
||||
import io.netty.channel.local.LocalChannel;
|
||||
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.netty.util.concurrent.EventExecutorGroup;
|
||||
import io.termd.core.function.Consumer;
|
||||
import io.termd.core.tty.TtyConnection;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author hengyunabc 2020-09-02
|
||||
*
|
||||
*/
|
||||
public class LocalTtyServerInitializer extends ChannelInitializer<LocalChannel> {
|
||||
|
||||
private final ChannelGroup group;
|
||||
private final Consumer<TtyConnection> handler;
|
||||
private EventExecutorGroup workerGroup;
|
||||
|
||||
public LocalTtyServerInitializer(ChannelGroup group, Consumer<TtyConnection> handler,
|
||||
EventExecutorGroup workerGroup) {
|
||||
this.group = group;
|
||||
this.handler = handler;
|
||||
this.workerGroup = workerGroup;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initChannel(LocalChannel ch) throws Exception {
|
||||
|
||||
ChannelPipeline pipeline = ch.pipeline();
|
||||
pipeline.addLast(new HttpServerCodec());
|
||||
pipeline.addLast(new ChunkedWriteHandler());
|
||||
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));
|
||||
}
|
||||
}
|
||||
+39
-14
@@ -1,11 +1,15 @@
|
||||
package com.taobao.arthas.core.shell.term.impl.http;
|
||||
|
||||
import com.taobao.arthas.common.ArthasConstants;
|
||||
|
||||
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.local.LocalAddress;
|
||||
import io.netty.channel.local.LocalServerChannel;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
||||
import io.netty.handler.logging.LogLevel;
|
||||
@@ -61,22 +65,43 @@ public class NettyWebsocketTtyBootstrap {
|
||||
public void start(Consumer<TtyConnection> handler, final Consumer<Throwable> doneHandler) {
|
||||
group = new NioEventLoopGroup(new DefaultThreadFactory("arthas-NettyWebsocketTtyBootstrap", true));
|
||||
|
||||
ServerBootstrap b = new ServerBootstrap();
|
||||
b.group(group).channel(NioServerSocketChannel.class).handler(new LoggingHandler(LogLevel.INFO))
|
||||
.childHandler(new TtyServerInitializer(channelGroup, handler, workerGroup));
|
||||
if (this.port > 0) {
|
||||
ServerBootstrap b = new ServerBootstrap();
|
||||
b.group(group).channel(NioServerSocketChannel.class).handler(new LoggingHandler(LogLevel.INFO))
|
||||
.childHandler(new TtyServerInitializer(channelGroup, handler, workerGroup));
|
||||
|
||||
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());
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// listen local address in VM communication
|
||||
ServerBootstrap b2 = new ServerBootstrap();
|
||||
b2.group(group).channel(LocalServerChannel.class).handler(new LoggingHandler(LogLevel.INFO))
|
||||
.childHandler(new LocalTtyServerInitializer(channelGroup, handler, workerGroup));
|
||||
|
||||
ChannelFuture bindLocalFuture = b2.bind(new LocalAddress(ArthasConstants.NETTY_LOCAL_ADDRESS));
|
||||
if (this.port < 0) { // 保证回调doneHandler
|
||||
bindLocalFuture.addListener(new GenericFutureListener<Future<? super Void>>() {
|
||||
@Override
|
||||
public void operationComplete(Future<? super Void> future) throws Exception {
|
||||
if (future.isSuccess()) {
|
||||
doneHandler.accept(null);
|
||||
} else {
|
||||
doneHandler.accept(future.cause());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public CompletableFuture<Void> start(Consumer<TtyConnection> handler) {
|
||||
|
||||
Reference in New Issue
Block a user