Ping server
This commit is contained in:
11
proto_1.12.2/build.gradle
Normal file
11
proto_1.12.2/build.gradle
Normal file
@@ -0,0 +1,11 @@
|
||||
group 'mc'
|
||||
version '1.0-SNAPSHOT'
|
||||
|
||||
dependencies {
|
||||
/* Core */
|
||||
compile_excludeCopy project(':core')
|
||||
|
||||
/* Components */
|
||||
compile (group: 'com.google.guava', name: 'guava', version: '24.1-jre')
|
||||
compile (group: 'com.google.code.gson', name: 'gson', version: '2.8.2')
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-06-10
|
||||
*/
|
||||
package mc.core.network.proto_1_12_2;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
public class ByteArrayOutputNetStream extends NetStream_p340 {
|
||||
private ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
|
||||
@Override
|
||||
public boolean readBoolean() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte readByte() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readBytes(byte[] buffer) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int readUnsignedByte() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int readUnsignedShort() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public short readShort() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int readInt() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long readLong() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public float readFloat() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public double readDouble() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeBoolean(boolean value) {
|
||||
baos.write(value ? 1 : 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeByte(int value) {
|
||||
baos.write(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeBytes(byte[] buffer) {
|
||||
baos.write(buffer, 0, buffer.length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeShort(int value) {
|
||||
baos.write((byte) value >>> 8);
|
||||
baos.write((byte) value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeInt(int value) {
|
||||
baos.write((byte)((int)(value >>> 24)));
|
||||
baos.write((byte)((int)(value >>> 16)));
|
||||
baos.write((byte)((int)(value >>> 8)));
|
||||
baos.write((byte)((int)(value)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeLong(long value) {
|
||||
baos.write((byte)((int)(value >>> 56)));
|
||||
baos.write((byte)((int)(value >>> 48)));
|
||||
baos.write((byte)((int)(value >>> 40)));
|
||||
baos.write((byte)((int)(value >>> 32)));
|
||||
baos.write((byte)((int)(value >>> 24)));
|
||||
baos.write((byte)((int)(value >>> 16)));
|
||||
baos.write((byte)((int)(value >>> 8)));
|
||||
baos.write((byte)((int)(value)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeFloat(float value) {
|
||||
writeInt(Float.floatToIntBits(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeDouble(double value) {
|
||||
writeLong(Double.doubleToLongBits(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void skipBytes(int count) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public byte[] toByteArray() {
|
||||
return baos.toByteArray();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-06-10
|
||||
*/
|
||||
package mc.core.network.proto_1_12_2;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.core.network.NetStream;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
@Slf4j
|
||||
public abstract class NetStream_p340 extends NetStream {
|
||||
@Override
|
||||
public int readVarInt() {
|
||||
int numRead = 0;
|
||||
int result = 0;
|
||||
byte read;
|
||||
do {
|
||||
read = readByte();
|
||||
int value = (read & 0b01111111);
|
||||
result |= (value << (7 * numRead));
|
||||
|
||||
numRead++;
|
||||
if (numRead > 5) {
|
||||
log.warn("VarInt is too big");
|
||||
break;
|
||||
}
|
||||
} while ((read & 0b10000000) != 0);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeVarInt(int value) {
|
||||
do {
|
||||
byte temp = (byte)(value & 0b01111111);
|
||||
// Note: >>> means that the sign bit is shifted with the rest of the number rather than being left alone
|
||||
value >>>= 7;
|
||||
if (value != 0) {
|
||||
temp |= 0b10000000;
|
||||
}
|
||||
writeByte(temp);
|
||||
} while (value != 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String readString() {
|
||||
int size = readVarInt();
|
||||
if (size == 0) {
|
||||
log.warn("String zero length??");
|
||||
return "";
|
||||
}
|
||||
|
||||
byte[] bytes = new byte[size];
|
||||
readBytes(bytes);
|
||||
return new String(bytes, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeString(String value) {
|
||||
if (value.length() > Short.MAX_VALUE) {
|
||||
log.warn("String \"{}\" too long!", value);
|
||||
byte[] buf = value.substring(0, Short.MAX_VALUE).getBytes(StandardCharsets.UTF_8);
|
||||
writeVarInt(Short.MAX_VALUE);
|
||||
writeBytes(buf);
|
||||
} else {
|
||||
byte[] buf = value.getBytes(StandardCharsets.UTF_8);
|
||||
writeVarInt(value.length());
|
||||
writeBytes(buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-06-08
|
||||
*/
|
||||
package mc.core.network.proto_1_12_2;
|
||||
|
||||
import com.google.common.collect.BiMap;
|
||||
import com.google.common.collect.ImmutableBiMap;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.core.network.CSPacket;
|
||||
import mc.core.network.SCPacket;
|
||||
import mc.core.network.proto_1_12_2.packets.HandshakePacket;
|
||||
import mc.core.network.proto_1_12_2.packets.PingPacket;
|
||||
import mc.core.network.proto_1_12_2.packets.StatusRequestPacket;
|
||||
import mc.core.network.proto_1_12_2.packets.StatusResponsePacket;
|
||||
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public enum State {
|
||||
UNKNOWN(-1, null, null),
|
||||
HANDSHAKE(0,
|
||||
ImmutableBiMap.<Integer, Class<? extends CSPacket>>builder()
|
||||
.put(0, HandshakePacket.class)
|
||||
.build(),
|
||||
null
|
||||
),
|
||||
STATUS(1,
|
||||
ImmutableBiMap.<Integer, Class<? extends CSPacket>>builder()
|
||||
.put(0, StatusRequestPacket.class)
|
||||
.put(1, PingPacket.class)
|
||||
.build(),
|
||||
ImmutableBiMap.<Class<? extends SCPacket>, Integer>builder()
|
||||
.put(StatusResponsePacket.class, 0)
|
||||
.put(PingPacket.class, 1)
|
||||
.build()
|
||||
);
|
||||
|
||||
public static State valueOf(int id) {
|
||||
if (id == 0) return HANDSHAKE;
|
||||
else if (id == 1) return STATUS;
|
||||
else {
|
||||
log.warn("Unknown state: {}", id);
|
||||
return UNKNOWN;
|
||||
}
|
||||
}
|
||||
|
||||
@Getter
|
||||
private final int id;
|
||||
private final BiMap<Integer, Class<? extends CSPacket>> clientSidePacketsMap;
|
||||
private final BiMap<Class<? extends SCPacket>, Integer> serverSidePacketsMap;
|
||||
|
||||
public Class<? extends CSPacket> getClientSidePacket(int id) {
|
||||
return clientSidePacketsMap.get(id);
|
||||
}
|
||||
|
||||
public Integer getServerSidePacket(Class<? extends SCPacket> clazz) {
|
||||
return serverSidePacketsMap.get(clazz);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-06-10
|
||||
*/
|
||||
package mc.core.network.proto_1_12_2.packets;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.ToString;
|
||||
import mc.core.network.CSPacket;
|
||||
import mc.core.network.NetStream;
|
||||
import mc.core.network.proto_1_12_2.State;
|
||||
|
||||
@NoArgsConstructor
|
||||
@Getter
|
||||
@ToString
|
||||
public class HandshakePacket implements CSPacket {
|
||||
private int protocolVersion;
|
||||
private String address;
|
||||
private int serverPort;
|
||||
private State nextState;
|
||||
|
||||
@Override
|
||||
public void readSelf(NetStream netStream) {
|
||||
this.protocolVersion = netStream.readVarInt();
|
||||
this.address = netStream.readString();
|
||||
this.serverPort = netStream.readUnsignedShort();
|
||||
this.nextState = State.valueOf(netStream.readVarInt());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-06-10
|
||||
*/
|
||||
package mc.core.network.proto_1_12_2.packets;
|
||||
|
||||
import lombok.ToString;
|
||||
import mc.core.network.CSPacket;
|
||||
import mc.core.network.NetStream;
|
||||
import mc.core.network.SCPacket;
|
||||
import mc.core.network.proto_1_12_2.ByteArrayOutputNetStream;
|
||||
|
||||
@ToString
|
||||
public class PingPacket implements CSPacket, SCPacket {
|
||||
private long payload;
|
||||
|
||||
@Override
|
||||
public void readSelf(NetStream netStream) {
|
||||
this.payload = netStream.readLong();
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] toByteArray() {
|
||||
ByteArrayOutputNetStream netStream = new ByteArrayOutputNetStream();
|
||||
netStream.writeLong(payload);
|
||||
return netStream.toByteArray();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-06-10
|
||||
*/
|
||||
package mc.core.network.proto_1_12_2.packets;
|
||||
|
||||
import lombok.ToString;
|
||||
import mc.core.network.CSPacket;
|
||||
import mc.core.network.NetStream;
|
||||
|
||||
@ToString
|
||||
public class StatusRequestPacket implements CSPacket {
|
||||
@Override
|
||||
public void readSelf(NetStream netStream) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-06-10
|
||||
*/
|
||||
package mc.core.network.proto_1_12_2.packets;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import mc.core.network.SCPacket;
|
||||
import mc.core.network.proto_1_12_2.ByteArrayOutputNetStream;
|
||||
|
||||
@Setter
|
||||
@ToString
|
||||
public class StatusResponsePacket implements SCPacket {
|
||||
private static final JsonObject versionObj;
|
||||
|
||||
static {
|
||||
versionObj = new JsonObject();
|
||||
versionObj.addProperty("name", "1.12.2");
|
||||
versionObj.addProperty("protocol", 340);
|
||||
}
|
||||
|
||||
private int maxOnline;
|
||||
private int online;
|
||||
private String description;
|
||||
private byte[] faviconBase64;
|
||||
|
||||
@Override
|
||||
public byte[] toByteArray() {
|
||||
ByteArrayOutputNetStream netStream = new ByteArrayOutputNetStream();
|
||||
|
||||
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