BungeeRotator project.
This commit is contained in:
parent
006e20bb13
commit
2f563f8277
@ -213,6 +213,30 @@
|
||||
</jar>
|
||||
<copy file="../bin/DDoSProtectionSwitcher.jar" todir="../../Testing/DDoSProtectionSwitcher/"/>
|
||||
</target>
|
||||
<target name ="BungeeRotator" description="BungeeRotator">
|
||||
<jar jarfile="../bin/BungeeRotator.jar">
|
||||
<fileset dir="../Mineplex.BungeeRotator/bin">
|
||||
<include name="**/*.class"/>
|
||||
</fileset>
|
||||
|
||||
<zipfileset src="../Libraries/mysql.zip" />
|
||||
|
||||
<manifest>
|
||||
<attribute name="Main-Class"
|
||||
value="mineplex.bungee.BungeeRotator"/>
|
||||
</manifest>
|
||||
|
||||
<zipfileset src="../Libraries/httpclient-4.2.jar" />
|
||||
<zipfileset src="../Libraries/httpcore-4.2.jar" />
|
||||
<zipfileset src="../Libraries/httpclient-cache-4.2.jar" />
|
||||
<zipfileset src="../Libraries/httpmime-4.2.jar" />
|
||||
<zipfileset src="../Libraries/gson-2.2.1.jar" />
|
||||
<zipfileset src="../Libraries/javax.mail.jar" />
|
||||
<zipfileset src="../Libraries/commons-logging-1.1.1.jar" />
|
||||
<zipfileset src="../Libraries/commons-codec-1.6.jar" />
|
||||
</jar>
|
||||
<copy file="../bin/BungeeRotator.jar" todir="../../Testing/BungeeRotator/"/>
|
||||
</target>
|
||||
<target name ="ServerMonitor" description="ServerMonitor">
|
||||
<jar jarfile="../bin/ServerMonitor.jar">
|
||||
<fileset dir="../Mineplex.Core.Common/bin">
|
||||
|
12
Plugins/Mineplex.BungeeRotator/.classpath
Normal file
12
Plugins/Mineplex.BungeeRotator/.classpath
Normal file
@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jre7"/>
|
||||
<classpathentry kind="var" path="REPO_DIR/Plugins/Libraries/httpclient-4.2.jar"/>
|
||||
<classpathentry kind="var" path="REPO_DIR/Plugins/Libraries/httpcore-4.2.jar"/>
|
||||
<classpathentry kind="var" path="REPO_DIR/Plugins/Libraries/commons-codec-1.6.jar"/>
|
||||
<classpathentry kind="var" path="REPO_DIR/Plugins/Libraries/gson-2.2.1.jar"/>
|
||||
<classpathentry kind="var" path="REPO_DIR/Plugins/Libraries/javax.mail.jar"/>
|
||||
<classpathentry combineaccessrules="false" kind="src" path="/Mineplex.Core"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<launchConfiguration type="org.eclipse.ant.AntBuilderLaunchConfigurationType">
|
||||
<stringAttribute key="org.eclipse.ant.ui.ATTR_ANT_AFTER_CLEAN_TARGETS" value="BungeeRotator,"/>
|
||||
<stringAttribute key="org.eclipse.ant.ui.ATTR_ANT_AUTO_TARGETS" value="BungeeRotator,"/>
|
||||
<stringAttribute key="org.eclipse.ant.ui.ATTR_ANT_MANUAL_TARGETS" value="BungeeRotator,"/>
|
||||
<booleanAttribute key="org.eclipse.ant.ui.ATTR_TARGETS_UPDATED" value="true"/>
|
||||
<booleanAttribute key="org.eclipse.ant.ui.DEFAULT_VM_INSTALL" value="false"/>
|
||||
<stringAttribute key="org.eclipse.debug.core.ATTR_REFRESH_SCOPE" value="${project}"/>
|
||||
<booleanAttribute key="org.eclipse.debug.ui.ATTR_LAUNCH_IN_BACKGROUND" value="false"/>
|
||||
<stringAttribute key="org.eclipse.jdt.launching.CLASSPATH_PROVIDER" value="org.eclipse.ant.ui.AntClasspathProvider"/>
|
||||
<booleanAttribute key="org.eclipse.jdt.launching.DEFAULT_CLASSPATH" value="true"/>
|
||||
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value=""/>
|
||||
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_LOCATION" value="${BUILD_FILES}/common.xml"/>
|
||||
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_RUN_BUILD_KINDS" value="full,incremental,auto,"/>
|
||||
<booleanAttribute key="org.eclipse.ui.externaltools.ATTR_TRIGGERS_CONFIGURED" value="true"/>
|
||||
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_WORKING_DIRECTORY" value="${workspace_loc:/Mineplex.BungeeRotator}"/>
|
||||
</launchConfiguration>
|
27
Plugins/Mineplex.BungeeRotator/.project
Normal file
27
Plugins/Mineplex.BungeeRotator/.project
Normal file
@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Mineplex.BungeeRotator</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.ui.externaltools.ExternalToolBuilder</name>
|
||||
<triggers>auto,full,incremental,</triggers>
|
||||
<arguments>
|
||||
<dictionary>
|
||||
<key>LaunchConfigHandle</key>
|
||||
<value><project>/.externalToolBuilders/BungeeRotator.launch</value>
|
||||
</dictionary>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
@ -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
|
@ -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<BungeeServer> getBungeeServers()
|
||||
{
|
||||
List<BungeeServer> bungeeServers = new ArrayList<BungeeServer>();
|
||||
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;
|
||||
}
|
||||
}
|
@ -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<ProcessRunner> _processes = new HashSet<ProcessRunner>();
|
||||
|
||||
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<BungeeServer> bungeeServers = _repository.getBungeeServers();
|
||||
HashSet<String> usServers = new HashSet<String>();
|
||||
HashSet<String> euServers = new HashSet<String>();
|
||||
|
||||
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<DnsRecord> recordsToDelete = new ArrayList<DnsRecord>();
|
||||
List<DnsRecord> recordsToAdd = new ArrayList<DnsRecord>();
|
||||
|
||||
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<ProcessRunner> 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<ProcessRunner> iterator = _processes.iterator(); iterator.hasNext();)
|
||||
{
|
||||
iterator.next().abort();
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
|
||||
processWaits++;
|
||||
}
|
||||
|
||||
processWaits = 0;
|
||||
|
||||
try
|
||||
{
|
||||
Thread.sleep(30000);
|
||||
}
|
||||
catch (InterruptedException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package mineplex.bungee;
|
||||
|
||||
public class BungeeServer
|
||||
{
|
||||
public String Address;
|
||||
public int Players;
|
||||
public int MaxPlayers;
|
||||
public boolean US;
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package mineplex.bungee;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
public class BungeeSorter implements Comparator<BungeeServer>
|
||||
{
|
||||
public int compare(BungeeServer a, BungeeServer b)
|
||||
{
|
||||
if (a.Players < b.Players)
|
||||
return -1;
|
||||
|
||||
if (b.Players < a.Players)
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
package mineplex.bungee;
|
||||
|
||||
public interface GenericRunnable<T>
|
||||
{
|
||||
void run(T t);
|
||||
}
|
@ -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<Boolean> _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<Boolean> 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();
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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> T Execute(Type returnType)
|
||||
{
|
||||
HttpGet request = new HttpGet(ApiUrl + DomainId + Category + _action);
|
||||
|
||||
String response = execute(request);
|
||||
return new Gson().fromJson(response, returnType);
|
||||
}
|
||||
}
|
@ -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> T Execute(Class<T> returnClass)
|
||||
{
|
||||
return Execute(returnClass, (Object)null);
|
||||
}
|
||||
|
||||
public <T> 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);
|
||||
}
|
||||
}
|
@ -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> 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);
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
@ -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";
|
||||
}
|
||||
}
|
@ -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";
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package mineplex.bungee.api.token;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class DomainRecords
|
||||
{
|
||||
public List<DnsRecord> data;
|
||||
public int page;
|
||||
public int totalPage;
|
||||
public int totalRecords;
|
||||
}
|
Loading…
Reference in New Issue
Block a user