2018-07-23 05:44:20 +02:00
package net.ME1312.SubServers.Client.Sponge ;
import com.google.gson.Gson ;
import com.google.inject.Inject ;
2019-08-31 19:26:37 +02:00
import net.ME1312.SubData.Client.DataClient ;
2019-05-12 03:22:18 +02:00
import net.ME1312.SubData.Client.Encryption.AES ;
import net.ME1312.SubData.Client.Encryption.RSA ;
2019-08-31 19:26:37 +02:00
import net.ME1312.SubData.Client.Library.DisconnectReason ;
2019-05-12 03:22:18 +02:00
import net.ME1312.SubData.Client.SubDataClient ;
2018-07-23 05:44:20 +02:00
import net.ME1312.SubServers.Client.Sponge.Graphic.UIHandler ;
2019-05-12 03:22:18 +02:00
import net.ME1312.Galaxi.Library.Config.YAMLConfig ;
import net.ME1312.Galaxi.Library.Map.ObjectMap ;
2018-07-23 05:44:20 +02:00
import net.ME1312.SubServers.Client.Sponge.Library.Metrics ;
2019-05-12 03:22:18 +02:00
import net.ME1312.Galaxi.Library.NamedContainer ;
import net.ME1312.Galaxi.Library.UniversalFile ;
import net.ME1312.Galaxi.Library.Util ;
import net.ME1312.Galaxi.Library.Version.Version ;
import net.ME1312.SubServers.Client.Sponge.Library.Updates.ConfigUpdater ;
import net.ME1312.SubServers.Client.Sponge.Network.SubProtocol ;
2018-07-23 05:44:20 +02:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
import org.spongepowered.api.Game ;
import org.spongepowered.api.Sponge ;
import org.spongepowered.api.config.ConfigDir ;
import org.spongepowered.api.event.Listener ;
import org.spongepowered.api.event.game.state.GameInitializationEvent ;
import org.spongepowered.api.event.game.state.GamePreInitializationEvent ;
import org.spongepowered.api.event.game.state.GameStoppingEvent ;
import org.spongepowered.api.plugin.Plugin ;
import org.spongepowered.api.plugin.PluginContainer ;
import java.io.* ;
import java.lang.reflect.InvocationTargetException ;
import java.net.InetAddress ;
import java.net.URL ;
import java.nio.charset.Charset ;
import java.nio.file.Files ;
import java.nio.file.StandardCopyOption ;
2019-05-12 03:22:18 +02:00
import java.util.* ;
2018-07-23 05:44:20 +02:00
import java.util.concurrent.TimeUnit ;
/ * *
* SubServers Client Plugin Class
* /
2019-08-31 19:26:37 +02:00
@Plugin ( id = " subservers-client-sponge " , name = " SubServers-Client-Sponge " , authors = " ME1312 " , version = " 2.14.4a " , url = " https://github.com/ME1312/SubServers-2 " , description = " Take control of the server manager — from your servers " )
2018-07-23 05:44:20 +02:00
public final class SubPlugin {
2019-05-17 00:36:41 +02:00
HashMap < Integer , SubDataClient > subdata = new HashMap < Integer , SubDataClient > ( ) ;
NamedContainer < Long , Map < String , Map < String , String > > > lang = null ;
2018-07-23 05:44:20 +02:00
public YAMLConfig config ;
2019-05-12 03:22:18 +02:00
public SubProtocol subprotocol ;
2018-07-23 05:44:20 +02:00
@ConfigDir ( sharedRoot = false )
@Inject public File dir ;
2019-05-12 03:22:18 +02:00
public Logger log = LoggerFactory . getLogger ( " SubServers " ) ;
2018-07-23 05:44:20 +02:00
public UIHandler gui = null ;
public Version version ;
public SubAPI api ;
@Inject public PluginContainer plugin ;
@Inject public Game game ;
2019-08-31 19:26:37 +02:00
private long resetDate = 0 ;
2019-05-12 03:22:18 +02:00
private boolean reconnect = false ;
2018-07-23 05:44:20 +02:00
@Listener
public void setup ( GamePreInitializationEvent event ) {
if ( plugin . getVersion ( ) . isPresent ( ) ) {
2018-07-29 20:39:42 +02:00
version = Version . fromString ( plugin . getVersion ( ) . get ( ) ) ;
2018-07-27 21:36:51 +02:00
} else version = new Version ( " Custom " ) ;
2019-05-12 03:22:18 +02:00
subdata . put ( 0 , null ) ;
2018-07-23 05:44:20 +02:00
}
/ * *
* Enable Plugin
* /
@Listener
@SuppressWarnings ( " unchecked " )
public void enable ( GameInitializationEvent event ) {
api = new SubAPI ( this ) ;
try {
2019-05-12 03:22:18 +02:00
log . info ( " Loading SubServers.Client.Sponge v " + version . toString ( ) + " Libraries (for Minecraft " + api . getGameVersion ( ) + " ) " ) ;
2018-07-23 05:44:20 +02:00
dir . mkdirs ( ) ;
if ( new UniversalFile ( dir . getParentFile ( ) , " SubServers-Client:config.yml " ) . exists ( ) ) {
Files . move ( new UniversalFile ( dir . getParentFile ( ) , " SubServers-Client:config.yml " ) . toPath ( ) , new UniversalFile ( dir , " config.yml " ) . toPath ( ) , StandardCopyOption . REPLACE_EXISTING ) ;
Util . deleteDirectory ( new UniversalFile ( dir . getParentFile ( ) , " SubServers-Client " ) ) ;
}
2019-05-12 03:22:18 +02:00
ConfigUpdater . updateConfig ( new UniversalFile ( dir , " config.yml " ) ) ;
2018-07-23 05:44:20 +02:00
config = new YAMLConfig ( new UniversalFile ( dir , " config.yml " ) ) ;
2019-05-12 03:22:18 +02:00
if ( new UniversalFile ( new File ( System . getProperty ( " user.dir " ) ) , " subdata.json " ) . exists ( ) ) {
FileReader reader = new FileReader ( new UniversalFile ( new File ( System . getProperty ( " user.dir " ) ) , " subdata.json " ) ) ;
config . get ( ) . getMap ( " Settings " ) . set ( " SubData " , new ObjectMap < String > ( new Gson ( ) . fromJson ( Util . readAll ( reader ) , Map . class ) ) ) ;
2018-07-23 05:44:20 +02:00
config . save ( ) ;
reader . close ( ) ;
2019-05-12 03:22:18 +02:00
new UniversalFile ( new File ( System . getProperty ( " user.dir " ) ) , " subdata.json " ) . delete ( ) ;
}
if ( new UniversalFile ( new File ( System . getProperty ( " user.dir " ) ) , " subdata.rsa.key " ) . exists ( ) ) {
2019-05-27 21:33:52 +02:00
if ( new UniversalFile ( dir , " subdata.rsa.key " ) . exists ( ) ) new UniversalFile ( dir , " subdata.rsa.key " ) . delete ( ) ;
2019-05-12 03:22:18 +02:00
Files . move ( new UniversalFile ( new File ( System . getProperty ( " user.dir " ) ) , " subdata.rsa.key " ) . toPath ( ) , new UniversalFile ( dir , " subdata.rsa.key " ) . toPath ( ) ) ;
2018-07-23 05:44:20 +02:00
}
2019-05-12 03:22:18 +02:00
subprotocol = SubProtocol . get ( ) ;
2018-07-23 05:44:20 +02:00
reload ( false ) ;
2019-05-12 03:22:18 +02:00
if ( ! config . get ( ) . getMap ( " Settings " ) . getBoolean ( " API-Only-Mode " , false ) ) {
2019-04-18 16:02:09 +02:00
//gui = new InternalUIHandler(this);
Sponge . getCommandManager ( ) . register ( plugin , new SubCommand ( this ) . spec ( ) , " sub " , " subserver " , " subservers " ) ;
}
2018-07-23 05:44:20 +02:00
new Metrics ( this ) ;
game . getScheduler ( ) . createTaskBuilder ( ) . async ( ) . execute ( ( ) - > {
try {
2019-05-12 03:22:18 +02:00
ObjectMap < String > tags = new ObjectMap < String > ( new Gson ( ) . fromJson ( " { \" tags \" : " + Util . readAll ( new BufferedReader ( new InputStreamReader ( new URL ( " https://api.github.com/repos/ME1312/SubServers-2/git/refs/tags " ) . openStream ( ) , Charset . forName ( " UTF-8 " ) ) ) ) + '}' , Map . class ) ) ;
2018-07-29 20:39:42 +02:00
List < Version > versions = new LinkedList < Version > ( ) ;
2018-07-23 05:44:20 +02:00
Version updversion = version ;
int updcount = 0 ;
2019-05-12 03:22:18 +02:00
for ( ObjectMap < String > tag : tags . getMapList ( " tags " ) ) versions . add ( Version . fromString ( tag . getString ( " ref " ) . substring ( 10 ) ) ) ;
2018-07-29 20:39:42 +02:00
Collections . sort ( versions ) ;
for ( Version version : versions ) {
2018-07-29 05:02:47 +02:00
if ( version . compareTo ( updversion ) > 0 ) {
2018-07-27 21:36:51 +02:00
updversion = version ;
updcount + + ;
2018-07-23 05:44:20 +02:00
}
}
2019-05-12 03:22:18 +02:00
if ( updcount > 0 ) log . info ( " SubServers.Client.Sponge v " + updversion + " is available. You are " + updcount + " version " + ( ( updcount = = 1 ) ? " " : " s " ) + " behind. " ) ;
2018-07-23 05:44:20 +02:00
} catch ( Exception e ) { }
} ) . delay ( 0 , TimeUnit . MILLISECONDS ) . interval ( 2 , TimeUnit . DAYS ) . submit ( plugin ) ;
} catch ( IOException e ) {
e . printStackTrace ( ) ;
}
}
2019-05-12 03:22:18 +02:00
2018-07-23 05:44:20 +02:00
public void reload ( boolean notifyPlugins ) throws IOException {
2019-05-12 03:22:18 +02:00
reconnect = false ;
2019-08-31 19:26:37 +02:00
resetDate = Calendar . getInstance ( ) . getTime ( ) . getTime ( ) ;
2019-05-12 03:22:18 +02:00
ArrayList < SubDataClient > tmp = new ArrayList < SubDataClient > ( ) ;
tmp . addAll ( subdata . values ( ) ) ;
for ( SubDataClient client : tmp ) if ( client ! = null ) {
client . close ( ) ;
Util . isException ( client : : waitFor ) ;
}
subdata . clear ( ) ;
subdata . put ( 0 , null ) ;
2018-07-23 05:44:20 +02:00
2019-05-12 03:22:18 +02:00
ConfigUpdater . updateConfig ( new UniversalFile ( dir , " config.yml " ) ) ;
2018-07-23 05:44:20 +02:00
config . reload ( ) ;
2019-05-12 03:22:18 +02:00
subprotocol . unregisterCipher ( " AES " ) ;
subprotocol . unregisterCipher ( " AES-128 " ) ;
subprotocol . unregisterCipher ( " AES-192 " ) ;
subprotocol . unregisterCipher ( " AES-256 " ) ;
subprotocol . unregisterCipher ( " RSA " ) ;
api . name = config . get ( ) . getMap ( " Settings " ) . getMap ( " SubData " ) . getString ( " Name " , null ) ;
Logger log = LoggerFactory . getLogger ( " SubData " ) ;
if ( config . get ( ) . getMap ( " Settings " ) . getMap ( " SubData " ) . getRawString ( " Password " , " " ) . length ( ) > 0 ) {
subprotocol . registerCipher ( " AES " , new AES ( 128 , config . get ( ) . getMap ( " Settings " ) . getMap ( " SubData " ) . getRawString ( " Password " ) ) ) ;
subprotocol . registerCipher ( " AES-128 " , new AES ( 128 , config . get ( ) . getMap ( " Settings " ) . getMap ( " SubData " ) . getRawString ( " Password " ) ) ) ;
subprotocol . registerCipher ( " AES-192 " , new AES ( 192 , config . get ( ) . getMap ( " Settings " ) . getMap ( " SubData " ) . getRawString ( " Password " ) ) ) ;
subprotocol . registerCipher ( " AES-256 " , new AES ( 256 , config . get ( ) . getMap ( " Settings " ) . getMap ( " SubData " ) . getRawString ( " Password " ) ) ) ;
log . info ( " AES Encryption Available " ) ;
}
if ( new UniversalFile ( dir , " subdata.rsa.key " ) . exists ( ) ) {
try {
subprotocol . registerCipher ( " RSA " , new RSA ( new UniversalFile ( dir , " subdata.rsa.key " ) ) ) ;
log . info ( " RSA Encryption Available " ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
2018-07-23 05:44:20 +02:00
}
}
2019-05-12 03:22:18 +02:00
reconnect = true ;
log . info ( " " ) ;
2019-08-31 19:26:37 +02:00
log . info ( " Connecting to / " + config . get ( ) . getMap ( " Settings " ) . getMap ( " SubData " ) . getRawString ( " Address " , " 127.0.0.1:4391 " ) ) ;
connect ( null ) ;
2018-07-23 05:44:20 +02:00
if ( notifyPlugins ) {
List < Runnable > listeners = api . reloadListeners ;
if ( listeners . size ( ) > 0 ) {
for ( Object obj : listeners ) {
try {
( ( Runnable ) obj ) . run ( ) ;
} catch ( Throwable e ) {
new InvocationTargetException ( e , " Problem reloading plugin " ) . printStackTrace ( ) ;
}
}
}
}
}
2019-08-31 19:26:37 +02:00
private void connect ( NamedContainer < DisconnectReason , DataClient > disconnect ) throws IOException {
int reconnect = config . get ( ) . getMap ( " Settings " ) . getMap ( " SubData " ) . getInt ( " Reconnect " , 30 ) ;
if ( disconnect = = null | | ( this . reconnect & & reconnect > 0 & & disconnect . name ( ) ! = DisconnectReason . PROTOCOL_MISMATCH & & disconnect . name ( ) ! = DisconnectReason . ENCRYPTION_MISMATCH ) ) {
long reset = resetDate ;
Logger log = LoggerFactory . getLogger ( " SubData " ) ;
Sponge . getScheduler ( ) . createTaskBuilder ( ) . async ( ) . execute ( new Runnable ( ) {
@Override
public void run ( ) {
try {
if ( reset = = resetDate & & subdata . getOrDefault ( 0 , null ) = = null )
subdata . put ( 0 , subprotocol . open ( ( config . get ( ) . getMap ( " Settings " ) . getMap ( " SubData " ) . getRawString ( " Address " , " 127.0.0.1:4391 " ) . split ( " : " ) [ 0 ] . equals ( " 0.0.0.0 " ) ) ?
null : InetAddress . getByName ( config . get ( ) . getMap ( " Settings " ) . getMap ( " SubData " ) . getRawString ( " Address " , " 127.0.0.1:4391 " ) . split ( " : " ) [ 0 ] ) ,
Integer . parseInt ( config . get ( ) . getMap ( " Settings " ) . getMap ( " SubData " ) . getRawString ( " Address " , " 127.0.0.1:4391 " ) . split ( " : " ) [ 1 ] ) ) ) ;
} catch ( IOException e ) {
log . info ( " Connection was unsuccessful, retrying in " + reconnect + " seconds " ) ;
Sponge . getScheduler ( ) . createTaskBuilder ( ) . async ( ) . execute ( this ) . delay ( reconnect , TimeUnit . SECONDS ) . submit ( plugin ) ;
}
}
} ) . delay ( ( disconnect = = null ) ? 0 : reconnect , TimeUnit . SECONDS ) . submit ( plugin ) ;
}
2019-05-12 03:22:18 +02:00
}
2018-07-23 05:44:20 +02:00
/ * *
* Disable Plugin
* /
@Listener
public void disable ( GameStoppingEvent event ) {
if ( subdata ! = null ) try {
2019-05-12 03:22:18 +02:00
reconnect = false ;
ArrayList < SubDataClient > temp = new ArrayList < SubDataClient > ( ) ;
temp . addAll ( subdata . values ( ) ) ;
for ( SubDataClient client : temp ) if ( client ! = null ) {
client . close ( ) ;
client . waitFor ( ) ;
}
subdata . clear ( ) ;
subdata . put ( 0 , null ) ;
} catch ( IOException | InterruptedException e ) {
2018-07-23 05:44:20 +02:00
e . printStackTrace ( ) ;
}
}
}