Zond: соединение с Bridge
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
group = 'asys'
|
group = 'asys'
|
||||||
version = '0.7.6-SNAPSHOT'
|
version = '0.7.7-SNAPSHOT'
|
||||||
|
|
||||||
apply plugin: 'application'
|
apply plugin: 'application'
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,8 @@
|
|||||||
*/
|
*/
|
||||||
package asys.zond;
|
package asys.zond;
|
||||||
|
|
||||||
import asys.zond.proxy.Connector;
|
import asys.zond.proxy.client.Connector;
|
||||||
|
import asys.zond.proxy.server.Server;
|
||||||
import asys.zond.shell.CommandHandler;
|
import asys.zond.shell.CommandHandler;
|
||||||
import asys.zond.shell.Shell;
|
import asys.zond.shell.Shell;
|
||||||
import org.apache.commons.exec.CommandLine;
|
import org.apache.commons.exec.CommandLine;
|
||||||
@@ -19,6 +20,7 @@ public class ZondCommandHandler implements CommandHandler {
|
|||||||
private CommandLine commandLine;
|
private CommandLine commandLine;
|
||||||
private ExecuteWatchdog watchdog;
|
private ExecuteWatchdog watchdog;
|
||||||
private Thread threadExec;
|
private Thread threadExec;
|
||||||
|
private Server server;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handle(String commandLine) {
|
public void handle(String commandLine) {
|
||||||
@@ -32,6 +34,7 @@ public class ZondCommandHandler implements CommandHandler {
|
|||||||
private void internalCommand(String line) {
|
private void internalCommand(String line) {
|
||||||
if (line.equalsIgnoreCase("exit") || line.equalsIgnoreCase("quit")) {
|
if (line.equalsIgnoreCase("exit") || line.equalsIgnoreCase("quit")) {
|
||||||
if (watchdog != null) {
|
if (watchdog != null) {
|
||||||
|
server.shutdown();
|
||||||
watchdog.destroyProcess();
|
watchdog.destroyProcess();
|
||||||
threadExec.interrupt();
|
threadExec.interrupt();
|
||||||
}
|
}
|
||||||
@@ -44,12 +47,18 @@ public class ZondCommandHandler implements CommandHandler {
|
|||||||
Runnable task = () -> {
|
Runnable task = () -> {
|
||||||
int code = 0;
|
int code = 0;
|
||||||
try {
|
try {
|
||||||
|
server = new Server();
|
||||||
|
server.start(Config.getInstance().getInt("bridge.port"));
|
||||||
|
|
||||||
code = executor.execute(commandLine);
|
code = executor.execute(commandLine);
|
||||||
} catch (ExecuteException ignore) {
|
} catch (ExecuteException ignore) {
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Shell.getInstance().getOutput().println("Exception message: " + e.getMessage());
|
Shell.getInstance().getOutput().println("Exception message: " + e.getMessage());
|
||||||
code = -99;
|
code = -99;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
server.shutdown();
|
||||||
|
server = null;
|
||||||
watchdog = null;
|
watchdog = null;
|
||||||
Shell.getInstance().getOutput().println("Process finished. Code: " + code);
|
Shell.getInstance().getOutput().println("Process finished. Code: " + code);
|
||||||
};
|
};
|
||||||
@@ -58,6 +67,7 @@ public class ZondCommandHandler implements CommandHandler {
|
|||||||
}
|
}
|
||||||
} else if (line.equalsIgnoreCase("kill")) {
|
} else if (line.equalsIgnoreCase("kill")) {
|
||||||
if (watchdog != null) {
|
if (watchdog != null) {
|
||||||
|
server.shutdown();
|
||||||
watchdog.destroyProcess();
|
watchdog.destroyProcess();
|
||||||
threadExec.interrupt();
|
threadExec.interrupt();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,30 +10,30 @@ public class TaskTicker implements Runnable {
|
|||||||
private Thread thread;
|
private Thread thread;
|
||||||
private boolean loop = false;
|
private boolean loop = false;
|
||||||
|
|
||||||
TaskTicker setTask(Runnable task) {
|
public TaskTicker setTask(Runnable task) {
|
||||||
this.task = task;
|
this.task = task;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
TaskTicker setStepTimeMs(long stepTimeMs) {
|
public TaskTicker setStepTimeMs(long stepTimeMs) {
|
||||||
this.stepTimeMs = stepTimeMs;
|
this.stepTimeMs = stepTimeMs;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
void start() {
|
public void start() {
|
||||||
thread = new Thread(this, "TaskTicker");
|
thread = new Thread(this, "TaskTicker");
|
||||||
loop = true;
|
loop = true;
|
||||||
thread.start();
|
thread.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
void stop() {
|
public void stop() {
|
||||||
loop = false;
|
loop = false;
|
||||||
if (thread != null) {
|
if (thread != null) {
|
||||||
thread.interrupt();
|
thread.interrupt();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean isActive() {
|
public boolean isActive() {
|
||||||
return loop;
|
return loop;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
* DmitriyMX <dimon550@gmail.com>
|
* DmitriyMX <dimon550@gmail.com>
|
||||||
* 2017-06-16
|
* 2017-06-16
|
||||||
*/
|
*/
|
||||||
package asys.zond.proxy;
|
package asys.zond.proxy.client;
|
||||||
|
|
||||||
import asys.mcsmanager.packets.Packet;
|
import asys.mcsmanager.packets.Packet;
|
||||||
import asys.mcsmanager.packets.codec.PacketDecoder;
|
import asys.mcsmanager.packets.codec.PacketDecoder;
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
* DmitriyMX <dimon550@gmail.com>
|
* DmitriyMX <dimon550@gmail.com>
|
||||||
* 2017-06-16
|
* 2017-06-16
|
||||||
*/
|
*/
|
||||||
package asys.zond.proxy;
|
package asys.zond.proxy.client;
|
||||||
|
|
||||||
import asys.mcsmanager.packets.*;
|
import asys.mcsmanager.packets.*;
|
||||||
import asys.zond.Config;
|
import asys.zond.Config;
|
||||||
@@ -34,7 +34,8 @@ public class ClientPacketHandler extends ChannelInboundHandlerAdapter implements
|
|||||||
|
|
||||||
knownPackets = ImmutableBiMap.of(
|
knownPackets = ImmutableBiMap.of(
|
||||||
3, CS_Ping.class,
|
3, CS_Ping.class,
|
||||||
4, CS_ConsoleMessage.class
|
4, CS_ConsoleMessage.class,
|
||||||
|
5, SC_Command.class
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2,10 +2,11 @@
|
|||||||
* DmitriyMX <dimon550@gmail.com>
|
* DmitriyMX <dimon550@gmail.com>
|
||||||
* 2017-06-16
|
* 2017-06-16
|
||||||
*/
|
*/
|
||||||
package asys.zond.proxy;
|
package asys.zond.proxy.client;
|
||||||
|
|
||||||
import asys.mcsmanager.packets.Packet;
|
import asys.mcsmanager.packets.Packet;
|
||||||
import asys.zond.Config;
|
import asys.zond.Config;
|
||||||
|
import asys.zond.proxy.TaskTicker;
|
||||||
import asys.zond.shell.Shell;
|
import asys.zond.shell.Shell;
|
||||||
|
|
||||||
public class Connector {
|
public class Connector {
|
||||||
57
zond/src/main/java/asys/zond/proxy/server/Server.java
Normal file
57
zond/src/main/java/asys/zond/proxy/server/Server.java
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
/*
|
||||||
|
* DmitriyMX <dimon550@gmail.com>
|
||||||
|
* 2017-06-17
|
||||||
|
*/
|
||||||
|
package asys.zond.proxy.server;
|
||||||
|
|
||||||
|
import asys.mcsmanager.packets.codec.PacketDecoder;
|
||||||
|
import asys.mcsmanager.packets.codec.PacketEncoder;
|
||||||
|
import asys.mcsmanager.packets.codec.PacketHandler;
|
||||||
|
import io.netty.bootstrap.ServerBootstrap;
|
||||||
|
import io.netty.channel.ChannelHandler;
|
||||||
|
import io.netty.channel.ChannelInitializer;
|
||||||
|
import io.netty.channel.EventLoopGroup;
|
||||||
|
import io.netty.channel.nio.NioEventLoopGroup;
|
||||||
|
import io.netty.channel.socket.SocketChannel;
|
||||||
|
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
||||||
|
|
||||||
|
public class Server {
|
||||||
|
private EventLoopGroup bossGroup, workerGroup;
|
||||||
|
|
||||||
|
public void start(int port) {
|
||||||
|
bossGroup = new NioEventLoopGroup(1);
|
||||||
|
workerGroup = new NioEventLoopGroup();
|
||||||
|
|
||||||
|
ServerBootstrap serverBootstrap = createServerBootstrap();
|
||||||
|
serverBootstrap.bind("127.0.0.1", port);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void shutdown() {
|
||||||
|
workerGroup.shutdownGracefully();
|
||||||
|
bossGroup.shutdownGracefully();
|
||||||
|
}
|
||||||
|
|
||||||
|
private ServerBootstrap createServerBootstrap() {
|
||||||
|
ServerBootstrap bootstrap = new ServerBootstrap();
|
||||||
|
|
||||||
|
bootstrap.group(bossGroup, workerGroup)
|
||||||
|
.channel(NioServerSocketChannel.class)
|
||||||
|
.childHandler(createChannelInitializer());
|
||||||
|
|
||||||
|
return bootstrap;
|
||||||
|
}
|
||||||
|
|
||||||
|
private ChannelHandler createChannelInitializer() {
|
||||||
|
return new ChannelInitializer<SocketChannel>() {
|
||||||
|
@Override
|
||||||
|
protected void initChannel(SocketChannel socketChannel) throws Exception {
|
||||||
|
socketChannel.pipeline().addLast(
|
||||||
|
new PacketEncoder(),
|
||||||
|
new PacketDecoder(),
|
||||||
|
new PacketHandler(),
|
||||||
|
new ServerPacketHandler()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
/*
|
||||||
|
* DmitriyMX <dimon550@gmail.com>
|
||||||
|
* 2017-06-17
|
||||||
|
*/
|
||||||
|
package asys.zond.proxy.server;
|
||||||
|
|
||||||
|
import asys.mcsmanager.packets.*;
|
||||||
|
import asys.zond.proxy.client.Connector;
|
||||||
|
import asys.zond.shell.Shell;
|
||||||
|
import com.google.common.collect.BiMap;
|
||||||
|
import com.google.common.collect.ImmutableBiMap;
|
||||||
|
import com.google.common.collect.ImmutableMap;
|
||||||
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
|
import io.netty.channel.ChannelInboundHandlerAdapter;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static asys.mcsmanager.packets.codec.Params.KNOWN_HANDLERS;
|
||||||
|
import static asys.mcsmanager.packets.codec.Params.KNOWN_PACKETS;
|
||||||
|
|
||||||
|
public class ServerPacketHandler extends ChannelInboundHandlerAdapter implements IPacketHandler {
|
||||||
|
private final BiMap<Integer, Class<? extends Packet>> knownPackets;
|
||||||
|
private final Map<Class<? extends Packet>, IPacketHandler> knownHandlers;
|
||||||
|
|
||||||
|
ServerPacketHandler() {
|
||||||
|
knownPackets = ImmutableBiMap.of(
|
||||||
|
3, CS_Ping.class,
|
||||||
|
5, SC_Command.class
|
||||||
|
);
|
||||||
|
knownHandlers = ImmutableMap.of(
|
||||||
|
CS_Ping.class, this,
|
||||||
|
CS_ConsoleMessage.class, this
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||||
|
ctx.channel().attr(KNOWN_PACKETS).set(knownPackets);
|
||||||
|
ctx.channel().attr(KNOWN_HANDLERS).set(knownHandlers);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||||
|
ctx.channel().attr(KNOWN_PACKETS).remove();
|
||||||
|
ctx.channel().attr(KNOWN_HANDLERS).remove();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handle(Packet packet, ChannelHandlerContext context) {
|
||||||
|
if (packet instanceof CS_Ping) {
|
||||||
|
handleCSPing((CS_Ping) packet);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleCSPing(CS_Ping packet) {
|
||||||
|
Shell.getInstance().getOutput().println("proxied CS_Ping... [" + packet.getTime() + "]");
|
||||||
|
Connector.getInstance().sendPacket(packet);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
serverId = SpigotServer0
|
serverId = SpigotServer0
|
||||||
host = 127.0.0.1
|
host = 127.0.0.1
|
||||||
port = 8779
|
port = 8779
|
||||||
passcode = testpassphrase
|
passcode = testpassphrase
|
||||||
|
bridge.port = 8710
|
||||||
Reference in New Issue
Block a user