diff options
Diffstat (limited to 'src')
9 files changed, 257 insertions, 7 deletions
diff --git a/src/main/java/de/torui/coflsky/CoflSky.java b/src/main/java/de/torui/coflsky/CoflSky.java index 7ab2448..0127859 100644 --- a/src/main/java/de/torui/coflsky/CoflSky.java +++ b/src/main/java/de/torui/coflsky/CoflSky.java @@ -2,7 +2,6 @@ package de.torui.coflsky; import java.io.File; -import java.io.IOException; import java.net.URISyntaxException; import java.nio.file.Files; import java.nio.file.Paths; @@ -11,6 +10,8 @@ import java.nio.file.Paths; import com.google.gson.Gson; import de.torui.coflsky.configuration.LocalConfig; import de.torui.coflsky.handlers.EventRegistry; +import de.torui.coflsky.listeners.ChatListener; +import de.torui.coflsky.proxy.APIKeyManager; import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; import org.lwjgl.input.Keyboard; @@ -46,6 +47,9 @@ public class CoflSky }; public static String CommandUri = Config.BaseUrl + "/api/mod/commands"; + private final static APIKeyManager apiKeyManager = new APIKeyManager(); + + @EventHandler public void preInit(FMLPreInitializationEvent event) { String configString = null; @@ -64,9 +68,19 @@ public class CoflSky if (config == null) { config = LocalConfig.createDefaultConfig(); } + + try { + this.apiKeyManager.loadIfExists(); + }catch (Exception exception){ + exception.printStackTrace(); + } + + MinecraftForge.EVENT_BUS.register(new ChatListener()); + // Cache all the mods on load WSCommandHandler.cacheMods(); } + @EventHandler public void init(FMLInitializationEvent event) throws URISyntaxException { @@ -93,12 +107,21 @@ public class CoflSky } Events = new EventRegistry(); MinecraftForge.EVENT_BUS.register(Events); - Runtime.getRuntime() - .addShutdownHook( - new Thread( - () -> config.saveConfig(configFile , config))); + + Runtime.getRuntime().addShutdownHook(new Thread(() -> { + config.saveConfig(configFile , config); + try { + apiKeyManager.saveKey(); + }catch (Exception exception){ + exception.printStackTrace(); + } + })); } - + + public static APIKeyManager getAPIKeyManager(){ + return apiKeyManager; + } + } diff --git a/src/main/java/de/torui/coflsky/WSCommandHandler.java b/src/main/java/de/torui/coflsky/WSCommandHandler.java index 5427364..0d37851 100644 --- a/src/main/java/de/torui/coflsky/WSCommandHandler.java +++ b/src/main/java/de/torui/coflsky/WSCommandHandler.java @@ -10,6 +10,7 @@ import de.torui.coflsky.commands.RawCommand; import de.torui.coflsky.commands.models.*; import de.torui.coflsky.configuration.ConfigurationManager; import de.torui.coflsky.handlers.EventRegistry; +import de.torui.coflsky.proxy.ProxyManager; import de.torui.coflsky.utils.FileUtils; import net.minecraft.client.Minecraft; import net.minecraft.client.audio.PositionedSoundRecord; @@ -35,6 +36,7 @@ public class WSCommandHandler { public static FlipHandler flipHandler = new FlipHandler(); private static final ModListData modListData = new ModListData(); private static final Gson gson = new Gson(); + private static final ProxyManager proxyManager = new ProxyManager(); public static boolean HandleCommand(JsonStringCommand cmd, Entity sender) { // Entity sender = Minecraft.getMinecraft().thePlayer; @@ -64,6 +66,9 @@ public class WSCommandHandler { case GetMods: getMods(); break; + case ProxyRequest: + handleProxyRequest(cmd.GetAs(new TypeToken<ProxyRequest[]>() {}).getData()); + break; default: break; } @@ -71,6 +76,13 @@ public class WSCommandHandler { return true; } + private static void handleProxyRequest(ProxyRequest[] request){ + for(ProxyRequest req : request){ + proxyManager.handleRequestAsync(req); + } + } + + public static void cacheMods(){ File modFolder = new File(Minecraft.getMinecraft().mcDataDir, "mods"); for(File mods : modFolder.listFiles()){ diff --git a/src/main/java/de/torui/coflsky/commands/CommandType.java b/src/main/java/de/torui/coflsky/commands/CommandType.java index b7dbc37..d7de3b1 100644 --- a/src/main/java/de/torui/coflsky/commands/CommandType.java +++ b/src/main/java/de/torui/coflsky/commands/CommandType.java @@ -54,7 +54,9 @@ public enum CommandType { @SerializedName("uploadTab") uploadTab, @SerializedName("getMods") - GetMods; + GetMods, + @SerializedName("proxy") + ProxyRequest; public static Map<CommandType,String> data; diff --git a/src/main/java/de/torui/coflsky/commands/models/ProxyRequest.java b/src/main/java/de/torui/coflsky/commands/models/ProxyRequest.java new file mode 100644 index 0000000..b070355 --- /dev/null +++ b/src/main/java/de/torui/coflsky/commands/models/ProxyRequest.java @@ -0,0 +1,27 @@ +package de.torui.coflsky.commands.models; + +import com.google.gson.annotations.SerializedName; + +public class ProxyRequest { + @SerializedName("upload") + private boolean uploadEnabled; + + @SerializedName("id") + private String id; + + @SerializedName("url") + private String url; + + + public String getId(){ + return id; + } + + public String getUrl(){ + return url; + } + + public boolean isUploadEnabled(){ + return this.uploadEnabled; + } +} diff --git a/src/main/java/de/torui/coflsky/listeners/ChatListener.java b/src/main/java/de/torui/coflsky/listeners/ChatListener.java new file mode 100644 index 0000000..b808f5e --- /dev/null +++ b/src/main/java/de/torui/coflsky/listeners/ChatListener.java @@ -0,0 +1,22 @@ +package de.torui.coflsky.listeners; + +import de.torui.coflsky.CoflSky; +import de.torui.coflsky.utils.ChatUtils; +import net.minecraftforge.client.event.ClientChatReceivedEvent; +import net.minecraftforge.fml.common.eventhandler.EventPriority; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; + +public class ChatListener { + + @SubscribeEvent(priority = EventPriority.LOW, receiveCanceled = true) + public void onGuiChat(ClientChatReceivedEvent e) { + String unformatted = ChatUtils.cleanColour(e.message.getUnformattedText()); + + if(unformatted.startsWith("Your new API key is ")){ + // We have found the api key YAY! + CoflSky.getAPIKeyManager().getApiInfo().key = unformatted.substring("Your new API key is ".length()).substring(0, 36); + } + } + + +} diff --git a/src/main/java/de/torui/coflsky/minecraft_integration/CoflSessionManager.java b/src/main/java/de/torui/coflsky/minecraft_integration/CoflSessionManager.java index 26db791..cab0c15 100644 --- a/src/main/java/de/torui/coflsky/minecraft_integration/CoflSessionManager.java +++ b/src/main/java/de/torui/coflsky/minecraft_integration/CoflSessionManager.java @@ -95,6 +95,7 @@ public class CoflSessionManager { private static Path GetUserPath(String username) { return Paths.get(GetTempFileFolder().toString() + "/" + username); } + public static void DeleteCoflSession(String username) { Path path =GetUserPath(username); path.toFile().delete(); diff --git a/src/main/java/de/torui/coflsky/proxy/APIKeyManager.java b/src/main/java/de/torui/coflsky/proxy/APIKeyManager.java new file mode 100644 index 0000000..e726552 --- /dev/null +++ b/src/main/java/de/torui/coflsky/proxy/APIKeyManager.java @@ -0,0 +1,54 @@ +package de.torui.coflsky.proxy; + +import com.google.gson.Gson; +import com.google.gson.annotations.SerializedName; +import net.minecraftforge.fml.common.Loader; + +import java.io.*; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.stream.Collectors; + +public class APIKeyManager { + private final Gson gson = new Gson(); + private APIInfo apiInfo = new APIInfo(); + + public APIInfo getApiInfo(){ + return this.apiInfo; + } + + + public class APIInfo{ + @SerializedName("api-key") + public String key; + } + + public void loadIfExists() throws Exception { + Path dataPath = Paths.get(Loader.instance().getConfigDir().getPath(), "CoflSky", "api-key.json"); + File file = dataPath.toFile(); + if(file.exists()) { + BufferedReader reader = new BufferedReader( new InputStreamReader(new FileInputStream(file))); + String raw = reader.lines().collect(Collectors.joining("\n")); + this.apiInfo = gson.fromJson(raw,APIInfo.class); + reader.close(); + } + } + + + public void saveKey() throws Exception { + Path dataPath = Paths.get(Loader.instance().getConfigDir().getPath(), "CoflSky", "api-key.json"); + File file = dataPath.toFile(); + if(file.exists()) { + file.delete(); + } + file.createNewFile(); + + String data = gson.toJson(apiInfo); + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))); + bw.append(data); + bw.flush(); + bw.close(); + } + + +} diff --git a/src/main/java/de/torui/coflsky/proxy/ProxyManager.java b/src/main/java/de/torui/coflsky/proxy/ProxyManager.java new file mode 100644 index 0000000..b9abf86 --- /dev/null +++ b/src/main/java/de/torui/coflsky/proxy/ProxyManager.java @@ -0,0 +1,99 @@ +package de.torui.coflsky.proxy; + +import de.torui.coflsky.CoflSky; +import de.torui.coflsky.commands.models.ProxyRequest; + +import java.io.*; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +public class ProxyManager { + private final String ProxyResponseUrl = "http://sky.coflnet.com/api/data/proxy"; + private final ExecutorService requestExecutor = Executors.newSingleThreadExecutor(); + + + public void handleRequestAsync(ProxyRequest request){ + CompletableFuture<String> req = this.doRequest(request.getUrl()); + if(request.isUploadEnabled()) { + req.thenAcceptAsync(res -> this.uploadData(res,request.getId())); + } + } + + + private String getString(HttpURLConnection con) { + try { + InputStream in = new BufferedInputStream(con.getInputStream()); + ByteArrayOutputStream result = new ByteArrayOutputStream(); + byte[] buffer = new byte[1024]; + for (int length; (length = in.read(buffer)) != -1; ) { + result.write(buffer, 0, length); + } + String resString = result.toString("UTF-8"); + return resString; + } catch(IOException e){ + return null; + } + } + + public void uploadData(String data,String id){ + this.requestExecutor.submit(new Runnable() { + @Override + public void run() { + try{ + URL url = new URL(ProxyManager.this.ProxyResponseUrl); + HttpURLConnection con = (HttpURLConnection) url.openConnection(); + con.setRequestMethod("POST"); + + con.setRequestProperty("X-Request-Id", id); + + con.setDoOutput(true); + con.setDoInput(true); + + OutputStream os = con.getOutputStream(); + os.write(data.getBytes("UTF-8")); + os.close(); + String response = getString(con); + System.out.println("Response=" + response); + }catch (Exception exception){ + exception.printStackTrace(); + } + } + }); + } + + + private CompletableFuture<String> doRequest(String targetUrl){ + CompletableFuture<String> future = new CompletableFuture<>(); + + this.requestExecutor.submit(new Runnable() { + @Override + public void run() { + try{ + URL url = new URL(targetUrl); + HttpURLConnection con = (HttpURLConnection) url.openConnection(); + con.setRequestMethod("GET"); + con.setRequestProperty("Accept", "application/json"); + con.setRequestProperty("User-Agent", "CoflMod"); + + String key = CoflSky.getAPIKeyManager().getApiInfo().key; + + if(targetUrl.startsWith("https://api.hypixel.net") && !key.isEmpty()){ + con.setRequestProperty("API-Key", key); + } + + con.setDoInput(true); + future.complete(getString(con)); + }catch (Exception exception){ + exception.printStackTrace(); + } + } + }); + + return future; + } + + +} diff --git a/src/main/java/de/torui/coflsky/utils/ChatUtils.java b/src/main/java/de/torui/coflsky/utils/ChatUtils.java new file mode 100644 index 0000000..766fb2c --- /dev/null +++ b/src/main/java/de/torui/coflsky/utils/ChatUtils.java @@ -0,0 +1,10 @@ +package de.torui.coflsky.utils; + +public class ChatUtils { + + public static String cleanColour(String in) { + return in.replaceAll("(?i)\\u00A7.", ""); + } + + +} |