Remove unused imports in SongodaAuth and apply code formatting

This commit is contained in:
Christian Koop 2023-03-04 12:47:53 +01:00
parent d9586cfe86
commit 6145021ecb
No known key found for this signature in database
GPG Key ID: 89A8181384E010A3
1 changed files with 14 additions and 25 deletions

View File

@ -1,64 +1,52 @@
package com.songoda.core.utils;
import com.songoda.core.commands.AbstractCommand;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.bukkit.Bukkit;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.math.BigInteger;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.util.Properties;
import java.util.UUID;
public class SongodaAuth {
public static boolean isAuthorized(boolean allowOffline) {
String productId = "%%__PLUGIN__%%";
try {
Integer.parseInt(productId);
} catch (NumberFormatException e) {
//Self compiled, return true
// Self compiled
return true;
}
UUID uuid = getUUID();
try {
URL url = new URL("https://marketplace.songoda.com/api/v2/products/license/validate");
HttpURLConnection con = (HttpURLConnection)url.openConnection();
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept", "application/json");
con.setDoOutput(true);
String jsonInputString = "{\"product_id\":" + productId + ",\"license\":\"" + uuid + "\",\"user_id\":\"%%__USER__%%\"}";
try(OutputStream os = con.getOutputStream()) {
try (OutputStream os = con.getOutputStream()) {
byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8);
os.write(input, 0, input.length);
}
try(BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8))) {
try (BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8))) {
StringBuilder response = new StringBuilder();
String responseLine = null;
String responseLine;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}
JSONObject jsonObject = (JSONObject) new JSONParser().parse(response.toString());
if (jsonObject.get("error") != null) {
//Got an error, return false and print error
@ -74,7 +62,7 @@ public class SongodaAuth {
}
public static UUID getUUID() {
File serverProperties = new File(new File("."),"server.properties");
File serverProperties = new File(new File("."), "server.properties");
Properties prop = new Properties();
try {
prop.load(new FileReader(serverProperties));
@ -95,18 +83,19 @@ public class SongodaAuth {
public static String getIP() {
try {
URL url = new URL("https://marketplace.songoda.com/api/v2/products/license/ip");
HttpURLConnection con = (HttpURLConnection)url.openConnection();
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("Accept", "application/json");
con.setDoOutput(true);
try(BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8))) {
try (BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8))) {
StringBuilder response = new StringBuilder();
String responseLine = null;
String responseLine;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}
JSONParser parser = new JSONParser();
JSONObject jsonObject = (JSONObject) parser.parse(response.toString());
JSONObject jsonObject = (JSONObject) new JSONParser().parse(response.toString());
return jsonObject.get("ip").toString();
}
} catch (Exception ex) {