2019-07-10 06:34:10 +00:00
|
|
|
/*
|
2021-01-01 15:10:36 +00:00
|
|
|
* Copyright (c) 2019-2021 GeyserMC. http://geysermc.org
|
2019-07-10 06:34:10 +00:00
|
|
|
*
|
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-10 06:34:10 +00:00
|
|
|
*
|
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.
|
2019-07-10 06:34:10 +00:00
|
|
|
*
|
|
|
|
* @author GeyserMC
|
|
|
|
* @link https://github.com/GeyserMC/Geyser
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.geysermc.connector.network;
|
|
|
|
|
2019-08-02 01:38:21 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.BedrockPacket;
|
2020-08-11 14:00:14 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.BedrockPacketCodec;
|
2021-03-31 18:06:05 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.data.ExperimentData;
|
2021-03-09 17:51:48 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.data.ResourcePackType;
|
2019-07-12 20:59:36 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.*;
|
2020-09-17 00:08:26 +00:00
|
|
|
import org.geysermc.connector.GeyserConnector;
|
2020-06-27 22:27:00 +00:00
|
|
|
import org.geysermc.connector.common.AuthType;
|
2020-06-10 22:58:29 +00:00
|
|
|
import org.geysermc.connector.configuration.GeyserConfiguration;
|
2019-07-10 06:34:10 +00:00
|
|
|
import org.geysermc.connector.network.session.GeyserSession;
|
2020-05-25 01:07:05 +00:00
|
|
|
import org.geysermc.connector.network.translators.PacketTranslatorRegistry;
|
2021-07-13 01:19:40 +00:00
|
|
|
import org.geysermc.connector.registry.BlockRegistries;
|
|
|
|
import org.geysermc.connector.registry.Registries;
|
2021-01-12 02:23:09 +00:00
|
|
|
import org.geysermc.connector.utils.*;
|
2019-07-10 06:34:10 +00:00
|
|
|
|
2020-09-17 00:08:26 +00:00
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
2019-08-02 01:38:21 +00:00
|
|
|
public class UpstreamPacketHandler extends LoggingPacketHandler {
|
2019-07-10 06:34:10 +00:00
|
|
|
|
|
|
|
public UpstreamPacketHandler(GeyserConnector connector, GeyserSession session) {
|
2019-08-02 01:38:21 +00:00
|
|
|
super(connector, session);
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean translateAndDefault(BedrockPacket packet) {
|
2020-05-25 01:07:05 +00:00
|
|
|
return PacketTranslatorRegistry.BEDROCK_TRANSLATOR.translate(packet.getClass(), packet, session);
|
2019-07-10 06:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean handle(LoginPacket loginPacket) {
|
2020-08-11 14:00:14 +00:00
|
|
|
BedrockPacketCodec packetCodec = BedrockProtocol.getBedrockCodec(loginPacket.getProtocolVersion());
|
|
|
|
if (packetCodec == null) {
|
|
|
|
if (loginPacket.getProtocolVersion() > BedrockProtocol.DEFAULT_BEDROCK_CODEC.getProtocolVersion()) {
|
|
|
|
// Too early to determine session locale
|
2020-11-17 18:28:08 +00:00
|
|
|
session.getConnector().getLogger().info(LanguageUtils.getLocaleStringLog("geyser.network.outdated.server", BedrockProtocol.DEFAULT_BEDROCK_CODEC.getMinecraftVersion()));
|
2020-08-11 14:00:14 +00:00
|
|
|
session.disconnect(LanguageUtils.getLocaleStringLog("geyser.network.outdated.server", BedrockProtocol.DEFAULT_BEDROCK_CODEC.getMinecraftVersion()));
|
|
|
|
return true;
|
|
|
|
} else if (loginPacket.getProtocolVersion() < BedrockProtocol.DEFAULT_BEDROCK_CODEC.getProtocolVersion()) {
|
2020-11-17 18:28:08 +00:00
|
|
|
session.getConnector().getLogger().info(LanguageUtils.getLocaleStringLog("geyser.network.outdated.client", BedrockProtocol.DEFAULT_BEDROCK_CODEC.getMinecraftVersion()));
|
2020-08-11 14:00:14 +00:00
|
|
|
session.disconnect(LanguageUtils.getLocaleStringLog("geyser.network.outdated.client", BedrockProtocol.DEFAULT_BEDROCK_CODEC.getMinecraftVersion()));
|
|
|
|
return true;
|
|
|
|
}
|
2019-07-10 06:34:10 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 14:00:14 +00:00
|
|
|
session.getUpstream().getSession().setPacketCodec(packetCodec);
|
|
|
|
|
2021-03-09 17:51:48 +00:00
|
|
|
// Set the block translation based off of version
|
2021-07-13 01:19:40 +00:00
|
|
|
session.setBlockMappings(BlockRegistries.BLOCKS.forVersion(loginPacket.getProtocolVersion()));
|
|
|
|
session.setItemMappings(Registries.ITEMS.forVersion(loginPacket.getProtocolVersion()));
|
2021-03-09 17:51:48 +00:00
|
|
|
|
2019-08-02 03:02:17 +00:00
|
|
|
LoginEncryptionUtils.encryptPlayerConnection(connector, session, loginPacket);
|
2019-07-10 06:34:10 +00:00
|
|
|
|
|
|
|
PlayStatusPacket playStatus = new PlayStatusPacket();
|
|
|
|
playStatus.setStatus(PlayStatusPacket.Status.LOGIN_SUCCESS);
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacket(playStatus);
|
2019-07-10 06:34:10 +00:00
|
|
|
|
|
|
|
ResourcePacksInfoPacket resourcePacksInfo = new ResourcePacksInfoPacket();
|
2020-09-17 00:08:26 +00:00
|
|
|
for(ResourcePack resourcePack : ResourcePack.PACKS.values()) {
|
|
|
|
ResourcePackManifest.Header header = resourcePack.getManifest().getHeader();
|
2020-12-09 16:30:59 +00:00
|
|
|
resourcePacksInfo.getResourcePackInfos().add(new ResourcePacksInfoPacket.Entry(
|
|
|
|
header.getUuid().toString(), header.getVersionString(), resourcePack.getFile().length(),
|
|
|
|
"", "", "", false, false));
|
2020-09-17 00:08:26 +00:00
|
|
|
}
|
|
|
|
resourcePacksInfo.setForcedToAccept(GeyserConnector.getInstance().getConfig().isForceResourcePacks());
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacket(resourcePacksInfo);
|
2019-07-10 06:34:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-11-02 21:14:28 +00:00
|
|
|
public boolean handle(ResourcePackClientResponsePacket packet) {
|
|
|
|
switch (packet.getStatus()) {
|
2019-07-10 06:34:10 +00:00
|
|
|
case COMPLETED:
|
2021-03-17 15:15:57 +00:00
|
|
|
session.connect();
|
2020-07-05 23:35:51 +00:00
|
|
|
connector.getLogger().info(LanguageUtils.getLocaleStringLog("geyser.network.connect", session.getAuthData().getName()));
|
2019-07-10 06:34:10 +00:00
|
|
|
break;
|
2020-09-17 00:08:26 +00:00
|
|
|
|
|
|
|
case SEND_PACKS:
|
|
|
|
for(String id : packet.getPackIds()) {
|
|
|
|
ResourcePackDataInfoPacket data = new ResourcePackDataInfoPacket();
|
|
|
|
String[] packID = id.split("_");
|
|
|
|
ResourcePack pack = ResourcePack.PACKS.get(packID[0]);
|
|
|
|
ResourcePackManifest.Header header = pack.getManifest().getHeader();
|
|
|
|
|
|
|
|
data.setPackId(header.getUuid());
|
|
|
|
int chunkCount = (int) Math.ceil((int) pack.getFile().length() / (double) ResourcePack.CHUNK_SIZE);
|
|
|
|
data.setChunkCount(chunkCount);
|
|
|
|
data.setCompressedPackSize(pack.getFile().length());
|
|
|
|
data.setMaxChunkSize(ResourcePack.CHUNK_SIZE);
|
|
|
|
data.setHash(pack.getSha256());
|
|
|
|
data.setPackVersion(packID[1]);
|
|
|
|
data.setPremium(false);
|
|
|
|
data.setType(ResourcePackType.RESOURCE);
|
|
|
|
|
|
|
|
session.sendUpstreamPacket(data);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2019-07-10 06:34:10 +00:00
|
|
|
case HAVE_ALL_PACKS:
|
2020-09-17 00:08:26 +00:00
|
|
|
ResourcePackStackPacket stackPacket = new ResourcePackStackPacket();
|
2020-10-08 22:33:36 +00:00
|
|
|
stackPacket.setExperimentsPreviouslyToggled(false);
|
2020-09-17 00:08:26 +00:00
|
|
|
stackPacket.setForcedToAccept(false); // Leaving this as false allows the player to choose to download or not
|
|
|
|
stackPacket.setGameVersion(session.getClientData().getGameVersion());
|
|
|
|
|
|
|
|
for (ResourcePack pack : ResourcePack.PACKS.values()) {
|
|
|
|
ResourcePackManifest.Header header = pack.getManifest().getHeader();
|
|
|
|
stackPacket.getResourcePacks().add(new ResourcePackStackPacket.Entry(header.getUuid().toString(), header.getVersionString(), ""));
|
|
|
|
}
|
|
|
|
|
2021-07-13 01:19:40 +00:00
|
|
|
if (session.getItemMappings().getFurnaceMinecartData() != null) {
|
2021-03-31 18:06:05 +00:00
|
|
|
// Allow custom items to work
|
|
|
|
stackPacket.getExperiments().add(new ExperimentData("data_driven_items", true));
|
|
|
|
}
|
|
|
|
|
2021-07-14 00:48:45 +00:00
|
|
|
if (session.getConnector().getConfig().isExtendedWorldHeight()) {
|
|
|
|
// Allow extended world height in the overworld to work
|
|
|
|
stackPacket.getExperiments().add(new ExperimentData("caves_and_cliffs", true));
|
|
|
|
}
|
|
|
|
|
2020-09-17 00:08:26 +00:00
|
|
|
session.sendUpstreamPacket(stackPacket);
|
2019-07-10 06:34:10 +00:00
|
|
|
break;
|
2020-09-17 00:08:26 +00:00
|
|
|
|
2019-07-10 06:34:10 +00:00
|
|
|
default:
|
2020-04-25 22:53:35 +00:00
|
|
|
session.disconnect("disconnectionScreen.resourcePack");
|
2019-07-10 06:34:10 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-07-13 19:00:51 +00:00
|
|
|
|
2019-07-10 06:34:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-07-12 20:59:36 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean handle(ModalFormResponsePacket packet) {
|
2020-10-29 23:58:17 +00:00
|
|
|
session.getFormCache().handleResponse(packet);
|
2020-10-30 00:31:22 +00:00
|
|
|
return true;
|
2019-07-12 20:59:36 +00:00
|
|
|
}
|
|
|
|
|
2019-08-02 20:15:32 +00:00
|
|
|
private boolean couldLoginUserByName(String bedrockUsername) {
|
|
|
|
if (connector.getConfig().getUserAuths() != null) {
|
2020-04-29 20:01:53 +00:00
|
|
|
GeyserConfiguration.IUserAuthenticationInfo info = connector.getConfig().getUserAuths().get(bedrockUsername);
|
2019-08-02 20:15:32 +00:00
|
|
|
|
|
|
|
if (info != null) {
|
2020-07-05 23:35:51 +00:00
|
|
|
connector.getLogger().info(LanguageUtils.getLocaleStringLog("geyser.auth.stored_credentials", session.getAuthData().getName()));
|
2021-01-11 20:52:02 +00:00
|
|
|
session.setMicrosoftAccount(info.isMicrosoftAccount());
|
2019-12-01 21:16:52 +00:00
|
|
|
session.authenticate(info.getEmail(), info.getPassword());
|
2019-08-02 20:55:26 +00:00
|
|
|
|
|
|
|
// TODO send a message to bedrock user telling them they are connected (if nothing like a motd
|
|
|
|
// somes from the Java server w/in a few seconds)
|
2019-08-02 20:15:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-12 20:59:36 +00:00
|
|
|
@Override
|
2020-05-20 15:12:03 +00:00
|
|
|
public boolean handle(SetLocalPlayerAsInitializedPacket packet) {
|
2020-10-29 22:30:52 +00:00
|
|
|
LanguageUtils.loadGeyserLocale(session.getLocale());
|
2020-07-05 23:35:51 +00:00
|
|
|
|
2021-03-17 15:15:57 +00:00
|
|
|
if (!session.isLoggedIn() && !session.isLoggingIn() && session.getRemoteAuthType() == AuthType.ONLINE) {
|
2019-08-02 20:15:32 +00:00
|
|
|
// TODO it is safer to key authentication on something that won't change (UUID, not username)
|
2019-12-21 17:35:48 +00:00
|
|
|
if (!couldLoginUserByName(session.getAuthData().getName())) {
|
2020-10-29 23:58:17 +00:00
|
|
|
LoginEncryptionUtils.buildAndShowLoginWindow(session);
|
2019-08-02 20:15:32 +00:00
|
|
|
}
|
|
|
|
// else we were able to log the user in
|
2019-09-25 21:52:28 +00:00
|
|
|
}
|
2020-05-20 15:12:03 +00:00
|
|
|
return translateAndDefault(packet);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean handle(MovePlayerPacket packet) {
|
2019-09-25 21:52:28 +00:00
|
|
|
if (session.isLoggingIn()) {
|
2020-11-24 16:14:02 +00:00
|
|
|
SetTitlePacket titlePacket = new SetTitlePacket();
|
|
|
|
titlePacket.setType(SetTitlePacket.Type.ACTIONBAR);
|
|
|
|
titlePacket.setText(LanguageUtils.getPlayerLocaleString("geyser.auth.login.wait", session.getLocale()));
|
|
|
|
titlePacket.setFadeInTime(0);
|
|
|
|
titlePacket.setFadeOutTime(1);
|
|
|
|
titlePacket.setStayTime(2);
|
2021-07-13 01:19:40 +00:00
|
|
|
titlePacket.setXuid("");
|
|
|
|
titlePacket.setPlatformOnlineId("");
|
2020-11-24 16:14:02 +00:00
|
|
|
session.sendUpstreamPacket(titlePacket);
|
2019-07-24 06:29:54 +00:00
|
|
|
}
|
2019-09-14 00:37:06 +00:00
|
|
|
|
|
|
|
return translateAndDefault(packet);
|
2019-07-12 20:59:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-09-25 21:52:28 +00:00
|
|
|
boolean defaultHandler(BedrockPacket packet) {
|
2019-08-10 00:15:06 +00:00
|
|
|
return translateAndDefault(packet);
|
|
|
|
}
|
2020-09-17 00:08:26 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean handle(ResourcePackChunkRequestPacket packet) {
|
|
|
|
ResourcePackChunkDataPacket data = new ResourcePackChunkDataPacket();
|
|
|
|
ResourcePack pack = ResourcePack.PACKS.get(packet.getPackId().toString());
|
|
|
|
|
|
|
|
data.setChunkIndex(packet.getChunkIndex());
|
|
|
|
data.setProgress(packet.getChunkIndex() * ResourcePack.CHUNK_SIZE);
|
|
|
|
data.setPackVersion(packet.getPackVersion());
|
|
|
|
data.setPackId(packet.getPackId());
|
|
|
|
|
|
|
|
int offset = packet.getChunkIndex() * ResourcePack.CHUNK_SIZE;
|
|
|
|
byte[] packData = new byte[(int) MathUtils.constrain(pack.getFile().length() - offset, 0, ResourcePack.CHUNK_SIZE)];
|
|
|
|
|
|
|
|
try (InputStream inputStream = new FileInputStream(pack.getFile())) {
|
|
|
|
inputStream.skip(offset);
|
|
|
|
inputStream.read(packData, 0, packData.length);
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
data.setData(packData);
|
|
|
|
|
|
|
|
session.sendUpstreamPacket(data);
|
|
|
|
return true;
|
|
|
|
}
|
2019-12-23 17:47:31 +00:00
|
|
|
}
|