2016-12-24 05:55:17 +01:00
package net.ME1312.SubServers.Bungee.Network ;
2016-12-05 04:21:04 +01:00
2016-12-24 05:55:17 +01:00
import net.ME1312.SubServers.Bungee.Library.Exception.IllegalPacketException ;
2017-01-30 21:22:36 +01:00
import net.ME1312.SubServers.Bungee.Library.Util ;
2016-12-24 05:55:17 +01:00
import net.ME1312.SubServers.Bungee.Library.Version.Version ;
import net.ME1312.SubServers.Bungee.Network.Packet.* ;
import net.ME1312.SubServers.Bungee.SubPlugin ;
2016-12-05 04:21:04 +01:00
import org.json.JSONObject ;
import java.io.IOException ;
2017-07-19 01:28:59 +02:00
import java.lang.reflect.InvocationTargetException ;
2016-12-15 22:04:39 +01:00
import java.net.* ;
2016-12-05 04:21:04 +01:00
import java.util.ArrayList ;
2017-01-08 03:30:03 +01:00
import java.util.Collection ;
2016-12-05 04:21:04 +01:00
import java.util.HashMap ;
import java.util.List ;
/ * *
2016-12-15 22:04:39 +01:00
* SubDataServer Class
2016-12-05 04:21:04 +01:00
* /
2016-12-15 22:04:39 +01:00
public final class SubDataServer {
2017-07-19 01:28:59 +02:00
protected static final int MAX_QUEUE = 64 ;
2016-12-15 22:04:39 +01:00
private static HashMap < Class < ? extends PacketOut > , String > pOut = new HashMap < Class < ? extends PacketOut > , String > ( ) ;
2017-01-06 21:44:34 +01:00
private static HashMap < String , List < PacketIn > > pIn = new HashMap < String , List < PacketIn > > ( ) ;
2016-12-15 22:04:39 +01:00
private static List < InetAddress > allowedAddresses = new ArrayList < InetAddress > ( ) ;
private static boolean defaults = false ;
2017-07-19 01:28:59 +02:00
private HashMap < String , Client > clients = new HashMap < String , Client > ( ) ;
2016-12-05 04:21:04 +01:00
private ServerSocket server ;
2017-06-30 15:36:16 +02:00
private Encryption encryption ;
2017-01-21 17:49:37 +01:00
protected SubPlugin plugin ;
2016-12-05 04:21:04 +01:00
2017-06-30 15:36:16 +02:00
public enum Encryption {
NONE ,
AES ,
AES_128 ,
AES_192 ,
AES_256 ,
}
2016-12-05 04:21:04 +01:00
/ * *
2016-12-15 22:04:39 +01:00
* SubData Server Instance
2016-12-05 04:21:04 +01:00
*
* @param plugin SubPlugin
* @param port Port
2017-07-19 01:28:59 +02:00
* @param address Bind
* @param encryption Encryption Type
2016-12-05 04:21:04 +01:00
* @throws IOException
* /
2017-07-19 01:28:59 +02:00
public SubDataServer ( SubPlugin plugin , int port , InetAddress address , Encryption encryption ) throws IOException {
if ( Util . isNull ( plugin , port , encryption , MAX_QUEUE ) ) throw new NullPointerException ( ) ;
2017-04-03 19:08:00 +02:00
if ( address = = null ) {
2017-07-19 01:28:59 +02:00
server = new ServerSocket ( port , MAX_QUEUE ) ;
2017-04-03 19:08:00 +02:00
allowConnection ( InetAddress . getByName ( " 127.0.0.1 " ) ) ;
} else {
2017-07-19 01:28:59 +02:00
server = new ServerSocket ( port , MAX_QUEUE , address ) ;
2017-04-03 19:08:00 +02:00
allowConnection ( address ) ;
}
2016-12-05 04:21:04 +01:00
this . plugin = plugin ;
2017-06-30 15:36:16 +02:00
this . encryption = encryption ;
2016-12-05 04:21:04 +01:00
2016-12-15 22:04:39 +01:00
if ( ! defaults ) loadDefaults ( ) ;
2016-12-05 04:21:04 +01:00
}
private void loadDefaults ( ) {
2016-12-15 22:04:39 +01:00
defaults = true ;
2017-07-30 02:41:02 +02:00
for ( String s : plugin . config . get ( ) . getSection ( " Settings " ) . getSection ( " SubData " ) . getStringList ( " Allowed-Connections " , new ArrayList < String > ( ) ) ) {
2016-12-05 04:21:04 +01:00
try {
allowedAddresses . add ( InetAddress . getByName ( s ) ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
}
2016-12-19 01:38:02 +01:00
plugin . getPluginManager ( ) . registerListener ( null , new PacketOutRunEvent ( plugin ) ) ;
2016-12-05 04:21:04 +01:00
registerPacket ( new PacketAuthorization ( plugin ) , " Authorization " ) ;
2016-12-19 01:38:02 +01:00
registerPacket ( new PacketCommandServer ( plugin ) , " SubCommandServer " ) ;
registerPacket ( new PacketCreateServer ( plugin ) , " SubCreateServer " ) ;
registerPacket ( new PacketDownloadHostInfo ( plugin ) , " SubDownloadHostInfo " ) ;
registerPacket ( new PacketDownloadLang ( plugin ) , " SubDownloadLang " ) ;
2017-07-19 01:28:59 +02:00
registerPacket ( new PacketDownloadNetworkList ( plugin ) , " SubDownloadNetworkList " ) ;
2016-12-19 01:38:02 +01:00
registerPacket ( new PacketDownloadPlayerList ( plugin ) , " SubDownloadPlayerList " ) ;
2017-04-10 05:39:22 +02:00
registerPacket ( new PacketDownloadProxyInfo ( plugin ) , " SubDownloadProxyInfo " ) ;
2016-12-19 01:38:02 +01:00
registerPacket ( new PacketDownloadServerInfo ( plugin ) , " SubDownloadServerInfo " ) ;
registerPacket ( new PacketDownloadServerList ( plugin ) , " SubDownloadServerList " ) ;
2017-04-24 18:28:16 +02:00
registerPacket ( new PacketEditServer ( plugin ) , " SubEditServer " ) ;
2017-04-01 07:37:48 +02:00
registerPacket ( new PacketExAddServer ( ) , " SubExAddServer " ) ;
registerPacket ( new PacketExConfigureHost ( plugin ) , " SubExConfigureHost " ) ;
2017-07-23 06:32:57 +02:00
registerPacket ( new PacketExCreateServer ( null ) , " SubExCreateServer " ) ;
2017-04-01 07:37:48 +02:00
registerPacket ( new PacketExDeleteServer ( ) , " SubExDeleteServer " ) ;
registerPacket ( new PacketExRemoveServer ( ) , " SubExRemoveServer " ) ;
registerPacket ( new PacketExUpdateServer ( plugin ) , " SubExUpdateServer " ) ;
registerPacket ( new PacketInExLogMessage ( ) , " SubExLogMessage " ) ;
registerPacket ( new PacketInExRequestQueue ( plugin ) , " SubExRequestQueue " ) ;
registerPacket ( new PacketLinkExHost ( plugin ) , " SubLinkExHost " ) ;
2016-12-19 01:38:02 +01:00
registerPacket ( new PacketLinkServer ( plugin ) , " SubLinkServer " ) ;
2017-04-24 18:28:16 +02:00
registerPacket ( new PacketListenLog ( plugin ) , " SubListenLog " ) ;
2016-12-19 01:38:02 +01:00
registerPacket ( new PacketStartServer ( plugin ) , " SubStartServer " ) ;
registerPacket ( new PacketStopServer ( plugin ) , " SubStopServer " ) ;
registerPacket ( new PacketTeleportPlayer ( plugin ) , " SubTeleportPlayer " ) ;
2016-12-05 04:21:04 +01:00
registerPacket ( PacketAuthorization . class , " Authorization " ) ;
2016-12-19 01:38:02 +01:00
registerPacket ( PacketCommandServer . class , " SubCommandServer " ) ;
registerPacket ( PacketCreateServer . class , " SubCreateServer " ) ;
registerPacket ( PacketDownloadHostInfo . class , " SubDownloadHostInfo " ) ;
registerPacket ( PacketDownloadLang . class , " SubDownloadLang " ) ;
2017-07-19 01:28:59 +02:00
registerPacket ( PacketDownloadNetworkList . class , " SubDownloadNetworkList " ) ;
2016-12-19 01:38:02 +01:00
registerPacket ( PacketDownloadPlayerList . class , " SubDownloadPlayerList " ) ;
2017-04-10 05:39:22 +02:00
registerPacket ( PacketDownloadProxyInfo . class , " SubDownloadProxyInfo " ) ;
2016-12-19 01:38:02 +01:00
registerPacket ( PacketDownloadServerInfo . class , " SubDownloadServerInfo " ) ;
registerPacket ( PacketDownloadServerList . class , " SubDownloadServerList " ) ;
2017-04-24 18:28:16 +02:00
registerPacket ( PacketEditServer . class , " SubEditServer " ) ;
2017-04-01 07:37:48 +02:00
registerPacket ( PacketExAddServer . class , " SubExAddServer " ) ;
registerPacket ( PacketExConfigureHost . class , " SubExConfigureHost " ) ;
registerPacket ( PacketExCreateServer . class , " SubExCreateServer " ) ;
registerPacket ( PacketExDeleteServer . class , " SubExDeleteServer " ) ;
registerPacket ( PacketExRemoveServer . class , " SubExRemoveServer " ) ;
registerPacket ( PacketExUpdateServer . class , " SubExUpdateServer " ) ;
registerPacket ( PacketLinkExHost . class , " SubLinkExHost " ) ;
2016-12-19 01:38:02 +01:00
registerPacket ( PacketLinkServer . class , " SubLinkServer " ) ;
2017-04-24 18:28:16 +02:00
registerPacket ( PacketListenLog . class , " SubListenLog " ) ;
2016-12-19 01:38:02 +01:00
registerPacket ( PacketOutRunEvent . class , " SubRunEvent " ) ;
2017-04-01 22:31:57 +02:00
registerPacket ( PacketOutReset . class , " SubReset " ) ;
2016-12-19 01:38:02 +01:00
registerPacket ( PacketStartServer . class , " SubStartServer " ) ;
registerPacket ( PacketStopServer . class , " SubStopServer " ) ;
registerPacket ( PacketTeleportPlayer . class , " SubTeleportPlayer " ) ;
2016-12-05 04:21:04 +01:00
}
/ * *
* Gets the Server Socket
*
* @return Server Socket
* /
public ServerSocket getServer ( ) {
return server ;
}
2017-06-30 15:36:16 +02:00
/ * *
* Gets the Server ' s Encryption method
*
* @return Encryption method
* /
public Encryption getEncryption ( ) {
return encryption ;
}
2016-12-05 04:21:04 +01:00
/ * *
* Add a Client to the Network
*
* @param socket Client to add
* @throws IOException
* /
public Client addClient ( Socket socket ) throws IOException {
2017-01-30 21:22:36 +01:00
if ( Util . isNull ( socket ) ) throw new NullPointerException ( ) ;
2016-12-05 04:21:04 +01:00
if ( allowedAddresses . contains ( socket . getInetAddress ( ) ) ) {
2017-01-09 20:37:57 +01:00
Client client = new Client ( this , socket ) ;
2016-12-05 04:21:04 +01:00
System . out . println ( " SubData > " + client . getAddress ( ) . toString ( ) + " has connected " ) ;
2017-07-19 01:28:59 +02:00
clients . put ( client . getAddress ( ) . toString ( ) , client ) ;
2016-12-05 04:21:04 +01:00
return client ;
} else {
2017-04-10 05:39:22 +02:00
System . out . println ( " SubData > " + socket . getInetAddress ( ) . toString ( ) + " attempted to connect, but isn't white-listed " ) ;
2016-12-05 04:21:04 +01:00
socket . close ( ) ;
return null ;
}
}
/ * *
* Grabs a Client from the Network
*
* @param socket Socket to search
* @return Client
* /
public Client getClient ( Socket socket ) {
2017-01-30 21:22:36 +01:00
if ( Util . isNull ( socket ) ) throw new NullPointerException ( ) ;
2017-07-19 01:28:59 +02:00
return clients . get ( new InetSocketAddress ( socket . getInetAddress ( ) , socket . getPort ( ) ) . toString ( ) ) ;
2016-12-05 04:21:04 +01:00
}
/ * *
* Grabs a Client from the Network
*
* @param address Address to search
* @return Client
* /
2016-12-15 22:04:39 +01:00
public Client getClient ( InetSocketAddress address ) {
2017-07-19 01:28:59 +02:00
if ( Util . isNull ( address ) ) throw new NullPointerException ( ) ;
return clients . get ( address . toString ( ) ) ;
}
/ * *
* Grabs a Client from the Network
*
* @param address Address to search
* @return Client
* /
public Client getClient ( String address ) {
2017-01-30 21:22:36 +01:00
if ( Util . isNull ( address ) ) throw new NullPointerException ( ) ;
2016-12-05 04:21:04 +01:00
return clients . get ( address ) ;
}
2017-01-08 03:30:03 +01:00
/ * *
2017-06-30 15:36:16 +02:00
* Grabs all the Clients on the Network
2017-01-08 03:30:03 +01:00
*
* @return Client List
* /
public Collection < Client > getClients ( ) {
return clients . values ( ) ;
}
2016-12-05 04:21:04 +01:00
/ * *
* Remove a Client from the Network
*
* @param client Client to Kick
* @throws IOException
* /
public void removeClient ( Client client ) throws IOException {
2017-01-30 21:22:36 +01:00
if ( Util . isNull ( client ) ) throw new NullPointerException ( ) ;
2016-12-05 04:21:04 +01:00
SocketAddress address = client . getAddress ( ) ;
2017-07-19 01:28:59 +02:00
if ( clients . keySet ( ) . contains ( address . toString ( ) ) ) {
clients . remove ( address . toString ( ) ) ;
2016-12-05 04:21:04 +01:00
client . disconnect ( ) ;
System . out . println ( " SubData > " + client . getAddress ( ) . toString ( ) + " has disconnected " ) ;
}
}
/ * *
* Remove a Client from the Network
*
* @param address Address to Kick
* @throws IOException
* /
2016-12-15 22:04:39 +01:00
public void removeClient ( InetSocketAddress address ) throws IOException {
2017-07-19 01:28:59 +02:00
if ( Util . isNull ( address ) ) throw new NullPointerException ( ) ;
Client client = clients . get ( address . toString ( ) ) ;
if ( clients . keySet ( ) . contains ( address . toString ( ) ) ) {
clients . remove ( address . toString ( ) ) ;
client . disconnect ( ) ;
System . out . println ( " SubData > " + client . getAddress ( ) . toString ( ) + " has disconnected " ) ;
}
}
/ * *
* Remove a Client from the Network
*
* @param address Address to Kick
* @throws IOException
* /
public void removeClient ( String address ) throws IOException {
2017-01-30 21:22:36 +01:00
if ( Util . isNull ( address ) ) throw new NullPointerException ( ) ;
2016-12-05 04:21:04 +01:00
Client client = clients . get ( address ) ;
if ( clients . keySet ( ) . contains ( address ) ) {
clients . remove ( address ) ;
client . disconnect ( ) ;
System . out . println ( " SubData > " + client . getAddress ( ) . toString ( ) + " has disconnected " ) ;
}
}
/ * *
2016-12-15 22:04:39 +01:00
* Register PacketIn to the Network
2016-12-05 04:21:04 +01:00
*
* @param packet PacketIn to register
* @param handle Handle to Bind
* /
2016-12-15 22:04:39 +01:00
public static void registerPacket ( PacketIn packet , String handle ) {
2017-01-30 21:22:36 +01:00
if ( Util . isNull ( packet , handle ) ) throw new NullPointerException ( ) ;
2017-01-06 21:44:34 +01:00
List < PacketIn > list = ( pIn . keySet ( ) . contains ( handle ) ) ? pIn . get ( handle ) : new ArrayList < PacketIn > ( ) ;
2017-06-30 15:36:16 +02:00
if ( ! list . contains ( packet ) ) {
list . add ( packet ) ;
pIn . put ( handle , list ) ;
}
2016-12-05 04:21:04 +01:00
}
2017-01-08 03:30:03 +01:00
/ * *
* Unregister PacketIn from the Network
*
* @param packet PacketIn to unregister
* /
public static void unregisterPacket ( PacketIn packet ) {
2017-01-30 21:22:36 +01:00
if ( Util . isNull ( packet ) ) throw new NullPointerException ( ) ;
2017-06-30 15:36:16 +02:00
List < String > search = new ArrayList < String > ( ) ;
search . addAll ( pIn . keySet ( ) ) ;
for ( String handle : search ) if ( pIn . get ( handle ) . contains ( packet ) ) {
List < PacketIn > list = pIn . get ( handle ) ;
list . remove ( packet ) ;
if ( list . isEmpty ( ) ) {
pIn . remove ( handle ) ;
} else {
pIn . put ( handle , list ) ;
}
}
2017-01-08 03:30:03 +01:00
}
2016-12-05 04:21:04 +01:00
/ * *
2016-12-15 22:04:39 +01:00
* Register PacketOut to the Network
2016-12-05 04:21:04 +01:00
*
* @param packet PacketOut to register
* @param handle Handle to bind
* /
2016-12-15 22:04:39 +01:00
public static void registerPacket ( Class < ? extends PacketOut > packet , String handle ) {
2017-01-30 21:22:36 +01:00
if ( Util . isNull ( packet , handle ) ) throw new NullPointerException ( ) ;
2017-01-06 21:44:34 +01:00
pOut . put ( packet , handle ) ;
2016-12-15 22:04:39 +01:00
}
2017-01-08 03:30:03 +01:00
/ * *
* Unregister PacketOut to the Network
*
* @param packet PacketOut to unregister
* /
public static void unregisterPacket ( Class < ? extends PacketOut > packet ) {
2017-01-30 21:22:36 +01:00
if ( Util . isNull ( packet ) ) throw new NullPointerException ( ) ;
2017-01-08 03:30:03 +01:00
pOut . remove ( packet ) ;
}
2016-12-15 22:04:39 +01:00
/ * *
2017-01-06 21:44:34 +01:00
* Grab PacketIn Instances via handle
2016-12-15 22:04:39 +01:00
*
* @param handle Handle
* @return PacketIn
* /
2017-01-06 21:44:34 +01:00
public static List < ? extends PacketIn > getPacket ( String handle ) {
2017-01-30 21:22:36 +01:00
if ( Util . isNull ( handle ) ) throw new NullPointerException ( ) ;
2017-01-07 20:06:54 +01:00
return new ArrayList < PacketIn > ( pIn . get ( handle ) ) ;
2016-12-05 04:21:04 +01:00
}
/ * *
2017-01-06 21:44:34 +01:00
* Broadcast a Packet to everything on the Network < br >
2016-12-05 04:21:04 +01:00
* < b > Warning : < / b > There are usually different types of applications on the network at once , they may not recognise the same packet handles
*
* @param packet Packet to send
* /
public void broadcastPacket ( PacketOut packet ) {
2017-01-30 21:22:36 +01:00
if ( Util . isNull ( packet ) ) throw new NullPointerException ( ) ;
2017-08-07 21:51:11 +02:00
List < Client > clients = new ArrayList < Client > ( ) ;
clients . addAll ( getClients ( ) ) ;
for ( Client client : clients ) {
2016-12-05 04:21:04 +01:00
client . sendPacket ( packet ) ;
}
}
/ * *
* Allow Connections from an Address
*
* @param address Address to allow
* /
2016-12-15 22:04:39 +01:00
public static void allowConnection ( InetAddress address ) {
2017-01-30 21:22:36 +01:00
if ( Util . isNull ( address ) ) throw new NullPointerException ( ) ;
2016-12-05 04:21:04 +01:00
if ( ! allowedAddresses . contains ( address ) ) allowedAddresses . add ( address ) ;
}
/ * *
* Deny Connections from an Address
*
* @param address Address to deny
* /
2016-12-15 22:04:39 +01:00
public static void denyConnection ( InetAddress address ) {
2017-01-30 21:22:36 +01:00
if ( Util . isNull ( address ) ) throw new NullPointerException ( ) ;
2016-12-05 04:21:04 +01:00
allowedAddresses . remove ( address ) ;
}
/ * *
* JSON Encode PacketOut
*
* @param packet PacketOut
* @return JSON Formatted Packet
* @throws IllegalPacketException
* /
2017-07-19 01:28:59 +02:00
protected static JSONObject encodePacket ( Client client , PacketOut packet ) throws IllegalPacketException , InvocationTargetException {
2016-12-05 04:21:04 +01:00
JSONObject json = new JSONObject ( ) ;
2017-07-19 01:28:59 +02:00
if ( ! pOut . keySet ( ) . contains ( packet . getClass ( ) ) ) throw new IllegalPacketException ( packet . getClass ( ) . getCanonicalName ( ) + " : Unknown PacketOut Channel: " + packet . getClass ( ) . getCanonicalName ( ) ) ;
if ( packet . getVersion ( ) . toString ( ) = = null ) throw new NullPointerException ( packet . getClass ( ) . getCanonicalName ( ) + " : PacketOut getVersion() cannot be null: " + packet . getClass ( ) . getCanonicalName ( ) ) ;
try {
JSONObject contents = packet . generate ( ) ;
json . put ( " h " , pOut . get ( packet . getClass ( ) ) ) ;
json . put ( " v " , packet . getVersion ( ) . toString ( ) ) ;
if ( contents ! = null ) json . put ( " c " , contents ) ;
return json ;
} catch ( Throwable e ) {
throw new InvocationTargetException ( e , packet . getClass ( ) . getCanonicalName ( ) + " : Exception while encoding packet " ) ;
}
2016-12-05 04:21:04 +01:00
}
/ * *
* JSON Decode PacketIn
*
* @param json JSON to Decode
* @return PacketIn
* @throws IllegalPacketException
* /
2017-07-19 01:28:59 +02:00
protected static List < PacketIn > decodePacket ( Client client , JSONObject json ) throws IllegalPacketException {
if ( ! json . keySet ( ) . contains ( " h " ) | | ! json . keySet ( ) . contains ( " v " ) ) throw new IllegalPacketException ( client . getAddress ( ) . toString ( ) + " : Unknown Packet Format: " + json . toString ( ) ) ;
if ( ! pIn . keySet ( ) . contains ( json . getString ( " h " ) ) ) throw new IllegalPacketException ( client . getAddress ( ) . toString ( ) + " : Unknown PacketIn Channel: " + json . getString ( " h " ) ) ;
2016-12-05 04:21:04 +01:00
2017-01-06 21:44:34 +01:00
List < PacketIn > list = new ArrayList < PacketIn > ( ) ;
for ( PacketIn packet : pIn . get ( json . getString ( " h " ) ) ) {
if ( new Version ( json . getString ( " v " ) ) . equals ( packet . getVersion ( ) ) ) {
list . add ( packet ) ;
} else {
2017-07-19 01:28:59 +02:00
new IllegalPacketException ( client . getAddress ( ) . toString ( ) + " : Packet Version Mismatch in " + json . getString ( " h " ) + " : " + json . getString ( " v " ) + " =/= " + packet . getVersion ( ) . toString ( ) ) . printStackTrace ( ) ;
2017-01-06 21:44:34 +01:00
}
}
return list ;
2016-12-05 04:21:04 +01:00
}
/ * *
* Drops All Connections and Stops the SubData Listener
*
* @throws IOException
* /
public void destroy ( ) throws IOException {
while ( clients . size ( ) > 0 ) {
removeClient ( ( Client ) clients . values ( ) . toArray ( ) [ 0 ] ) ;
}
server . close ( ) ;
System . out . println ( " SubServers > The SubData Listener has been closed " ) ;
plugin . subdata = null ;
}
}