2019-07-10 06:34:10 +00:00
|
|
|
/*
|
2019-07-11 21:30:35 +00:00
|
|
|
* Copyright (c) 2019 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;
|
2019-07-12 20:59:36 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.*;
|
2019-07-10 06:34:10 +00:00
|
|
|
import org.geysermc.connector.GeyserConnector;
|
2019-08-02 20:15:32 +00:00
|
|
|
import org.geysermc.connector.configuration.UserAuthenticationInfo;
|
2019-07-10 06:34:10 +00:00
|
|
|
import org.geysermc.connector.network.session.GeyserSession;
|
2019-07-24 20:12:42 +00:00
|
|
|
import org.geysermc.connector.network.translators.Registry;
|
2019-08-02 03:02:17 +00:00
|
|
|
import org.geysermc.connector.utils.LoginEncryptionUtils;
|
2019-07-10 06:34:10 +00:00
|
|
|
|
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) {
|
2019-09-25 21:52:28 +00:00
|
|
|
return Registry.BEDROCK.translate(packet.getClass(), packet, session);
|
2019-07-10 06:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean handle(LoginPacket loginPacket) {
|
2019-12-11 01:06:18 +00:00
|
|
|
if (loginPacket.getProtocolVersion() != GeyserConnector.BEDROCK_1_14_PROTOCOL_VERSION) {
|
2019-07-10 06:34:10 +00:00
|
|
|
session.getUpstream().disconnect("Unsupported Bedrock version. Are you running an outdated version?");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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);
|
2019-11-02 21:14:28 +00:00
|
|
|
session.getUpstream().sendPacket(playStatus);
|
2019-07-10 06:34:10 +00:00
|
|
|
|
|
|
|
ResourcePacksInfoPacket resourcePacksInfo = new ResourcePacksInfoPacket();
|
2019-11-02 21:14:28 +00:00
|
|
|
session.getUpstream().sendPacket(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:
|
2019-07-10 17:52:29 +00:00
|
|
|
session.connect(connector.getRemoteServer());
|
2019-07-24 06:29:54 +00:00
|
|
|
connector.getLogger().info("Player connected with username " + session.getAuthenticationData().getName());
|
2019-07-10 06:34:10 +00:00
|
|
|
break;
|
|
|
|
case HAVE_ALL_PACKS:
|
|
|
|
ResourcePackStackPacket stack = new ResourcePackStackPacket();
|
|
|
|
stack.setExperimental(false);
|
|
|
|
stack.setForcedToAccept(false);
|
2019-11-02 21:14:28 +00:00
|
|
|
stack.setGameVersion(GeyserConnector.BEDROCK_PACKET_CODEC.getMinecraftVersion());
|
|
|
|
session.getUpstream().sendPacket(stack);
|
2019-07-10 06:34:10 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
session.getUpstream().disconnect("disconnectionScreen.resourcePack");
|
|
|
|
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) {
|
2019-08-02 03:02:17 +00:00
|
|
|
return LoginEncryptionUtils.authenticateFromForm(session, connector, packet.getFormData());
|
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) {
|
|
|
|
UserAuthenticationInfo info = connector.getConfig().getUserAuths().get(bedrockUsername);
|
|
|
|
|
|
|
|
if (info != null) {
|
2019-08-02 20:55:26 +00:00
|
|
|
connector.getLogger().info("using stored credentials for bedrock user " + session.getAuthenticationData().getName());
|
2019-08-02 20:15:32 +00:00
|
|
|
session.authenticate(info.email, info.password);
|
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
|
|
|
|
public boolean handle(MovePlayerPacket packet) {
|
2019-09-21 07:42:44 +00:00
|
|
|
if (!session.isLoggedIn() && !session.isLoggingIn()) {
|
2019-08-02 20:15:32 +00:00
|
|
|
// TODO it is safer to key authentication on something that won't change (UUID, not username)
|
|
|
|
if (!couldLoginUserByName(session.getAuthenticationData().getName())) {
|
|
|
|
LoginEncryptionUtils.showLoginWindow(session);
|
|
|
|
}
|
|
|
|
// else we were able to log the user in
|
2019-07-24 06:29:54 +00:00
|
|
|
return true;
|
2019-09-25 21:52:28 +00:00
|
|
|
}
|
|
|
|
if (session.isLoggingIn()) {
|
2019-09-21 07:42:44 +00:00
|
|
|
session.sendMessage("Please wait until you are logged in...");
|
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);
|
|
|
|
}
|
2019-09-29 23:25:42 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean handle(InventoryTransactionPacket packet) {
|
|
|
|
return translateAndDefault(packet);
|
|
|
|
}
|
2019-07-10 06:34:10 +00:00
|
|
|
}
|