add tunnel-common module. #1548

pull/1585/head^2
hengyunabc 4 years ago
parent a2b45c45b8
commit 79c27ad13c

@ -58,6 +58,7 @@
<modules>
<module>spy</module>
<module>common</module>
<module>tunnel-common</module>
<module>tunnel-client</module>
<module>core</module>
<module>agent</module>

@ -16,6 +16,11 @@
<artifactId>arthas-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.taobao.arthas</groupId>
<artifactId>arthas-tunnel-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>

@ -9,6 +9,9 @@ import javax.net.ssl.SSLException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.arthas.tunnel.common.MethodConstants;
import com.alibaba.arthas.tunnel.common.URIConstans;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
@ -57,9 +60,9 @@ public class TunnelClient {
public ChannelFuture connect(boolean reconnect) throws SSLException, URISyntaxException, InterruptedException {
QueryStringEncoder queryEncoder = new QueryStringEncoder(this.tunnelServerUrl);
queryEncoder.addParam("method", "agentRegister");
queryEncoder.addParam(URIConstans.METHOD, MethodConstants.AGENT_REGISTER);
if (id != null) {
queryEncoder.addParam("id", id);
queryEncoder.addParam(URIConstans.ID, id);
}
// ws://127.0.0.1:7777/ws?method=agentRegister
final URI agentRegisterURI = queryEncoder.toUri();

@ -2,26 +2,24 @@
package com.alibaba.arthas.tunnel.client;
import java.net.URI;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.arthas.tunnel.common.MethodConstants;
import com.alibaba.arthas.tunnel.common.URIConstans;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.handler.codec.http.QueryStringDecoder;
import io.netty.handler.codec.http.QueryStringEncoder;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
import io.netty.util.concurrent.DefaultThreadFactory;
/**
*
@ -57,25 +55,25 @@ public class TunnelClientSocketClientHandler extends SimpleChannelInboundHandler
QueryStringDecoder queryDecoder = new QueryStringDecoder(text);
Map<String, List<String>> parameters = queryDecoder.parameters();
List<String> methodList = parameters.get("method");
List<String> methodList = parameters.get(URIConstans.METHOD);
String method = null;
if (methodList != null && !methodList.isEmpty()) {
method = methodList.get(0);
}
if ("agentRegister".equals(method)) {
List<String> idList = parameters.get("id");
if (MethodConstants.AGENT_REGISTER.equals(method)) {
List<String> idList = parameters.get(URIConstans.ID);
if (idList != null && !idList.isEmpty()) {
this.tunnelClient.setId(idList.get(0));
}
registerPromise.setSuccess();
}
if ("startTunnel".equals(method)) {
if (MethodConstants.START_TUNNEL.equals(method)) {
QueryStringEncoder queryEncoder = new QueryStringEncoder(this.tunnelClient.getTunnelServerUrl());
queryEncoder.addParam("method", "openTunnel");
queryEncoder.addParam("clientConnectionId", parameters.get("clientConnectionId").get(0));
queryEncoder.addParam("id", parameters.get("id").get(0));
queryEncoder.addParam(URIConstans.METHOD, MethodConstants.OPEN_TUNNEL);
queryEncoder.addParam(URIConstans.CLIENT_CONNECTION_ID, parameters.get(URIConstans.CLIENT_CONNECTION_ID).get(0));
queryEncoder.addParam(URIConstans.ID, parameters.get(URIConstans.ID).get(0));
final URI forwardUri = queryEncoder.toUri();

@ -0,0 +1,34 @@
<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.taobao.arthas</groupId>
<artifactId>arthas-all</artifactId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>arthas-tunnel-common</artifactId>
<name>arthas-tunnel-common</name>
<dependencies>
</dependencies>
<build>
<finalName>arthas-tunnel-common</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
</plugins>
</build>
</project>

@ -0,0 +1,52 @@
package com.alibaba.arthas.tunnel.common;
/**
* tunnel clientserver URIURI method
*
* @author hengyunabc 2020-10-22
*
*/
public class MethodConstants {
/**
*
* <pre>
* tunnel client method
*
* ws://192.168.1.10:7777/ws?method=agentRegister
*
* tunnel server
*
* response:/?method=agentRegister&id=bvDOe8XbTM2pQWjF4cfw
*
* id
* </pre>
*/
public static final String AGENT_REGISTER = "agentRegister";
/**
* <pre>
* tunnel server tunnel client
*
* response:/?method=startTunnel&id=bvDOe8XbTM2pQWjF4cfw&clientConnectionId=AMku9EFz2gxeL2gedGOC
* </pre>
*/
public static final String START_TUNNEL = "startTunnel";
/**
* <pre>
* browser tunnel server tunnel client
*
* ws://192.168.1.10:7777/ws?method=connectArthas&id=bvDOe8XbTM2pQWjF4cfw
* </pre>
*/
public static final String CONNECT_ARTHAS = "connectArthas";
/**
* <pre>
* tunnel client startTunnel URI
*
* ws://127.0.0.1:7777/ws/?method=openTunnel&clientConnectionId=AMku9EFz2gxeL2gedGOC&id=bvDOe8XbTM2pQWjF4cfw
* </pre>
*/
public static final String OPEN_TUNNEL = "openTunnel";
}

@ -0,0 +1,25 @@
package com.alibaba.arthas.tunnel.common;
/**
*
* @author hengyunabc 2020-10-22
*
*/
public class URIConstans {
/**
* @see MethodConstants
*/
public static final String METHOD = "method";
public static final String RESPONSE = "response";
/**
* agent id
*/
public static final String ID = "id";
/**
* tunnel server tunnel client id
*/
public static final String CLIENT_CONNECTION_ID = "clientConnectionId";
}

@ -32,6 +32,11 @@
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.taobao.arthas</groupId>
<artifactId>arthas-tunnel-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
@ -55,11 +60,27 @@
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>

@ -17,6 +17,9 @@ import org.slf4j.LoggerFactory;
import org.springframework.util.MultiValueMap;
import org.springframework.web.util.UriComponentsBuilder;
import com.alibaba.arthas.tunnel.common.MethodConstants;
import com.alibaba.arthas.tunnel.common.URIConstans;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
@ -56,12 +59,12 @@ public class TunnelSocketFrameHandler extends SimpleChannelInboundHandler<WebSoc
MultiValueMap<String, String> parameters = UriComponentsBuilder.fromUriString(uri).build().getQueryParams();
String method = parameters.getFirst("method");
if ("connectArthas".equals(method)) { // form browser
if (MethodConstants.CONNECT_ARTHAS.equals(method)) { // form browser
connectArthas(ctx, parameters);
} else if ("agentRegister".equals(method)) { // form arthas agent, register
} else if (MethodConstants.AGENT_REGISTER.equals(method)) { // form arthas agent, register
agentRegister(ctx, uri);
}
if ("openTunnel".equals(method)) { // from arthas agent open tunnel
if (MethodConstants.OPEN_TUNNEL.equals(method)) { // from arthas agent open tunnel
String clientConnectionId = parameters.getFirst("clientConnectionId");
openTunnel(ctx, clientConnectionId);
}
@ -95,8 +98,11 @@ public class TunnelSocketFrameHandler extends SimpleChannelInboundHandler<WebSoc
String clientConnectionId = RandomStringUtils.random(20, true, true).toUpperCase();
logger.info("random clientConnectionId: " + clientConnectionId);
URI uri = new URI("response", null, "/",
"method=startTunnel" + "&id=" + agentId.get(0) + "&clientConnectionId=" + clientConnectionId, null);
// URI uri = new URI("response", null, "/",
// "method=" + MethodConstants.START_TUNNEL + "&id=" + agentId.get(0) + "&clientConnectionId=" + clientConnectionId, null);
URI uri = UriComponentsBuilder.newInstance().scheme(URIConstans.RESPONSE).path("/")
.queryParam(URIConstans.METHOD, MethodConstants.START_TUNNEL).queryParam(URIConstans.ID, agentId)
.queryParam(URIConstans.CLIENT_CONNECTION_ID, clientConnectionId).build().toUri();
logger.info("startTunnel response: " + uri);
@ -173,7 +179,10 @@ public class TunnelSocketFrameHandler extends SimpleChannelInboundHandler<WebSoc
final String finalId = id;
URI responseUri = new URI("response", null, "/", "method=agentRegister" + "&id=" + id, null);
// URI responseUri = new URI("response", null, "/", "method=" + MethodConstants.AGENT_REGISTER + "&id=" + id, null);
URI responseUri = UriComponentsBuilder.newInstance().scheme(URIConstans.RESPONSE).path("/")
.queryParam(URIConstans.METHOD, MethodConstants.AGENT_REGISTER).queryParam(URIConstans.ID, id).build()
.encode().toUri();
AgentInfo info = new AgentInfo();
SocketAddress remoteAddress = ctx.channel().remoteAddress();

Loading…
Cancel
Save