2017-01-30 21:22:36 +01:00
package net.ME1312.SubServers.Host ;
2018-09-09 00:06:21 +02:00
import net.ME1312.Galaxi.Engine.GalaxiEngine ;
2021-02-21 10:40:43 +01:00
import net.ME1312.Galaxi.Event.Engine.GalaxiReloadEvent ;
2018-09-09 00:06:21 +02:00
import net.ME1312.Galaxi.Library.Config.YAMLConfig ;
import net.ME1312.Galaxi.Library.Config.YAMLSection ;
2020-11-14 08:07:25 +01:00
import net.ME1312.Galaxi.Library.Container.Pair ;
2021-10-24 06:14:07 +02:00
import net.ME1312.Galaxi.Library.Directories ;
2020-11-16 21:34:59 +01:00
import net.ME1312.Galaxi.Library.Map.ObjectMap ;
2020-06-12 07:45:49 +02:00
import net.ME1312.Galaxi.Library.Platform ;
2018-09-09 00:06:21 +02:00
import net.ME1312.Galaxi.Library.Util ;
import net.ME1312.Galaxi.Library.Version.Version ;
import net.ME1312.Galaxi.Library.Version.VersionType ;
2021-10-24 06:14:07 +02:00
import net.ME1312.Galaxi.Log.Logger ;
2019-05-17 00:36:41 +02:00
import net.ME1312.Galaxi.Plugin.App ;
2018-09-09 00:06:21 +02:00
import net.ME1312.Galaxi.Plugin.PluginInfo ;
2019-08-31 19:26:37 +02:00
import net.ME1312.SubData.Client.DataClient ;
2019-05-13 05:28:27 +02:00
import net.ME1312.SubData.Client.Encryption.AES ;
2020-06-12 07:45:49 +02:00
import net.ME1312.SubData.Client.Encryption.DHE ;
2019-05-13 05:28:27 +02:00
import net.ME1312.SubData.Client.Encryption.RSA ;
2019-08-31 19:26:37 +02:00
import net.ME1312.SubData.Client.Library.DisconnectReason ;
2019-05-13 05:28:27 +02:00
import net.ME1312.SubData.Client.SubDataClient ;
2019-08-30 21:44:54 +02:00
import net.ME1312.SubServers.Host.Executable.SubCreatorImpl ;
import net.ME1312.SubServers.Host.Executable.SubLoggerImpl ;
2019-08-30 02:01:52 +02:00
import net.ME1312.SubServers.Host.Executable.SubServerImpl ;
2020-06-12 07:45:49 +02:00
import net.ME1312.SubServers.Host.Library.ConfigUpdater ;
2020-11-16 21:34:59 +01:00
import net.ME1312.SubServers.Host.Library.Metrics ;
2019-05-13 05:28:27 +02:00
import net.ME1312.SubServers.Host.Network.SubProtocol ;
2020-11-16 21:34:59 +01:00
import com.dosse.upnp.UPnP ;
2017-04-01 18:50:09 +02:00
import org.json.JSONObject ;
2017-01-30 21:22:36 +01:00
2017-04-14 04:39:51 +02:00
import java.io.* ;
2017-01-30 21:22:36 +01:00
import java.net.InetAddress ;
import java.net.URL ;
2017-08-18 11:58:06 +02:00
import java.nio.charset.Charset ;
2017-01-30 21:22:36 +01:00
import java.nio.file.Files ;
import java.util.* ;
2017-04-01 18:50:09 +02:00
import java.util.concurrent.TimeUnit ;
2018-09-09 00:06:21 +02:00
import java.util.jar.Manifest ;
2017-01-30 21:22:36 +01:00
/ * *
* SubServers . Host Main Class
* /
2021-12-01 05:55:50 +01:00
@App ( name = " SubServers.Host " , version = " 2.18a " , authors = " ME1312 " , website = " https://github.com/ME1312/SubServers-2 " , description = " Host subservers on separate machines " )
2017-04-16 19:02:14 +02:00
public final class ExHost {
2019-05-17 00:36:41 +02:00
HashMap < Integer , SubDataClient > subdata = new HashMap < Integer , SubDataClient > ( ) ;
2020-11-14 08:07:25 +01:00
Pair < Long , Map < String , Map < String , String > > > lang = null ;
2020-06-20 03:00:50 +02:00
public HashMap < String , SubCreatorImpl . ServerTemplate > templatesR = new HashMap < String , SubCreatorImpl . ServerTemplate > ( ) ;
2019-08-30 21:44:54 +02:00
public HashMap < String , SubCreatorImpl . ServerTemplate > templates = new HashMap < String , SubCreatorImpl . ServerTemplate > ( ) ;
2019-08-30 02:01:52 +02:00
public HashMap < String , SubServerImpl > servers = new HashMap < String , SubServerImpl > ( ) ;
2019-08-30 21:44:54 +02:00
public SubCreatorImpl creator ;
2017-04-01 07:37:48 +02:00
2018-07-23 05:44:20 +02:00
public Logger log ;
2018-09-09 00:06:21 +02:00
public PluginInfo info ;
public GalaxiEngine engine ;
2017-01-30 21:22:36 +01:00
public YAMLConfig config ;
2019-05-13 05:28:27 +02:00
public ObjectMap < String > host = null ;
public SubProtocol subprotocol ;
2017-01-30 21:22:36 +01:00
2018-06-02 21:02:03 +02:00
public final SubAPI api = new SubAPI ( this ) ;
2017-01-30 21:22:36 +01:00
2019-08-31 19:26:37 +02:00
private long resetDate = 0 ;
2019-05-13 05:28:27 +02:00
private boolean reconnect = true ;
2017-12-06 23:34:47 +01:00
private boolean running = false ;
2017-01-30 21:22:36 +01:00
2017-02-08 01:39:18 +01:00
/ * *
* SubServers . Host Launch
*
* @param args Args
* @throws Exception
* /
2018-09-09 00:06:21 +02:00
static void main ( String [ ] args ) throws Exception {
2018-01-12 22:56:22 +01:00
if ( System . getProperty ( " RM.subservers " , " true " ) . equalsIgnoreCase ( " true " ) ) {
2018-06-09 18:19:25 +02:00
joptsimple . OptionParser parser = new joptsimple . OptionParser ( ) ;
parser . allowsUnrecognizedOptions ( ) ;
parser . accepts ( " v " ) ;
parser . accepts ( " version " ) ;
parser . accepts ( " noconsole " ) ;
joptsimple . OptionSet options = parser . parse ( args ) ;
if ( options . has ( " version " ) | | options . has ( " v " ) ) {
2019-05-17 00:36:41 +02:00
Version galaxi = Version . fromString ( GalaxiEngine . class . getAnnotation ( App . class ) . version ( ) ) ;
Version subservers = Version . fromString ( ExHost . class . getAnnotation ( App . class ) . version ( ) ) ;
2018-09-09 00:06:21 +02:00
Version galaxibuild = null ;
Version subserversbuild = null ;
2018-07-17 09:10:17 +02:00
try {
2018-09-09 00:06:21 +02:00
Manifest manifest = new Manifest ( GalaxiEngine . class . getResourceAsStream ( " /META-INF/GalaxiEngine.MF " ) ) ;
if ( manifest . getMainAttributes ( ) . getValue ( " Implementation-Version " ) ! = null & & manifest . getMainAttributes ( ) . getValue ( " Implementation-Version " ) . length ( ) > 0 )
galaxibuild = new Version ( manifest . getMainAttributes ( ) . getValue ( " Implementation-Version " ) ) ;
} catch ( Exception e ) { } try {
2019-01-15 00:28:18 +01:00
if ( Util . reflect ( Version . class . getDeclaredField ( " type " ) , subservers ) ! = VersionType . SNAPSHOT & & ExHost . class . getPackage ( ) . getSpecificationTitle ( ) ! = null )
2018-09-09 00:06:21 +02:00
subserversbuild = new Version ( ExHost . class . getPackage ( ) . getSpecificationTitle ( ) ) ;
2018-07-17 09:10:17 +02:00
} catch ( Exception e ) { }
2018-06-09 18:19:25 +02:00
System . out . println ( " " ) ;
2021-02-13 19:43:09 +01:00
System . out . println ( Platform . getSystemName ( ) + ' ' + Platform . getSystemVersion ( ) + ( ( Platform . getSystemBuild ( ) ! = null ) ? " ( " + Platform . getSystemBuild ( ) + ')' : " " ) + ( ( ! Platform . getSystemArchitecture ( ) . equals ( " unknown " ) ) ? " [ " + Platform . getSystemArchitecture ( ) + ']' : " " ) + ',' ) ;
2020-06-12 07:45:49 +02:00
System . out . println ( " Java " + Platform . getJavaVersion ( ) + ( ( ! Platform . getJavaArchitecture ( ) . equals ( " unknown " ) ) ? " [ " + Platform . getJavaArchitecture ( ) + ']' : " " ) + ',' ) ;
2019-05-17 00:36:41 +02:00
System . out . println ( GalaxiEngine . class . getAnnotation ( App . class ) . name ( ) + " v " + galaxi . toExtendedString ( ) + ( ( galaxibuild ! = null ) ? " ( " + galaxibuild + ')' : " " ) + ',' ) ;
System . out . println ( ExHost . class . getAnnotation ( App . class ) . name ( ) + " v " + subservers . toExtendedString ( ) + ( ( subserversbuild ! = null ) ? " ( " + subserversbuild + ')' : " " ) ) ;
2018-06-09 18:19:25 +02:00
System . out . println ( " " ) ;
} else {
new ExHost ( options ) ;
}
2018-01-12 22:56:22 +01:00
} else {
2020-06-12 07:45:49 +02:00
System . out . println ( " >> SubServers' hosting capabilities have been disallowed on this machine " ) ;
2018-01-12 22:56:22 +01:00
System . out . println ( " >> Check with your provider for more information " ) ;
System . exit ( 1 ) ;
}
2017-01-30 21:22:36 +01:00
}
2018-01-21 21:45:27 +01:00
2018-06-09 18:19:25 +02:00
private ExHost ( joptsimple . OptionSet options ) {
2017-01-30 21:22:36 +01:00
log = new Logger ( " SubServers " ) ;
2018-09-09 00:06:21 +02:00
2017-01-30 21:22:36 +01:00
try {
2021-02-21 10:40:43 +01:00
info = PluginInfo . load ( this ) ;
2018-09-09 00:06:21 +02:00
info . setLogger ( log ) ;
2021-02-13 06:04:20 +01:00
if ( ExHost . class . getPackage ( ) . getSpecificationTitle ( ) ! = null ) info . setBuild ( new Version ( ExHost . class . getPackage ( ) . getSpecificationTitle ( ) ) ) ;
2018-10-24 07:37:55 +02:00
info . setIcon ( ExHost . class . getResourceAsStream ( " /net/ME1312/SubServers/Host/Library/Files/icon.png " ) ) ;
2018-09-09 00:06:21 +02:00
engine = GalaxiEngine . init ( info ) ;
log . info . println ( " Loading SubServers.Host v " + info . getVersion ( ) . toString ( ) + " Libraries " ) ;
2019-05-13 05:28:27 +02:00
2021-10-24 06:14:07 +02:00
ConfigUpdater . updateConfig ( new File ( engine . getRuntimeDirectory ( ) , " config.yml " ) ) ;
config = new YAMLConfig ( new File ( engine . getRuntimeDirectory ( ) , " config.yml " ) ) ;
2017-04-01 07:37:48 +02:00
2021-10-24 06:14:07 +02:00
if ( ! ( new File ( engine . getRuntimeDirectory ( ) , " Templates " ) . exists ( ) ) ) {
new File ( engine . getRuntimeDirectory ( ) , " Templates " ) . mkdirs ( ) ;
2019-04-29 17:05:06 +02:00
log . info . println ( " Created ./Templates/ " ) ;
2018-01-21 21:45:27 +01:00
}
2021-10-24 06:14:07 +02:00
if ( new File ( engine . getRuntimeDirectory ( ) , " Recently Deleted " ) . exists ( ) ) {
int f = new File ( engine . getRuntimeDirectory ( ) , " Recently Deleted " ) . listFiles ( ) . length ;
for ( File file : new File ( engine . getRuntimeDirectory ( ) , " Recently Deleted " ) . listFiles ( ) ) {
2017-04-01 18:50:09 +02:00
try {
if ( file . isDirectory ( ) ) {
2021-10-24 06:14:07 +02:00
if ( new File ( engine . getRuntimeDirectory ( ) , " Recently Deleted/ " + file . getName ( ) + " /info.json " ) . exists ( ) ) {
FileReader reader = new FileReader ( new File ( engine . getRuntimeDirectory ( ) , " Recently Deleted/ " + file . getName ( ) + " /info.json " ) ) ;
2018-03-14 08:01:44 +01:00
JSONObject json = new JSONObject ( Util . readAll ( reader ) ) ;
2018-03-27 06:31:38 +02:00
reader . close ( ) ;
2017-04-01 18:50:09 +02:00
if ( json . keySet ( ) . contains ( " Timestamp " ) ) {
if ( TimeUnit . MILLISECONDS . toDays ( Calendar . getInstance ( ) . getTime ( ) . getTime ( ) - json . getLong ( " Timestamp " ) ) > = 7 ) {
2021-10-24 06:14:07 +02:00
Directories . delete ( file ) ;
2017-04-01 18:50:09 +02:00
f - - ;
2019-04-29 17:05:06 +02:00
log . info . println ( " Removed ./Recently Deleted/ " + file . getName ( ) ) ;
2017-04-01 18:50:09 +02:00
}
} else {
2021-10-24 06:14:07 +02:00
Directories . delete ( file ) ;
2017-04-01 18:50:09 +02:00
f - - ;
2019-04-29 17:05:06 +02:00
log . info . println ( " Removed ./Recently Deleted/ " + file . getName ( ) ) ;
2017-04-01 18:50:09 +02:00
}
} else {
2021-10-24 06:14:07 +02:00
Directories . delete ( file ) ;
2017-04-01 18:50:09 +02:00
f - - ;
2019-04-29 17:05:06 +02:00
log . info . println ( " Removed ./Recently Deleted/ " + file . getName ( ) ) ;
2017-04-01 18:50:09 +02:00
}
} else {
Files . delete ( file . toPath ( ) ) ;
f - - ;
2019-04-29 17:05:06 +02:00
log . info . println ( " Removed ./Recently Deleted/ " + file . getName ( ) ) ;
2017-04-01 18:50:09 +02:00
}
} catch ( Exception e ) {
2017-04-03 19:52:28 +02:00
log . error . println ( e ) ;
2017-04-01 18:50:09 +02:00
}
}
if ( f < = 0 ) {
2021-10-24 06:14:07 +02:00
Files . delete ( new File ( engine . getRuntimeDirectory ( ) , " Recently Deleted " ) . toPath ( ) ) ;
2017-04-01 18:50:09 +02:00
}
}
2017-01-30 21:22:36 +01:00
2019-08-30 21:44:54 +02:00
Util . reflect ( SubLoggerImpl . class . getDeclaredField ( " logn " ) , null , config . get ( ) . getMap ( " Settings " ) . getBoolean ( " Network-Log " , true ) ) ;
Util . reflect ( SubLoggerImpl . class . getDeclaredField ( " logc " ) , null , config . get ( ) . getMap ( " Settings " ) . getBoolean ( " Console-Log " , true ) ) ;
2019-01-17 19:12:32 +01:00
2020-11-28 04:00:54 +01:00
loadDefaults ( ) ;
2021-10-24 06:14:07 +02:00
engine . getPluginManager ( ) . loadPlugins ( new File ( engine . getRuntimeDirectory ( ) , " Plugins " ) ) ;
2017-01-30 21:22:36 +01:00
2018-03-21 21:45:59 +01:00
running = true ;
2019-08-30 21:44:54 +02:00
creator = new SubCreatorImpl ( this ) ;
2020-11-28 04:00:54 +01:00
reload ( false ) ;
subdata . put ( 0 , null ) ;
2019-05-13 05:28:27 +02:00
subprotocol = SubProtocol . get ( ) ;
2020-06-12 07:45:49 +02:00
subprotocol . registerCipher ( " DHE " , DHE . get ( 128 ) ) ;
subprotocol . registerCipher ( " DHE-128 " , DHE . get ( 128 ) ) ;
subprotocol . registerCipher ( " DHE-192 " , DHE . get ( 192 ) ) ;
subprotocol . registerCipher ( " DHE-256 " , DHE . get ( 256 ) ) ;
2021-10-24 20:53:15 +02:00
api . name = config . get ( ) . getMap ( " Settings " ) . getMap ( " SubData " ) . getString ( " Name " ) ;
2020-11-28 04:00:54 +01:00
Logger log = new Logger ( " SubData " ) ;
2021-10-24 20:53:15 +02:00
if ( config . get ( ) . getMap ( " Settings " ) . getMap ( " SubData " ) . getString ( " Password " , " " ) . length ( ) > 0 ) {
subprotocol . registerCipher ( " AES " , new AES ( 128 , config . get ( ) . getMap ( " Settings " ) . getMap ( " SubData " ) . getString ( " Password " ) ) ) ;
subprotocol . registerCipher ( " AES-128 " , new AES ( 128 , config . get ( ) . getMap ( " Settings " ) . getMap ( " SubData " ) . getString ( " Password " ) ) ) ;
subprotocol . registerCipher ( " AES-192 " , new AES ( 192 , config . get ( ) . getMap ( " Settings " ) . getMap ( " SubData " ) . getString ( " Password " ) ) ) ;
subprotocol . registerCipher ( " AES-256 " , new AES ( 256 , config . get ( ) . getMap ( " Settings " ) . getMap ( " SubData " ) . getString ( " Password " ) ) ) ;
2020-11-28 04:00:54 +01:00
log . info . println ( " AES Encryption Available " ) ;
}
2021-10-24 06:14:07 +02:00
if ( new File ( engine . getRuntimeDirectory ( ) , " subdata.rsa.key " ) . exists ( ) ) {
2020-11-28 04:00:54 +01:00
try {
2021-10-24 06:14:07 +02:00
subprotocol . registerCipher ( " RSA " , new RSA ( new File ( engine . getRuntimeDirectory ( ) , " subdata.rsa.key " ) ) ) ;
2020-11-28 04:00:54 +01:00
log . info . println ( " RSA Encryption Available " ) ;
} catch ( Exception e ) {
log . error . println ( e ) ;
}
}
reconnect = true ;
log . info . println ( ) ;
2021-10-24 20:53:15 +02:00
log . info . println ( " Connecting to / " + config . get ( ) . getMap ( " Settings " ) . getMap ( " SubData " ) . getString ( " Address " , " 127.0.0.1:4391 " ) ) ;
2020-11-28 04:00:54 +01:00
connect ( log . toPrimitive ( ) , null ) ;
2017-08-18 11:58:06 +02:00
2018-03-27 06:31:38 +02:00
new Metrics ( this ) ;
2018-09-15 03:05:39 +02:00
info . setUpdateChecker ( ( ) - > {
2018-09-09 00:06:21 +02:00
try {
YAMLSection tags = new YAMLSection ( new JSONObject ( " { \" 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 " ) ) ) ) + '}' ) ) ;
List < Version > versions = new LinkedList < Version > ( ) ;
Version updversion = info . getVersion ( ) ;
int updcount = 0 ;
2019-05-13 05:28:27 +02:00
for ( ObjectMap < String > tag : tags . getMapList ( " tags " ) ) versions . add ( Version . fromString ( tag . getString ( " ref " ) . substring ( 10 ) ) ) ;
2018-09-09 00:06:21 +02:00
Collections . sort ( versions ) ;
for ( Version version : versions ) {
if ( version . compareTo ( updversion ) > 0 ) {
updversion = version ;
updcount + + ;
2017-08-18 11:58:06 +02:00
}
2018-09-09 00:06:21 +02:00
}
2021-02-07 05:53:03 +01:00
if ( updcount > 0 ) {
log . info . println ( " SubServers.Host v " + updversion + " is available. You are " + updcount + " version " + ( ( updcount = = 1 ) ? " " : " s " ) + " behind. " ) ;
return true ;
}
2018-09-09 00:06:21 +02:00
} catch ( Exception e ) { }
2021-02-07 05:53:03 +01:00
return false ;
2018-09-09 00:06:21 +02:00
} ) ;
2017-08-18 11:58:06 +02:00
2018-09-09 00:06:21 +02:00
engine . start ( this : : stop ) ;
2018-10-08 01:25:08 +02:00
if ( ! UPnP . isUPnPAvailable ( ) ) {
2021-02-07 05:53:03 +01:00
log . warn . println ( " UPnP service is unavailable. SubServers can't port-forward for you from this device. " ) ;
2018-10-08 01:25:08 +02:00
}
2017-01-30 21:22:36 +01:00
} catch ( Exception e ) {
2019-05-31 21:20:17 +02:00
if ( engine = = null ) {
e . printStackTrace ( ) ;
System . exit ( 1 ) ;
} else {
log . error . println ( e ) ;
engine . stop ( 1 ) ;
}
2017-01-30 21:22:36 +01:00
}
}
2019-05-13 05:28:27 +02:00
private void loadDefaults ( ) {
2020-06-12 07:45:49 +02:00
new SubCommand ( this ) . load ( ) ;
2019-05-13 05:28:27 +02:00
}
public void reload ( boolean notifyPlugins ) throws IOException {
2019-08-31 19:26:37 +02:00
resetDate = Calendar . getInstance ( ) . getTime ( ) . getTime ( ) ;
2017-12-14 01:04:58 +01:00
2021-10-24 06:14:07 +02:00
ConfigUpdater . updateConfig ( new File ( engine . getRuntimeDirectory ( ) , " config.yml " ) ) ;
2017-12-14 01:04:58 +01:00
config . reload ( ) ;
2020-06-20 03:00:50 +02:00
creator . load ( false ) ;
2017-12-14 01:04:58 +01:00
2019-05-13 05:28:27 +02:00
if ( notifyPlugins ) {
engine . getPluginManager ( ) . executeEvent ( new GalaxiReloadEvent ( engine ) ) ;
}
2018-06-26 02:51:14 +02:00
}
2017-01-30 21:22:36 +01:00
2020-11-14 08:07:25 +01:00
private void connect ( java . util . logging . Logger log , Pair < DisconnectReason , DataClient > disconnect ) throws IOException {
2020-11-11 22:07:22 +01:00
int reconnect = config . get ( ) . getMap ( " Settings " ) . getMap ( " SubData " ) . getInt ( " Reconnect " , 60 ) ;
2020-11-14 08:07:25 +01:00
if ( disconnect = = null | | ( this . reconnect & & reconnect > 0 & & disconnect . key ( ) ! = DisconnectReason . PROTOCOL_MISMATCH & & disconnect . key ( ) ! = DisconnectReason . ENCRYPTION_MISMATCH ) ) {
2019-08-31 19:26:37 +02:00
long reset = resetDate ;
Timer timer = new Timer ( SubAPI . getInstance ( ) . getAppInfo ( ) . getName ( ) + " ::SubData_Reconnect_Handler " ) ;
2020-11-28 04:00:54 +01:00
if ( disconnect ! = null ) log . info ( " Attempting reconnect in " + reconnect + " seconds " ) ;
2019-08-31 19:26:37 +02:00
timer . scheduleAtFixedRate ( new TimerTask ( ) {
@Override
public void run ( ) {
try {
2019-10-19 22:38:44 +02:00
if ( reset = = resetDate & & ( subdata . getOrDefault ( 0 , null ) = = null | | subdata . get ( 0 ) . isClosed ( ) ) ) {
2021-10-24 20:53:15 +02:00
SubDataClient open = subprotocol . open ( InetAddress . getByName ( config . get ( ) . getMap ( " Settings " ) . getMap ( " SubData " ) . getString ( " Address " , " 127.0.0.1:4391 " ) . split ( " : " ) [ 0 ] ) ,
Integer . parseInt ( config . get ( ) . getMap ( " Settings " ) . getMap ( " SubData " ) . getString ( " Address " , " 127.0.0.1:4391 " ) . split ( " : " ) [ 1 ] ) ) ;
2019-10-19 00:24:58 +02:00
if ( subdata . getOrDefault ( 0 , null ) ! = null ) subdata . get ( 0 ) . reconnect ( open ) ;
subdata . put ( 0 , open ) ;
}
2019-08-31 19:26:37 +02:00
timer . cancel ( ) ;
} catch ( IOException e ) {
log . info ( " Connection was unsuccessful, retrying in " + reconnect + " seconds " ) ;
}
}
} , ( disconnect = = null ) ? 0 : TimeUnit . SECONDS . toMillis ( reconnect ) , TimeUnit . SECONDS . toMillis ( reconnect ) ) ;
}
2017-02-05 23:03:17 +01:00
}
2021-02-07 09:09:39 +01:00
@SuppressWarnings ( " unchecked " )
2018-09-09 00:06:21 +02:00
private void stop ( ) {
if ( running ) {
2021-02-07 05:53:03 +01:00
log . info . println ( " Stopping hosted servers " ) ;
2021-02-07 09:09:39 +01:00
Map . Entry < String , SubServerImpl > [ ] subservers = servers . entrySet ( ) . toArray ( new Map . Entry [ 0 ] ) ;
2017-12-06 23:34:47 +01:00
2021-02-07 09:09:39 +01:00
for ( Map . Entry < String , SubServerImpl > entry : subservers ) {
if ( entry . getValue ( ) . isRunning ( ) ) {
log . info . println ( " Stopping " + entry . getValue ( ) . getName ( ) ) ;
entry . getValue ( ) . stop ( ) ;
try {
entry . getValue ( ) . waitFor ( ) ;
} catch ( Exception e ) {
log . error . println ( e ) ;
}
2017-12-06 23:34:47 +01:00
}
2021-02-07 09:09:39 +01:00
servers . remove ( entry . getKey ( ) ) ;
if ( UPnP . isUPnPAvailable ( ) & & UPnP . isMappedTCP ( entry . getValue ( ) . getPort ( ) ) ) UPnP . closePortTCP ( entry . getValue ( ) . getPort ( ) ) ;
2017-12-06 23:34:47 +01:00
}
servers . clear ( ) ;
if ( creator ! = null ) {
creator . terminate ( ) ;
try {
creator . waitFor ( ) ;
} catch ( Exception e ) {
log . error . println ( e ) ;
}
}
running = false ;
2017-04-01 07:37:48 +02:00
try {
2017-12-06 23:34:47 +01:00
Thread . sleep ( 500 ) ;
2017-04-01 07:37:48 +02:00
} catch ( Exception e ) {
log . error . println ( e ) ;
}
2019-05-13 05:28:27 +02:00
reconnect = false ;
try {
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 ( Exception e ) {
log . error . println ( e ) ;
}
2017-04-01 07:37:48 +02:00
2021-10-24 06:14:07 +02:00
if ( new File ( engine . getRuntimeDirectory ( ) , " Cache/Remote " ) . exists ( ) ) Directories . delete ( new File ( engine . getRuntimeDirectory ( ) , " Cache/Remote " ) ) ;
2017-04-01 07:37:48 +02:00
}
2017-01-30 21:22:36 +01:00
}
}