2022-08-30 12:03:34 +02:00
package net.ME1312.SubServers.Web ;
2022-08-31 17:04:55 +02:00
import net.ME1312.Galaxi.Engine.GalaxiEngine ;
import net.ME1312.Galaxi.Event.Engine.GalaxiReloadEvent ;
2022-08-31 21:32:46 +02:00
import net.ME1312.Galaxi.Event.Engine.GalaxiStopEvent ;
2022-08-31 17:04:55 +02:00
import net.ME1312.Galaxi.Library.Config.YAMLConfig ;
import net.ME1312.Galaxi.Library.Container.Pair ;
import net.ME1312.Galaxi.Library.Map.ObjectMap ;
import net.ME1312.Galaxi.Library.Util ;
import net.ME1312.Galaxi.Log.Logger ;
import net.ME1312.Galaxi.Plugin.App ;
import net.ME1312.Galaxi.Plugin.PluginInfo ;
import net.ME1312.SubData.Client.DataClient ;
import net.ME1312.SubData.Client.Encryption.AES ;
import net.ME1312.SubData.Client.Encryption.DHE ;
import net.ME1312.SubData.Client.Encryption.RSA ;
import net.ME1312.SubData.Client.Library.DisconnectReason ;
import net.ME1312.SubData.Client.SubDataClient ;
2022-08-31 21:32:46 +02:00
import net.ME1312.SubServers.Bungee.SubProxy ;
2022-08-31 17:04:55 +02:00
import net.ME1312.SubServers.Host.Executable.SubCreatorImpl ;
import net.ME1312.SubServers.Host.Executable.SubLoggerImpl ;
import net.ME1312.SubServers.Host.Executable.SubServerImpl ;
import net.ME1312.SubServers.Web.Library.ConfigUpdater ;
import net.ME1312.SubServers.Web.Network.SubProtocol ;
import net.ME1312.SubServers.Web.Endpoints.StatusServlet ;
2022-08-30 12:03:34 +02:00
import org.eclipse.jetty.server.Connector ;
import org.eclipse.jetty.server.Server ;
import org.eclipse.jetty.server.ServerConnector ;
import org.eclipse.jetty.servlet.ServletHandler ;
2022-08-31 17:04:55 +02:00
import java.io.File ;
import java.io.IOException ;
import java.net.InetAddress ;
import java.util.* ;
import java.util.concurrent.TimeUnit ;
@App ( name = " SubServers.Web " , version = " 2.19a " , authors = " ME1312 " , website = " https://github.com/ME1312/SubServers-2 " , description = " Operate subservers from web browsers " )
2022-08-30 12:03:34 +02:00
public class JettyServer {
public Server server ;
2022-08-31 21:32:46 +02:00
public boolean isPlugin = true ;
2022-08-30 12:03:34 +02:00
2022-08-31 17:04:55 +02:00
HashMap < Integer , SubDataClient > subdata = new HashMap < Integer , SubDataClient > ( ) ;
Pair < Long , Map < String , Map < String , String > > > lang = null ;
public HashMap < String , SubCreatorImpl . ServerTemplate > templatesR = new HashMap < String , SubCreatorImpl . ServerTemplate > ( ) ;
public HashMap < String , SubCreatorImpl . ServerTemplate > templates = new HashMap < String , SubCreatorImpl . ServerTemplate > ( ) ;
public HashMap < String , SubServerImpl > servers = new HashMap < String , SubServerImpl > ( ) ;
public Logger log ;
public PluginInfo info ;
public GalaxiEngine engine ;
public YAMLConfig config ;
public ObjectMap < String > host = null ;
public SubProtocol subprotocol ;
public final SubAPI api = new SubAPI ( this ) ;
private long resetDate = 0 ;
private boolean reconnect = true ;
private boolean running = false ;
2022-08-31 21:32:46 +02:00
public void start ( SubProxy proxy ) throws Exception {
if ( proxy = = null ) {
isPlugin = false ;
}
2022-08-31 17:04:55 +02:00
log = new Logger ( " SubServers " ) ;
info = PluginInfo . load ( this ) ;
info . setLogger ( log ) ;
engine = GalaxiEngine . init ( info ) ;
2022-08-31 21:32:46 +02:00
ConfigUpdater . updateConfig ( new File ( engine . getRuntimeDirectory ( ) , " config.yml " ) , isPlugin ) ;
2022-08-31 17:04:55 +02:00
config = new YAMLConfig ( new File ( engine . getRuntimeDirectory ( ) , " config.yml " ) ) ;
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 ) ) ;
engine . getPluginManager ( ) . loadPlugins ( new File ( engine . getRuntimeDirectory ( ) , " Plugins " ) ) ;
running = true ;
2022-08-31 21:32:46 +02:00
this . isPlugin = isPlugin ;
2022-08-31 17:04:55 +02:00
reload ( false ) ;
subdata . put ( 0 , null ) ;
subprotocol = SubProtocol . get ( ) ;
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 ) ) ;
api . name = config . get ( ) . getMap ( " Settings " ) . getMap ( " SubData " ) . getString ( " Name " ) ;
Logger log = new Logger ( " SubData " ) ;
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 " ) ) ) ;
log . info . println ( " AES Encryption Available " ) ;
}
if ( new File ( engine . getRuntimeDirectory ( ) , " subdata.rsa.key " ) . exists ( ) ) {
try {
subprotocol . registerCipher ( " RSA " , new RSA ( new File ( engine . getRuntimeDirectory ( ) , " subdata.rsa.key " ) ) ) ;
log . info . println ( " RSA Encryption Available " ) ;
} catch ( Exception e ) {
log . error . println ( e ) ;
}
}
reconnect = true ;
log . info . println ( ) ;
log . info . println ( " Connecting to / " + config . get ( ) . getMap ( " Settings " ) . getMap ( " SubData " ) . getString ( " Address " , " 127.0.0.1:4391 " ) ) ;
connect ( log . toPrimitive ( ) , null ) ;
2022-08-30 12:03:34 +02:00
server = new Server ( ) ;
ServerConnector connector = new ServerConnector ( server ) ;
connector . setPort ( 8090 ) ;
server . setConnectors ( new Connector [ ] { connector } ) ;
ServletHandler handler = new ServletHandler ( ) ;
server . setHandler ( handler ) ;
2022-08-30 22:31:23 +02:00
handler . addServletWithMapping ( StatusServlet . class , " /status " ) ;
2022-08-30 12:03:34 +02:00
server . start ( ) ;
}
2022-08-31 17:04:55 +02:00
public void reload ( boolean notifyPlugins ) throws IOException {
resetDate = Calendar . getInstance ( ) . getTime ( ) . getTime ( ) ;
2022-08-31 21:32:46 +02:00
ConfigUpdater . updateConfig ( new File ( engine . getRuntimeDirectory ( ) , " config.yml " ) , isPlugin ) ;
2022-08-31 17:04:55 +02:00
config . reload ( ) ;
if ( notifyPlugins ) {
engine . getPluginManager ( ) . executeEvent ( new GalaxiReloadEvent ( engine ) ) ;
}
}
private void connect ( final java . util . logging . Logger log , Pair < DisconnectReason , DataClient > disconnect ) throws IOException {
2022-08-31 21:32:46 +02:00
if ( ! isPlugin ) {
final int reconnect = config . get ( ) . getMap ( " Settings " ) . getMap ( " SubData " ) . getInt ( " Reconnect " , 60 ) ;
if ( disconnect = = null | | ( this . reconnect & & reconnect > 0 & & disconnect . key ( ) ! = DisconnectReason . PROTOCOL_MISMATCH & & disconnect . key ( ) ! = DisconnectReason . ENCRYPTION_MISMATCH ) ) {
final long reset = resetDate ;
final Timer timer = new Timer ( SubAPI . getInstance ( ) . getAppInfo ( ) . getName ( ) + " ::SubData_Reconnect_Handler " ) ;
if ( disconnect ! = null ) log . info ( " Attempting reconnect in " + reconnect + " seconds " ) ;
timer . scheduleAtFixedRate ( new TimerTask ( ) {
@Override
public void run ( ) {
try {
if ( reset = = resetDate & & ( subdata . getOrDefault ( 0 , null ) = = null | | subdata . get ( 0 ) . isClosed ( ) ) ) {
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 ] ) ) ;
if ( subdata . getOrDefault ( 0 , null ) ! = null ) subdata . get ( 0 ) . reconnect ( open ) ;
subdata . put ( 0 , open ) ;
}
timer . cancel ( ) ;
} catch ( IOException e ) {
log . info ( " Connection was unsuccessful, retrying in " + reconnect + " seconds " ) ;
2022-08-31 17:04:55 +02:00
}
}
2022-08-31 21:32:46 +02:00
} , ( disconnect = = null ) ? 0 : TimeUnit . SECONDS . toMillis ( reconnect ) , TimeUnit . SECONDS . toMillis ( reconnect ) ) ;
}
2022-08-31 17:04:55 +02:00
}
2022-08-31 21:32:46 +02:00
//TODO: do stuff if we are a plugin
}
public void stop ( ) throws Exception {
running = false ;
engine . getPluginManager ( ) . executeEvent ( new GalaxiStopEvent ( engine , 0 ) ) ;
server . stop ( ) ;
2022-08-31 17:04:55 +02:00
}
2022-08-30 12:03:34 +02:00
}