Geyser/connector/src/main/java/org/geysermc/connector/network/UpstreamPacketHandler.java

107 lines
4.6 KiB
Java

/*
* Copyright (c) 2019 GeyserMC. http://geysermc.org
*
* 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:
*
* 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;
import com.nimbusds.jose.JWSObject;
import com.nukkitx.protocol.bedrock.handler.BedrockPacketHandler;
import com.nukkitx.protocol.bedrock.packet.LoginPacket;
import com.nukkitx.protocol.bedrock.packet.PlayStatusPacket;
import com.nukkitx.protocol.bedrock.packet.ResourcePackClientResponsePacket;
import com.nukkitx.protocol.bedrock.packet.ResourcePackStackPacket;
import com.nukkitx.protocol.bedrock.packet.ResourcePacksInfoPacket;
import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
import net.minidev.json.JSONValue;
import org.geysermc.connector.GeyserConnector;
import org.geysermc.connector.network.session.GeyserSession;
import java.util.UUID;
public class UpstreamPacketHandler implements BedrockPacketHandler {
private GeyserConnector connector;
private GeyserSession session;
public UpstreamPacketHandler(GeyserConnector connector, GeyserSession session) {
this.connector = connector;
this.session = session;
}
@Override
public boolean handle(LoginPacket loginPacket) {
// TODO: Implement support for multiple protocols
if (loginPacket.getProtocolVersion() != GeyserConnector.BEDROCK_PACKET_CODEC.getProtocolVersion()) {
session.getUpstream().disconnect("Unsupported Bedrock version. Are you running an outdated version?");
return true;
}
session.getUpstream().setPacketCodec(GeyserConnector.BEDROCK_PACKET_CODEC);
try {
JSONObject chainData = (JSONObject) JSONValue.parse(loginPacket.getChainData().array());
JSONArray chainArray = (JSONArray) chainData.get("chain");
Object identityObject = chainArray.get(chainArray.size() - 1);
JWSObject identity = JWSObject.parse((String) identityObject);
JSONObject extraData = (JSONObject) identity.getPayload().toJSONObject().get("extraData");
session.setAuthenticationData(extraData.getAsString("displayName"), UUID.fromString(extraData.getAsString("identity")), extraData.getAsString("XUID"));
} catch (Exception ex) {
session.getUpstream().disconnect("An internal error occurred when connecting to this server.");
ex.printStackTrace();
return true;
}
PlayStatusPacket playStatus = new PlayStatusPacket();
playStatus.setStatus(PlayStatusPacket.Status.LOGIN_SUCCESS);
session.getUpstream().sendPacketImmediately(playStatus);
ResourcePacksInfoPacket resourcePacksInfo = new ResourcePacksInfoPacket();
session.getUpstream().sendPacketImmediately(resourcePacksInfo);
return true;
}
@Override
public boolean handle(ResourcePackClientResponsePacket textPacket) {
switch (textPacket.getStatus()) {
case COMPLETED:
session.connect(connector.getRemoteServer());
connector.getLogger().info("Player connected with " + session.getAuthenticationData().getName());
break;
case HAVE_ALL_PACKS:
ResourcePackStackPacket stack = new ResourcePackStackPacket();
stack.setExperimental(false);
stack.setForcedToAccept(false);
session.getUpstream().sendPacketImmediately(stack);
break;
default:
session.getUpstream().disconnect("disconnectionScreen.resourcePack");
break;
}
return true;
}
}