2016-05-12 19:40:39 +00:00
|
|
|
package me.totalfreedom.totalfreedommod;
|
|
|
|
|
2017-08-01 19:59:24 +00:00
|
|
|
import java.io.UnsupportedEncodingException;
|
2018-07-31 07:01:29 +00:00
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.net.URLEncoder;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
import java.security.NoSuchProviderException;
|
|
|
|
import java.security.SecureRandom;
|
2018-07-31 07:05:28 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2016-05-12 19:40:39 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.config.ConfigEntry;
|
|
|
|
import me.totalfreedom.totalfreedommod.util.FLog;
|
2019-07-28 06:04:16 +00:00
|
|
|
import org.apache.commons.lang.StringUtils;
|
2016-05-12 19:40:39 +00:00
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
import org.bukkit.command.CommandSender;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.scheduler.BukkitRunnable;
|
|
|
|
|
|
|
|
public class LogViewer extends FreedomService
|
|
|
|
{
|
|
|
|
@Override
|
2020-07-01 01:51:06 +00:00
|
|
|
public void onStart()
|
2016-05-12 19:40:39 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-07-01 01:51:06 +00:00
|
|
|
public void onStop()
|
2016-05-12 19:40:39 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public void updateLogsRegistration(final CommandSender sender, final Player target, final LogsRegistrationMode mode)
|
|
|
|
{
|
2017-08-01 19:59:24 +00:00
|
|
|
updateLogsRegistration(sender, target.getName(), mode);
|
2016-05-12 19:40:39 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 19:59:24 +00:00
|
|
|
public void updateLogsRegistration(final CommandSender sender, final String targetName, final LogsRegistrationMode mode)
|
2016-05-12 19:40:39 +00:00
|
|
|
{
|
|
|
|
final String logsRegisterUrl = ConfigEntry.LOGS_URL.getString();
|
|
|
|
final String logsRegisterPassword = ConfigEntry.LOGS_SECRET.getString();
|
|
|
|
|
|
|
|
if (logsRegisterUrl == null || logsRegisterPassword == null || logsRegisterUrl.isEmpty() || logsRegisterPassword.isEmpty())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
new BukkitRunnable()
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (sender != null)
|
|
|
|
{
|
|
|
|
sender.sendMessage(ChatColor.YELLOW + "Connecting...");
|
|
|
|
}
|
|
|
|
|
2017-08-01 19:59:24 +00:00
|
|
|
final String key = SecureCodeGenerator.generateCode(20);
|
|
|
|
|
|
|
|
final URL urlAdd = new URLBuilder(logsRegisterUrl)
|
|
|
|
.addQueryParameter("mode", mode.name())
|
2016-05-12 19:40:39 +00:00
|
|
|
.addQueryParameter("password", logsRegisterPassword)
|
|
|
|
.addQueryParameter("name", targetName)
|
2017-08-01 19:59:24 +00:00
|
|
|
.addQueryParameter("key", key)
|
2016-05-12 19:40:39 +00:00
|
|
|
.getURL();
|
|
|
|
|
2018-07-31 07:01:29 +00:00
|
|
|
final HttpURLConnection connection = (HttpURLConnection)urlAdd.openConnection();
|
2016-05-12 19:40:39 +00:00
|
|
|
connection.setConnectTimeout(1000 * 5);
|
|
|
|
connection.setReadTimeout(1000 * 5);
|
|
|
|
connection.setUseCaches(false);
|
|
|
|
connection.setRequestMethod("HEAD");
|
|
|
|
|
|
|
|
final int responseCode = connection.getResponseCode();
|
|
|
|
|
|
|
|
if (sender != null)
|
|
|
|
{
|
|
|
|
if (!plugin.isEnabled())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
new BukkitRunnable()
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
if (responseCode == 200)
|
|
|
|
{
|
2017-08-01 19:59:24 +00:00
|
|
|
if (mode == LogsRegistrationMode.ADD)
|
|
|
|
{
|
|
|
|
String link = null;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
final URL urlVerify = new URLBuilder(logsRegisterUrl)
|
|
|
|
.addQueryParameter("mode", LogsRegistrationMode.VERIFY.name())
|
|
|
|
.addQueryParameter("name", targetName)
|
|
|
|
.addQueryParameter("key", key)
|
|
|
|
.getURL();
|
|
|
|
link = urlVerify.toString();
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
FLog.severe(ex);
|
|
|
|
}
|
|
|
|
|
|
|
|
sender.sendMessage(ChatColor.GREEN + "Open this link to verify your logviewer registration:\n" + ChatColor.DARK_GREEN + link);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sender.sendMessage(ChatColor.GREEN + "Logviewer access revoked successfully.");
|
|
|
|
}
|
2016-05-12 19:40:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sender.sendMessage(ChatColor.RED + "Error contacting logs registration server.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}.runTask(plugin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
FLog.severe(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}.runTaskAsynchronously(plugin);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static enum LogsRegistrationMode
|
|
|
|
{
|
|
|
|
|
2017-08-01 19:59:24 +00:00
|
|
|
ADD, DELETE, VERIFY;
|
2016-05-12 19:40:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static class URLBuilder
|
|
|
|
{
|
|
|
|
|
|
|
|
private final String requestPath;
|
|
|
|
private final Map<String, String> queryStringMap = new HashMap<>();
|
|
|
|
|
|
|
|
private URLBuilder(String requestPath)
|
|
|
|
{
|
|
|
|
this.requestPath = requestPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
public URLBuilder addQueryParameter(String key, String value)
|
|
|
|
{
|
|
|
|
queryStringMap.put(key, value);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public URL getURL() throws MalformedURLException
|
|
|
|
{
|
|
|
|
List<String> pairs = new ArrayList<>();
|
|
|
|
Iterator<Map.Entry<String, String>> it = queryStringMap.entrySet().iterator();
|
|
|
|
while (it.hasNext())
|
|
|
|
{
|
|
|
|
Map.Entry<String, String> pair = it.next();
|
2017-08-01 19:59:24 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
pairs.add(URLEncoder.encode(pair.getKey(), "UTF-8") + "=" + URLEncoder.encode(pair.getValue(), "UTF-8"));
|
|
|
|
}
|
|
|
|
catch (UnsupportedEncodingException ex)
|
|
|
|
{
|
|
|
|
FLog.severe(ex);
|
|
|
|
}
|
2016-05-12 19:40:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return new URL(requestPath + "?" + StringUtils.join(pairs, "&"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-01 19:59:24 +00:00
|
|
|
private static class SecureCodeGenerator
|
|
|
|
{
|
|
|
|
|
|
|
|
private static final String CHARACTER_SET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
|
|
|
|
|
|
|
public static String generateCode(final int length)
|
|
|
|
{
|
|
|
|
SecureRandom random;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
random = SecureRandom.getInstance("SHA1PRNG", "SUN");
|
|
|
|
}
|
|
|
|
catch (NoSuchAlgorithmException | NoSuchProviderException ex)
|
|
|
|
{
|
|
|
|
random = new SecureRandom();
|
|
|
|
FLog.severe(ex);
|
|
|
|
}
|
|
|
|
|
|
|
|
final StringBuilder sb = new StringBuilder();
|
|
|
|
for (int i = 0; i < length; i++)
|
|
|
|
{
|
|
|
|
sb.append(CHARACTER_SET.charAt(random.nextInt(CHARACTER_SET.length())));
|
|
|
|
}
|
|
|
|
return sb.toString();
|
|
|
|
}
|
|
|
|
}
|
2016-05-12 19:40:39 +00:00
|
|
|
}
|