From 2f563f8277e50674e55be2a1e1cab6958d275a46 Mon Sep 17 00:00:00 2001 From: Jonathan Williams Date: Wed, 29 Oct 2014 17:15:32 -0700 Subject: [PATCH] BungeeRotator project. --- Plugins/BuildFiles/common.xml | 24 +++ Plugins/Mineplex.BungeeRotator/.classpath | 12 ++ .../BungeeRotator.launch | 17 ++ Plugins/Mineplex.BungeeRotator/.project | 27 +++ .../.settings/org.eclipse.jdt.core.prefs | 11 ++ .../src/mineplex/bungee/BungeeRepository.java | 142 ++++++++++++++ .../src/mineplex/bungee/BungeeRotator.java | 177 ++++++++++++++++++ .../src/mineplex/bungee/BungeeServer.java | 9 + .../src/mineplex/bungee/BungeeSorter.java | 17 ++ .../src/mineplex/bungee/GenericRunnable.java | 6 + .../src/mineplex/bungee/ProcessRunner.java | 81 ++++++++ .../mineplex/bungee/api/ApiDeleteCall.java | 18 ++ .../src/mineplex/bungee/api/ApiGetCall.java | 33 ++++ .../src/mineplex/bungee/api/ApiPostCall.java | 77 ++++++++ .../src/mineplex/bungee/api/ApiPutCall.java | 79 ++++++++ .../bungee/api/DnsMadeEasyApiCallBase.java | 140 ++++++++++++++ .../mineplex/bungee/api/token/ARecord.java | 14 ++ .../bungee/api/token/CNameRecord.java | 14 ++ .../mineplex/bungee/api/token/DnsRecord.java | 11 ++ .../bungee/api/token/DomainRecords.java | 11 ++ 20 files changed, 920 insertions(+) create mode 100644 Plugins/Mineplex.BungeeRotator/.classpath create mode 100644 Plugins/Mineplex.BungeeRotator/.externalToolBuilders/BungeeRotator.launch create mode 100644 Plugins/Mineplex.BungeeRotator/.project create mode 100644 Plugins/Mineplex.BungeeRotator/.settings/org.eclipse.jdt.core.prefs create mode 100644 Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/BungeeRepository.java create mode 100644 Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/BungeeRotator.java create mode 100644 Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/BungeeServer.java create mode 100644 Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/BungeeSorter.java create mode 100644 Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/GenericRunnable.java create mode 100644 Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/ProcessRunner.java create mode 100644 Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/api/ApiDeleteCall.java create mode 100644 Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/api/ApiGetCall.java create mode 100644 Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/api/ApiPostCall.java create mode 100644 Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/api/ApiPutCall.java create mode 100644 Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/api/DnsMadeEasyApiCallBase.java create mode 100644 Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/api/token/ARecord.java create mode 100644 Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/api/token/CNameRecord.java create mode 100644 Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/api/token/DnsRecord.java create mode 100644 Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/api/token/DomainRecords.java diff --git a/Plugins/BuildFiles/common.xml b/Plugins/BuildFiles/common.xml index 98bb39185..a479bb3bb 100644 --- a/Plugins/BuildFiles/common.xml +++ b/Plugins/BuildFiles/common.xml @@ -213,6 +213,30 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Plugins/Mineplex.BungeeRotator/.classpath b/Plugins/Mineplex.BungeeRotator/.classpath new file mode 100644 index 000000000..feffdd123 --- /dev/null +++ b/Plugins/Mineplex.BungeeRotator/.classpath @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/Plugins/Mineplex.BungeeRotator/.externalToolBuilders/BungeeRotator.launch b/Plugins/Mineplex.BungeeRotator/.externalToolBuilders/BungeeRotator.launch new file mode 100644 index 000000000..894d3947c --- /dev/null +++ b/Plugins/Mineplex.BungeeRotator/.externalToolBuilders/BungeeRotator.launch @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + diff --git a/Plugins/Mineplex.BungeeRotator/.project b/Plugins/Mineplex.BungeeRotator/.project new file mode 100644 index 000000000..6378b1f50 --- /dev/null +++ b/Plugins/Mineplex.BungeeRotator/.project @@ -0,0 +1,27 @@ + + + Mineplex.BungeeRotator + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.ui.externaltools.ExternalToolBuilder + auto,full,incremental, + + + LaunchConfigHandle + <project>/.externalToolBuilders/BungeeRotator.launch + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/Plugins/Mineplex.BungeeRotator/.settings/org.eclipse.jdt.core.prefs b/Plugins/Mineplex.BungeeRotator/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 000000000..7341ab168 --- /dev/null +++ b/Plugins/Mineplex.BungeeRotator/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/BungeeRepository.java b/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/BungeeRepository.java new file mode 100644 index 000000000..f7236016a --- /dev/null +++ b/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/BungeeRepository.java @@ -0,0 +1,142 @@ +package mineplex.bungee; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.List; + +public class BungeeRepository +{ + // Yip Yip actual IP because if null route happens we can't resolve the HOSTNAME DERP FACE DEFEK7!!! -defek7 + private String _connectionString = "jdbc:mysql://10.35.74.133:3306/BungeeServers?autoReconnect=true&failOverReadOnly=false&maxReconnects=10"; + private String _userName = "root"; + private String _password = "tAbechAk3wR7tuTh"; + + private static String CREATE_ONLINE_TABLE = "CREATE TABLE IF NOT EXISTS bungeeOnlineStatus (id INT NOT NULL AUTO_INCREMENT, address VARCHAR(40), online BOOLEAN NOT NULL DEFAULT 0, updated LONG, us BOOLEAN NOT NULL DEFAULT 1, lastOnline LONG, PRIMARY KEY (id), UNIQUE INDEX addressIndex(address));"; + private static String CREATE_PLAYER_TABLE = "CREATE TABLE IF NOT EXISTS BungeeServers (id INT NOT NULL AUTO_INCREMENT, address VARCHAR(256), updated LONG, players INT, maxPlayers INT, ram INT, maxRam INT, PRIMARY KEY (id));"; + + private static String RETRIEVE_SERVERS_PLAYERCOUNTS = "SELECT bos.address, players, maxPlayers, BungeeServers.updated, bos.lastOnline, BungeeServers.US, now() FROM BungeeServers INNER JOIN bungeeOnlineStatus AS bos ON bos.address = BungeeServers.address WHERE TIME_TO_SEC(TIMEDIFF(now(), BungeeServers.updated)) < 15;"; + + public void initialize() + { + Connection connection = null; + PreparedStatement preparedStatement = null; + + try + { + connection = DriverManager.getConnection(_connectionString, _userName, _password); + + // Create table + preparedStatement = connection.prepareStatement(CREATE_ONLINE_TABLE); + preparedStatement.execute(); + + preparedStatement.close(); + + preparedStatement = connection.prepareStatement(CREATE_PLAYER_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 List getBungeeServers() + { + List bungeeServers = new ArrayList(); + Connection connection = null; + PreparedStatement preparedStatement = null; + ResultSet resultSet = null; + + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + + try + { + connection = DriverManager.getConnection(_connectionString, _userName, _password); + + preparedStatement = connection.prepareStatement(RETRIEVE_SERVERS_PLAYERCOUNTS); + + resultSet = preparedStatement.executeQuery(); + + while (resultSet.next()) + { + long current = dateFormat.parse(resultSet.getString(7)).getTime(); + long updated = dateFormat.parse(resultSet.getString(4)).getTime(); + long lastOnline = dateFormat.parse(resultSet.getString(5)).getTime(); + + if (current - updated <= 15000 && current - lastOnline <= 660000) + { + BungeeServer server = new BungeeServer(); + server.Address = resultSet.getString(1).split(":")[0]; + server.Players = resultSet.getInt(2); + server.MaxPlayers = resultSet.getInt(3); + server.US = resultSet.getBoolean(6); + + bungeeServers.add(server); + } + } + } + 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(); + } + } + } + + return bungeeServers; + } +} diff --git a/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/BungeeRotator.java b/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/BungeeRotator.java new file mode 100644 index 000000000..4b639dbc6 --- /dev/null +++ b/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/BungeeRotator.java @@ -0,0 +1,177 @@ +package mineplex.bungee; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; + +import mineplex.bungee.api.ApiDeleteCall; +import mineplex.bungee.api.ApiGetCall; +import mineplex.bungee.api.ApiPostCall; +import mineplex.bungee.api.token.ARecord; +import mineplex.bungee.api.token.DnsRecord; +import mineplex.bungee.api.token.DomainRecords; + +public class BungeeRotator +{ + private static BungeeRepository _repository = null; + private static HashSet _processes = new HashSet(); + + public static void main(String args[]) + { + try + { + Class.forName("com.mysql.jdbc.Driver"); + } + catch (ClassNotFoundException e1) + { + e1.printStackTrace(); + } + + _repository = new BungeeRepository(); + BungeeSorter bungeeSorter = new BungeeSorter(); + int maxRecordCount = 10; + + while (true) + { + List bungeeServers = _repository.getBungeeServers(); + HashSet usServers = new HashSet(); + HashSet euServers = new HashSet(); + + Collections.sort(bungeeServers, bungeeSorter); + + for (BungeeServer server : bungeeServers) + { + if (usServers.size() < maxRecordCount && server.US) + { + System.out.println("SELECTED " + server.Address + " " + (server.US ? "us" : "eu") + " " + server.Players + "/" + server.MaxPlayers); + usServers.add(server.Address); + } + else if (euServers.size() < maxRecordCount && !server.US) + { + System.out.println("SELECTED " + server.Address + " " + (server.US ? "us" : "eu") + " " + server.Players + "/" + server.MaxPlayers); + euServers.add(server.Address); + } + } + + DomainRecords records = new ApiGetCall("https://api.dnsmadeeasy.com/V2.0/dns/managed/", 962728, + "/records", "").Execute(DomainRecords.class); + List recordsToDelete = new ArrayList(); + List recordsToAdd = new ArrayList(); + + for (DnsRecord record : records.data) + { + if (record.type.equalsIgnoreCase("A")) + { + if (record.name.equalsIgnoreCase("us")) + { + if (usServers.contains(record.value)) + usServers.remove(record.value); + else + recordsToDelete.add(record); + } + else if (record.name.equalsIgnoreCase("eu")) + { + if (euServers.contains(record.value)) + euServers.remove(record.value); + else + recordsToDelete.add(record); + } + } + } + + for (String address : usServers) + { + recordsToAdd.add(new ARecord("us", address, 300)); + System.out.println("Addding server address in DNS : " + "us " + address); + } + + for (String address : euServers) + { + recordsToAdd.add(new ARecord("eu", address, 300)); + System.out.println("Addding server address in DNS : " + "eu " + address); + } + + if (recordsToAdd.size() > 0) + { + new ApiPostCall("https://api.dnsmadeeasy.com/V2.0/dns/managed/", 962728, "/records/", "createMulti").Execute(recordsToAdd); + System.out.println("Created " + recordsToAdd.size() + " records."); + } + + if (recordsToDelete.size() > 0) + { + StringBuilder idBuilder = new StringBuilder(); + + for (DnsRecord record : recordsToDelete) + { + if (idBuilder.length() != 0) + idBuilder.append("&"); + + idBuilder.append("ids=" + record.id); + } + + new ApiDeleteCall("https://api.dnsmadeeasy.com/V2.0/dns/managed/", 962728, "/records?" + idBuilder.toString()).Execute(); + System.out.println("Deleted " + recordsToDelete.size() + " records."); + } + + int processWaits = 0; + + while (_processes.size() > 0) + { + for (Iterator iterator = _processes.iterator(); iterator.hasNext();) + { + ProcessRunner pr = iterator.next(); + + try + { + pr.join(100); + } + catch (InterruptedException e) + { + e.printStackTrace(); + } + + if (pr.isDone()) + iterator.remove(); + } + + if (_processes.size() > 0) + { + try + { + Thread.sleep(6000); + } + catch (InterruptedException e) + { + e.printStackTrace(); + } + } + + if (processWaits >= 60) + { + System.out.println("Killing stale processes."); + + for (Iterator iterator = _processes.iterator(); iterator.hasNext();) + { + iterator.next().abort(); + iterator.remove(); + } + } + + processWaits++; + } + + processWaits = 0; + + try + { + Thread.sleep(30000); + } + catch (InterruptedException e) + { + e.printStackTrace(); + } + } + } +} diff --git a/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/BungeeServer.java b/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/BungeeServer.java new file mode 100644 index 000000000..dc56124f8 --- /dev/null +++ b/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/BungeeServer.java @@ -0,0 +1,9 @@ +package mineplex.bungee; + +public class BungeeServer +{ + public String Address; + public int Players; + public int MaxPlayers; + public boolean US; +} diff --git a/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/BungeeSorter.java b/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/BungeeSorter.java new file mode 100644 index 000000000..c42f31f04 --- /dev/null +++ b/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/BungeeSorter.java @@ -0,0 +1,17 @@ +package mineplex.bungee; + +import java.util.Comparator; + +public class BungeeSorter implements Comparator +{ + public int compare(BungeeServer a, BungeeServer b) + { + if (a.Players < b.Players) + return -1; + + if (b.Players < a.Players) + return 1; + + return 0; + } +} \ No newline at end of file diff --git a/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/GenericRunnable.java b/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/GenericRunnable.java new file mode 100644 index 000000000..4a82613f5 --- /dev/null +++ b/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/GenericRunnable.java @@ -0,0 +1,6 @@ +package mineplex.bungee; + +public interface GenericRunnable +{ + void run(T t); +} diff --git a/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/ProcessRunner.java b/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/ProcessRunner.java new file mode 100644 index 000000000..27ba8323d --- /dev/null +++ b/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/ProcessRunner.java @@ -0,0 +1,81 @@ +package mineplex.bungee; + +import java.io.BufferedReader; +import java.io.InputStreamReader; + +public class ProcessRunner extends Thread +{ + private ProcessBuilder _processBuilder; + private Process _process; + private GenericRunnable _runnable; + + boolean _done = false; + Boolean _error = false; + + ProcessRunner(String[] args) + { + super("ProcessRunner " + args); + _processBuilder = new ProcessBuilder(args); + } + + public void run() + { + try + { + _process = _processBuilder.start(); + _process.waitFor(); + + BufferedReader reader=new BufferedReader(new InputStreamReader(_process.getInputStream())); + String line = reader.readLine(); + + while(line != null) + { + if (line.equals("255")) + _error = true; + + line=reader.readLine(); + } + } + catch (Exception e) + { + System.out.println(e.getMessage()); + } + finally + { + _done = true; + + if (_runnable != null) + _runnable.run(_error); + } + } + + public void start(GenericRunnable runnable) + { + super.start(); + + _runnable = runnable; + } + + public int exitValue() throws IllegalStateException + { + if (_process != null) + { + return _process.exitValue(); + } + + throw new IllegalStateException("Process not started yet"); + } + + public boolean isDone() + { + return _done; + } + + public void abort() + { + if (!isDone()) + { + _process.destroy(); + } + } + } diff --git a/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/api/ApiDeleteCall.java b/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/api/ApiDeleteCall.java new file mode 100644 index 000000000..57c24889c --- /dev/null +++ b/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/api/ApiDeleteCall.java @@ -0,0 +1,18 @@ +package mineplex.bungee.api; + +import org.apache.http.client.methods.HttpDelete; + +public class ApiDeleteCall extends DnsMadeEasyApiCallBase +{ + public ApiDeleteCall(String apiUrl, int domainId, String category) + { + super(apiUrl, domainId, category); + } + + public void Execute() + { + HttpDelete request = new HttpDelete(ApiUrl + DomainId + Category); + + execute(request); + } +} diff --git a/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/api/ApiGetCall.java b/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/api/ApiGetCall.java new file mode 100644 index 000000000..a28aaa148 --- /dev/null +++ b/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/api/ApiGetCall.java @@ -0,0 +1,33 @@ +package mineplex.bungee.api; + +import java.lang.reflect.Type; + +import org.apache.http.client.methods.HttpGet; + +import com.google.gson.Gson; + +public class ApiGetCall extends DnsMadeEasyApiCallBase +{ + private String _action; + + public ApiGetCall(String apiUrl, int domainId, String category, String action) + { + super(apiUrl, domainId, category); + + _action = action; + } + + public void Execute() + { + HttpGet request = new HttpGet(ApiUrl + DomainId + Category + _action); + execute(request); + } + + public T Execute(Type returnType) + { + HttpGet request = new HttpGet(ApiUrl + DomainId + Category + _action); + + String response = execute(request); + return new Gson().fromJson(response, returnType); + } +} diff --git a/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/api/ApiPostCall.java b/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/api/ApiPostCall.java new file mode 100644 index 000000000..26d605386 --- /dev/null +++ b/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/api/ApiPostCall.java @@ -0,0 +1,77 @@ +package mineplex.bungee.api; + +import java.lang.reflect.Type; + +import org.apache.http.client.methods.HttpPost; +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 ApiPostCall extends DnsMadeEasyApiCallBase +{ + private String _action; + + public ApiPostCall(String apiUrl, int domainId, String category, String action) + { + super(apiUrl, domainId, category); + + _action = action; + } + + public void Execute(Object argument) + { + Gson gson = new Gson(); + HttpPost request = new HttpPost(ApiUrl + DomainId + Category + _action); + + 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); + } + } + + execute(request); + } + + public T Execute(Class returnClass) + { + return Execute(returnClass, (Object)null); + } + + public T Execute(Type returnType, Object argument) + { + Gson gson = new Gson(); + HttpPost request = new HttpPost(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(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); + } +} diff --git a/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/api/ApiPutCall.java b/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/api/ApiPutCall.java new file mode 100644 index 000000000..bfe4bdd83 --- /dev/null +++ b/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/api/ApiPutCall.java @@ -0,0 +1,79 @@ +package mineplex.bungee.api; + +import java.lang.reflect.Type; + +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 ApiPutCall(Object): \n" + exception.getMessage()); + + for (StackTraceElement trace : exception.getStackTrace()) + { + System.out.println(trace); + } + } + + System.out.println(execute(request)); + } + + public T Execute(Class returnClass) + { + return Execute(returnClass, (Object)null); + } + + public 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); + } +} diff --git a/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/api/DnsMadeEasyApiCallBase.java b/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/api/DnsMadeEasyApiCallBase.java new file mode 100644 index 000000000..87ade774a --- /dev/null +++ b/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/api/DnsMadeEasyApiCallBase.java @@ -0,0 +1,140 @@ +package mineplex.bungee.api; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Locale; +import java.util.TimeZone; + +import javax.crypto.Mac; +import javax.crypto.spec.SecretKeySpec; + +import org.apache.commons.codec.binary.Hex; +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpRequestBase; +import org.apache.http.conn.scheme.PlainSocketFactory; +import org.apache.http.conn.scheme.Scheme; +import org.apache.http.conn.scheme.SchemeRegistry; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.impl.conn.PoolingClientConnectionManager; + +public abstract class DnsMadeEasyApiCallBase +{ + protected String ApiUrl = "http://api.dnsmadeeasy.com/V2.0/dns/managed/"; + protected int DomainId = 962728; + protected String Category = "/records/"; + + public DnsMadeEasyApiCallBase(String apiUrl, int domainId, String category) + { + ApiUrl = apiUrl; + DomainId = domainId; + Category = category; + } + + protected String execute(HttpRequestBase request) + { + SchemeRegistry schemeRegistry = new SchemeRegistry(); + schemeRegistry.register(new Scheme("https", 80, PlainSocketFactory.getSocketFactory())); + + PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager(schemeRegistry); + connectionManager.setMaxTotal(200); + connectionManager.setDefaultMaxPerRoute(20); + + HttpClient httpClient = new DefaultHttpClient(connectionManager); + InputStream in = null; + String response = ""; + + try + { + String timeStamp = getServerTime(); + SecretKeySpec keySpec = new SecretKeySpec("8c9af8cc-d306-4df3-8de8-944deafa8239".getBytes(), "HmacSHA1"); + Mac mac = Mac.getInstance("HmacSHA1"); + mac.init(keySpec); + byte[] hashBytes = mac.doFinal((timeStamp + "").getBytes()); + Hex.encodeHexString(hashBytes); + + request.addHeader("x-dnsme-apiKey", "610e21ee-4250-4b55-b637-a1fcc3847850"); + request.addHeader("x-dnsme-requestDate", timeStamp + ""); + request.addHeader("x-dnsme-hmac", Hex.encodeHexString(hashBytes)); + request.addHeader("Content-Type", "application/json"); + + HttpResponse httpResponse = httpClient.execute(request); + + if (httpResponse != null) + { + in = httpResponse.getEntity().getContent(); + response = convertStreamToString(in); + } + } + catch (Exception ex) + { + System.out.println("DnsMadeEasyApiCall Error:\n" + ex.getMessage()); + + for (StackTraceElement trace : ex.getStackTrace()) + { + System.out.println(trace); + } + } + finally + { + httpClient.getConnectionManager().shutdown(); + + if (in != null) + { + try + { + in.close(); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + } + + return response; + } + + protected String getServerTime() + { + Calendar calendar = Calendar.getInstance(); + SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US); + dateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); + return dateFormat.format(calendar.getTime()); + } + + protected String convertStreamToString(InputStream is) + { + BufferedReader reader = new BufferedReader(new InputStreamReader(is)); + StringBuilder sb = new StringBuilder(); + + String line = null; + try + { + while ((line = reader.readLine()) != null) + { + sb.append(line + "\n"); + } + } + catch (IOException e) + { + e.printStackTrace(); + } + finally + { + try + { + is.close(); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + return sb.toString(); + } +} diff --git a/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/api/token/ARecord.java b/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/api/token/ARecord.java new file mode 100644 index 000000000..f8e66deab --- /dev/null +++ b/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/api/token/ARecord.java @@ -0,0 +1,14 @@ +package mineplex.bungee.api.token; + +public class ARecord extends DnsRecord +{ + public ARecord(String recordName, String ip, int recordTtl) + { + name = recordName; + value = ip; + ttl = recordTtl; + + type = "A"; + gtdLocation = "DEFAULT"; + } +} diff --git a/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/api/token/CNameRecord.java b/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/api/token/CNameRecord.java new file mode 100644 index 000000000..e928daa89 --- /dev/null +++ b/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/api/token/CNameRecord.java @@ -0,0 +1,14 @@ +package mineplex.bungee.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"; + } +} diff --git a/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/api/token/DnsRecord.java b/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/api/token/DnsRecord.java new file mode 100644 index 000000000..a4d50c840 --- /dev/null +++ b/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/api/token/DnsRecord.java @@ -0,0 +1,11 @@ +package mineplex.bungee.api.token; + +public class DnsRecord +{ + public int id; + public String name; + public String type; + public String value; + public String gtdLocation; + public int ttl; +} diff --git a/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/api/token/DomainRecords.java b/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/api/token/DomainRecords.java new file mode 100644 index 000000000..79308ce73 --- /dev/null +++ b/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/api/token/DomainRecords.java @@ -0,0 +1,11 @@ +package mineplex.bungee.api.token; + +import java.util.List; + +public class DomainRecords +{ + public List data; + public int page; + public int totalPage; + public int totalRecords; +}