2
0
mirror of https://github.com/proxiodev/RedisBungee.git synced 2024-12-23 02:28:01 +00:00

updated config, added supported for velocity

This commit is contained in:
mohammed jasem alaajel 2022-07-17 09:51:24 +04:00
parent a9600a7d8a
commit 6a6e303334
6 changed files with 731 additions and 329 deletions

View File

@ -18,6 +18,7 @@ max-redis-connections: 10
# since redis can support ssl by version 6 you can use ssl in redis bungee too!
# but there is more configuration needed to work see https://github.com/ProxioDev/RedisBungee/issues/18
# in cluster mode using ssl without password is ignored due fact is not supported in Jedis lib
useSSL: false
# An identifier for this BungeeCord instance. Will randomly generate if leaving it blank.

View File

@ -35,6 +35,7 @@ import net.md_5.bungee.config.YamlConfiguration;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.*;
import redis.clients.jedis.exceptions.JedisConnectionException;
import redis.clients.jedis.exceptions.JedisException;
import java.io.*;
import java.lang.reflect.Field;
@ -486,11 +487,58 @@ public class RedisBungeeBungeePlugin extends Plugin implements RedisBungeePlugin
UUIDFetcher.setHttpClient(httpClient);
// init lua manager
LuaManager luaManager = new LuaManager(this);
if (getRedisBungeeMode() == RedisBungeeMode.CLUSTER) {
this.getRedisClusterTimeScript = luaManager.createScript(IOUtil.readInputStreamAsString(getResourceAsStream("lua/get_cluster_time.lua")));
}
new RedisTask<Void>(this) {
@Override
public Void jedisTask(Jedis jedis) {
// This is more portable than INFO <section>
String info = jedis.info();
for (String s : info.split("\r\n")) {
if (s.startsWith("redis_version:")) {
String version = s.split(":")[1];
getLogger().info(version + " <- redis version");
if (!RedisUtil.isRedisVersionRight(version)) {
getLogger().severe("Your version of Redis (" + version + ") is not at least version 3.0 RedisBungee requires a newer version of Redis.");
throw new RuntimeException("Unsupported Redis version detected");
}
long uuidCacheSize = jedis.hlen("uuid-cache");
if (uuidCacheSize > 750000) {
getLogger().info("Looks like you have a really big UUID cache! Run https://www.spigotmc.org/resources/redisbungeecleaner.8505/ as soon as possible.");
}
break;
}
}
return null;
}
@Override
public Void clusterJedisTask(JedisCluster jedisCluster) {
// This is more portable than INFO <section>
try {
getRedisClusterTimeScript = luaManager.createScript(IOUtil.readInputStreamAsString(getResourceAsStream("lua/get_cluster_time.lua")));
} catch (JedisException e) {
throw new RuntimeException("possible not supported redis version", e);
}
String info = (String) luaManager.createScript(IOUtil.readInputStreamAsString(getResourceAsStream("lua/get_cluster_info.lua"))).eval(Collections.singletonList("0"), Collections.emptyList());
for (String s : info.split("\r\n")) {
if (s.startsWith("redis_version:")) {
String version = s.split(":")[1];
getLogger().info(version + " <- redis version");
if (!RedisUtil.isRedisVersionRight(version)) {
getLogger().severe("Your version of Redis (" + version + ") is not at least version 3.0 RedisBungee requires a newer version of Redis.");
throw new RuntimeException("Unsupported Redis version detected");
}
long uuidCacheSize = jedisCluster.hlen("uuid-cache");
if (uuidCacheSize > 750000) {
getLogger().info("Looks like you have a really big UUID cache! Run https://www.spigotmc.org/resources/redisbungeecleaner.8505/ as soon as possible.");
}
break;
}
}
return null;
}
}.execute();
getLogger().info("lua manager was loaded");
// check if this proxy is recovering from a crash
// check if this proxy is recovering from a crash and start heart the beat.
new RedisTask<Void>(api) {
@Override
public Void jedisTask(Jedis jedis) {
@ -509,6 +557,8 @@ public class RedisBungeeBungeePlugin extends Plugin implements RedisBungeePlugin
}
} catch (NumberFormatException ignored) {
}
} else {
jedis.hset("heartbeats", configuration.getServerId(), jedis.time().get(0));
}
return null;
@ -523,6 +573,7 @@ public class RedisBungeeBungeePlugin extends Plugin implements RedisBungeePlugin
try {
long value = Long.parseLong(jedisCluster.hget("heartbeats", configuration.getServerId()));
long redisTime = getRedisClusterTime();
if (redisTime < value + 20) {
getLogger().severe("You have launched a possible impostor Velocity / Bungeecord instance. Another instance is already running.");
getLogger().severe("For data consistency reasons, RedisBungee will now disable itself.");
@ -531,55 +582,8 @@ public class RedisBungeeBungeePlugin extends Plugin implements RedisBungeePlugin
}
} catch (NumberFormatException ignored) {
}
}
return null;
}
}.execute();
new RedisTask<Void>(this) {
@Override
public Void jedisTask(Jedis jedis) {
// This is more portable than INFO <section>
String info = jedis.info();
for (String s : info.split("\r\n")) {
if (s.startsWith("redis_version:")) {
String version = s.split(":")[1];
getLogger().info(version + " <- redis version");
if (!RedisUtil.isRedisVersionRight(version)) {
getLogger().severe("Your version of Redis (" + version + ") is not at least version 3.0 RedisBungee requires a newer version of Redis.");
throw new RuntimeException("Unsupported Redis version detected");
}
break;
}
}
jedis.hset("heartbeats", configuration.getServerId(), jedis.time().get(0));
long uuidCacheSize = jedis.hlen("uuid-cache");
if (uuidCacheSize > 750000) {
getLogger().info("Looks like you have a really big UUID cache! Run https://www.spigotmc.org/resources/redisbungeecleaner.8505/ as soon as possible.");
}
return null;
}
@Override
public Void clusterJedisTask(JedisCluster jedisCluster) {
// This is more portable than INFO <section>
String info = (String) luaManager.createScript(IOUtil.readInputStreamAsString(getResourceAsStream("lua/get_cluster_info.lua"))).eval(Collections.singletonList("0"), Collections.emptyList());
for (String s : info.split("\r\n")) {
if (s.startsWith("redis_version:")) {
String version = s.split(":")[1];
getLogger().info(version + " <- redis version");
if (!RedisUtil.isRedisVersionRight(version)) {
getLogger().severe("Your version of Redis (" + version + ") is not at least version 3.0 RedisBungee requires a newer version of Redis.");
throw new RuntimeException("Unsupported Redis version detected");
}
break;
}
}
jedisCluster.hset("heartbeats", configuration.getServerId(), String.valueOf(getRedisClusterTime()));
long uuidCacheSize = jedisCluster.hlen("uuid-cache");
if (uuidCacheSize > 750000) {
getLogger().info("Looks like you have a really big UUID cache! Run https://www.spigotmc.org/resources/redisbungeecleaner.8505/ as soon as possible.");
} else {
jedisCluster.hset("heartbeats", configuration.getServerId(), String.valueOf(getRedisClusterTime()));
}
return null;
}
@ -798,7 +802,6 @@ public class RedisBungeeBungeePlugin extends Plugin implements RedisBungeePlugin
getProxy().getPluginManager().unregisterListeners(this);
new RedisTask<Void>(api) {
@Override
public Void jedisTask(Jedis jedis) {
@ -886,7 +889,8 @@ public class RedisBungeeBungeePlugin extends Plugin implements RedisBungeePlugin
} else {
this.jedisSummoner = new ClusterJedisSummoner(new JedisCluster(new HostAndPort(redisServer, redisPort), 5000, 5000, 60, poolConfig));
getLogger().warning("SSL option is ignored in Cluster mode if no PASSWORD is set");
}this.redisBungeeMode = RedisBungeeMode.CLUSTER;
}
this.redisBungeeMode = RedisBungeeMode.CLUSTER;
getLogger().log(Level.INFO, "RedisBungee MODE: CLUSTER");
} else {
JedisPoolConfig config = new JedisPoolConfig();

View File

@ -4,13 +4,14 @@ import com.google.gson.Gson;
import com.imaginarycode.minecraft.redisbungee.api.AbstractDataManager;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ServerConnection;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.Pipeline;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
public class RBUtils {
public class PlayerUtils {
private static final Gson gson = new Gson();
protected static void createPlayer(Player player, Pipeline pipeline, boolean fireEvent) {
@ -32,5 +33,22 @@ public class RBUtils {
}
}
protected static void createPlayer(Player player, JedisCluster jedisCluster, boolean fireEvent) {
Optional<ServerConnection> server = player.getCurrentServer();
server.ifPresent(serverConnection -> jedisCluster.hset("player:" + player.getUniqueId().toString(), "server", serverConnection.getServerInfo().getName()));
Map<String, String> playerData = new HashMap<>(4);
playerData.put("online", "0");
playerData.put("ip", player.getRemoteAddress().getHostName());
playerData.put("proxy", RedisBungeeAPI.getRedisBungeeApi().getServerId());
jedisCluster.sadd("proxy:" + RedisBungeeAPI.getRedisBungeeApi().getServerId() + ":usersOnline", player.getUniqueId().toString());
jedisCluster.hmset("player:" + player.getUniqueId().toString(), playerData);
if (fireEvent) {
jedisCluster.publish("redisbungee-data", gson.toJson(new AbstractDataManager.DataManagerMessage<>(
player.getUniqueId(), RedisBungeeAPI.getRedisBungeeApi().getServerId(), AbstractDataManager.DataManagerMessage.Action.JOIN,
new AbstractDataManager.LoginPayload(player.getRemoteAddress().getAddress()))));
}
}
}

View File

@ -6,11 +6,12 @@ import com.google.common.collect.Multimap;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import com.imaginarycode.minecraft.redisbungee.internal.AbstractRedisBungeeListener;
import com.imaginarycode.minecraft.redisbungee.internal.AbstractDataManager;
import com.imaginarycode.minecraft.redisbungee.internal.RedisBungeePlugin;
import com.imaginarycode.minecraft.redisbungee.api.AbstractRedisBungeeListener;
import com.imaginarycode.minecraft.redisbungee.api.AbstractDataManager;
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin;
import com.imaginarycode.minecraft.redisbungee.api.tasks.RedisTask;
import com.imaginarycode.minecraft.redisbungee.events.PubSubMessageEvent;
import com.imaginarycode.minecraft.redisbungee.internal.RedisUtil;
import com.imaginarycode.minecraft.redisbungee.api.util.RedisUtil;
import com.velocitypowered.api.event.Continuation;
import com.velocitypowered.api.event.PostOrder;
import com.velocitypowered.api.event.ResultedEvent;
@ -27,6 +28,7 @@ import com.velocitypowered.api.proxy.ServerConnection;
import com.velocitypowered.api.proxy.server.ServerPing;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.Pipeline;
import java.net.InetAddress;
@ -43,9 +45,9 @@ public class RedisBungeeVelocityListener extends AbstractRedisBungeeListener<Log
@Subscribe
public void onLogin(LoginEvent event, Continuation continuation) {
plugin.executeAsync(new RedisCallable<Void>(plugin) {
plugin.executeAsync(new RedisTask<Void>(plugin) {
@Override
protected Void call(Jedis jedis) {
public Void jedisTask(Jedis jedis) {
try {
if (!event.getResult().isAllowed()) {
return null;
@ -73,21 +75,49 @@ public class RedisBungeeVelocityListener extends AbstractRedisBungeeListener<Log
continuation.resume();
}
}
@Override
public Void clusterJedisTask(JedisCluster jedisCluster) {
try {
if (!event.getResult().isAllowed()) {
return null;
}
// We make sure they aren't trying to use an existing player's name.
// This is problematic for online-mode servers as they always disconnect old clients.
if (plugin.isOnlineMode()) {
Player player = (Player) plugin.getPlayer(event.getPlayer().getUsername());
if (player != null) {
event.setResult(ResultedEvent.ComponentResult.denied(serializer.deserialize(ONLINE_MODE_RECONNECT)));
return null;
}
}
for (String s : plugin.getServerIds()) {
if (jedisCluster.sismember("proxy:" + s + ":usersOnline", event.getPlayer().getUniqueId().toString())) {
event.setResult(ResultedEvent.ComponentResult.denied(serializer.deserialize(ALREADY_LOGGED_IN)));
return null;
}
}
return null;
} finally {
continuation.resume();
}
}
});
}
@Override
@Subscribe
public void onPostLogin(PostLoginEvent event) {
plugin.executeAsync(new RedisCallable<Void>(plugin) {
plugin.executeAsync(new RedisTask<Void>(plugin) {
@Override
protected Void call(Jedis jedis) {
// this code was moved out from login event due being async..
// and it can be cancelled but it will show as false in redis-bungee
// which will register the player into the redis database.
public Void jedisTask(Jedis jedis) {
Pipeline pipeline = jedis.pipelined();
plugin.getUuidTranslator().persistInfo(event.getPlayer().getUsername(), event.getPlayer().getUniqueId(), pipeline);
RBUtils.createPlayer(event.getPlayer(), pipeline, false);
PlayerUtils.createPlayer(event.getPlayer(), pipeline, false);
pipeline.sync();
// the end of moved code.
@ -96,20 +126,39 @@ public class RedisBungeeVelocityListener extends AbstractRedisBungeeListener<Log
new AbstractDataManager.LoginPayload(event.getPlayer().getRemoteAddress().getAddress()))));
return null;
}
@Override
public Void clusterJedisTask(JedisCluster jedisCluster) {
plugin.getUuidTranslator().persistInfo(event.getPlayer().getUsername(), event.getPlayer().getUniqueId(), jedisCluster);
PlayerUtils.createPlayer(event.getPlayer(), jedisCluster, false);
// the end of moved code.
jedisCluster.publish("redisbungee-data", gson.toJson(new AbstractDataManager.DataManagerMessage<>(
event.getPlayer().getUniqueId(), plugin.getApi().getServerId(), AbstractDataManager.DataManagerMessage.Action.JOIN,
new AbstractDataManager.LoginPayload(event.getPlayer().getRemoteAddress().getAddress()))));
return null;
}
});
}
@Override
@Subscribe
public void onPlayerDisconnect(DisconnectEvent event) {
plugin.executeAsync(new RedisCallable<Void>(plugin) {
plugin.executeAsync(new RedisTask<Void>(plugin) {
@Override
protected Void call(Jedis jedis) {
public Void jedisTask(Jedis jedis) {
Pipeline pipeline = jedis.pipelined();
RedisUtil.cleanUpPlayer(event.getPlayer().getUniqueId().toString(), pipeline);
pipeline.sync();
return null;
}
@Override
public Void clusterJedisTask(JedisCluster jedisCluster) {
RedisUtil.cleanUpPlayer(event.getPlayer().getUniqueId().toString(), jedisCluster);
return null;
}
});
}
@ -119,15 +168,25 @@ public class RedisBungeeVelocityListener extends AbstractRedisBungeeListener<Log
public void onServerChange(ServerConnectedEvent event) {
Optional<ServerConnection> optionalServerConnection = event.getPlayer().getCurrentServer();
final String currentServer = optionalServerConnection.map(serverConnection -> serverConnection.getServerInfo().getName()).orElse(null);
plugin.executeAsync(new RedisCallable<Void>(plugin) {
plugin.executeAsync(new RedisTask<Void>(plugin) {
@Override
protected Void call(Jedis jedis) {
public Void jedisTask(Jedis jedis) {
jedis.hset("player:" + event.getPlayer().getUniqueId().toString(), "server", event.getServer().getServerInfo().getName());
jedis.publish("redisbungee-data", gson.toJson(new AbstractDataManager.DataManagerMessage<>(
event.getPlayer().getUniqueId(), plugin.getApi().getServerId(), AbstractDataManager.DataManagerMessage.Action.SERVER_CHANGE,
new AbstractDataManager.ServerChangePayload(event.getServer().getServerInfo().getName(), currentServer))));
return null;
}
@Override
public Void clusterJedisTask(JedisCluster jedisCluster) {
jedisCluster.hset("player:" + event.getPlayer().getUniqueId().toString(), "server", event.getServer().getServerInfo().getName());
jedisCluster.publish("redisbungee-data", gson.toJson(new AbstractDataManager.DataManagerMessage<>(
event.getPlayer().getUniqueId(), plugin.getApi().getServerId(), AbstractDataManager.DataManagerMessage.Action.SERVER_CHANGE,
new AbstractDataManager.ServerChangePayload(event.getServer().getServerInfo().getName(), currentServer))));
return null;
}
});
}

View File

@ -1,8 +1,8 @@
package com.imaginarycode.minecraft.redisbungee;
import com.imaginarycode.minecraft.redisbungee.events.PubSubMessageEvent;
import com.imaginarycode.minecraft.redisbungee.internal.AbstractDataManager;
import com.imaginarycode.minecraft.redisbungee.internal.RedisBungeePlugin;
import com.imaginarycode.minecraft.redisbungee.api.AbstractDataManager;
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.connection.DisconnectEvent;
import com.velocitypowered.api.event.connection.PostLoginEvent;