Add license stuff

This commit is contained in:
ceze88 2023-01-11 21:41:04 +01:00
parent 336c5c49f3
commit ad6b52ca4a
5 changed files with 259 additions and 1 deletions

View File

@ -8,6 +8,8 @@ import com.songoda.core.core.PluginInfo;
import com.songoda.core.core.PluginInfoModule;
import com.songoda.core.core.SongodaCoreCommand;
import com.songoda.core.core.SongodaCoreDiagCommand;
import com.songoda.core.core.SongodaCoreIPCommand;
import com.songoda.core.core.SongodaCoreUUIDCommand;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@ -172,7 +174,7 @@ public class SongodaCore {
private void init() {
shadingListener = new ShadedEventListener();
commandManager.registerCommandDynamically(new SongodaCoreCommand())
.addSubCommand(new SongodaCoreDiagCommand());
.addSubCommands(new SongodaCoreDiagCommand(), new SongodaCoreIPCommand(), new SongodaCoreUUIDCommand());
Bukkit.getPluginManager().registerEvents(loginListener, piggybackedPlugin);
Bukkit.getPluginManager().registerEvents(shadingListener, piggybackedPlugin);

View File

@ -4,6 +4,7 @@ import com.songoda.core.configuration.Config;
import com.songoda.core.database.DataManagerAbstract;
import com.songoda.core.locale.Locale;
import com.songoda.core.utils.Metrics;
import com.songoda.core.utils.SongodaAuth;
import de.tr7zw.changeme.nbtapi.utils.MinecraftVersion;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
@ -90,6 +91,24 @@ public abstract class SongodaPlugin extends JavaPlugin {
return;
}
//Check plugin access, don't load plugin if user don't have access
if (!SongodaAuth.isAuthorized(true)) {
Thread thread = new Thread(() -> {
console.sendMessage(ChatColor.RED + "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
console.sendMessage(ChatColor.RED + "You do not have access to this plugin.");
console.sendMessage(ChatColor.YELLOW + "Please purchase a license at https://sngda.to/marketplace");
console.sendMessage(ChatColor.YELLOW + "or set up your license at https://sngda.to/licenses");
console.sendMessage(ChatColor.YELLOW + "License setup steps:");
console.sendMessage(ChatColor.YELLOW + "Visit the link mentioned above and click the 'Create License button'");
console.sendMessage(ChatColor.YELLOW + "Copy the following ip and uuid and click create.");
console.sendMessage(ChatColor.YELLOW + "IP: " + SongodaAuth.getIP());
console.sendMessage(ChatColor.YELLOW + "UUID: " + SongodaAuth.getUUID());
console.sendMessage(ChatColor.RED + "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
});
thread.start();
return;
}
console.sendMessage(" "); // blank line to separate chatter
console.sendMessage(ChatColor.GREEN + "=============================");
console.sendMessage(String.format("%s%s %s by %sSongoda <3!", ChatColor.GRAY,

View File

@ -0,0 +1,73 @@
package com.songoda.core.core;
import com.songoda.core.commands.AbstractCommand;
import com.songoda.core.utils.SongodaAuth;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
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.command.CommandSender;
import org.bukkit.entity.Player;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.List;
public class SongodaCoreIPCommand extends AbstractCommand {
public SongodaCoreIPCommand() {
super(CommandType.CONSOLE_OK, "myip");
}
@Override
protected ReturnType runCommand(CommandSender sender, String... args) {
Thread thread = new Thread(() -> {
String ip = SongodaAuth.getIP();
sender.sendMessage("");
sender.sendMessage("IP Information");
sender.sendMessage("");
if (sender instanceof Player) {
TextComponent component = new TextComponent("Your public IP is: " + ip);
component.setClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, ip));
component.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new BaseComponent[]{new TextComponent("Click to copy")}));
sender.spigot().sendMessage(component);
} else {
sender.sendMessage("Your public IP is: " + ip);
}
sender.sendMessage("");
});
thread.start();
return ReturnType.SUCCESS;
}
@Override
protected List<String> onTab(CommandSender sender, String... args) {
return null;
}
@Override
public String getPermissionNode() {
return "songoda.admin";
}
@Override
public String getSyntax() {
return "/songoda myip";
}
@Override
public String getDescription() {
return "Displays your public IP address.";
}
}

View File

@ -0,0 +1,55 @@
package com.songoda.core.core;
import com.songoda.core.commands.AbstractCommand;
import com.songoda.core.utils.SongodaAuth;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.List;
import java.util.UUID;
public class SongodaCoreUUIDCommand extends AbstractCommand {
public SongodaCoreUUIDCommand() {
super(CommandType.CONSOLE_OK, "uuid");
}
@Override
protected ReturnType runCommand(CommandSender sender, String... args) {
sender.sendMessage("");
if (sender instanceof Player) {
TextComponent component = new TextComponent("Your server UUID is: " + SongodaAuth.getUUID());
component.setClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, SongodaAuth.getUUID().toString()));
component.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new BaseComponent[]{new TextComponent("Click to copy")}));
sender.spigot().sendMessage(component);
} else {
sender.sendMessage("Your server UUID is: " + SongodaAuth.getUUID());
}
sender.sendMessage("");
return ReturnType.SUCCESS;
}
@Override
protected List<String> onTab(CommandSender sender, String... args) {
return null;
}
@Override
public String getPermissionNode() {
return "songodacore.admin";
}
@Override
public String getSyntax() {
return "/songodacore uuid";
}
@Override
public String getDescription() {
return "Returns your server's uuid";
}
}

View File

@ -0,0 +1,109 @@
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) {
UUID uuid = getUUID();
try {
URL url = new URL("https://marketplace.songoda.com/api/v2/products/license/validate");
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\":" + "%%__PLUGIN__%%" + ",\"license\":\"" + uuid + "\",\"user_id\":\"%%__USER__%%\"}";
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))) {
StringBuilder response = new StringBuilder();
String responseLine = null;
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
Bukkit.getLogger().warning("Error validating license: " + jsonObject.get("error"));
return false;
} else {
return (boolean) jsonObject.get("valid");
}
}
} catch (Exception e) {
return allowOffline;
}
}
public static UUID getUUID() {
File serverProperties = new File(Bukkit.getWorldContainer(),"server.properties");
Properties prop = new Properties();
try {
prop.load(new FileReader(serverProperties));
String uuid = prop.getProperty("uuid");
if (uuid == null || uuid.isEmpty()) {
UUID newUUID = UUID.randomUUID();
prop.setProperty("uuid", newUUID.toString());
prop.store(new FileWriter(serverProperties), null);
return newUUID;
} else {
return UUID.fromString(uuid);
}
} catch (Exception ex) {
throw new RuntimeException("Could not fetch UUID from server.properties", ex);
}
}
public static String getIP() {
try {
URL url = new URL("https://marketplace.songoda.com/api/v2/products/license/ip");
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))) {
StringBuilder response = new StringBuilder();
String responseLine = null;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}
JSONParser parser = new JSONParser();
JSONObject jsonObject = (JSONObject) parser.parse(response.toString());
return jsonObject.get("ip").toString();
}
} catch (Exception ex) {
throw new RuntimeException("Could not fetch IP address", ex);
}
}
}