Skip to content

Commit

Permalink
starter
Browse files Browse the repository at this point in the history
  • Loading branch information
wucao committed Oct 24, 2019
1 parent eb11fbd commit c369bca
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 93 deletions.
2 changes: 1 addition & 1 deletion natx-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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
58 changes: 1 addition & 57 deletions natx-client/src/main/java/com/xxg/natx/client/NatxClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@
import com.xxg.natx.common.codec.NatxMessageDecoder;
import com.xxg.natx.common.codec.NatxMessageEncoder;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import org.apache.commons.cli.*;

import java.io.IOException;

Expand All @@ -21,59 +17,7 @@
*/
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;
}
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;
}

connect(serverAddress, Integer.parseInt(serverPort), password, Integer.parseInt(remotePort), proxyAddress, Integer.parseInt(proxyPort));
}
}

private static void connect(String serverAddress, int serverPort, String password, int remotePort, String proxyAddress, int proxyPort) throws IOException, InterruptedException {
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
Expand Down
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));
}
}
}
2 changes: 1 addition & 1 deletion natx-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

0 comments on commit c369bca

Please sign in to comment.