<commit_before>package org.spacehq.mc.protocol;

import org.spacehq.mc.auth.GameProfile;
import org.spacehq.mc.auth.SessionService;
import org.spacehq.mc.auth.exception.AuthenticationUnavailableException;
import org.spacehq.mc.protocol.data.status.ServerStatusInfo;
import org.spacehq.mc.protocol.data.status.handler.ServerInfoBuilder;
import org.spacehq.mc.protocol.packet.handshake.client.HandshakePacket;
import org.spacehq.mc.protocol.packet.ingame.client.ClientKeepAlivePacket;
import org.spacehq.mc.protocol.packet.ingame.server.ServerDisconnectPacket;
import org.spacehq.mc.protocol.packet.ingame.server.ServerKeepAlivePacket;
import org.spacehq.mc.protocol.packet.login.client.EncryptionResponsePacket;
import org.spacehq.mc.protocol.packet.login.client.LoginStartPacket;
import org.spacehq.mc.protocol.packet.login.server.EncryptionRequestPacket;
import org.spacehq.mc.protocol.packet.login.server.LoginDisconnectPacket;
import org.spacehq.mc.protocol.packet.login.server.LoginSuccessPacket;
import org.spacehq.mc.protocol.packet.status.client.StatusPingPacket;
import org.spacehq.mc.protocol.packet.status.client.StatusQueryPacket;
import org.spacehq.mc.protocol.packet.status.server.StatusPongPacket;
import org.spacehq.mc.protocol.packet.status.server.StatusResponsePacket;
import org.spacehq.mc.util.CryptUtil;
import org.spacehq.packetlib.Session;
import org.spacehq.packetlib.event.session.DisconnectingEvent;
import org.spacehq.packetlib.event.session.PacketReceivedEvent;
import org.spacehq.packetlib.event.session.SessionAdapter;

import javax.crypto.SecretKey;
import java.math.BigInteger;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.util.Arrays;
import java.util.Random;
import java.util.UUID;

public class ServerListener extends SessionAdapter {

	private static KeyPair pair = CryptUtil.generateKeyPair();

	private byte verifyToken[] = new byte[4];
	private String serverId = "";
	private String username = "";

	private long lastPingTime = 0;
	private int lastPingId = 0;

	public ServerListener() {
		new Random().nextBytes(this.verifyToken);
	}

	@Override
	public void packetReceived(PacketReceivedEvent event) {
		MinecraftProtocol protocol = (MinecraftProtocol) event.getSession().getPacketProtocol();
		if(protocol.getMode() == ProtocolMode.HANDSHAKE) {
			if(event.getPacket() instanceof HandshakePacket) {
				HandshakePacket packet = event.getPacket();
				switch(packet.getIntent()) {
					case STATUS:
						protocol.setMode(ProtocolMode.STATUS, false, event.getSession());
						break;
					case LOGIN:
						protocol.setMode(ProtocolMode.LOGIN, false, event.getSession());
						if(packet.getProtocolVersion() > ProtocolConstants.PROTOCOL_VERSION) {
							event.getSession().disconnect("Outdated server! I'm still on " + ProtocolConstants.GAME_VERSION + ".");
						} else if(packet.getProtocolVersion() < ProtocolConstants.PROTOCOL_VERSION) {
							event.getSession().disconnect("Outdated client! Please use " + ProtocolConstants.GAME_VERSION + ".");
						}

						break;
					default:
						throw new UnsupportedOperationException("Invalid client intent: " + packet.getIntent());
				}
			}
		}

		if(protocol.getMode() == ProtocolMode.LOGIN) {
			if(event.getPacket() instanceof LoginStartPacket) {
				this.username = event.<LoginStartPacket>getPacket().getUsername();
				boolean verify = event.getSession().getFlag(ProtocolConstants.VERIFY_USERS_KEY);
				if(verify) {
					event.getSession().send(new EncryptionRequestPacket(this.serverId, pair.getPublic(), this.verifyToken));
				} else {
					event.getSession().send(new LoginSuccessPacket(new GameProfile((UUID) null, this.username)));
					event.getSession().setFlag(ProtocolConstants.PROFILE_KEY, new GameProfile((UUID) null, this.username));
					protocol.setMode(ProtocolMode.GAME, false, event.getSession());
					new KeepAliveThread(event.getSession()).start();
					ServerLoginHandler handler = event.getSession().getFlag(ProtocolConstants.SERVER_LOGIN_HANDLER_KEY);
					if(handler != null) {
						handler.loggedIn(event.getSession());
					}
				}
			} else if(event.getPacket() instanceof EncryptionResponsePacket) {
				EncryptionResponsePacket packet = event.getPacket();
				PrivateKey privateKey = pair.getPrivate();
				if(!Arrays.equals(this.verifyToken, packet.getVerifyToken(privateKey))) {
					throw new IllegalStateException("Invalid nonce!");
				} else {
					SecretKey key = packet.getSecretKey(privateKey);
					protocol.enableEncryption(key);<commit_msg>Make verify users flag optional<commit_after>package org.spacehq.mc.protocol;

import org.spacehq.mc.auth.GameProfile;
import org.spacehq.mc.auth.SessionService;
import org.spacehq.mc.auth.exception.AuthenticationUnavailableException;
import org.spacehq.mc.protocol.data.status.ServerStatusInfo;
import org.spacehq.mc.protocol.data.status.handler.ServerInfoBuilder;
import org.spacehq.mc.protocol.packet.handshake.client.HandshakePacket;
import org.spacehq.mc.protocol.packet.ingame.client.ClientKeepAlivePacket;
import org.spacehq.mc.protocol.packet.ingame.server.ServerDisconnectPacket;
import org.spacehq.mc.protocol.packet.ingame.server.ServerKeepAlivePacket;
import org.spacehq.mc.protocol.packet.login.client.EncryptionResponsePacket;
import org.spacehq.mc.protocol.packet.login.client.LoginStartPacket;
import org.spacehq.mc.protocol.packet.login.server.EncryptionRequestPacket;
import org.spacehq.mc.protocol.packet.login.server.LoginDisconnectPacket;
import org.spacehq.mc.protocol.packet.login.server.LoginSuccessPacket;
import org.spacehq.mc.protocol.packet.status.client.StatusPingPacket;
import org.spacehq.mc.protocol.packet.status.client.StatusQueryPacket;
import org.spacehq.mc.protocol.packet.status.server.StatusPongPacket;
import org.spacehq.mc.protocol.packet.status.server.StatusResponsePacket;
import org.spacehq.mc.util.CryptUtil;
import org.spacehq.packetlib.Session;
import org.spacehq.packetlib.event.session.DisconnectingEvent;
import org.spacehq.packetlib.event.session.PacketReceivedEvent;
import org.spacehq.packetlib.event.session.SessionAdapter;

import javax.crypto.SecretKey;
import java.math.BigInteger;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.util.Arrays;
import java.util.Random;
import java.util.UUID;

public class ServerListener extends SessionAdapter {

	private static KeyPair pair = CryptUtil.generateKeyPair();

	private byte verifyToken[] = new byte[4];
	private String serverId = "";
	private String username = "";

	private long lastPingTime = 0;
	private int lastPingId = 0;

	public ServerListener() {
		new Random().nextBytes(this.verifyToken);
	}

	@Override
	public void packetReceived(PacketReceivedEvent event) {
		MinecraftProtocol protocol = (MinecraftProtocol) event.getSession().getPacketProtocol();
		if(protocol.getMode() == ProtocolMode.HANDSHAKE) {
			if(event.getPacket() instanceof HandshakePacket) {
				HandshakePacket packet = event.getPacket();
				switch(packet.getIntent()) {
					case STATUS:
						protocol.setMode(ProtocolMode.STATUS, false, event.getSession());
						break;
					case LOGIN:
						protocol.setMode(ProtocolMode.LOGIN, false, event.getSession());
						if(packet.getProtocolVersion() > ProtocolConstants.PROTOCOL_VERSION) {
							event.getSession().disconnect("Outdated server! I'm still on " + ProtocolConstants.GAME_VERSION + ".");
						} else if(packet.getProtocolVersion() < ProtocolConstants.PROTOCOL_VERSION) {
							event.getSession().disconnect("Outdated client! Please use " + ProtocolConstants.GAME_VERSION + ".");
						}

						break;
					default:
						throw new UnsupportedOperationException("Invalid client intent: " + packet.getIntent());
				}
			}
		}

		if(protocol.getMode() == ProtocolMode.LOGIN) {
			if(event.getPacket() instanceof LoginStartPacket) {
				this.username = event.<LoginStartPacket>getPacket().getUsername();
				boolean verify = event.getSession().hasFlag(ProtocolConstants.VERIFY_USERS_KEY) ? event.getSession().<Boolean>getFlag(ProtocolConstants.VERIFY_USERS_KEY) : true;
				if(verify) {
					event.getSession().send(new EncryptionRequestPacket(this.serverId, pair.getPublic(), this.verifyToken));
				} else {
					event.getSession().send(new LoginSuccessPacket(new GameProfile((UUID) null, this.username)));
					event.getSession().setFlag(ProtocolConstants.PROFILE_KEY, new GameProfile((UUID) null, this.username));
					protocol.setMode(ProtocolMode.GAME, false, event.getSession());
					new KeepAliveThread(event.getSession()).start();
					ServerLoginHandler handler = event.getSession().getFlag(ProtocolConstants.SERVER_LOGIN_HANDLER_KEY);
					if(handler != null) {
						handler.loggedIn(event.getSession());
					}
				}
			} else if(event.getPacket() instanceof EncryptionResponsePacket) {
				EncryptionResponsePacket packet = event.getPacket();
				PrivateKey privateKey = pair.getPrivate();
				if(!Arrays.equals(this.verifyToken, packet.getVerifyToken(privateKey))) {
					throw new IllegalStateException("Invalid nonce!");
				} else {
					SecretKey key = packet.getSecretKey(privateKey);
					protocol.enableEncryption(key);
{'fraction_non_alphanumeric': 0.11690588364194149, 'fraction_numerical': 0.0006573901610605894, 'mean_word_length': 5.330097087378641, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2889070', 'n_tokens_mistral': 3013, 'n_tokens_neox': 2682, 'n_words': 417}