Search in sources :

Example 1 with ProxyData

use of net.glowstone.net.ProxyData in project Glowstone by GlowstoneMC.

the class LoginStartHandler method handle.

@Override
public void handle(GlowSession session, LoginStartMessage message) {
    String name = message.getUsername();
    if (session.getServer().getOnlineMode()) {
        // Get necessary information to create our request message
        String sessionId = session.getSessionId();
        //Convert to X509 format
        byte[] publicKey = SecurityUtils.generateX509Key(session.getServer().getKeyPair().getPublic()).getEncoded();
        byte[] verifyToken = SecurityUtils.generateVerifyToken();
        // Set verify data on session for use in the response handler
        session.setVerifyToken(verifyToken);
        session.setVerifyUsername(name);
        // Send created request message and wait for the response
        session.send(new EncryptionKeyRequestMessage(sessionId, publicKey, verifyToken));
    } else {
        PlayerProfile profile;
        ProxyData proxy = session.getProxyData();
        if (proxy == null) {
            UUID uuid = UUID.nameUUIDFromBytes(("OfflinePlayer:" + name).getBytes(StandardCharsets.UTF_8));
            profile = new PlayerProfile(name, uuid);
        } else {
            profile = proxy.getProfile();
            if (profile == null) {
                profile = proxy.getProfile(name);
            }
        }
        AsyncPlayerPreLoginEvent event = EventFactory.onPlayerPreLogin(profile.getName(), session.getAddress(), profile.getUniqueId());
        if (event.getLoginResult() != Result.ALLOWED) {
            session.disconnect(event.getKickMessage(), true);
            return;
        }
        PlayerProfile finalProfile = profile;
        session.getServer().getScheduler().runTask(null, () -> session.setPlayer(finalProfile));
    }
}
Also used : ProxyData(net.glowstone.net.ProxyData) EncryptionKeyRequestMessage(net.glowstone.net.message.login.EncryptionKeyRequestMessage) PlayerProfile(net.glowstone.entity.meta.profile.PlayerProfile) AsyncPlayerPreLoginEvent(org.bukkit.event.player.AsyncPlayerPreLoginEvent) UUID(java.util.UUID)

Example 2 with ProxyData

use of net.glowstone.net.ProxyData in project Glowstone by GlowstoneMC.

the class HandshakeHandler method handle.

@Override
public void handle(GlowSession session, HandshakeMessage message) {
    ProtocolType protocol;
    if (message.getState() == 1) {
        protocol = ProtocolType.STATUS;
    } else if (message.getState() == 2) {
        protocol = ProtocolType.LOGIN;
    } else {
        session.disconnect("Invalid state");
        return;
    }
    session.setVersion(message.getVersion());
    session.setHostname(message.getAddress() + ":" + message.getPort());
    // the client's UUID and (optionally) properties
    if (session.getServer().getProxySupport()) {
        try {
            session.setProxyData(new ProxyData(session, message.getAddress()));
        } catch (IllegalArgumentException ex) {
            session.disconnect("Invalid proxy data provided.");
            // protocol is still set here and below to prevent errors
            // trying to decode packets after this one under the wrong
            // protocol, even though client is kicked
            session.setProtocol(protocol);
            return;
        } catch (Exception ex) {
            GlowServer.logger.log(Level.SEVERE, "Error parsing proxy data for " + session, ex);
            session.disconnect("Failed to parse proxy data.");
            session.setProtocol(protocol);
            return;
        }
    }
    session.setProtocol(protocol);
    if (protocol == ProtocolType.LOGIN) {
        if (message.getVersion() < GlowServer.PROTOCOL_VERSION) {
            session.disconnect("Outdated client! I'm running " + GlowServer.GAME_VERSION);
        } else if (message.getVersion() > GlowServer.PROTOCOL_VERSION) {
            session.disconnect("Outdated server! I'm running " + GlowServer.GAME_VERSION);
        }
    }
}
Also used : ProxyData(net.glowstone.net.ProxyData) ProtocolType(net.glowstone.net.protocol.ProtocolType)

Aggregations

ProxyData (net.glowstone.net.ProxyData)2 UUID (java.util.UUID)1 PlayerProfile (net.glowstone.entity.meta.profile.PlayerProfile)1 EncryptionKeyRequestMessage (net.glowstone.net.message.login.EncryptionKeyRequestMessage)1 ProtocolType (net.glowstone.net.protocol.ProtocolType)1 AsyncPlayerPreLoginEvent (org.bukkit.event.player.AsyncPlayerPreLoginEvent)1