Archived
0

Handshake

This commit is contained in:
2018-04-10 21:35:29 +03:00
parent 704dd19488
commit 4da826248e
7 changed files with 77 additions and 2 deletions

View File

@@ -34,6 +34,11 @@ public class ByteArrayOutputNetStream extends NetStream {
baos.write(buffer, 0, buffer.length);
}
@Override
public void skipBytes(int count) {
throw new UnsupportedOperationException();
}
public byte[] toByteArray() {
return baos.toByteArray();
}

View File

@@ -81,5 +81,5 @@ public abstract class NetStream {
public abstract void writeByte(int value);
public abstract void writeBytes(byte[] buffer);
public abstract void skipBytes(int count);
}

View File

@@ -53,6 +53,11 @@ public class ByteArrayOutputNetStream extends NetStream {
baos.write(buffer, 0, buffer.length);
}
@Override
public void skipBytes(int count) {
throw new UnsupportedOperationException();
}
public byte[] toByteArray() {
return baos.toByteArray();
}

View File

@@ -11,6 +11,7 @@ import lombok.extern.slf4j.Slf4j;
import mc.core.CSPacket;
import mc.core.Config;
import mc.core.Main;
import mc.core.netty.proto_125.packets.HandshakePacket;
import mc.core.netty.proto_125.packets.KickPacket;
import mc.core.netty.proto_125.packets.PingPacket;
@@ -20,6 +21,8 @@ import java.util.Optional;
@Slf4j
public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> {
private static Config config = Main.appContext.getBean("config", Config.class); //FIXME
@Override
protected void channelRead0(ChannelHandlerContext ctx, CSPacket packet) throws Exception {
log.debug("{}: {}", packet.getClass().getSimpleName(), packet.toString());
@@ -38,9 +41,12 @@ public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> {
}
public void onPingPacket(Channel channel, PingPacket packet) {
Config config = Main.appContext.getBean("config", Config.class);
KickPacket pkt = new KickPacket();
pkt.setPongMessage(config.getDescriptionServer(), 0, config.getMaxPlayers());
channel.writeAndFlush(pkt);
}
public void onHandshakePacket(Channel channel, HandshakePacket packet) {
channel.writeAndFlush(packet);
}
}

View File

@@ -8,11 +8,13 @@ import com.google.common.collect.BiMap;
import com.google.common.collect.ImmutableBiMap;
import mc.core.CSPacket;
import mc.core.SCPacket;
import mc.core.netty.proto_125.packets.HandshakePacket;
import mc.core.netty.proto_125.packets.KickPacket;
import mc.core.netty.proto_125.packets.PingPacket;
public class PacketManager {
private static final BiMap<Integer, Class<?>> packetMap = ImmutableBiMap.of(
0x02, HandshakePacket.class,
0xFE, PingPacket.class,
0xFF, KickPacket.class
);

View File

@@ -16,6 +16,14 @@ import java.nio.charset.StandardCharsets;
public class WrapperNetStream extends NetStream {
private final ByteBuf byteBuf;
@Override
public String readString() {
int size = byteBuf.readUnsignedByte() * 2;
byte[] bytes = new byte[size];
byteBuf.readBytes(bytes);
return new String(bytes, StandardCharsets.UTF_16BE);
}
@Override
public byte readByte() {
return byteBuf.readByte();
@@ -40,4 +48,9 @@ public class WrapperNetStream extends NetStream {
public void writeBytes(byte[] buffer) {
byteBuf.writeBytes(buffer);
}
@Override
public void skipBytes(int count) {
byteBuf.skipBytes(count);
}
}

View File

@@ -0,0 +1,44 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2018-04-10
*/
package mc.core.netty.proto_125.packets;
import lombok.Getter;
import lombok.ToString;
import mc.core.CSPacket;
import mc.core.NetStream;
import mc.core.SCPacket;
import mc.core.netty.proto_125.ByteArrayOutputNetStream;
@Getter
@ToString
public class HandshakePacket implements CSPacket, SCPacket {
private String playerName;
private String host;
private int port;
@Override
public void readSelf(NetStream netStream) {
netStream.skipBytes(1);
String[] str = netStream.readString().split(";");
playerName = str[0];
if (str[1].contains(":")) {
str = str[1].split(":");
host = str[0];
port = Integer.parseInt(str[1]);
} else {
host = str[1];
port = 25565;
}
}
@Override
public byte[] toByteArray() {
ByteArrayOutputNetStream netStream = new ByteArrayOutputNetStream();
netStream.writeByte(0);
netStream.writeString("-");
return netStream.toByteArray();
}
}