Java Code Examples for protocolsupport.protocol.ConnectionImpl#getFromChannel()

The following examples show how to use protocolsupport.protocol.ConnectionImpl#getFromChannel() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InitialPacketDecoder.java    From ProtocolSupport with GNU Affero General Public License v3.0 6 votes vote down vote up
protected static ConnectionImpl prepare(Channel channel, ProtocolVersion version) {
	channel.pipeline().remove(ChannelHandlers.INITIAL_DECODER);
	ConnectionImpl connection = ConnectionImpl.getFromChannel(channel);
	if (ServerPlatform.get().getMiscUtils().isDebugging()) {
		ProtocolSupport.logInfo(MessageFormat.format("{0} connected with protocol version {1}", connection.getAddress(), version));
	}
	connection.getNetworkManagerWrapper().setPacketListener(ServerPlatform.get().getMiscUtils().createHandshakeListener(connection.getNetworkManagerWrapper()));
	if (!ProtocolSupportAPI.isProtocolVersionEnabled(version)) {
		if (version.getProtocolType() == ProtocolType.PC) {
			version = version.isBeforeOrEq(ProtocolVersion.MINECRAFT_1_6_4) ? ProtocolVersion.MINECRAFT_LEGACY : ProtocolVersion.MINECRAFT_FUTURE;
		} else {
			throw new IllegalArgumentException(MessageFormat.format("Unable to get legacy or future version for disabled protocol version {0}", version));
		}
	}
	connection.setVersion(version);
	return connection;
}
 
Example 2
Source File: AbstractLoginListenerPlay.java    From ProtocolSupport with GNU Affero General Public License v3.0 5 votes vote down vote up
protected AbstractLoginListenerPlay(NetworkManagerWrapper networkmanager) {
	this.networkManager = networkmanager;
	this.connection = ConnectionImpl.getFromChannel(networkmanager.getChannel());

	synchronized (keepConnectionLock) {
		this.keepConnectionTask = connection.getIOExecutor().scheduleWithFixedDelay(this::keepConnection, 4, 4, TimeUnit.SECONDS);
	}
}
 
Example 3
Source File: AbstractLoginListener.java    From ProtocolSupport with GNU Affero General Public License v3.0 5 votes vote down vote up
public AbstractLoginListener(NetworkManagerWrapper networkmanager) {
	this.networkManager = networkmanager;
	this.connection = ConnectionImpl.getFromChannel(networkmanager.getChannel());
	ThreadLocalRandom.current().nextBytes(randomBytes);

	synchronized (timeoutTaskLock) {
		this.timeoutTask = connection.getIOExecutor().schedule(() -> disconnect("Took too long to log in"), 30, TimeUnit.SECONDS);
	}
}
 
Example 4
Source File: InitialPacketDecoder.java    From ProtocolSupportBungee with GNU Affero General Public License v3.0 4 votes vote down vote up
protected static ConnectionImpl prepare(Channel channel, ProtocolVersion version) {
	channel.pipeline().remove(ChannelHandlers.INITIAL_DECODER);
	ConnectionImpl connection = ConnectionImpl.getFromChannel(channel);
	connection.setVersion(version);
	return connection;
}
 
Example 5
Source File: PSInitialHandler.java    From ProtocolSupportBungee with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void connected(ChannelWrapper channel) throws Exception {
	super.connected(channel);
	this.channel = channel;
	this.connection = ConnectionImpl.getFromChannel(channel.getHandle());
}
 
Example 6
Source File: AbstractHandshakeListener.java    From ProtocolSupport with GNU Affero General Public License v3.0 4 votes vote down vote up
public void handleSetProtocol(NetworkState nextState, String hostname, int port) {
	switch (nextState) {
		case LOGIN: {
			networkManager.setProtocol(NetworkState.LOGIN);
			try {
				final InetAddress address = networkManager.getAddress().getAddress();
				if (ThrottleTracker.isEnabled() && !ServerPlatform.get().getMiscUtils().isProxyEnabled()) {
					if (ThrottleTracker.throttle(address)) {
						disconnect("Connection throttled! Please wait before reconnecting.");
						return;
					}
				}
			} catch (Throwable t) {
				Bukkit.getLogger().log(Level.WARNING, "Failed to check connection throttle", t);
			}

			ConnectionImpl connection = ConnectionImpl.getFromChannel(networkManager.getChannel());
			if (!connection.getVersion().isSupported()) {
				disconnect(MessageFormat.format(ServerPlatform.get().getMiscUtils().getOutdatedServerMessage().replace("'", "''"), ServerPlatform.get().getMiscUtils().getVersionName()));
				break;
			}

			ConnectionHandshakeEvent event = new ConnectionHandshakeEvent(connection, hostname);
			Bukkit.getPluginManager().callEvent(event);
			if (event.getSpoofedAddress() != null) {
				connection.changeAddress(event.getSpoofedAddress());
			}
			if (event.isLoginDenied()) {
				disconnect(event.getDenyLoginMessage());
				return;
			}
			hostname = event.getHostname();
			boolean proxyEnabled = event.shouldParseHostname() && ServerPlatform.get().getMiscUtils().isProxyEnabled();
			SpoofedData sdata = SpoofedDataParser.tryParse(hostname, proxyEnabled);
			if (sdata.isFailed()) {
				disconnect(sdata.getFailMessage());
				return;
			}
			if (proxyEnabled) {
				String spoofedRemoteAddress = sdata.getAddress();
				if (spoofedRemoteAddress != null) {
					connection.changeAddress(new InetSocketAddress(sdata.getAddress(), connection.getAddress().getPort()));
				}
				networkManager.setSpoofedProfile(sdata.getUUID(), sdata.getProperties());
			}
			hostname = sdata.getHostname();

			networkManager.setPacketListener(getLoginListener(networkManager));
			break;
		}
		case STATUS: {
			networkManager.setProtocol(NetworkState.STATUS);
			networkManager.setPacketListener(getStatusListener(networkManager));
			break;
		}
		default: {
			throw new UnsupportedOperationException("Invalid intention " + nextState);
		}
	}

	if (hostname != null) {
		networkManager.setVirtualHost(InetSocketAddress.createUnresolved(hostname, port));
	} else {
		networkManager.setVirtualHost(InetSocketAddress.createUnresolved("127.0.0.1", port));
	}
}