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
* /
2023-09-30 09:34:21 +00:00
package com.imaginarycode.minecraft.redisbungee.api.config.loaders ;
2022-07-22 08:29:39 +00:00
2022-07-22 11:12:32 +00:00
2022-07-25 12:49:57 +00:00
import com.google.common.reflect.TypeToken ;
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeeMode ;
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin ;
2023-09-30 09:34:21 +00:00
import com.imaginarycode.minecraft.redisbungee.api.config.RedisBungeeConfiguration ;
2022-07-25 15:13:25 +00:00
import com.imaginarycode.minecraft.redisbungee.api.summoners.JedisClusterSummoner ;
2022-07-26 13:47:18 +00:00
import com.imaginarycode.minecraft.redisbungee.api.summoners.JedisPooledSummoner ;
2022-07-25 12:49:57 +00:00
import com.imaginarycode.minecraft.redisbungee.api.summoners.Summoner ;
import ninja.leaping.configurate.ConfigurationNode ;
import ninja.leaping.configurate.objectmapping.ObjectMappingException ;
import ninja.leaping.configurate.yaml.YAMLConfigurationLoader ;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig ;
import redis.clients.jedis.* ;
2022-10-25 11:52:50 +00:00
import redis.clients.jedis.providers.ClusterConnectionProvider ;
2022-10-25 04:01:41 +00:00
import redis.clients.jedis.providers.PooledConnectionProvider ;
2022-07-25 12:49:57 +00:00
2022-07-22 08:29:39 +00:00
import java.io.IOException ;
import java.nio.file.Path ;
2022-07-25 12:49:57 +00:00
import java.util.* ;
2022-07-22 08:29:39 +00:00
2023-09-30 09:34:21 +00:00
public interface ConfigLoader extends GenericConfigLoader {
2022-07-25 12:49:57 +00:00
2023-09-30 09:34:21 +00:00
int CONFIG_VERSION = 2 ;
2022-07-25 12:49:57 +00:00
default void loadConfig ( RedisBungeePlugin < ? > plugin , Path dataFolder ) throws IOException {
2023-09-30 09:34:21 +00:00
Path configFile = createConfigFile ( dataFolder , " config.yml " , " config.yml " ) ;
2022-07-25 12:49:57 +00:00
final YAMLConfigurationLoader yamlConfigurationFileLoader = YAMLConfigurationLoader . builder ( ) . setPath ( configFile ) . build ( ) ;
ConfigurationNode node = yamlConfigurationFileLoader . load ( ) ;
2023-09-30 09:34:21 +00:00
if ( node . getNode ( " config-version " ) . getInt ( 0 ) ! = CONFIG_VERSION ) {
handleOldConfig ( dataFolder , " config.yml " , " config.yml " ) ;
2022-07-25 12:49:57 +00:00
node = yamlConfigurationFileLoader . load ( ) ;
}
final boolean useSSL = node . getNode ( " useSSL " ) . getBoolean ( false ) ;
2023-08-31 10:32:51 +00:00
final boolean kickWhenOnline = node . getNode ( " kick-when-online " ) . getBoolean ( true ) ;
2022-11-16 04:22:30 +00:00
String redisPassword = node . getNode ( " redis-password " ) . getString ( " " ) ;
2023-04-12 18:40:01 +00:00
String redisUsername = node . getNode ( " redis-username " ) . getString ( " " ) ;
2024-04-22 12:27:14 +00:00
String networkId = node . getNode ( " network-id " ) . getString ( " main " ) ;
2024-04-12 18:01:10 +00:00
String proxyId = node . getNode ( " proxy-id " ) . getString ( " proxy-1 " ) ;
2022-07-25 12:49:57 +00:00
final int maxConnections = node . getNode ( " max-redis-connections " ) . getInt ( 10 ) ;
List < String > exemptAddresses ;
try {
exemptAddresses = node . getNode ( " exempt-ip-addresses " ) . getList ( TypeToken . of ( String . class ) ) ;
} catch ( ObjectMappingException e ) {
exemptAddresses = Collections . emptyList ( ) ;
}
// check redis password
2022-11-16 04:22:30 +00:00
if ( ( redisPassword . isEmpty ( ) | | redisPassword . equals ( " none " ) ) ) {
2022-07-25 12:49:57 +00:00
redisPassword = null ;
2022-11-16 04:22:30 +00:00
plugin . logWarn ( " password is empty " ) ;
2022-07-26 11:14:14 +00:00
}
2023-04-12 18:40:01 +00:00
if ( ( redisUsername . isEmpty ( ) | | redisUsername . equals ( " none " ) ) ) {
redisUsername = null ;
}
2024-04-12 18:01:10 +00:00
// env var
String proxyIdFromEnv = System . getenv ( " REDISBUNGEE_PROXY_ID " ) ;
if ( proxyIdFromEnv ! = null ) {
plugin . logInfo ( " Overriding current configured proxy id {} and been set to {} by Environment variable REDISBUNGEE_PROXY_ID " , proxyId , proxyIdFromEnv ) ;
proxyId = proxyIdFromEnv ;
2022-07-25 12:49:57 +00:00
}
2024-04-22 12:27:14 +00:00
String networkIdFromEnv = System . getenv ( " REDISBUNGEE_NETWORK_ID " ) ;
if ( networkIdFromEnv ! = null ) {
plugin . logInfo ( " Overriding current configured network id {} and been set to {} by Environment variable REDISBUNGEE_NETWORK_ID " , networkId , networkIdFromEnv ) ;
networkId = networkIdFromEnv ;
}
2022-07-25 12:49:57 +00:00
// Configuration sanity checks.
if ( proxyId = = null | | proxyId . isEmpty ( ) ) {
String genId = UUID . randomUUID ( ) . toString ( ) ;
plugin . logInfo ( " Generated proxy id " + genId + " and saving it to config. " ) ;
node . getNode ( " proxy-id " ) . setValue ( genId ) ;
yamlConfigurationFileLoader . save ( node ) ;
proxyId = genId ;
plugin . logInfo ( " proxy id was generated: " + proxyId ) ;
} else {
plugin . logInfo ( " Loaded proxy id " + proxyId ) ;
}
2024-04-22 12:27:14 +00:00
if ( networkId . isEmpty ( ) ) {
networkId = " main " ;
plugin . logWarn ( " network id was empty and replaced with 'main' " ) ;
}
plugin . logInfo ( " Loaded network id " + networkId ) ;
2023-08-31 10:26:21 +00:00
boolean reconnectToLastServer = node . getNode ( " reconnect-to-last-server " ) . getBoolean ( ) ;
boolean handleMotd = node . getNode ( " handle-motd " ) . getBoolean ( true ) ;
2024-04-12 18:01:10 +00:00
plugin . logInfo ( " handle reconnect to last server: {} " , reconnectToLastServer ) ;
plugin . logInfo ( " handle motd: {} " , handleMotd ) ;
2023-08-31 10:26:21 +00:00
2024-04-18 08:48:00 +00:00
// commands
2024-04-18 10:23:17 +00:00
boolean redisBungeeEnabled = node . getNode ( " commands " , " redisbungee " , " enabled " ) . getBoolean ( true ) ;
boolean redisBungeeLegacyEnabled = node . getNode ( " commands " , " redisbungee-legacy " , " enabled " ) . getBoolean ( false ) ;
2024-04-18 08:48:00 +00:00
2024-04-18 10:23:17 +00:00
boolean glistEnabled = node . getNode ( " commands " , " redisbungee-legacy " , " subcommands " , " glist " , " enabled " ) . getBoolean ( false ) ;
boolean findEnabled = node . getNode ( " commands " , " redisbungee-legacy " , " subcommands " , " find " , " enabled " ) . getBoolean ( false ) ;
boolean lastseenEnabled = node . getNode ( " commands " , " redisbungee-legacy " , " subcommands " , " lastseen " , " enabled " ) . getBoolean ( false ) ;
boolean ipEnabled = node . getNode ( " commands " , " redisbungee-legacy " , " subcommands " , " ip " , " enabled " ) . getBoolean ( false ) ;
boolean pproxyEnabled = node . getNode ( " commands " , " redisbungee-legacy " , " subcommands " , " pproxy " , " enabled " ) . getBoolean ( false ) ;
boolean sendToAllEnabled = node . getNode ( " commands " , " redisbungee-legacy " , " subcommands " , " sendtoall " , " enabled " ) . getBoolean ( false ) ;
boolean serverIdEnabled = node . getNode ( " commands " , " redisbungee-legacy " , " subcommands " , " serverid " , " enabled " ) . getBoolean ( false ) ;
boolean serverIdsEnabled = node . getNode ( " commands " , " redisbungee-legacy " , " subcommands " , " serverids " , " enabled " ) . getBoolean ( false ) ;
boolean pListEnabled = node . getNode ( " commands " , " redisbungee-legacy " , " subcommands " , " plist " , " enabled " ) . getBoolean ( false ) ;
2024-04-18 08:48:00 +00:00
2024-04-18 10:23:17 +00:00
boolean installGlist = node . getNode ( " commands " , " redisbungee-legacy " , " subcommands " , " glist " , " install " ) . getBoolean ( false ) ;
boolean installFind = node . getNode ( " commands " , " redisbungee-legacy " , " subcommands " , " find " , " install " ) . getBoolean ( false ) ;
boolean installLastseen = node . getNode ( " commands " , " redisbungee-legacy " , " subcommands " , " lastseen " , " install " ) . getBoolean ( false ) ;
boolean installIp = node . getNode ( " commands " , " redisbungee-legacy " , " subcommands " , " ip " , " install " ) . getBoolean ( false ) ;
boolean installPproxy = node . getNode ( " commands " , " redisbungee-legacy " , " subcommands " , " pproxy " , " install " ) . getBoolean ( false ) ;
boolean installSendToAll = node . getNode ( " commands " , " redisbungee-legacy " , " subcommands " , " sendtoall " , " install " ) . getBoolean ( false ) ;
boolean installServerid = node . getNode ( " commands " , " redisbungee-legacy " , " subcommands " , " serverid " , " install " ) . getBoolean ( false ) ;
boolean installServerIds = node . getNode ( " commands " , " redisbungee-legacy " , " subcommands " , " serverids " , " install " ) . getBoolean ( false ) ;
boolean installPlist = node . getNode ( " commands " , " redisbungee-legacy " , " subcommands " , " plist " , " install " ) . getBoolean ( false ) ;
2024-04-18 08:48:00 +00:00
2024-04-22 12:27:14 +00:00
RedisBungeeConfiguration configuration = new RedisBungeeConfiguration ( networkId , proxyId , exemptAddresses , kickWhenOnline , reconnectToLastServer , handleMotd , new RedisBungeeConfiguration . CommandsConfiguration (
2024-04-18 08:48:00 +00:00
redisBungeeEnabled , redisBungeeLegacyEnabled ,
new RedisBungeeConfiguration . LegacySubCommandsConfiguration (
findEnabled , glistEnabled , ipEnabled ,
lastseenEnabled , pListEnabled , pproxyEnabled ,
sendToAllEnabled , serverIdEnabled , serverIdsEnabled ,
installFind , installGlist , installIp ,
installLastseen , installPlist , installPproxy ,
installSendToAll , installServerid , installServerIds )
) ) ;
2022-07-25 12:49:57 +00:00
Summoner < ? > summoner ;
RedisBungeeMode redisBungeeMode ;
2024-04-12 18:01:10 +00:00
if ( useSSL ) {
plugin . logInfo ( " Using ssl " ) ;
}
2022-07-25 12:49:57 +00:00
if ( node . getNode ( " cluster-mode-enabled " ) . getBoolean ( false ) ) {
plugin . logInfo ( " RedisBungee MODE: CLUSTER " ) ;
Set < HostAndPort > hostAndPortSet = new HashSet < > ( ) ;
GenericObjectPoolConfig < Connection > poolConfig = new GenericObjectPoolConfig < > ( ) ;
poolConfig . setMaxTotal ( maxConnections ) ;
2022-07-26 13:51:41 +00:00
poolConfig . setBlockWhenExhausted ( true ) ;
2022-07-25 12:49:57 +00:00
node . getNode ( " redis-cluster-servers " ) . getChildrenList ( ) . forEach ( ( childNode ) - > {
Map < Object , ? extends ConfigurationNode > hostAndPort = childNode . getChildrenMap ( ) ;
String host = hostAndPort . get ( " host " ) . getString ( ) ;
int port = hostAndPort . get ( " port " ) . getInt ( ) ;
hostAndPortSet . add ( new HostAndPort ( host , port ) ) ;
} ) ;
plugin . logInfo ( hostAndPortSet . size ( ) + " cluster nodes were specified " ) ;
if ( hostAndPortSet . isEmpty ( ) ) {
throw new RuntimeException ( " No redis cluster servers specified " ) ;
}
2023-04-12 18:40:01 +00:00
summoner = new JedisClusterSummoner ( new ClusterConnectionProvider ( hostAndPortSet , DefaultJedisClientConfig . builder ( ) . user ( redisUsername ) . password ( redisPassword ) . ssl ( useSSL ) . socketTimeoutMillis ( 5000 ) . timeoutMillis ( 10000 ) . build ( ) , poolConfig ) ) ;
2022-07-25 12:49:57 +00:00
redisBungeeMode = RedisBungeeMode . CLUSTER ;
} else {
plugin . logInfo ( " RedisBungee MODE: SINGLE " ) ;
final String redisServer = node . getNode ( " redis-server " ) . getString ( " 127.0.0.1 " ) ;
final int redisPort = node . getNode ( " redis-port " ) . getInt ( 6379 ) ;
if ( redisServer ! = null & & redisServer . isEmpty ( ) ) {
throw new RuntimeException ( " No redis server specified " ) ;
}
2022-07-26 13:47:18 +00:00
JedisPool jedisPool = null ;
2023-08-31 09:00:19 +00:00
if ( node . getNode ( " enable-jedis-pool-compatibility " ) . getBoolean ( false ) ) {
2022-07-26 13:47:18 +00:00
JedisPoolConfig config = new JedisPoolConfig ( ) ;
2022-07-26 13:51:41 +00:00
config . setMaxTotal ( node . getNode ( " compatibility-max-connections " ) . getInt ( 3 ) ) ;
config . setBlockWhenExhausted ( true ) ;
2023-04-12 18:40:01 +00:00
jedisPool = new JedisPool ( config , redisServer , redisPort , 5000 , redisUsername , redisPassword , useSSL ) ;
2022-07-26 13:55:16 +00:00
plugin . logInfo ( " Compatibility JedisPool was created " ) ;
2022-07-26 13:47:18 +00:00
}
GenericObjectPoolConfig < Connection > poolConfig = new GenericObjectPoolConfig < > ( ) ;
poolConfig . setMaxTotal ( maxConnections ) ;
2022-07-26 13:51:41 +00:00
poolConfig . setBlockWhenExhausted ( true ) ;
2023-04-12 18:40:01 +00:00
summoner = new JedisPooledSummoner ( new PooledConnectionProvider ( new ConnectionFactory ( new HostAndPort ( redisServer , redisPort ) , DefaultJedisClientConfig . builder ( ) . user ( redisUsername ) . timeoutMillis ( 5000 ) . ssl ( useSSL ) . password ( redisPassword ) . build ( ) ) , poolConfig ) , jedisPool ) ;
2022-07-25 12:49:57 +00:00
redisBungeeMode = RedisBungeeMode . SINGLE ;
}
plugin . logInfo ( " Successfully connected to Redis. " ) ;
onConfigLoad ( configuration , summoner , redisBungeeMode ) ;
}
void onConfigLoad ( RedisBungeeConfiguration configuration , Summoner < ? > summoner , RedisBungeeMode mode ) ;
2022-07-22 08:29:39 +00:00
}