Implement #22 and fix pom.xml.

Signed-off-by: Lemon <minecraftoxlemonxo@gmail.com>
This commit is contained in:
Lemon
2018-02-22 17:55:04 +05:00
parent 2bffcef9a9
commit 0551337e8b
12 changed files with 376 additions and 15 deletions

View File

@ -0,0 +1,47 @@
package me.totalfreedom.totalfreedommod.amp;
import me.totalfreedom.totalfreedommod.FreedomService;
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
import me.totalfreedom.totalfreedommod.config.ConfigEntry;
public class AMP extends FreedomService
{
public AMPManager ampManager;
public Boolean enabled;
public AMP(TotalFreedomMod plugin)
{
super(plugin);
}
@Override
protected void onStart()
{
if(!plugin.config.getBoolean(ConfigEntry.AMP_ENABLED))
{
return;
}
ampManager = new AMPManager(plugin, plugin.config.getString(ConfigEntry.AMP_URL), plugin.config.getString(ConfigEntry.AMP_USERNAME), plugin.config.getString(ConfigEntry.AMP_PASSWORD));
LoginCallback callback = new LoginCallback()
{
@Override
public void loginDone(boolean success)
{
enabled = success;
}
};
ampManager.connectAsync(callback);
}
public void restartServer()
{
ampManager.restartAsync();
}
@Override
protected void onStop() {
}
}

View File

@ -0,0 +1,27 @@
package me.totalfreedom.totalfreedommod.amp;
public enum AMPEndpoints
{
LOGIN("/API/Core/Login" , "{username:\"%s\", password:\"%s\", token:\"\", rememberMe:false}"),
RESTART("/API/Core/Restart", "{SESSIONID:\"%s\"}");
private final String text;
private final String parameters;
AMPEndpoints(String text, String parameters)
{
this.text = text;
this.parameters = parameters;
}
@Override
public String toString()
{
return text;
}
public String getParameters()
{
return parameters;
}
}

View File

@ -0,0 +1,168 @@
package me.totalfreedom.totalfreedommod.amp;
import com.google.gson.Gson;
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
import me.totalfreedom.totalfreedommod.util.FLog;
import org.bukkit.scheduler.BukkitRunnable;
import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class AMPManager
{
private TotalFreedomMod plugin;
private String url, username, password;
private String sessionID;
public AMPManager(TotalFreedomMod plugin, String url, String username, String password)
{
this.plugin = plugin; this.url = url; this.username = username; this.password = password;
}
public void connectAsync(final LoginCallback callback)
{
new BukkitRunnable()
{
@Override
public void run()
{
String apiEndpoint = url + AMPEndpoints.LOGIN.toString();
String body = String.format(AMPEndpoints.LOGIN.getParameters(), username, password);
try
{
LoginResult resp = new Gson().fromJson(postRequestToEndpoint(apiEndpoint, body), LoginResult.class);
if(!resp.getSuccess())
{
FLog.severe("AMP login unsuccessful. Check if login details are correct.");
sessionID = "";
callback.loginDone(false);
return;
}
sessionID = resp.getSessionID();
callback.loginDone(true);
}
catch(IOException ex)
{
FLog.severe("Could not login to AMP. Check if URL is correct. Stacktrace: " + ex.getMessage());
sessionID = "";
callback.loginDone(false);
}
}
}.runTaskAsynchronously(plugin);
}
public void restartAsync()
{
new BukkitRunnable()
{
@Override
public void run()
{
final String apiEndpoint = url + AMPEndpoints.RESTART.toString();
final String body = String.format(AMPEndpoints.RESTART.getParameters(), sessionID);
try
{
String resp = postRequestToEndpoint(apiEndpoint, body);
if(resp.contains("Unauthorized Access"))
{
//try connecting one more time
LoginCallback callback = new LoginCallback()
{
@Override
public void loginDone(boolean success)
{
if(!success)
{
FLog.severe("Failed to connect to AMP. Did the panel go down? Were panel user details changed/deleted? Check for more info above. Connection was successful when plugin started, but unsuccessful now." +
" Using server.shutdown() instead.");
plugin.getServer().shutdown();
return;
}
try
{
String response = postRequestToEndpoint(apiEndpoint, body);
if(response.contains("Unauthorized Access"))
{
FLog.severe("Contact a developer. Panel gives Session ID but trying to use it gives a no perms error. The panel user set in config doesn't" +
" have perms to restart server. Using server.shutdown() instead. ");
plugin.getServer().shutdown();
}
}
catch (IOException e)
{
FLog.severe("Could not restart. Using server.shutdown() instead. Stacktrace" + e.getMessage());
plugin.getServer().shutdown();
}
}
};
plugin.amp.ampManager.connectAsync(callback);
}
}
catch(IOException ex)
{
FLog.severe("Could not restart. Using server.shutdown() instead. Stacktrace: " + ex.getMessage());
plugin.getServer().shutdown();
}
}
}.runTaskAsynchronously(plugin);
}
private String postRequestToEndpoint(String endpoint, String body) throws IOException
{
URL url = new URL(endpoint);
if(endpoint.startsWith("https://"))
{
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept", "application/json");
connection.setDoOutput(true);
DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(body);
outputStream.flush();
outputStream.close();
BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null)
{
response.append(inputLine);
}
in.close();
return response.toString();
}
else
{
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept", "application/json");
connection.setDoOutput(true);
DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(body);
outputStream.flush();
outputStream.close();
BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
return response.toString();
}
}
}

View File

@ -0,0 +1,6 @@
package me.totalfreedom.totalfreedommod.amp;
public interface LoginCallback
{
void loginDone(boolean success);
}

View File

@ -0,0 +1,50 @@
package me.totalfreedom.totalfreedommod.amp;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
public class LoginResult
{
@SerializedName("result")
@Expose
@Getter
@Setter
private Integer result;
@SerializedName("success")
@Expose
@Getter
@Setter
private Boolean success;
@SerializedName("permissions")
@Expose
@Getter
@Setter
private List<String> permissions = null;
@SerializedName("sessionID")
@Expose
@Getter
@Setter
private String sessionID;
@SerializedName("rememberMeToken")
@Expose
@Getter
@Setter
private String rememberMeToken;
@SerializedName("gravatarHash")
@Expose
@Getter
@Setter
private String gravatarHash;
@SerializedName("username")
@Expose
@Getter
@Setter
private String username;
private final static long serialVersionUID = -523050232433919883L;
}