Updated DnsMadeEasy api.

Cleaned up some bungee code.

Added InternetStatus to Bungees.

Fixed some portal stuff.
This commit is contained in:
Jonathan Williams 2014-07-08 12:20:19 -07:00
parent 082cf98e99
commit e6e86c2042
10 changed files with 379 additions and 304 deletions

View File

@ -4,6 +4,7 @@ import mineplex.bungee.lobbyBalancer.LobbyBalancer;
import mineplex.bungee.motd.MotdManager;
import mineplex.bungee.playerCount.PlayerCount;
import mineplex.bungee.playerStats.PlayerStats;
import mineplex.bungee.status.InternetStatus;
import net.md_5.bungee.api.plugin.Plugin;
public class Mineplexer extends Plugin
@ -16,5 +17,6 @@ public class Mineplexer extends Plugin
new PlayerCount(this);
new FileUpdater(this);
new PlayerStats(this);
new InternetStatus(this);
}
}

View File

@ -1,29 +0,0 @@
package mineplex.bungee.groupManager;
import java.util.concurrent.TimeUnit;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.api.plugin.Plugin;
public class GroupManager implements Listener, Runnable
{
private Plugin _plugin;
private GroupRepository _repository;
public GroupManager(Plugin plugin)
{
_plugin = plugin;
_plugin.getProxy().getScheduler().schedule(_plugin, this, 3L, 3L, TimeUnit.SECONDS);
_plugin.getProxy().getPluginManager().registerListener(_plugin, this);
_repository = new GroupRepository();
_repository.initialize();
}
@Override
public void run()
{
}
}

View File

@ -1,194 +0,0 @@
package mineplex.bungee.groupManager;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class GroupRepository
{
private Connection _connection = null;
private String _connectionString = "jdbc:mysql://db.mineplex.com:3306/BungeeServers?autoReconnect=true&failOverReadOnly=false&maxReconnects=10";
private String _userName = "root";
private String _password = "tAbechAk3wR7tuTh";
private static String CREATE_GROUPS_TABLE = "CREATE TABLE IF NOT EXISTS groups (id INT NOT NULL AUTO_INCREMENT, name VARCHAR(256), updated LONG, players INT, maxPlayers INT, ram INT, maxRam INT, PRIMARY KEY (id));";
private static String INSERT_PLAYER_COUNT = "INSERT INTO BungeeServers(address, updated, players, maxPlayers, ram, maxRam) values(?, now(), ?, ?, ?, ?);";
private static String UPDATE_PLAYER_COUNT = "UPDATE BungeeServers SET updated = now(), players = ?, maxPlayers = ?, ram = ?, maxRam = ? WHERE id = ?;";
private static String RETRIEVE_ID = "SELECT id FROM BungeeServers WHERE address = ?;";
private static String RETRIEVE_PLAYER_COUNT = "SELECT SUM(players) AS playerCount, SUM(maxPlayers) AS maxPlayerCount FROM BungeeServers WHERE TIME_TO_SEC(TIMEDIFF(now(), BungeeServers.updated)) < 10;";
private int _id = -1;
private String _address;
private int _maxPlayers = 0;
public void initialize()
{
ResultSet resultSet = null;
PreparedStatement preparedStatement = null;
PreparedStatement preparedStatementRetrieve = null;
PreparedStatement preparedStatementInsert = null;
try
{
if (_connection == null || _connection.isClosed())
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
// Create table
preparedStatement = _connection.prepareStatement(CREATE_GROUPS_TABLE);
preparedStatement.execute();
// Retrieve id
preparedStatementRetrieve = _connection.prepareStatement(RETRIEVE_ID);
preparedStatementRetrieve.setString(1, _address);
resultSet = preparedStatementRetrieve.executeQuery();
while (resultSet.next())
{
_id = resultSet.getInt("id");
}
// Insert if not there
if (_id == -1)
{
preparedStatementInsert = _connection.prepareStatement(INSERT_PLAYER_COUNT, Statement.RETURN_GENERATED_KEYS);
preparedStatementInsert.setString(1, _address);
preparedStatementInsert.setInt(2, 0);
preparedStatementInsert.setInt(3, _maxPlayers);
preparedStatementInsert.setInt(4, (int) ((Runtime.getRuntime().maxMemory() - Runtime.getRuntime().freeMemory()) / 1048576));
preparedStatementInsert.setInt(5, (int) (Runtime.getRuntime().maxMemory() / 1048576));
int affectedRows = preparedStatementInsert.executeUpdate();
if (affectedRows == 0)
{
throw new SQLException("Creating bungee server failed, no rows affected.");
}
resultSet.close();
resultSet = preparedStatementInsert.getGeneratedKeys();
if (resultSet.next())
{
_id = resultSet.getInt(1);
System.out.println("id = " + _id);
}
}
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
if (preparedStatement != null)
{
try
{
preparedStatement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
if (preparedStatementRetrieve != null)
{
try
{
preparedStatementRetrieve.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
if (preparedStatementInsert != null)
{
try
{
preparedStatementInsert.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
if (resultSet != null)
{
try
{
resultSet.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
public boolean updatePlayerCountInDatabase(int players)
{
PreparedStatement preparedStatement = null;
try
{
if (_connection == null || _connection.isClosed())
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
preparedStatement = _connection.prepareStatement(UPDATE_PLAYER_COUNT, Statement.RETURN_GENERATED_KEYS);
preparedStatement.setInt(1, players);
preparedStatement.setInt(2, _maxPlayers);
preparedStatement.setInt(3, (int) ((Runtime.getRuntime().maxMemory() - Runtime.getRuntime().freeMemory()) / 1048576));
preparedStatement.setInt(4, (int) (Runtime.getRuntime().maxMemory() / 1048576));
preparedStatement.setInt(5, _id);
int affectedRows = preparedStatement.executeUpdate();
if (affectedRows == 0)
{
throw new SQLException("Updating bungee server player count failed, no rows affected.");
}
return true;
}
catch (Exception exception)
{
exception.printStackTrace();
try
{
Thread.sleep(10);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
return updatePlayerCountInDatabase(players);
}
finally
{
if (preparedStatement != null)
{
try
{
preparedStatement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
}

View File

@ -0,0 +1,59 @@
package mineplex.bungee.status;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.concurrent.TimeUnit;
import net.md_5.bungee.api.config.ListenerInfo;
import net.md_5.bungee.api.plugin.Plugin;
public class InternetStatus implements Runnable
{
private Plugin _plugin;
private StatusRepository _repository;
public InternetStatus(Plugin plugin)
{
_plugin = plugin;
ListenerInfo listenerInfo = _plugin.getProxy().getConfigurationAdapter().getListeners().iterator().next();
boolean us = !new File("eu.dat").exists();
String address = listenerInfo.getHost().getAddress().getHostAddress() + ":" + listenerInfo.getHost().getPort();
_repository = new StatusRepository(address, us);
_repository.initialize();
_plugin.getProxy().getScheduler().schedule(_plugin, this, 5L, 5L, TimeUnit.MINUTES);
}
@Override
public void run()
{
_repository.updateOnlineStatus(isOnline());
}
private boolean isOnline()
{
try
{
if (InetAddress.getByName("http://www.google.com").isReachable(1000))
return true;
else if (InetAddress.getByName("http://www.espn.com").isReachable(1000))
return true;
else if (InetAddress.getByName("http://www.bing.com").isReachable(1000))
return true;
}
catch (UnknownHostException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return false;
}
}

View File

@ -0,0 +1,120 @@
package mineplex.bungee.status;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class StatusRepository
{
private String _connectionString = "jdbc:mysql://db.mineplex.com:3306/BungeeServers?autoReconnect=true&failOverReadOnly=false&maxReconnects=10";
private String _userName = "root";
private String _password = "tAbechAk3wR7tuTh";
private String _address;
private boolean _us;
private static String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS bungeeOnlineStatus (id INT NOT NULL AUTO_INCREMENT, address VARCHAR(40), online BOOLEAN NOT NULL DEFAULT 0, updated LONG, PRIMARY KEY (id), UNIQUE INDEX addressIndex(address));";
private static String INSERT_SERVER = "INSERT INTO bungeeOnlineStatus (address, online, us, updated) values(?, ?, ?, now()) ON DUPLICATE KEY UPDATE online = VALUES(online);";
public StatusRepository(String address, boolean us)
{
_address = address;
_us = us;
}
public void initialize()
{
Connection connection = null;
PreparedStatement preparedStatement = null;
try
{
connection = DriverManager.getConnection(_connectionString, _userName, _password);
// Create table
preparedStatement = connection.prepareStatement(CREATE_TABLE);
preparedStatement.execute();
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
if (preparedStatement != null)
{
try
{
preparedStatement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
if (connection != null)
{
try
{
connection.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
public void updateOnlineStatus(boolean online)
{
Connection connection = null;
PreparedStatement preparedStatement = null;
try
{
connection = DriverManager.getConnection(_connectionString, _userName, _password);
preparedStatement = connection.prepareStatement(INSERT_SERVER);
preparedStatement.setString(1, _address);
preparedStatement.setBoolean(2, online);
preparedStatement.setBoolean(2, _us);
preparedStatement.executeUpdate();
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
if (preparedStatement != null)
{
try
{
preparedStatement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
if (connection != null)
{
try
{
connection.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
}

View File

@ -3,16 +3,15 @@ package mineplex.core.portal;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Iterator;
import java.util.Map.Entry;
import org.bukkit.Bukkit;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;
import mineplex.core.MiniPlugin;
@ -27,18 +26,19 @@ import mineplex.core.updater.event.UpdateEvent;
public class Portal extends MiniPlugin
{
private static Object _transferLock = new Object();
private HashSet<String> _connectingPlayers = new HashSet<String>();
private PortalRepository _repository = new PortalRepository();
private List<String> _transfers = new ArrayList<String>();
private boolean _retrieve = true;
public Portal(JavaPlugin plugin)
private String _serverName;
public Portal(JavaPlugin plugin, String serverName)
{
super("Portal", plugin);
_serverName = serverName;
Bukkit.getMessenger().registerOutgoingPluginChannel(GetPlugin(), "BungeeCord");
_repository.initialize(plugin.getConfig().getBoolean("serverstatus.us"), plugin.getConfig().getString("serverstatus.connectionurl"));
@ -145,36 +145,13 @@ public class Portal extends MiniPlugin
AddCommand(new SendCommand(this));
}
@EventHandler
public void addTransferDelete(final PlayerQuitEvent event)
{
boolean delete = false;
synchronized (_transferLock)
{
delete = _transfers.remove(event.getPlayer().getName());
}
if (delete)
{
Bukkit.getScheduler().runTaskAsynchronously(GetPlugin(), new Runnable()
{
public void run()
{
_repository.deleteServerTransfers(event.getPlayer().getName());
}
});
}
}
@EventHandler
public void checkForServerTransfers(UpdateEvent event)
{
_retrieve = !_retrieve;
if (event.getType() != UpdateType.SEC || Bukkit.getOnlinePlayers().length == 0)
return;
_retrieve = !_retrieve;
if (_retrieve)
{
@ -182,12 +159,19 @@ public class Portal extends MiniPlugin
{
public void run()
{
final NautHashMap<String, String> entries = _repository.retrieveServerTransfers();
final NautHashMap<String, String> entries = _repository.retrieveServerTransfers();
synchronized (_transferLock)
for (Iterator<String> iterator = entries.keySet().iterator(); iterator.hasNext();)
{
_transfers.addAll(entries.keySet());
}
String playerName = iterator.next();
String serverName = entries.get(playerName);
if (serverName.equalsIgnoreCase(_serverName))
{
_repository.deleteServerTransfers(playerName);
iterator.remove();
}
}
Bukkit.getScheduler().runTask(GetPlugin(), new Runnable()
{

View File

@ -1,65 +1,103 @@
package mineplex.ddos;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.google.gson.reflect.TypeToken;
import mineplex.ddos.api.ApiGetCall;
import mineplex.ddos.api.ApiPostCall;
import mineplex.ddos.api.ApiPutCall;
import mineplex.ddos.api.token.ARecord;
import mineplex.ddos.api.token.CNameRecord;
import mineplex.ddos.api.token.DnsRecord;
import mineplex.ddos.api.token.DomainRecords;
public class DDoSProtectionSwitcher
{
public static void main (String args[])
public static void main(String args[])
{
/*
List<ARecord> records = new ArrayList<ARecord>();
records.add(new ARecord("us2", "108.178.20.166", 300));
records.add(new ARecord("us2", "108.163.217.110", 300));
records.add(new ARecord("us2", "108.178.34.142", 300));
records.add(new ARecord("us2", "108.163.254.134", 300));
records.add(new ARecord("us2", "108.163.216.250", 300));
records.add(new ARecord("us2", "108.163.216.106", 300));
records.add(new ARecord("us2", "184.154.215.170", 300));
records.add(new ARecord("us2", "96.127.174.206", 300));
records.add(new ARecord("us2", "184.154.127.10", 300));
records.add(new ARecord("us2", "96.127.174.146", 300));
records.add(new ARecord("us2", "108.178.7.206", 300));
records.add(new ARecord("us2", "184.154.39.146", 300));
records.add(new ARecord("us2", "108.163.217.250", 300));
records.add(new ARecord("us2", "69.175.15.242", 300));
records.add(new ARecord("us2", "107.6.129.126", 300));
records.add(new ARecord("us2", "108.163.222.174", 300));
records.add(new ARecord("us2", "108.178.34.118", 300));
records.add(new ARecord("us2", "69.175.4.38", 300));
records.add(new ARecord("us2", "107.6.158.78", 300));
records.add(new ARecord("us2", "184.154.13.118", 300));
new ApiPostCall("http://api.dnsmadeeasy.com/V2.0/dns/managed/", 962728, "/records/", "createMulti").Execute(records);
/*
records.add(new ARecord("us", "108.178.20.166", 300));
records.add(new ARecord("us", "108.163.217.110", 300));
records.add(new ARecord("us", "108.178.34.142", 300));
records.add(new ARecord("us", "108.163.254.134", 300));
records.add(new ARecord("us", "108.163.216.250", 300));
records.add(new ARecord("us", "108.163.216.106", 300));
records.add(new ARecord("us", "184.154.215.170", 300));
records.add(new ARecord("us", "96.127.174.206", 300));
records.add(new ARecord("us", "184.154.127.10", 300));
records.add(new ARecord("us", "96.127.174.146", 300));
records.add(new ARecord("us", "108.178.7.206", 300));
records.add(new ARecord("us", "184.154.39.146", 300));
records.add(new ARecord("us", "108.163.217.250", 300));
records.add(new ARecord("us", "69.175.15.242", 300));
records.add(new ARecord("us", "107.6.129.126", 300));
records.add(new ARecord("us", "108.163.222.174", 300));
records.add(new ARecord("us", "108.178.34.118", 300));
records.add(new ARecord("us", "69.175.4.38", 300));
records.add(new ARecord("us", "107.6.158.78", 300));
records.add(new ARecord("us", "184.154.13.118", 300));
*/
DomainRecords records = new ApiGetCall("https://api.dnsmadeeasy.com/V2.0/dns/managed/", 962728, "/records", "").Execute(DomainRecords.class);
List<DnsRecord> recordsToDelete = new ArrayList<DnsRecord>();
// Switch off ddos protection
for (DnsRecord record : records.data)
{
if (record.type.equalsIgnoreCase("CNAME"))
{
if (record.name.equalsIgnoreCase("eu"))
recordsToDelete.add(record);
}
}
// Remove CNAME record for eu to us
// Loop through and update us to neustar, us2 to us, eu2 to us.
// Loop through and update us to us2, eu to eu2 and neustar to us.
// Add CNAME record for eu to us
return;
records.add(new ARecord("eu", "107.6.176.194", 300));
records.add(new ARecord("eu", "107.6.176.122", 300));
records.add(new ARecord("eu", "107.6.176.166", 300));
records.add(new ARecord("eu", "107.6.176.14", 300));
records.add(new ARecord("eu", "107.6.176.114", 300));
records.add(new ARecord("eu", "107.6.176.26", 300));
records.add(new ARecord("eu", "107.6.176.110", 300));
records.add(new ARecord("eu", "107.6.176.138", 300));
new ApiPostCall("https://api.dnsmadeeasy.com/V2.0/dns/managed/", 962728, "/records/", "createMulti")
.Execute(records);
/*
*
* DomainRecords records = new
* ApiGetCall("https://api.dnsmadeeasy.com/V2.0/dns/managed/", 962728,
* "/records", "").Execute(DomainRecords.class); List<DnsRecord>
* recordsToDelete = new ArrayList<DnsRecord>(); List<DnsRecord>
* recordsToAdd = new ArrayList<DnsRecord>(); List<DnsRecord>
* recordsToModify = new ArrayList<DnsRecord>();
*
* // Switch off ddos protection for (DnsRecord record : records.data) {
* if (record.type.equalsIgnoreCase("CNAME")) { if
* (record.name.equalsIgnoreCase("eu")) recordsToDelete.add(record); }
* else if (record.type.equalsIgnoreCase("A")) { if
* (record.name.equalsIgnoreCase("us")) { record.name = "neustar";
* recordsToModify.add(record); } else if
* (record.name.equalsIgnoreCase("us2")) { record.name = "us";
* recordsToModify.add(record); } else if
* (record.name.equalsIgnoreCase("eu2")) { record.name = "eu";
* recordsToModify.add(record); } } }
*
* /* // Switch on ddos protection for (DnsRecord record : records.data)
* { if (record.type.equalsIgnoreCase("A")) { if
* (record.name.equalsIgnoreCase("neustar")) { record.name = "us";
* recordsToModify.put(record.id, record); } else if
* (record.name.equalsIgnoreCase("us")) { record.name = "us2";
* recordsToModify.put(record.id, record); } else if
* (record.name.equalsIgnoreCase("eu")) { record.name = "eu2";
* recordsToModify.put(record.id, record); } } }
*
* recordsToAdd.add(new CNameRecord("eu", "us", 300));
*
*
* if (recordsToAdd.size() > 0) new
* ApiPostCall("https://api.dnsmadeeasy.com/V2.0/dns/managed/", 962728,
* "/records/", "createMulti").Execute(records);
*
* if (recordsToModify.size() > 0) { new
* ApiPutCall("https://api.dnsmadeeasy.com/V2.0/dns/managed/", 962728,
* "/records/", "updateMulti").Execute(recordsToModify); }
*
* if (recordsToDelete.size() > 0) new
* ApiPostCall("https://api.dnsmadeeasy.com/V2.0/dns/managed/", 962728,
* "/records/", "createMulti").Execute(records);
*
* System.exit(0);
*/
}
}

View File

@ -0,0 +1,80 @@
package mineplex.ddos.api;
import java.lang.reflect.Type;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import com.google.gson.Gson;
public class ApiPutCall extends DnsMadeEasyApiCallBase
{
private String _action;
public ApiPutCall(String apiUrl, int domainId, String category, String action)
{
super(apiUrl, domainId, category);
_action = action;
}
public void Execute(Object argument)
{
Gson gson = new Gson();
HttpPut request = new HttpPut(ApiUrl + DomainId + Category + _action);
System.out.println(request.getURI().toString());
try
{
StringEntity params = new StringEntity(gson.toJson(argument));
params.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
request.setEntity(params);
}
catch (Exception exception)
{
System.out.println("Error executing ApiPostCall(Object): \n" + exception.getMessage());
for (StackTraceElement trace : exception.getStackTrace())
{
System.out.println(trace);
}
}
System.out.println(execute(request));
}
public <T> T Execute(Class<T> returnClass)
{
return Execute(returnClass, (Object)null);
}
public <T> T Execute(Type returnType, Object argument)
{
Gson gson = new Gson();
HttpPut request = new HttpPut(ApiUrl + DomainId + Category + _action);
System.out.println(request.getURI().toString());
try
{
StringEntity params = new StringEntity(gson.toJson(argument));
params.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
request.setEntity(params);
}
catch (Exception exception)
{
System.out.println("Error executing ApiPutCall(Type, Object): \n" + exception.getMessage());
for (StackTraceElement trace : exception.getStackTrace())
{
System.out.println(trace);
}
}
String response = execute(request);
System.out.println(response);
return new Gson().fromJson(response, returnType);
}
}

View File

@ -0,0 +1,14 @@
package mineplex.ddos.api.token;
public class CNameRecord extends DnsRecord
{
public CNameRecord(String recordName, String ip, int recordTtl)
{
name = recordName;
value = ip;
ttl = recordTtl;
type = "CNAME";
gtdLocation = "DEFAULT";
}
}

View File

@ -2,6 +2,7 @@ package mineplex.ddos.api.token;
public class DnsRecord
{
public int id;
public String name;
public String type;
public String value;