Skip to content

Commit

Permalink
Merge pull request #9 from wucao/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
wucao authored Apr 2, 2020
2 parents a8e9fae + c369bca commit fccb02d
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 107 deletions.
6 changes: 3 additions & 3 deletions natx-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<artifactId>natx</artifactId>
<groupId>com.xxg</groupId>
<version>1.0.2</version>
<version>1.0.3</version>
</parent>

<modelVersion>4.0.0</modelVersion>
Expand All @@ -16,7 +16,7 @@
<dependency>
<groupId>com.xxg</groupId>
<artifactId>natx-common</artifactId>
<version>1.0.2</version>
<version>1.0.3</version>
</dependency>
</dependencies>

Expand All @@ -35,7 +35,7 @@
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.xxg.natx.client.NatxClient</mainClass>
<mainClass>com.xxg.natx.client.NatxClientStarter</mainClass>
</transformer>
</transformers>
</configuration>
Expand Down
92 changes: 33 additions & 59 deletions natx-client/src/main/java/com/xxg/natx/client/NatxClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,76 +4,50 @@
import com.xxg.natx.client.net.TcpConnection;
import com.xxg.natx.common.codec.NatxMessageDecoder;
import com.xxg.natx.common.codec.NatxMessageEncoder;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.timeout.IdleStateHandler;
import org.apache.commons.cli.*;

import java.io.IOException;

/**
* Created by wucao on 2019/2/27.
*/
public class NatxClient {

public static void main(String[] args) throws Exception {

// args
Options options = new Options();
options.addOption("h", false, "Help");
options.addOption("server_addr", true, "Natx server address");
options.addOption("server_port", true, "Natx server port");
options.addOption("password", true, "Natx server password");
options.addOption("proxy_addr", true, "Proxy server address");
options.addOption("proxy_port", true, "Proxy server port");
options.addOption("remote_port", true, "Proxy server remote port");

CommandLineParser parser = new DefaultParser();
CommandLine cmd = parser.parse(options, args);

if (cmd.hasOption("h")) {
// print help
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("options", options);
} else {

String serverAddress = cmd.getOptionValue("server_addr");
if (serverAddress == null) {
System.out.println("server_addr cannot be null");
return;
}
String serverPort = cmd.getOptionValue("server_port");
if (serverPort == null) {
System.out.println("server_port cannot be null");
return;
public void connect(String serverAddress, int serverPort, String password, int remotePort, String proxyAddress, int proxyPort) throws IOException, InterruptedException {
TcpConnection natxConnection = new TcpConnection();
ChannelFuture future = natxConnection.connect(serverAddress, serverPort, new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
NatxClientHandler natxClientHandler = new NatxClientHandler(remotePort, password,
proxyAddress, proxyPort);
ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4),
new NatxMessageDecoder(), new NatxMessageEncoder(),
new IdleStateHandler(60, 30, 0), natxClientHandler);
}
String password = cmd.getOptionValue("password");
String proxyAddress = cmd.getOptionValue("proxy_addr");
if (proxyAddress == null) {
System.out.println("proxy_addr cannot be null");
return;
}
String proxyPort = cmd.getOptionValue("proxy_port");
if (proxyPort == null) {
System.out.println("proxy_port cannot be null");
return;
}
String remotePort = cmd.getOptionValue("remote_port");
if (remotePort == null) {
System.out.println("remote_port cannot be null");
return;
}

TcpConnection natxConnection = new TcpConnection();
natxConnection.connect(serverAddress, Integer.parseInt(serverPort), new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
NatxClientHandler natxClientHandler = new NatxClientHandler(Integer.parseInt(remotePort), password,
proxyAddress, Integer.parseInt(proxyPort));
ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4),
new NatxMessageDecoder(), new NatxMessageEncoder(),
new IdleStateHandler(60, 30, 0), natxClientHandler);
});

// channel close retry connect
future.addListener(future1 -> new Thread() {
@Override
public void run() {
while (true) {
try {
connect(serverAddress, serverPort, password, remotePort, proxyAddress, proxyPort);
break;
} catch (Exception e) {
e.printStackTrace();
try {
Thread.sleep(10000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
});
}
}
}.start());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.xxg.natx.client;

import org.apache.commons.cli.*;

/**
* Created by wucao on 2019/2/27.
*/
public class NatxClientStarter {

public static void main(String[] args) throws Exception {

// args
Options options = new Options();
options.addOption("h", false, "Help");
options.addOption("server_addr", true, "Natx server address");
options.addOption("server_port", true, "Natx server port");
options.addOption("password", true, "Natx server password");
options.addOption("proxy_addr", true, "Proxy server address");
options.addOption("proxy_port", true, "Proxy server port");
options.addOption("remote_port", true, "Proxy server remote port");

CommandLineParser parser = new DefaultParser();
CommandLine cmd = parser.parse(options, args);

if (cmd.hasOption("h")) {
// print help
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("options", options);
} else {

String serverAddress = cmd.getOptionValue("server_addr");
if (serverAddress == null) {
System.out.println("server_addr cannot be null");
return;
}
String serverPort = cmd.getOptionValue("server_port");
if (serverPort == null) {
System.out.println("server_port cannot be null");
return;
}
String password = cmd.getOptionValue("password");
String proxyAddress = cmd.getOptionValue("proxy_addr");
if (proxyAddress == null) {
System.out.println("proxy_addr cannot be null");
return;
}
String proxyPort = cmd.getOptionValue("proxy_port");
if (proxyPort == null) {
System.out.println("proxy_port cannot be null");
return;
}
String remotePort = cmd.getOptionValue("remote_port");
if (remotePort == null) {
System.out.println("remote_port cannot be null");
return;
}

NatxClient client = new NatxClient();
client.connect(serverAddress, Integer.parseInt(serverPort), password, Integer.parseInt(remotePort), proxyAddress, Integer.parseInt(proxyPort));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class NatxClientHandler extends NatxCommonHandler {
private int proxyPort;

private ConcurrentHashMap<String, NatxCommonHandler> channelHandlerMap = new ConcurrentHashMap<>();
private static ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
private ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

public NatxClientHandler(int port, String password, String proxyAddress, int proxyPort) {
this.port = port;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@
*/
public class TcpConnection {

private Channel channel;

/**
*
* @param host
* @param port
* @param channelInitializer
* @throws InterruptedException
*/
public void connect(String host, int port, ChannelInitializer channelInitializer) throws InterruptedException, IOException {
public ChannelFuture connect(String host, int port, ChannelInitializer channelInitializer) throws InterruptedException, IOException {

NioEventLoopGroup workerGroup = new NioEventLoopGroup();

Expand All @@ -32,8 +30,8 @@ public void connect(String host, int port, ChannelInitializer channelInitializer
b.option(ChannelOption.SO_KEEPALIVE, true);
b.handler(channelInitializer);

channel = b.connect(host, port).sync().channel();
channel.closeFuture().addListener((ChannelFutureListener) future -> workerGroup.shutdownGracefully());
Channel channel = b.connect(host, port).sync().channel();
return channel.closeFuture().addListener(future -> workerGroup.shutdownGracefully());
} catch (Exception e) {
workerGroup.shutdownGracefully();
throw e;
Expand Down
2 changes: 1 addition & 1 deletion natx-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<artifactId>natx</artifactId>
<groupId>com.xxg</groupId>
<version>1.0.2</version>
<version>1.0.3</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>natx-common</artifactId>
Expand Down
6 changes: 3 additions & 3 deletions natx-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<artifactId>natx</artifactId>
<groupId>com.xxg</groupId>
<version>1.0.2</version>
<version>1.0.3</version>
</parent>

<modelVersion>4.0.0</modelVersion>
Expand All @@ -16,7 +16,7 @@
<dependency>
<groupId>com.xxg</groupId>
<artifactId>natx-common</artifactId>
<version>1.0.2</version>
<version>1.0.3</version>
</dependency>
</dependencies>

Expand All @@ -35,7 +35,7 @@
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.xxg.natx.server.NatxServer</mainClass>
<mainClass>com.xxg.natx.server.NatxServerStarter</mainClass>
</transformer>
</transformers>
</configuration>
Expand Down
47 changes: 13 additions & 34 deletions natx-server/src/main/java/com/xxg/natx/server/NatxServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,24 @@
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.timeout.IdleStateHandler;
import org.apache.commons.cli.*;

/**
* Created by wucao on 2019/2/27.
*/
public class NatxServer {

public static void main(String[] args) throws InterruptedException, ParseException {

// args
Options options = new Options();
options.addOption("h", false, "Help");
options.addOption("port", true, "Natx server port");
options.addOption("password", true, "Natx server password");

CommandLineParser parser = new DefaultParser();
CommandLine cmd = parser.parse(options, args);

if (cmd.hasOption("h")) {
// print help
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("options", options);
} else {

int port = Integer.parseInt(cmd.getOptionValue("port", "7731"));
String password = cmd.getOptionValue("password");

TcpServer natxClientServer = new TcpServer();
natxClientServer.bind(port, new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch)
throws Exception {
NatxServerHandler natxServerHandler = new NatxServerHandler(password);
ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4),
new NatxMessageDecoder(), new NatxMessageEncoder(),
new IdleStateHandler(60, 30, 0), natxServerHandler);
}
});
System.out.println("Natx server started on port " + port);
}
public void start(int port, String password) throws InterruptedException {

TcpServer natxClientServer = new TcpServer();
natxClientServer.bind(port, new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch)
throws Exception {
NatxServerHandler natxServerHandler = new NatxServerHandler(password);
ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4),
new NatxMessageDecoder(), new NatxMessageEncoder(),
new IdleStateHandler(60, 30, 0), natxServerHandler);
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.xxg.natx.server;

import org.apache.commons.cli.*;

/**
* Created by wucao on 2019/10/23.
*/
public class NatxServerStarter {

public static void main(String[] args) throws ParseException, InterruptedException {

// args
Options options = new Options();
options.addOption("h", false, "Help");
options.addOption("port", true, "Natx server port");
options.addOption("password", true, "Natx server password");

CommandLineParser parser = new DefaultParser();
CommandLine cmd = parser.parse(options, args);

if (cmd.hasOption("h")) {
// print help
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("options", options);
} else {

int port = Integer.parseInt(cmd.getOptionValue("port", "7731"));
String password = cmd.getOptionValue("password");
NatxServer server = new NatxServer();
server.start(port, password);

System.out.println("Natx server started on port " + port);
}
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>com.xxg</groupId>
<artifactId>natx</artifactId>
<packaging>pom</packaging>
<version>1.0.2</version>
<version>1.0.3</version>

<modules>
<module>natx-server</module>
Expand Down

0 comments on commit fccb02d

Please sign in to comment.