use of com.github.dirtpowered.dirtmv.network.server.ServerSession in project DirtMultiversion by DirtPowered.
the class ProtocolRelease5To4 method registerTranslators.
@Override
public void registerTranslators() {
// status ping
addTranslator(0x00, ProtocolState.STATUS, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
String json = data.read(Type.V1_7_STRING, 0);
ServerPing serverPing = DirtMultiVersion.GSON.fromJson(json, ServerPing.class);
ServerPing.Version versionObj = new ServerPing.Version();
versionObj.setName("1.7.10");
versionObj.setProtocol(5);
serverPing.setVersion(versionObj);
List<ServerPing.Player> samplePlayers = serverPing.getPlayers().getSample();
if (samplePlayers != null) {
for (ServerPing.Player player : samplePlayers) {
player.setId(dashedFromTrimmedUUID(player.getId()));
}
}
return PacketUtil.createPacket(0x00, new TypeHolder[] { set(Type.V1_7_STRING, serverPing.toString()) });
}
});
// login success
addTranslator(0x02, ProtocolState.LOGIN, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
String trimmedUUID = data.read(Type.V1_7_STRING, 0);
return PacketUtil.createPacket(0x02, new TypeHolder[] { set(Type.V1_7_STRING, dashedFromTrimmedUUID(trimmedUUID)), data.read(1) });
}
});
// spawn player
addTranslator(0x0C, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
String trimmedUUID = data.read(Type.V1_7_STRING, 1);
return PacketUtil.createPacket(0x0C, new TypeHolder[] { data.read(0), set(Type.V1_7_STRING, dashedFromTrimmedUUID(trimmedUUID)), data.read(2), set(Type.VAR_INT, 0), data.read(3), data.read(4), data.read(5), data.read(6), data.read(7), data.read(8), data.read(9) });
}
});
}
use of com.github.dirtpowered.dirtmv.network.server.ServerSession in project DirtMultiversion by DirtPowered.
the class ProtocolRelease73To61 method registerTranslators.
@Override
public void registerTranslators() {
// login
addTranslator(0x01, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
int entityId = data.read(Type.INT, 0);
session.getUserData().setEntityId(entityId);
EntityTracker tracker = new EntityTracker();
tracker.addEntity(entityId, EntityType.HUMAN);
session.getUserData().getProtocolStorage().set(EntityTracker.class, tracker);
// send entity attributes (fixes fast movement)
session.sendPacket(data, PacketDirection.TO_CLIENT, getFrom());
session.sendPacket(getDefaultAttributes(entityId), PacketDirection.TO_CLIENT, getFrom());
return cancel();
}
});
// respawn
addTranslator(0x09, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
int entityId = session.getUserData().getEntityId();
session.sendPacket(data, PacketDirection.TO_CLIENT, getFrom());
session.sendPacket(getDefaultAttributes(entityId), PacketDirection.TO_CLIENT, getFrom());
return cancel();
}
});
// handshake
addTranslator(0x02, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x02, new TypeHolder[] { set(Type.BYTE, (byte) 61), data.read(1), data.read(2), data.read(3) });
}
});
// kick disconnect
addTranslator(0xFF, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
if (session.getUserData().getPreNettyProtocolState() != PreNettyProtocolState.STATUS)
return data;
String reason = data.read(Type.STRING, 0);
ServerMotd pingMessage = ServerMotd.deserialize(reason);
pingMessage.setVersionName("1.6.1");
pingMessage.setProtocol(session.getUserData().getClientVersion().getRegistryId());
return PacketUtil.createPacket(0xFF, new TypeHolder[] { set(Type.STRING, ServerMotd.serialize(pingMessage)) });
}
});
// chat
addTranslator(0x03, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
String message = data.read(Type.STRING, 0);
return PacketUtil.createPacket(0x03, new TypeHolder[] { set(Type.STRING, ChatUtils.legacyToJsonString(message)) });
}
});
// update health
addTranslator(0x08, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x08, new TypeHolder[] { set(Type.FLOAT, data.read(Type.SHORT, 0).floatValue()), data.read(1), data.read(2) });
}
});
// mob spawn
addTranslator(0x18, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
EntityType entityType = EntityType.fromEntityTypeId(data.read(Type.BYTE, 1));
int entityId = data.read(Type.INT, 0);
EntityTracker tracker = session.getUserData().getProtocolStorage().get(EntityTracker.class);
tracker.addEntity(entityId, entityType);
WatchableObject[] oldMeta = data.read(Type.V1_4R_METADATA, 11);
WatchableObject[] newMeta = metadataTransformer.transformMetadata(entityType, oldMeta);
return PacketUtil.createPacket(0x18, new TypeHolder[] { data.read(0), data.read(1), data.read(2), data.read(3), data.read(4), data.read(5), data.read(6), data.read(7), data.read(8), data.read(9), data.read(10), set(Type.V1_4R_METADATA, newMeta) });
}
});
// entity destroy
addTranslator(0x1D, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
int[] entities = data.read(Type.BYTE_INT_ARRAY, 0);
EntityTracker tracker = session.getUserData().getProtocolStorage().get(EntityTracker.class);
for (int entityId : entities) {
tracker.removeEntity(entityId);
}
return data;
}
});
// entity metadata
addTranslator(0x28, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
int entityId = data.read(Type.INT, 0);
EntityTracker tracker = session.getUserData().getProtocolStorage().get(EntityTracker.class);
EntityType entityType = tracker.getEntityById(entityId);
if (entityType == null) {
Logger.warn("[{}] skipping translating metadata for {}. Entity is not tracked", session.getLogTag(), entityId);
return cancel();
}
WatchableObject[] oldMeta = data.read(Type.V1_4R_METADATA, 1);
WatchableObject[] newMeta = metadataTransformer.transformMetadata(entityType, oldMeta);
return PacketUtil.createPacket(0x28, new TypeHolder[] { data.read(0), set(Type.V1_4R_METADATA, newMeta) });
}
});
// name entity spawn
addTranslator(0x14, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
int entityId = data.read(Type.INT, 0);
EntityTracker tracker = session.getUserData().getProtocolStorage().get(EntityTracker.class);
tracker.addEntity(entityId, EntityType.HUMAN);
WatchableObject[] oldMeta = data.read(Type.V1_4R_METADATA, 8);
WatchableObject[] newMeta = metadataTransformer.transformMetadata(EntityType.HUMAN, oldMeta);
return PacketUtil.createPacket(0x14, new TypeHolder[] { data.read(0), data.read(1), data.read(2), data.read(3), data.read(4), data.read(5), data.read(6), data.read(7), set(Type.V1_4R_METADATA, newMeta) });
}
});
// vehicle spawn
addTranslator(0x17, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
int entityId = data.read(Type.INT, 0);
byte vehicleEntity = data.read(Type.BYTE, 1);
EntityTracker tracker = session.getUserData().getProtocolStorage().get(EntityTracker.class);
switch(vehicleEntity) {
case 0x02:
/* item */
tracker.addEntity(entityId, EntityType.ITEM);
break;
case 0x46:
/* sand, gravel */
tracker.addEntity(entityId, EntityType.FALLING_OBJECT);
break;
}
return data;
}
});
// statistics
addTranslator(0xC8, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0xC8, new TypeHolder[] { data.read(0), set(Type.INT, data.read(Type.BYTE, 1).intValue()) });
}
});
// level sound
addTranslator(0x3E, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
String soundName = data.read(Type.STRING, 0);
String newSoundName = soundRemapper.getNewSoundName(soundName);
if (newSoundName.isEmpty())
return cancel();
return PacketUtil.createPacket(0x3E, new TypeHolder[] { set(Type.STRING, newSoundName), data.read(1), data.read(2), data.read(3), data.read(4), data.read(5) });
}
});
// player abilities
addTranslator(0xCA, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0xCA, new TypeHolder[] { data.read(0), set(Type.BYTE, (byte) (data.read(Type.FLOAT, 1) * 255F)), set(Type.BYTE, (byte) (data.read(Type.FLOAT, 2) * 255F)) });
}
});
// player abilities
addTranslator(0xCA, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0xCA, new TypeHolder[] { data.read(0), set(Type.FLOAT, (data.read(Type.BYTE, 1) / 255F)), set(Type.FLOAT, (data.read(Type.BYTE, 2) / 255F)) });
}
});
// entity action
addTranslator(0x13, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
// client -> server
return PacketUtil.createPacket(0x13, new TypeHolder[] { data.read(0), data.read(1) });
}
});
// entity attach
addTranslator(0x27, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
VehicleTracker vehicleTracker = session.getUserData().getProtocolStorage().get(VehicleTracker.class);
int vehicleId = data.read(Type.INT, 1);
if (vehicleId != -1) {
vehicleTracker.setVehicleId(vehicleId);
}
return PacketUtil.createPacket(0x27, new TypeHolder[] { data.read(0), data.read(1), // leash
set(Type.BOOLEAN, false) });
}
});
// creative item get
addTranslator(0x6B, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
ItemStack item = data.read(Type.V1_3R_ITEM, 1);
if (item != null && !creativeTab.exists(item.getItemId())) {
// replace all unknown items to stone
item.setItemId(1);
item.setData(0);
}
return PacketUtil.createPacket(0x6B, new TypeHolder[] { data.read(0), set(Type.V1_3R_ITEM, item) });
}
});
// steer vehicle
addTranslator(0x1B, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
VehicleTracker vehicleTracker = session.getUserData().getProtocolStorage().get(VehicleTracker.class);
boolean dismount = data.read(Type.BOOLEAN, 3);
if (dismount && vehicleTracker.getVehicleId() != -999) {
int cachedId = vehicleTracker.getVehicleId();
vehicleTracker.setVehicleId(-999);
return PacketUtil.createPacket(0x07, new TypeHolder[] { set(Type.INT, session.getUserData().getEntityId()), set(Type.INT, cachedId), set(Type.BYTE, (byte) 0) });
}
// packet doesn't exist in 1.5
return cancel();
}
});
}
use of com.github.dirtpowered.dirtmv.network.server.ServerSession in project DirtMultiversion by DirtPowered.
the class ProtocolRelease74To73 method registerTranslators.
@Override
public void registerTranslators() {
// handshake
addTranslator(0x02, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x02, new TypeHolder[] { set(Type.BYTE, (byte) 73), data.read(1), data.read(2), data.read(3) });
}
});
// entity attributes/properties
addTranslator(0x2C, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
V1_6_1EntityAttributes attrObj = data.read(Type.V1_6_1_ENTITY_ATTRIBUTES, 1);
List<EntityAttribute> entityAttributes = new ArrayList<>();
for (Map.Entry<String, Double> entry : attrObj.getAttributes().entrySet()) {
entityAttributes.add(new EntityAttribute(entry.getKey(), entry.getValue()));
}
V1_6_2EntityAttributes attrObjModern = new V1_6_2EntityAttributes(entityAttributes);
return PacketUtil.createPacket(0x2C, new TypeHolder[] { data.read(0), set(Type.V1_6_2_ENTITY_ATTRIBUTES, attrObjModern) });
}
});
// kick disconnect
addTranslator(0xFF, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
if (session.getUserData().getPreNettyProtocolState() != PreNettyProtocolState.STATUS)
return data;
String reason = data.read(Type.STRING, 0);
ServerMotd pingMessage = ServerMotd.deserialize(reason);
pingMessage.setVersionName("1.6.2");
pingMessage.setProtocol(session.getUserData().getClientVersion().getRegistryId());
return PacketUtil.createPacket(0xFF, new TypeHolder[] { set(Type.STRING, ServerMotd.serialize(pingMessage)) });
}
});
// creative item get
addTranslator(0x6B, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
ItemStack item = data.read(Type.V1_3R_ITEM, 1);
boolean notNull = item != null;
if (notNull && !creativeTab.exists(item.getItemId())) {
// replace all unknown items to stone
item.setItemId(1);
item.setData(0);
}
return PacketUtil.createPacket(0x6B, new TypeHolder[] { data.read(0), set(Type.V1_3R_ITEM, item) });
}
});
// block place
addTranslator(0x0F, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
int x = data.read(Type.INT, 0);
int y = data.read(Type.UNSIGNED_BYTE, 1);
int z = data.read(Type.INT, 2);
ItemStack item = data.read(Type.V1_3R_ITEM, 4);
if (item == null)
return data;
int itemId = item.getItemId();
int face = data.read(Type.BYTE, 3);
if (face == 1) {
++y;
} else if (face == 2) {
--z;
} else if (face == 3) {
++z;
} else if (face == 4) {
--x;
} else if (face == 5) {
++x;
}
if (itemId == 323 && data.read(Type.SHORT, 5) > 0) {
PacketData signEditor = PacketUtil.createPacket(0x85, new TypeHolder[] { // ignored
set(Type.BYTE, (byte) 0), // x
set(Type.INT, x), // y
set(Type.INT, y), // z
set(Type.INT, z) });
session.sendPacket(signEditor, PacketDirection.TO_CLIENT, getFrom());
}
return data;
}
});
}
use of com.github.dirtpowered.dirtmv.network.server.ServerSession in project DirtMultiversion by DirtPowered.
the class Client method createClient.
public void createClient(UUID key, Callback callback) {
EventLoopGroup loopGroup = serverSession.getMain().getLoopGroup();
Bootstrap clientBootstrap = new Bootstrap();
clientBootstrap.group(loopGroup);
clientBootstrap.channel(NioSocketChannel.class);
clientBootstrap.option(ChannelOption.SO_KEEPALIVE, true);
clientBootstrap.option(ChannelOption.TCP_NODELAY, true);
Configuration c = serverSession.getMain().getConfiguration();
clientBootstrap.remoteAddress(new InetSocketAddress(c.getRemoteServerAddress(), c.getRemoteServerPort()));
clientBootstrap.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) {
ch.pipeline().addLast(ChannelConstants.DEFAULT_PIPELINE, new PipelineFactory(serverSession.getMain(), serverSession.getUserData(), PacketDirection.TO_CLIENT));
ch.pipeline().addLast(ChannelConstants.CLIENT_HANDLER, new ClientSession(serverSession.getMain(), key, serverSession, ch, callback));
}
});
clientBootstrap.connect().addListener((ChannelFutureListener) channelFuture -> {
if (!channelFuture.isSuccess()) {
serverSession.disconnect(ChatUtils.LEGACY_COLOR_CHAR + "cUnable to connect to remote server");
channelFuture.channel().close();
}
});
}
use of com.github.dirtpowered.dirtmv.network.server.ServerSession in project DirtMultiversion by DirtPowered.
the class ProtocolPassthroughEncrypted method registerTranslators.
@Override
public void registerTranslators() {
// server auth data
addTranslator(0xFD, PacketDirection.TO_CLIENT, new PacketTranslator() {
@SneakyThrows
@Override
public PacketData translate(ServerSession session, PacketData data) {
PacketData encryptRequest = EncryptionUtils.createEncryptionRequest(session);
SecretKey secretKey = EncryptionUtils.getSecretKey();
session.getUserData().setSecretKey(secretKey);
byte[] publicKeyBytes = (byte[]) data.read(1).getObject();
X509EncodedKeySpec spec = new X509EncodedKeySpec(publicKeyBytes);
PublicKey publicKey;
try {
KeyFactory keyFactory;
keyFactory = KeyFactory.getInstance("RSA");
publicKey = keyFactory.generatePublic(spec);
// fake proxy client response
byte[] token = (byte[]) data.read(2).getObject();
byte[] sharedKey = EncryptionUtils.getSharedKey(secretKey, publicKey);
byte[] encryptedData = EncryptionUtils.encrypt(publicKey, token);
PacketData response = PacketUtil.createPacket(0xFC, new TypeHolder[] { set(Type.SHORT_BYTE_ARRAY, sharedKey), set(Type.SHORT_BYTE_ARRAY, encryptedData) });
session.sendPacket(response, PacketDirection.TO_SERVER, getFrom());
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
e.printStackTrace();
}
return encryptRequest;
}
});
// client shared key
addTranslator(0xFC, PacketDirection.TO_SERVER, new PacketTranslator() {
@SneakyThrows
@Override
public PacketData translate(ServerSession session, PacketData data) {
SecretKey shared = EncryptionUtils.getSecret(data, session.getUserData().getProxyRequest());
// server -> client
EncryptionUtils.sendEmptyEncryptionResponse(session, getFrom());
// enable encryption
EncryptionUtils.setEncryption(session.getChannel(), shared);
// cancel packet
return cancel();
}
});
// client shared key
addTranslator(0xFC, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
// enable client connection encryption
EncryptionUtils.setEncryption(session.getClientSession().getChannel(), session.getUserData().getSecretKey());
// cancel packet
return cancel();
}
});
}
Aggregations