2022-10-29 20:02:09 +00:00
/ *
* Copyright ( c ) 2013 - present RedisBungee contributors
*
* All rights reserved . This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1 . 0
* which accompanies this distribution , and is available at
*
* http : //www.eclipse.org/legal/epl-v10.html
* /
2022-07-26 06:58:00 +00:00
package com.imaginarycode.minecraft.redisbungee.api.tasks ;
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin ;
import com.imaginarycode.minecraft.redisbungee.api.util.RedisUtil ;
import redis.clients.jedis.Jedis ;
import redis.clients.jedis.JedisCluster ;
2022-07-26 08:40:14 +00:00
import redis.clients.jedis.Protocol ;
2022-07-26 13:47:18 +00:00
import redis.clients.jedis.UnifiedJedis ;
2022-07-26 06:58:00 +00:00
import java.io.IOException ;
import java.nio.file.Files ;
import java.nio.file.Path ;
public class InitialUtils {
2022-07-26 08:40:14 +00:00
public static void checkRedisVersion ( RedisBungeePlugin < ? > plugin ) {
new RedisTask < Void > ( plugin ) {
2022-07-26 06:58:00 +00:00
@Override
2022-07-26 13:47:18 +00:00
public Void unifiedJedisTask ( UnifiedJedis unifiedJedis ) {
2022-07-26 06:58:00 +00:00
// This is more portable than INFO <section>
2022-07-26 08:40:14 +00:00
2022-07-26 13:47:18 +00:00
String info = new String ( ( byte [ ] ) unifiedJedis . sendCommand ( Protocol . Command . INFO ) ) ;
2022-07-26 06:58:00 +00:00
for ( String s : info . split ( " \ r \ n " ) ) {
if ( s . startsWith ( " redis_version: " ) ) {
String version = s . split ( " : " ) [ 1 ] ;
plugin . logInfo ( version + " <- redis version " ) ;
if ( ! RedisUtil . isRedisVersionRight ( version ) ) {
plugin . logFatal ( " 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 " ) ;
}
2022-07-26 13:47:18 +00:00
long uuidCacheSize = unifiedJedis . hlen ( " uuid-cache " ) ;
2022-07-26 06:58:00 +00:00
if ( uuidCacheSize > 750000 ) {
2022-11-27 08:10:29 +00:00
plugin . logInfo ( " Looks like you have a really big UUID cache! Run https://github.com/ProxioDev/Brains " ) ;
2022-07-26 06:58:00 +00:00
}
break ;
}
}
2022-07-26 08:40:14 +00:00
return null ;
2022-07-26 06:58:00 +00:00
}
} . execute ( ) ;
}
public static void checkIfRecovering ( RedisBungeePlugin < ? > plugin , Path dataFolder ) {
new RedisTask < Void > ( plugin ) {
@Override
2022-07-26 13:47:18 +00:00
public Void unifiedJedisTask ( UnifiedJedis unifiedJedis ) {
2022-07-26 06:58:00 +00:00
Path crashFile = dataFolder . resolve ( " restarted_from_crash.txt " ) ;
if ( Files . exists ( crashFile ) ) {
try {
Files . delete ( crashFile ) ;
} catch ( IOException e ) {
throw new RuntimeException ( e ) ;
}
plugin . logInfo ( " crash file was deleted " ) ;
2022-07-26 13:47:18 +00:00
} else if ( unifiedJedis . hexists ( " heartbeats " , plugin . getConfiguration ( ) . getProxyId ( ) ) ) {
2022-07-26 06:58:00 +00:00
try {
2022-07-26 13:47:18 +00:00
long value = Long . parseLong ( unifiedJedis . hget ( " heartbeats " , plugin . getConfiguration ( ) . getProxyId ( ) ) ) ;
long redisTime = plugin . getRedisTime ( unifiedJedis ) ;
2022-07-26 06:58:00 +00:00
if ( redisTime < value + RedisUtil . PROXY_TIMEOUT ) {
logImposter ( plugin ) ;
throw new RuntimeException ( " Possible impostor instance! " ) ;
}
} catch ( NumberFormatException ignored ) {
}
}
return null ;
}
} . execute ( ) ;
}
private static void logImposter ( RedisBungeePlugin < ? > plugin ) {
plugin . logFatal ( " You have launched a possible impostor Velocity / Bungeecord instance. Another instance is already running. " ) ;
plugin . logFatal ( " For data consistency reasons, RedisBungee will now disable itself. " ) ;
plugin . logFatal ( " If this instance is coming up from a crash, create a file in your RedisBungee plugins directory with the name 'restarted_from_crash.txt' and RedisBungee will not perform this check. " ) ;
}
}