Geyser/connector/src/main/java/org/geysermc/connector/network/session/GeyserSession.java

406 lines
17 KiB
Java
Raw Normal View History

/*
* Copyright (c) 2019-2020 GeyserMC. http://geysermc.org
*
2019-07-11 21:30:35 +00:00
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
2019-07-11 21:30:35 +00:00
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.connector.network.session;
import com.github.steveice10.mc.auth.data.GameProfile;
2019-07-24 06:29:54 +00:00
import com.github.steveice10.mc.auth.exception.request.RequestException;
import com.github.steveice10.mc.protocol.MinecraftProtocol;
2019-10-27 09:56:47 +00:00
import com.github.steveice10.mc.protocol.data.game.entity.player.GameMode;
import com.github.steveice10.mc.protocol.packet.ingame.server.ServerRespawnPacket;
2019-11-30 12:34:45 +00:00
import com.github.steveice10.mc.protocol.packet.handshake.client.HandshakePacket;
import com.github.steveice10.packetlib.Client;
2019-11-30 12:34:45 +00:00
import com.github.steveice10.packetlib.event.session.*;
2019-08-06 02:09:45 +00:00
import com.github.steveice10.packetlib.packet.Packet;
import com.github.steveice10.packetlib.tcp.TcpSessionFactory;
import com.nukkitx.math.GenericMath;
import com.nukkitx.math.TrigMath;
import com.nukkitx.math.vector.Vector2f;
import com.nukkitx.math.vector.Vector3f;
import com.nukkitx.math.vector.Vector3i;
2019-11-14 02:26:45 +00:00
import com.nukkitx.nbt.tag.CompoundTag;
import com.nukkitx.protocol.bedrock.BedrockServerSession;
2019-07-24 06:29:54 +00:00
import com.nukkitx.protocol.bedrock.data.GamePublishSetting;
import com.nukkitx.protocol.bedrock.data.GameRuleData;
import com.nukkitx.protocol.bedrock.data.PlayerPermission;
2019-12-04 18:13:49 +00:00
import com.nukkitx.protocol.bedrock.packet.*;
import lombok.Getter;
2019-08-06 02:09:45 +00:00
import lombok.Setter;
2020-01-04 05:58:58 +00:00
import org.geysermc.common.AuthType;
import org.geysermc.common.window.FormWindow;
import org.geysermc.connector.GeyserConnector;
import org.geysermc.connector.command.CommandSender;
2019-08-06 02:09:45 +00:00
import org.geysermc.connector.entity.PlayerEntity;
import org.geysermc.connector.inventory.PlayerInventory;
import org.geysermc.connector.network.remote.RemoteServer;
import org.geysermc.connector.network.session.auth.AuthData;
2019-11-30 12:34:45 +00:00
import org.geysermc.connector.network.session.auth.BedrockClientData;
2019-09-13 10:49:18 +00:00
import org.geysermc.connector.network.session.cache.*;
2019-07-11 22:39:28 +00:00
import org.geysermc.connector.network.translators.Registry;
import org.geysermc.connector.network.translators.block.BlockTranslator;
import org.geysermc.connector.utils.ChunkUtils;
2019-07-24 06:29:54 +00:00
import org.geysermc.connector.utils.Toolbox;
2019-11-30 12:34:45 +00:00
import org.geysermc.floodgate.util.BedrockData;
import org.geysermc.floodgate.util.EncryptionUtil;
2019-11-30 12:34:45 +00:00
import java.io.IOException;
import java.net.InetSocketAddress;
2019-11-30 12:34:45 +00:00
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
2019-08-06 02:09:45 +00:00
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
2019-08-06 02:09:45 +00:00
@Getter
public class GeyserSession implements CommandSender {
private final GeyserConnector connector;
private final UpstreamSession upstream;
2019-07-23 23:16:25 +00:00
private RemoteServer remoteServer;
private Client downstream;
2020-01-04 05:58:58 +00:00
@Setter private AuthData authData;
2019-11-30 12:34:45 +00:00
@Setter private BedrockClientData clientData;
2019-07-23 23:16:25 +00:00
2019-08-06 02:09:45 +00:00
private PlayerEntity playerEntity;
private PlayerInventory inventory;
private ChunkCache chunkCache;
2019-08-06 02:09:45 +00:00
private EntityCache entityCache;
private InventoryCache inventoryCache;
2019-07-30 02:57:43 +00:00
private ScoreboardCache scoreboardCache;
private WindowCache windowCache;
2019-07-30 02:57:43 +00:00
2019-08-06 02:09:45 +00:00
private DataCache<Packet> javaPacketCache;
private int renderDistance;
2019-09-13 10:49:18 +00:00
2019-07-24 06:29:54 +00:00
private boolean loggedIn;
private boolean loggingIn;
2019-07-24 06:29:54 +00:00
2019-08-06 02:09:45 +00:00
@Setter
private boolean spawned;
private boolean closed;
@Setter
2019-10-27 09:56:47 +00:00
private GameMode gameMode = GameMode.SURVIVAL;
private final AtomicInteger pendingDimSwitches = new AtomicInteger(0);
@Setter
private boolean sprinting;
@Setter
private boolean jumping;
@Setter
private boolean switchingDimension = false;
private boolean manyDimPackets = false;
private ServerRespawnPacket lastDimPacket = null;
public GeyserSession(GeyserConnector connector, BedrockServerSession bedrockServerSession) {
this.connector = connector;
this.upstream = new UpstreamSession(bedrockServerSession);
2019-07-23 23:16:25 +00:00
this.chunkCache = new ChunkCache(this);
2019-08-06 02:09:45 +00:00
this.entityCache = new EntityCache(this);
this.inventoryCache = new InventoryCache(this);
2019-07-30 02:57:43 +00:00
this.scoreboardCache = new ScoreboardCache(this);
this.windowCache = new WindowCache(this);
2019-07-30 02:57:43 +00:00
this.playerEntity = new PlayerEntity(new GameProfile(UUID.randomUUID(), "unknown"), 1, 1, Vector3f.ZERO, Vector3f.ZERO, Vector3f.ZERO);
2019-08-06 02:09:45 +00:00
this.inventory = new PlayerInventory();
this.javaPacketCache = new DataCache<>();
2019-08-06 02:09:45 +00:00
this.spawned = false;
2019-07-24 06:29:54 +00:00
this.loggedIn = false;
2019-08-06 02:09:45 +00:00
this.inventoryCache.getInventories().put(0, inventory);
}
2019-07-23 23:16:25 +00:00
public void connect(RemoteServer remoteServer) {
2019-07-24 06:29:54 +00:00
startGame();
this.remoteServer = remoteServer;
2019-11-30 18:10:29 +00:00
if (connector.getAuthType() != AuthType.ONLINE) {
connector.getLogger().info(
"Attempting to login using " + connector.getAuthType().name().toLowerCase() + " mode... " +
(connector.getAuthType() == AuthType.OFFLINE ?
"authentication is disabled." : "authentication will be encrypted"
)
);
authenticate(authData.getName());
2019-07-24 06:29:54 +00:00
}
2019-11-06 00:55:59 +00:00
2019-12-29 03:17:00 +00:00
ChunkUtils.sendEmptyChunks(this, playerEntity.getPosition().toInt(), 0, false);
2019-11-06 00:55:59 +00:00
BiomeDefinitionListPacket biomeDefinitionListPacket = new BiomeDefinitionListPacket();
biomeDefinitionListPacket.setTag(Toolbox.BIOMES);
upstream.sendPacket(biomeDefinitionListPacket);
2019-11-16 04:21:26 +00:00
2019-11-14 02:26:45 +00:00
AvailableEntityIdentifiersPacket entityPacket = new AvailableEntityIdentifiersPacket();
entityPacket.setTag(CompoundTag.EMPTY);
upstream.sendPacket(entityPacket);
2019-11-10 22:53:01 +00:00
2019-11-06 00:55:59 +00:00
PlayStatusPacket playStatusPacket = new PlayStatusPacket();
playStatusPacket.setStatus(PlayStatusPacket.Status.PLAYER_SPAWN);
upstream.sendPacket(playStatusPacket);
2019-07-24 06:29:54 +00:00
}
2019-07-24 06:29:54 +00:00
public void authenticate(String username) {
authenticate(username, "");
}
public void authenticate(String username, String password) {
if (loggedIn) {
connector.getLogger().severe(username + " is already logged in!");
return;
}
loggedIn = true;
// new thread so clients don't timeout
new Thread(() -> {
try {
MinecraftProtocol protocol;
if (password != null && !password.isEmpty()) {
protocol = new MinecraftProtocol(username, password);
} else {
protocol = new MinecraftProtocol(username);
2019-07-24 06:29:54 +00:00
}
2019-11-30 12:34:45 +00:00
boolean floodgate = connector.getAuthType() == AuthType.FLOODGATE;
final PublicKey publicKey;
if (floodgate) {
PublicKey key = null;
try {
key = EncryptionUtil.getKeyFromFile(
connector.getConfig().getFloodgateKeyFile(),
2019-11-30 12:34:45 +00:00
PublicKey.class
);
} catch (IOException | InvalidKeySpecException | NoSuchAlgorithmException e) {
connector.getLogger().error("Error while reading Floodgate key file", e);
}
publicKey = key;
} else publicKey = null;
2019-11-30 17:38:09 +00:00
if (publicKey != null) {
connector.getLogger().info("Loaded Floodgate key!");
}
downstream = new Client(remoteServer.getAddress(), remoteServer.getPort(), protocol, new TcpSessionFactory());
downstream.getSession().addListener(new SessionAdapter() {
2019-11-30 12:34:45 +00:00
@Override
public void packetSending(PacketSendingEvent event) {
//todo move this somewhere else
if (event.getPacket() instanceof HandshakePacket && floodgate) {
String encrypted = "";
try {
2019-11-30 14:32:13 +00:00
encrypted = EncryptionUtil.encryptBedrockData(publicKey, new BedrockData(
2019-11-30 12:34:45 +00:00
clientData.getGameVersion(),
2020-01-04 05:58:58 +00:00
authData.getName(),
authData.getXboxUUID(),
2019-11-30 12:34:45 +00:00
clientData.getDeviceOS().ordinal(),
clientData.getLanguageCode(),
clientData.getCurrentInputMode().ordinal(),
upstream.getSession().getAddress().getAddress().getHostAddress()
2019-11-30 12:34:45 +00:00
));
} catch (Exception e) {
connector.getLogger().error("Failed to encrypt message", e);
}
HandshakePacket handshakePacket = event.getPacket();
event.setPacket(new HandshakePacket(
handshakePacket.getProtocolVersion(),
handshakePacket.getHostname() + '\0' + BedrockData.FLOODGATE_IDENTIFIER + '\0' + encrypted,
2019-11-30 12:34:45 +00:00
handshakePacket.getPort(),
handshakePacket.getIntent()
));
}
}
@Override
public void connected(ConnectedEvent event) {
loggingIn = false;
loggedIn = true;
connector.getLogger().info(authData.getName() + " (logged in as: " + protocol.getProfile().getName() + ")" + " has connected to remote java server on address " + remoteServer.getAddress());
playerEntity.setUuid(protocol.getProfile().getId());
playerEntity.setUsername(protocol.getProfile().getName());
}
@Override
public void disconnected(DisconnectedEvent event) {
loggingIn = false;
loggedIn = false;
connector.getLogger().info(authData.getName() + " has disconnected from remote java server on address " + remoteServer.getAddress() + " because of " + event.getReason());
upstream.disconnect(event.getReason());
}
@Override
public void packetReceived(PacketReceivedEvent event) {
if (!closed) {
//handle consecutive respawn packets
if (event.getPacket().getClass().equals(ServerRespawnPacket.class)) {
manyDimPackets = lastDimPacket != null;
lastDimPacket = event.getPacket();
return;
} else if (lastDimPacket != null) {
Registry.JAVA.translate(lastDimPacket.getClass(), lastDimPacket, GeyserSession.this);
lastDimPacket = null;
}
Registry.JAVA.translate(event.getPacket().getClass(), event.getPacket(), GeyserSession.this);
}
}
});
downstream.getSession().connect();
connector.addPlayer(this);
} catch (RequestException ex) {
ex.printStackTrace();
}
}).start();
}
public void disconnect(String reason) {
if (!closed) {
2019-07-24 06:29:54 +00:00
loggedIn = false;
2019-08-02 20:54:40 +00:00
if (downstream != null && downstream.getSession() != null) {
downstream.getSession().disconnect(reason);
}
if (upstream != null && !upstream.isClosed()) {
2019-08-02 20:54:40 +00:00
upstream.disconnect(reason);
}
}
2019-08-06 02:09:45 +00:00
closed = true;
}
public void close() {
disconnect("Server closed.");
}
2019-07-23 23:16:25 +00:00
public void setAuthenticationData(AuthData authData) {
this.authData = authData;
}
@Override
public String getName() {
return authData.getName();
}
@Override
public void sendMessage(String message) {
TextPacket textPacket = new TextPacket();
textPacket.setPlatformChatId("");
textPacket.setSourceName("");
textPacket.setXuid("");
textPacket.setType(TextPacket.Type.CHAT);
textPacket.setNeedsTranslation(false);
textPacket.setMessage(message);
upstream.sendPacket(textPacket);
}
@Override
public boolean isConsole() {
return false;
}
2019-07-23 23:16:25 +00:00
public void sendForm(FormWindow window, int id) {
windowCache.showWindow(window, id);
}
2020-02-06 04:32:33 +00:00
public void setRenderDistance(int renderDistance) {
renderDistance = GenericMath.ceil(++renderDistance * TrigMath.SQRT_OF_TWO); //square to circle
2020-02-06 04:32:33 +00:00
if (renderDistance > 32) renderDistance = 32; // <3 u ViaVersion but I don't like crashing clients x)
this.renderDistance = renderDistance;
ChunkRadiusUpdatedPacket chunkRadiusUpdatedPacket = new ChunkRadiusUpdatedPacket();
chunkRadiusUpdatedPacket.setRadius(renderDistance);
upstream.sendPacket(chunkRadiusUpdatedPacket);
}
public InetSocketAddress getSocketAddress() {
return this.upstream.getAddress();
}
2019-07-23 23:16:25 +00:00
public void sendForm(FormWindow window) {
windowCache.showWindow(window);
}
2019-07-24 06:29:54 +00:00
private void startGame() {
StartGamePacket startGamePacket = new StartGamePacket();
2019-08-06 02:09:45 +00:00
startGamePacket.setUniqueEntityId(playerEntity.getGeyserId());
startGamePacket.setRuntimeEntityId(playerEntity.getGeyserId());
2019-07-24 06:29:54 +00:00
startGamePacket.setPlayerGamemode(0);
startGamePacket.setPlayerPosition(Vector3f.from(0, 69, 0));
startGamePacket.setRotation(Vector2f.from(1, 1));
2019-07-24 06:29:54 +00:00
2019-11-10 22:53:01 +00:00
startGamePacket.setSeed(-1);
2019-08-06 02:09:45 +00:00
startGamePacket.setDimensionId(playerEntity.getDimension());
startGamePacket.setGeneratorId(1);
2019-07-24 06:29:54 +00:00
startGamePacket.setLevelGamemode(0);
startGamePacket.setDifficulty(1);
startGamePacket.setDefaultSpawn(Vector3i.ZERO);
2019-12-04 18:13:49 +00:00
startGamePacket.setAchievementsDisabled(true);
2019-11-10 22:53:01 +00:00
startGamePacket.setTime(-1);
startGamePacket.setEduEditionOffers(0);
2019-07-24 06:29:54 +00:00
startGamePacket.setEduFeaturesEnabled(false);
startGamePacket.setRainLevel(0);
startGamePacket.setLightningLevel(0);
startGamePacket.setMultiplayerGame(true);
startGamePacket.setBroadcastingToLan(true);
startGamePacket.getGamerules().add(new GameRuleData<>("showcoordinates", true));
2019-07-24 06:29:54 +00:00
startGamePacket.setPlatformBroadcastMode(GamePublishSetting.PUBLIC);
startGamePacket.setXblBroadcastMode(GamePublishSetting.PUBLIC);
startGamePacket.setCommandsEnabled(true);
startGamePacket.setTexturePacksRequired(false);
startGamePacket.setBonusChestEnabled(false);
startGamePacket.setStartingWithMap(false);
startGamePacket.setTrustingPlayers(true);
startGamePacket.setDefaultPlayerPermission(PlayerPermission.OPERATOR);
2019-07-24 06:29:54 +00:00
startGamePacket.setServerChunkTickRange(4);
startGamePacket.setBehaviorPackLocked(false);
startGamePacket.setResourcePackLocked(false);
startGamePacket.setFromLockedWorldTemplate(false);
startGamePacket.setUsingMsaGamertagsOnly(false);
startGamePacket.setFromWorldTemplate(false);
startGamePacket.setWorldTemplateOptionLocked(false);
2019-08-06 02:09:45 +00:00
startGamePacket.setLevelId("world");
2019-07-24 06:29:54 +00:00
startGamePacket.setWorldName("world");
startGamePacket.setPremiumWorldTemplateId("00000000-0000-0000-0000-000000000000");
2019-11-10 22:53:01 +00:00
// startGamePacket.setCurrentTick(0);
2019-07-24 06:29:54 +00:00
startGamePacket.setEnchantmentSeed(0);
startGamePacket.setMultiplayerCorrelationId("");
startGamePacket.setBlockPalette(BlockTranslator.BLOCKS);
2019-07-24 06:29:54 +00:00
startGamePacket.setItemEntries(Toolbox.ITEMS);
startGamePacket.setVanillaVersion("*");
2019-11-10 22:53:01 +00:00
// startGamePacket.setMovementServerAuthoritative(true);
2019-07-24 06:29:54 +00:00
upstream.sendPacket(startGamePacket);
}
}