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 ;
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 ;
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 {
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 ;
private HashMap < InetSocketAddress , Client > clients = new HashMap < InetSocketAddress , Client > ( ) ;
2016-12-05 04:21:04 +01:00
private ServerSocket server ;
private SubPlugin plugin ;
/ * *
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
* @param backlog Connection Queue
* @param address Bind Address
* @throws IOException
* /
2016-12-15 22:04:39 +01:00
public SubDataServer ( SubPlugin plugin , int port , int backlog , InetAddress address ) throws IOException {
2016-12-05 04:21:04 +01:00
server = new ServerSocket ( port , backlog , address ) ;
this . plugin = plugin ;
allowConnection ( address ) ;
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 ;
2016-12-05 04:21:04 +01:00
for ( String s : plugin . config . get ( ) . getSection ( " Settings " ) . getSection ( " SubData " ) . getStringList ( " Allowed-Connections " ) ) {
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 PacketDownloadBuildScript ( plugin ) , " SubDownloadBuildScript " ) ;
registerPacket ( new PacketDownloadHostInfo ( plugin ) , " SubDownloadHostInfo " ) ;
registerPacket ( new PacketDownloadLang ( plugin ) , " SubDownloadLang " ) ;
registerPacket ( new PacketDownloadPlayerList ( plugin ) , " SubDownloadPlayerList " ) ;
registerPacket ( new PacketDownloadServerInfo ( plugin ) , " SubDownloadServerInfo " ) ;
registerPacket ( new PacketDownloadServerList ( plugin ) , " SubDownloadServerList " ) ;
registerPacket ( new PacketInfoPassthrough ( plugin ) , " SubInfoPassthrough " ) ;
registerPacket ( new PacketLinkServer ( plugin ) , " SubLinkServer " ) ;
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 ( PacketDownloadBuildScript . class , " SubDownloadBuildScript " ) ;
registerPacket ( PacketDownloadHostInfo . class , " SubDownloadHostInfo " ) ;
registerPacket ( PacketDownloadLang . class , " SubDownloadLang " ) ;
registerPacket ( PacketDownloadPlayerList . class , " SubDownloadPlayerList " ) ;
registerPacket ( PacketDownloadServerInfo . class , " SubDownloadServerInfo " ) ;
registerPacket ( PacketDownloadServerList . class , " SubDownloadServerList " ) ;
registerPacket ( PacketInfoPassthrough . class , " SubInfoPassthrough " ) ;
registerPacket ( PacketLinkServer . class , " SubLinkServer " ) ;
registerPacket ( PacketOutRunEvent . class , " SubRunEvent " ) ;
registerPacket ( PacketOutShutdown . class , " SubShutdown " ) ;
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 ;
}
/ * *
* Add a Client to the Network
*
* @param socket Client to add
* @throws IOException
* /
public Client addClient ( Socket socket ) throws IOException {
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 " ) ;
clients . put ( client . getAddress ( ) , client ) ;
return client ;
} else {
2016-12-15 22:04:39 +01:00
System . out . println ( " SubData > " + socket . getInetAddress ( ) . toString ( ) + " : " + socket . getPort ( ) + " attempted to connect, but isn't whitelisted " ) ;
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 ) {
2016-12-15 22:04:39 +01:00
return clients . get ( new InetSocketAddress ( socket . getInetAddress ( ) , socket . getPort ( ) ) ) ;
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 ) {
2016-12-05 04:21:04 +01:00
return clients . get ( address ) ;
}
2017-01-08 03:30:03 +01:00
/ * *
* Grabs all the Clients from the Network
*
* @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 {
SocketAddress address = client . getAddress ( ) ;
if ( clients . keySet ( ) . contains ( address ) ) {
clients . remove ( address ) ;
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 {
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-06 21:44:34 +01:00
List < PacketIn > list = ( pIn . keySet ( ) . contains ( handle ) ) ? pIn . get ( handle ) : new ArrayList < PacketIn > ( ) ;
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 ) {
for ( String handle : pIn . keySet ( ) ) if ( pIn . get ( handle ) . contains ( packet ) ) pIn . get ( handle ) . remove ( packet ) ;
}
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-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 ) {
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-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 ) {
for ( Client client : clients . values ( ) ) {
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 ) {
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 ) {
2016-12-05 04:21:04 +01:00
allowedAddresses . remove ( address ) ;
}
/ * *
* JSON Encode PacketOut
*
* @param packet PacketOut
* @return JSON Formatted Packet
* @throws IllegalPacketException
* /
2016-12-15 22:04:39 +01:00
protected static JSONObject encodePacket ( PacketOut packet ) throws IllegalPacketException {
2016-12-05 04:21:04 +01:00
JSONObject json = new JSONObject ( ) ;
if ( ! pOut . keySet ( ) . contains ( packet . getClass ( ) ) ) throw new IllegalPacketException ( " Unknown PacketOut Channel: " + packet . getClass ( ) . getCanonicalName ( ) ) ;
2017-01-07 20:06:54 +01:00
if ( packet . getVersion ( ) . toString ( ) = = null ) throw new NullPointerException ( " PacketOut getVersion() cannot be null: " + packet . getClass ( ) . getCanonicalName ( ) ) ;
2016-12-05 04:21:04 +01:00
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 ;
}
/ * *
* JSON Decode PacketIn
*
* @param json JSON to Decode
* @return PacketIn
* @throws IllegalPacketException
* /
2017-01-07 20:06:54 +01:00
protected static List < PacketIn > decodePacket ( JSONObject json ) throws IllegalPacketException {
2016-12-05 04:21:04 +01:00
if ( ! json . keySet ( ) . contains ( " h " ) | | ! json . keySet ( ) . contains ( " v " ) ) throw new IllegalPacketException ( " Unknown Packet Format: " + json . toString ( ) ) ;
if ( ! pIn . keySet ( ) . contains ( json . getString ( " h " ) ) ) throw new IllegalPacketException ( " Unknown PacketIn Channel: " + json . getString ( " h " ) ) ;
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 {
new IllegalPacketException ( " Packet Version Mismatch in " + json . getString ( " h " ) + " : " + json . getString ( " v " ) + " -> " + packet . getVersion ( ) . toString ( ) ) . printStackTrace ( ) ;
}
}
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 ;
}
}