refactor
This commit is contained in:
@ -0,0 +1,325 @@
|
||||
package mineplex.ddos;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.mail.Message;
|
||||
import javax.mail.MessagingException;
|
||||
import javax.mail.PasswordAuthentication;
|
||||
import javax.mail.Session;
|
||||
import javax.mail.Transport;
|
||||
import javax.mail.internet.AddressException;
|
||||
import javax.mail.internet.InternetAddress;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
import javax.mail.internet.MimeMessage.RecipientType;
|
||||
|
||||
import mineplex.ddos.api.ApiDeleteCall;
|
||||
import mineplex.ddos.api.ApiGetCall;
|
||||
import mineplex.ddos.api.ApiPostCall;
|
||||
import mineplex.ddos.api.ApiPutCall;
|
||||
import mineplex.ddos.api.token.DnsRecord;
|
||||
import mineplex.ddos.api.token.DomainRecords;
|
||||
|
||||
public class DDoSProtectionSwitcher
|
||||
{
|
||||
private static DnsMadeEasyRepository _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 DnsMadeEasyRepository();
|
||||
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
|
||||
|
||||
//while (true)
|
||||
//{
|
||||
//if (_repository.switchToDDOSProt())
|
||||
//{
|
||||
|
||||
System.out.println("Starting DDoS Protection Switch at " + dateFormat.format(new Date()));
|
||||
|
||||
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 on ddos protection
|
||||
for (DnsRecord record : records.data)
|
||||
{
|
||||
if (record.type.equalsIgnoreCase("A"))
|
||||
{
|
||||
if (record.name.equalsIgnoreCase("neustar"))
|
||||
{
|
||||
record.name = "us";
|
||||
recordsToModify.add(record);
|
||||
}
|
||||
else if (record.name.equalsIgnoreCase("us"))
|
||||
{
|
||||
record.name = "us2";
|
||||
recordsToModify.add(record);
|
||||
}
|
||||
else if (record.name.equalsIgnoreCase("eu"))
|
||||
{
|
||||
record.name = "eu2";
|
||||
recordsToModify.add(record);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (recordsToAdd.size() > 0)
|
||||
{
|
||||
new ApiPostCall("https://api.dnsmadeeasy.com/V2.0/dns/managed/", 962728, "/records/", "createMulti")
|
||||
.Execute(records);
|
||||
System.out.println("Created " + recordsToAdd.size() + " records.");
|
||||
}
|
||||
|
||||
if (recordsToModify.size() > 0)
|
||||
{
|
||||
new ApiPutCall("https://api.dnsmadeeasy.com/V2.0/dns/managed/", 962728, "/records/", "updateMulti")
|
||||
.Execute(recordsToModify);
|
||||
System.out.println("Modified " + recordsToModify.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.");
|
||||
}
|
||||
/*
|
||||
// Switching US Bungees
|
||||
switchServer("10.35.74.130", "108.178.20.166", "108.163.222.202", "108.178.20.165", "108.163.222.201");
|
||||
switchServer("10.35.74.132", "108.163.217.110", "108.178.44.50", "108.163.217.109", "108.178.44.49");
|
||||
switchServer("10.35.74.142", "108.178.16.90", "108.178.16.90", "108.178.16.89", "108.178.16.89");
|
||||
switchServer("10.35.74.135", "108.163.254.134", "108.178.16.106", "108.163.254.133", "108.178.16.105");
|
||||
switchServer("10.35.74.137", "108.163.216.250", "108.178.34.162", "108.163.216.249", "108.178.34.161");
|
||||
switchServer("10.35.74.147", "108.163.216.106", "184.154.39.126", "108.163.216.105", "184.154.39.125");
|
||||
switchServer("10.35.74.143", "184.154.215.170", "108.178.17.6", "184.154.215.169", "108.178.17.5");
|
||||
switchServer("10.35.74.145", "96.127.174.206", "108.178.7.118", "96.127.174.205", "108.178.7.117");
|
||||
switchServer("10.35.74.144", "184.154.127.10", "184.154.39.154", "184.154.127.9", "184.154.39.153");
|
||||
switchServer("10.35.74.146", "96.127.174.146", "108.178.16.26", "96.127.174.145", "108.178.16.25");
|
||||
switchServer("10.35.74.149", "108.178.7.206", "107.6.158.198", "108.178.7.205", "107.6.158.197");
|
||||
switchServer("10.35.74.136", "184.154.39.146", "184.154.13.218", "184.154.39.145", "184.154.13.217");
|
||||
switchServer("10.35.74.139", "108.163.217.250", "108.178.44.134", "108.163.217.249", "108.178.44.133");
|
||||
switchServer("10.35.74.140", "69.175.15.242", "108.163.216.38", "69.175.15.241", "108.163.216.37");
|
||||
switchServer("10.35.74.141", "107.6.129.126", "96.127.182.218", "107.6.129.125", "96.127.182.217");
|
||||
switchServer("10.35.74.134", "108.163.222.174", "108.163.216.82", "108.163.222.173", "108.163.216.81");
|
||||
switchServer("10.32.214.248", "108.178.34.118", "107.6.129.170", "108.178.34.117", "107.6.129.169");
|
||||
switchServer("10.32.214.250", "69.175.4.38", "107.6.129.250", "69.175.4.37", "107.6.129.249");
|
||||
switchServer("10.32.214.249", "107.6.158.78", "184.154.13.38", "107.6.158.77", "184.154.13.37");
|
||||
switchServer("10.32.214.247", "184.154.13.118", "108.163.242.98", "184.154.13.117", "108.163.242.97");
|
||||
*/
|
||||
/*
|
||||
// Switching EU Bungees
|
||||
switchServer("10.82.2.202", "107.6.176.194", "107.6.176.34", "107.6.176.193", "107.6.176.33");
|
||||
switchServer("10.82.2.204", "107.6.176.122", "107.6.176.50", "107.6.176.121", "107.6.176.49");
|
||||
switchServer("10.82.2.206", "107.6.176.166", "107.6.176.126", "107.6.176.165", "107.6.176.125");
|
||||
switchServer("10.83.27.77", "107.6.176.14", "107.6.176.98", "107.6.176.13", "107.6.176.97");
|
||||
switchServer("10.82.2.225", "107.6.176.114", "107.6.176.58", "107.6.176.113", "107.6.176.57");
|
||||
switchServer("10.82.2.227", "107.6.176.26", "107.6.176.46", "107.6.176.25", "107.6.176.45");
|
||||
switchServer("10.82.2.228", "107.6.176.110", "107.6.176.70", "107.6.176.109", "107.6.176.69");
|
||||
switchServer("10.82.2.226", "107.6.176.138", "107.6.176.234", "107.6.176.137", "107.6.176.233");
|
||||
*/
|
||||
//sendMail();
|
||||
//}
|
||||
|
||||
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(60000);
|
||||
}
|
||||
catch (InterruptedException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
//}
|
||||
/*
|
||||
* // 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); } } }
|
||||
*
|
||||
*
|
||||
*
|
||||
* recordsToAdd.add(new CNameRecord("eu", "us", 300));
|
||||
*/
|
||||
}
|
||||
|
||||
private static void sendMail()
|
||||
{
|
||||
Message message = new MimeMessage(getSession());
|
||||
|
||||
try
|
||||
{
|
||||
message.addRecipient(RecipientType.TO, new InternetAddress("ultrasupport@neustar.biz"));
|
||||
message.addFrom(new InternetAddress[] { new InternetAddress("it@mineplex.com") });
|
||||
|
||||
message.setSubject("Start Mitigation Incident");
|
||||
message.setText("We need to start mitigation.\n\n"
|
||||
+ "Jonathan Williams\n"
|
||||
+ "Director of Gaming Software Development\n"
|
||||
+ "Mineplex, LLC\n"
|
||||
+ "PH: 805.231.0407\n"
|
||||
+ "http://www.mineplex.com");
|
||||
|
||||
|
||||
Transport.send(message);
|
||||
|
||||
System.out.println("Sent Neustar Mitigation Email at " + new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(new Date()));
|
||||
}
|
||||
catch (AddressException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
catch (MessagingException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static Session getSession()
|
||||
{
|
||||
Authenticator authenticator = new Authenticator();
|
||||
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName());
|
||||
|
||||
properties.setProperty("mail.smtp.host", "smtp.fatcow.com");
|
||||
properties.setProperty("mail.smtp.socketFactory.port", "465");
|
||||
properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
|
||||
properties.setProperty("mail.smtp.auth", "true");
|
||||
properties.setProperty("mail.smtp.port", "465");
|
||||
|
||||
return Session.getInstance(properties, authenticator);
|
||||
}
|
||||
|
||||
private static void switchServer(final String privateIp, String currentIp, String newIp, String currentGateway,
|
||||
String newGateway)
|
||||
{
|
||||
String cmd = "/usr/bin/ssh";
|
||||
String args = "-to StrictHostKeyChecking=no -o ServerAliveInterval=10 mineplex@" + privateIp + " -p 5191";
|
||||
String remoteCmd = "\"sh /home/mineplex/config/switchBungeeIpRemote.sh";
|
||||
String remoteCmdEnd = "\"";
|
||||
|
||||
ProcessRunner pr = new ProcessRunner(new String[] { cmd, args, remoteCmd, currentIp, newIp,
|
||||
currentGateway, newGateway, remoteCmdEnd });
|
||||
pr.start(new GenericRunnable<Boolean>()
|
||||
{
|
||||
public void run(Boolean error)
|
||||
{
|
||||
if (error)
|
||||
System.out.println("[" + privateIp + "] Errored!");
|
||||
else
|
||||
System.out.println("[" + privateIp + "] Switched!");
|
||||
}
|
||||
});
|
||||
|
||||
try
|
||||
{
|
||||
pr.join(500);
|
||||
}
|
||||
catch (InterruptedException e1)
|
||||
{
|
||||
e1.printStackTrace();
|
||||
}
|
||||
|
||||
if (!pr.isDone())
|
||||
_processes.add(pr);
|
||||
}
|
||||
|
||||
private static class Authenticator extends javax.mail.Authenticator
|
||||
{
|
||||
private PasswordAuthentication authentication;
|
||||
|
||||
public Authenticator()
|
||||
{
|
||||
String username = "it@mineplex.com";
|
||||
String password = "BearT4bl312ust";
|
||||
authentication = new PasswordAuthentication(username, password);
|
||||
}
|
||||
|
||||
protected PasswordAuthentication getPasswordAuthentication()
|
||||
{
|
||||
return authentication;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,128 @@
|
||||
package mineplex.ddos;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
public class DnsMadeEasyRepository
|
||||
{
|
||||
// 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_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 SELECT_SERVERS = "SELECT updated, lastOnline, us, now() FROM bungeeOnlineStatus;";
|
||||
|
||||
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 boolean switchToDDOSProt()
|
||||
{
|
||||
Connection connection = null;
|
||||
PreparedStatement preparedStatement = null;
|
||||
ResultSet resultSet = null;
|
||||
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
int countOffline = 0;
|
||||
|
||||
try
|
||||
{
|
||||
connection = DriverManager.getConnection(_connectionString, _userName, _password);
|
||||
|
||||
preparedStatement = connection.prepareStatement(SELECT_SERVERS);
|
||||
|
||||
resultSet = preparedStatement.executeQuery();
|
||||
|
||||
while (resultSet.next())
|
||||
{
|
||||
long current = dateFormat.parse(resultSet.getString(4)).getTime();
|
||||
long updated = dateFormat.parse(resultSet.getString(1)).getTime();
|
||||
long lastOnline = dateFormat.parse(resultSet.getString(2)).getTime();
|
||||
|
||||
if (current - updated < 70000 && current - lastOnline > 660000)
|
||||
countOffline++;
|
||||
}
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//if (countOffline > 5)
|
||||
System.out.println(countOffline + " offline bungees.");
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
package mineplex.ddos;
|
||||
|
||||
public interface GenericRunnable<T>
|
||||
{
|
||||
void run(T t);
|
||||
}
|
@ -0,0 +1,81 @@
|
||||
package mineplex.ddos;
|
||||
|
||||
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,19 @@
|
||||
package mineplex.ddos.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);
|
||||
System.out.println(request.getURI().toString());
|
||||
|
||||
System.out.println(execute(request));
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package mineplex.ddos.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);
|
||||
|
||||
System.out.println(execute(request));
|
||||
}
|
||||
|
||||
public <T> T Execute(Type returnType)
|
||||
{
|
||||
HttpGet request = new HttpGet(ApiUrl + DomainId + Category + _action);
|
||||
|
||||
String response = execute(request);
|
||||
System.out.println(response);
|
||||
return new Gson().fromJson(response, returnType);
|
||||
}
|
||||
}
|
@ -0,0 +1,79 @@
|
||||
package mineplex.ddos.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);
|
||||
|
||||
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();
|
||||
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,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 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.ddos.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.ddos.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.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";
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package mineplex.ddos.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.ddos.api.token;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class DomainRecords
|
||||
{
|
||||
public List<DnsRecord> data;
|
||||
public int page;
|
||||
public int totalPage;
|
||||
public int totalRecords;
|
||||
}
|
Reference in New Issue
Block a user