Status response
This commit is contained in:
5
pom.xml
5
pom.xml
@@ -77,6 +77,11 @@
|
|||||||
<artifactId>guava</artifactId>
|
<artifactId>guava</artifactId>
|
||||||
<version>24.1-jre</version>
|
<version>24.1-jre</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.code.gson</groupId>
|
||||||
|
<artifactId>gson</artifactId>
|
||||||
|
<version>2.8.2</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
40
src/main/java/mc/core/ByteArrayOutputNetStream.java
Normal file
40
src/main/java/mc/core/ByteArrayOutputNetStream.java
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
* DmitriyMX <dimon550@gmail.com>
|
||||||
|
* 2018-04-08
|
||||||
|
*/
|
||||||
|
package mc.core;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
|
||||||
|
public class ByteArrayOutputNetStream extends NetStream {
|
||||||
|
private ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte readByte() {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void readBytes(byte[] buffer) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int readUnsignedShort() {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void writeByte(int value) {
|
||||||
|
baos.write(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void writeBytes(byte[] buffer) {
|
||||||
|
baos.write(buffer, 0, buffer.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] toByteArray() {
|
||||||
|
return baos.toByteArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,7 +7,7 @@ package mc.core;
|
|||||||
public interface Config {
|
public interface Config {
|
||||||
int getMaxPlayers();
|
int getMaxPlayers();
|
||||||
String getDescriptionServer();
|
String getDescriptionServer();
|
||||||
String getFaviconBase64();
|
byte[] getFaviconBase64();
|
||||||
String getHost();
|
String getHost();
|
||||||
int getPort();
|
int getPort();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,11 +10,13 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
|
|||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static ApplicationContext appContext; //FIXME
|
||||||
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
|
|
||||||
Config config = context.getBean("config", Config.class);
|
|
||||||
|
|
||||||
Server server = context.getBean("server", Server.class);
|
public static void main(String[] args) {
|
||||||
|
appContext = new ClassPathXmlApplicationContext("spring.xml");
|
||||||
|
Config config = appContext.getBean("config", Config.class);
|
||||||
|
|
||||||
|
Server server = appContext.getBean("server", Server.class);
|
||||||
try {
|
try {
|
||||||
server.start(config.getHost(), config.getPort());
|
server.start(config.getHost(), config.getPort());
|
||||||
} catch (StartServerException e) {
|
} catch (StartServerException e) {
|
||||||
|
|||||||
@@ -42,6 +42,18 @@ public abstract class NetStream {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void writeVarInt(int value) {
|
||||||
|
do {
|
||||||
|
byte temp = (byte)(value & 0b01111111);
|
||||||
|
value >>>= 7;
|
||||||
|
if (value != 0) {
|
||||||
|
temp |= 0b10000000;
|
||||||
|
}
|
||||||
|
|
||||||
|
writeByte(temp);
|
||||||
|
} while (value != 0);
|
||||||
|
}
|
||||||
|
|
||||||
public String readString() {
|
public String readString() {
|
||||||
int length = readVarInt();
|
int length = readVarInt();
|
||||||
|
|
||||||
@@ -51,7 +63,15 @@ public abstract class NetStream {
|
|||||||
return new String(buffer, StandardCharsets.UTF_8);
|
return new String(buffer, StandardCharsets.UTF_8);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void writeString(String value) {
|
||||||
|
writeVarInt(value.length());
|
||||||
|
writeBytes(value.getBytes());
|
||||||
|
}
|
||||||
|
|
||||||
public abstract byte readByte();
|
public abstract byte readByte();
|
||||||
public abstract void readBytes(byte[] buffer);
|
public abstract void readBytes(byte[] buffer);
|
||||||
public abstract int readUnsignedShort();
|
public abstract int readUnsignedShort();
|
||||||
|
|
||||||
|
public abstract void writeByte(int value);
|
||||||
|
public abstract void writeBytes(byte[] buffer);
|
||||||
}
|
}
|
||||||
|
|||||||
9
src/main/java/mc/core/SCPacket.java
Normal file
9
src/main/java/mc/core/SCPacket.java
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
/*
|
||||||
|
* DmitriyMX <dimon550@gmail.com>
|
||||||
|
* 2018-04-08
|
||||||
|
*/
|
||||||
|
package mc.core;
|
||||||
|
|
||||||
|
public interface SCPacket {
|
||||||
|
byte[] toByteArray();
|
||||||
|
}
|
||||||
@@ -19,7 +19,7 @@ import java.util.Base64;
|
|||||||
public class ConfigFromSpring implements Config {
|
public class ConfigFromSpring implements Config {
|
||||||
@Setter
|
@Setter
|
||||||
private String descriptionServer;
|
private String descriptionServer;
|
||||||
private String faviconBase64;
|
private byte[] faviconBase64;
|
||||||
@Setter
|
@Setter
|
||||||
private int maxPlayers;
|
private int maxPlayers;
|
||||||
@Setter
|
@Setter
|
||||||
@@ -30,10 +30,8 @@ public class ConfigFromSpring implements Config {
|
|||||||
public void setFaviconBase64(File faviconImageFile) {
|
public void setFaviconBase64(File faviconImageFile) {
|
||||||
log.debug("faviconImageFile: {}", faviconImageFile.getAbsolutePath());
|
log.debug("faviconImageFile: {}", faviconImageFile.getAbsolutePath());
|
||||||
try {
|
try {
|
||||||
faviconBase64 = new String(
|
faviconBase64 = Base64.getEncoder().encode(
|
||||||
Base64.getEncoder().encode(
|
FileUtils.readFileToByteArray(faviconImageFile)
|
||||||
FileUtils.readFileToByteArray(faviconImageFile)
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.warn("Can't load favicon", e);
|
log.warn("Can't load favicon", e);
|
||||||
|
|||||||
@@ -24,7 +24,8 @@ public class NettyServer implements Server{
|
|||||||
socketChannel.pipeline().addLast(
|
socketChannel.pipeline().addLast(
|
||||||
new LoggingHandler(),
|
new LoggingHandler(),
|
||||||
new PacketDecoder(),
|
new PacketDecoder(),
|
||||||
new PacketHandler()
|
new PacketHandler(),
|
||||||
|
new PacketEncoder()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
32
src/main/java/mc/core/netty/PacketEncoder.java
Normal file
32
src/main/java/mc/core/netty/PacketEncoder.java
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
/*
|
||||||
|
* DmitriyMX <dimon550@gmail.com>
|
||||||
|
* 2018-04-08
|
||||||
|
*/
|
||||||
|
package mc.core.netty;
|
||||||
|
|
||||||
|
import io.netty.buffer.ByteBuf;
|
||||||
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
|
import io.netty.handler.codec.MessageToByteEncoder;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import mc.core.NetStream;
|
||||||
|
import mc.core.SCPacket;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
public class PacketEncoder extends MessageToByteEncoder<SCPacket> {
|
||||||
|
@Override
|
||||||
|
protected void encode(ChannelHandlerContext ctx, SCPacket pkt, ByteBuf out) throws Exception {
|
||||||
|
State state = ctx.channel().attr(State.ATTR_STATE).get();
|
||||||
|
Integer id = state.getServerSidePaclet(pkt.getClass());
|
||||||
|
if (id == null) {
|
||||||
|
log.error("Packet not found: {}:{}", state.name(), pkt.getClass().getSimpleName());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
byte[] bytes = pkt.toByteArray();
|
||||||
|
NetStream netStream = new WrapperNetStream(out);
|
||||||
|
|
||||||
|
netStream.writeVarInt(bytes.length + NetStream.sizeVarInt(id));
|
||||||
|
netStream.writeVarInt(id);
|
||||||
|
netStream.writeBytes(bytes);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,11 +4,16 @@
|
|||||||
*/
|
*/
|
||||||
package mc.core.netty;
|
package mc.core.netty;
|
||||||
|
|
||||||
|
import io.netty.channel.Channel;
|
||||||
import io.netty.channel.ChannelHandlerContext;
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
import io.netty.channel.SimpleChannelInboundHandler;
|
import io.netty.channel.SimpleChannelInboundHandler;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import mc.core.CSPacket;
|
import mc.core.CSPacket;
|
||||||
|
import mc.core.Config;
|
||||||
|
import mc.core.Main;
|
||||||
import mc.core.netty.packets.StatusRequest;
|
import mc.core.netty.packets.StatusRequest;
|
||||||
|
import mc.core.netty.packets.StatusResponse;
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@@ -23,17 +28,27 @@ public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> {
|
|||||||
Optional<Method> optionalMethod = Arrays.stream(this.getClass().getDeclaredMethods())
|
Optional<Method> optionalMethod = Arrays.stream(this.getClass().getDeclaredMethods())
|
||||||
.filter(method -> method.getName().equals("on" + packet.getClass().getSimpleName())
|
.filter(method -> method.getName().equals("on" + packet.getClass().getSimpleName())
|
||||||
&& method.getParameterCount() == 2
|
&& method.getParameterCount() == 2
|
||||||
&& method.getParameterTypes()[0].isAssignableFrom(ctx.getClass())
|
&& method.getParameterTypes()[0].isAssignableFrom(Channel.class)
|
||||||
&& method.getParameterTypes()[1].isAssignableFrom(packet.getClass()))
|
&& method.getParameterTypes()[1].isAssignableFrom(packet.getClass()))
|
||||||
.findFirst();
|
.findFirst();
|
||||||
|
|
||||||
if (optionalMethod.isPresent()) {
|
if (optionalMethod.isPresent()) {
|
||||||
Method method = optionalMethod.get();
|
Method method = optionalMethod.get();
|
||||||
method.invoke(this, ctx, packet);
|
method.invoke(this, ctx.channel(), packet);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onStatusRequest(ChannelHandlerContext ctx, StatusRequest packet) {
|
public void onStatusRequest(Channel channel, StatusRequest packet) {
|
||||||
log.info("Catch!");
|
if (!packet.getNextState().equals(State.UNKNOWN)) {
|
||||||
|
channel.attr(State.ATTR_STATE).set(packet.getNextState());
|
||||||
|
}
|
||||||
|
|
||||||
|
Config config = Main.appContext.getBean("config", Config.class); //FIXME
|
||||||
|
StatusResponse pkt = new StatusResponse();
|
||||||
|
pkt.setDescription(config.getDescriptionServer());
|
||||||
|
pkt.setMaxOnline(config.getMaxPlayers());
|
||||||
|
pkt.setFaviconBase64(config.getFaviconBase64());
|
||||||
|
|
||||||
|
channel.writeAndFlush(pkt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,9 @@ import com.google.common.collect.ImmutableMap;
|
|||||||
import io.netty.util.AttributeKey;
|
import io.netty.util.AttributeKey;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import mc.core.CSPacket;
|
import mc.core.CSPacket;
|
||||||
|
import mc.core.SCPacket;
|
||||||
import mc.core.netty.packets.StatusRequest;
|
import mc.core.netty.packets.StatusRequest;
|
||||||
|
import mc.core.netty.packets.StatusResponse;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -16,10 +18,14 @@ import java.util.Optional;
|
|||||||
|
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public enum State {
|
public enum State {
|
||||||
UNKNOWN(0, ImmutableMap.of()),
|
UNKNOWN(0, ImmutableMap.of(), ImmutableMap.of()),
|
||||||
STATUS(1, ImmutableMap.of(
|
STATUS(1,
|
||||||
|
ImmutableMap.of(
|
||||||
0, StatusRequest.class
|
0, StatusRequest.class
|
||||||
));
|
), ImmutableMap.of(
|
||||||
|
StatusResponse.class, 0
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
public static final AttributeKey<State> ATTR_STATE = AttributeKey.newInstance("ATTR_STATE");
|
public static final AttributeKey<State> ATTR_STATE = AttributeKey.newInstance("ATTR_STATE");
|
||||||
|
|
||||||
@@ -33,8 +39,13 @@ public enum State {
|
|||||||
|
|
||||||
private final int id;
|
private final int id;
|
||||||
private final Map<Integer, Class<? extends CSPacket>> clientSidePackets;
|
private final Map<Integer, Class<? extends CSPacket>> clientSidePackets;
|
||||||
|
private final Map<Class<? extends SCPacket>, Integer> serverSidePackets;
|
||||||
|
|
||||||
public Class<? extends CSPacket> getClientSidePacket(final int packetId) {
|
public Class<? extends CSPacket> getClientSidePacket(final int packetId) {
|
||||||
return clientSidePackets.get(packetId);
|
return clientSidePackets.get(packetId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Integer getServerSidePaclet(final Class<? extends SCPacket> clazz) {
|
||||||
|
return serverSidePackets.get(clazz);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,4 +26,14 @@ public class WrapperNetStream extends NetStream {
|
|||||||
public int readUnsignedShort() {
|
public int readUnsignedShort() {
|
||||||
return byteBuf.readUnsignedShort();
|
return byteBuf.readUnsignedShort();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void writeByte(int value) {
|
||||||
|
byteBuf.writeByte(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void writeBytes(byte[] buffer) {
|
||||||
|
byteBuf.writeBytes(buffer);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
56
src/main/java/mc/core/netty/packets/StatusResponse.java
Normal file
56
src/main/java/mc/core/netty/packets/StatusResponse.java
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
/*
|
||||||
|
* DmitriyMX <dimon550@gmail.com>
|
||||||
|
* 2018-04-08
|
||||||
|
*/
|
||||||
|
package mc.core.netty.packets;
|
||||||
|
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
import lombok.Setter;
|
||||||
|
import lombok.ToString;
|
||||||
|
import mc.core.ByteArrayOutputNetStream;
|
||||||
|
import mc.core.SCPacket;
|
||||||
|
|
||||||
|
@ToString
|
||||||
|
public class StatusResponse implements SCPacket {
|
||||||
|
private static final String name = "1.12.2";
|
||||||
|
private static final int protocol = 340;
|
||||||
|
|
||||||
|
@Setter
|
||||||
|
private int maxOnline;
|
||||||
|
@Setter
|
||||||
|
private int online;
|
||||||
|
@Setter
|
||||||
|
private String description;
|
||||||
|
@Setter
|
||||||
|
private byte[] faviconBase64;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte[] toByteArray() {
|
||||||
|
ByteArrayOutputNetStream netStream = new ByteArrayOutputNetStream();
|
||||||
|
|
||||||
|
JsonObject versionObj = new JsonObject();
|
||||||
|
versionObj.addProperty("name", name);
|
||||||
|
versionObj.addProperty("protocol", protocol);
|
||||||
|
|
||||||
|
JsonObject playersObj = new JsonObject();
|
||||||
|
playersObj.addProperty("max", maxOnline);
|
||||||
|
playersObj.addProperty("online", online);
|
||||||
|
|
||||||
|
JsonObject descriptionObj = new JsonObject();
|
||||||
|
descriptionObj.addProperty("text", description);
|
||||||
|
|
||||||
|
JsonObject rootObj = new JsonObject();
|
||||||
|
rootObj.add("version", versionObj);
|
||||||
|
rootObj.add("players", playersObj);
|
||||||
|
rootObj.add("description", descriptionObj);
|
||||||
|
|
||||||
|
if (faviconBase64 != null && faviconBase64.length > 0) {
|
||||||
|
rootObj.addProperty("favicon",
|
||||||
|
"data:image/png;base64," + new String(faviconBase64)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
netStream.writeString(rootObj.toString());
|
||||||
|
return netStream.toByteArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user