-
Notifications
You must be signed in to change notification settings - Fork 446
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Only me, no regrets In the middle of the night Buying drinks to myself And you won't be on my mind
- Loading branch information
1 parent
3043925
commit 6f73640
Showing
12 changed files
with
425 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
src/main/java/gg/skytils/skytilsmod/tweaker/EssentialPlatformSetup.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* Skytils - Hypixel Skyblock Quality of Life Mod | ||
* Copyright (C) 2022 Skytils | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package gg.skytils.skytilsmod.tweaker; | ||
|
||
import gg.skytils.skytilsmod.Reference; | ||
import org.apache.commons.io.IOUtils; | ||
import org.apache.logging.log4j.LogManager; | ||
|
||
import java.io.File; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Locale; | ||
|
||
import static gg.skytils.skytilsmod.tweaker.TweakerUtil.*; | ||
|
||
public class EssentialPlatformSetup { | ||
private static final String[] dataURLCandidates = {System.getProperty("skytils.dataURL"), Reference.dataUrl, "https://skytilsmod-data.pages.dev/", "https://cdn.jsdelivr.net/gh/Skytils/SkytilsMod-Data@main/"}; | ||
|
||
private static boolean trySetDataUrl(String url) { | ||
if (url == null) return false; | ||
try { | ||
return makeRequest(url + "CANYOUSEEME").contains("YOUCANSEEME"); | ||
} catch (Exception e) { | ||
LogManager.getLogger().error("Failed to contact url " + url); | ||
return false; | ||
} | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static void setup() { | ||
try { | ||
String ver = System.getProperty("java.runtime.version", "unknown"); | ||
String javaLoc = System.getProperty("java.home"); | ||
if (ver.contains("1.8.0_51") || javaLoc.contains("jre-legacy")) { | ||
Path keyStoreLoc = Paths.get("./config/skytils/updates/files/skytilsletsencrypt.jks"); | ||
File keyStoreFile = keyStoreLoc.toFile(); | ||
if (!keyStoreFile.exists()) { | ||
System.out.println("Skytils is attempting to run keytool."); | ||
Files.createDirectories(keyStoreLoc.getParent()); | ||
try (InputStream in = EssentialPlatformSetup.class.getResourceAsStream("/skytilsletsencrypt.jks"); OutputStream os = Files.newOutputStream(keyStoreLoc)) { | ||
IOUtils.copy(in, os); | ||
} | ||
String os = System.getProperty("os.name", "unknown"); | ||
String keyStorePath = javaLoc + File.separator + "lib" + File.separator + "security" + File.separator + "cacerts"; | ||
String keyToolPath = javaLoc + File.separator + "bin" + File.separator + (os.toLowerCase(Locale.ENGLISH).startsWith("windows") ? "keytool.exe" : "keytool"); | ||
File log = new File("./config/skytils/updates/files/sslfix-" + System.currentTimeMillis() + ".log"); | ||
new ProcessBuilder() | ||
.command(keyToolPath, "-importkeystore", "-srckeystore", keyStoreFile.getAbsolutePath(), "-destkeystore", keyStorePath, "-srcstorepass", "skytilsontop", "-deststorepass", "changeit", "-noprompt") | ||
.redirectOutput(log) | ||
.redirectError(log) | ||
.start().waitFor(); | ||
System.out.println("A reboot of Minecraft is required for the code to work, force closing the game"); | ||
exit(); | ||
} | ||
} | ||
} catch (Throwable t) { | ||
t.printStackTrace(); | ||
} | ||
|
||
registerTransformerExclusions( | ||
"kotlin.", | ||
"kotlinx.", | ||
"gg.skytils.asmhelper.", | ||
"gg.skytils.skytilsmod.tweaker.", | ||
"gg.skytils.skytilsmod.asm." | ||
); | ||
|
||
for (final String url : dataURLCandidates) { | ||
if (trySetDataUrl(url)) { | ||
Reference.dataUrl = url; | ||
break; | ||
} | ||
} | ||
LogManager.getLogger().info("Data URL: " + Reference.dataUrl); | ||
|
||
try { | ||
if (Integer.parseInt(makeRequest(Reference.dataUrl + "api/version")) > Reference.apiVersion) { | ||
showMessage("<html><p>" + | ||
"Your version of Skytils requires a<br>" + | ||
"mandatory update before you can play!<br>" + | ||
"</p></html>"); | ||
} | ||
} catch (Throwable t) { | ||
t.printStackTrace(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
src/main/java/gg/skytils/skytilsmod/tweaker/SkytilsSecurityManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/* | ||
* Skytils - Hypixel Skyblock Quality of Life Mod | ||
* Copyright (C) 2022 Skytils | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package gg.skytils.skytilsmod.tweaker; | ||
|
||
import net.minecraftforge.fml.relauncher.FMLSecurityManager; | ||
import sun.security.util.SecurityConstants; | ||
|
||
import javax.swing.*; | ||
import java.awt.*; | ||
import java.awt.event.MouseAdapter; | ||
import java.awt.event.MouseEvent; | ||
import java.io.File; | ||
import java.lang.reflect.Field; | ||
import java.security.AccessController; | ||
import java.security.Permission; | ||
import java.security.PrivilegedAction; | ||
|
||
public class SkytilsSecurityManager extends SecurityManager { | ||
private final boolean isForge; | ||
|
||
public SkytilsSecurityManager(boolean isForge) { | ||
this.isForge = isForge; | ||
} | ||
|
||
@Override | ||
public void checkPermission(Permission perm) { | ||
String permName = perm.getName() != null ? perm.getName() : "missing"; | ||
if (permName.startsWith("exitVM")) { | ||
Class<?>[] classContexts = getClassContext(); | ||
String callingClass = classContexts.length > 3 ? classContexts[4].getName() : "none"; | ||
String callingParent = classContexts.length > 4 ? classContexts[5].getName() : "none"; | ||
// Skytils: allow Skytils tweaker classes to close the game | ||
if (callingClass.startsWith("gg.skytils.skytilsmod.tweaker.")) return; | ||
if (callingClass.startsWith("gg.skytils.skytilsmod.loader.")) return; | ||
// Skytils: allow the LaunchWrapper to close the game | ||
if (callingClass.equals("net.minecraft.launchwrapper.Launch") && callingParent.equals("net.minecraft.launchwrapper.Launch")) { | ||
showMessage(); | ||
return; | ||
} | ||
// FML is allowed to call system exit and the Minecraft applet (from the quit button) | ||
if (!isForge && !(callingClass.startsWith("net.minecraftforge.fml.") | ||
|| "net.minecraft.server.dedicated.ServerHangWatchdog$1".equals(callingClass) | ||
|| "net.minecraft.server.dedicated.ServerHangWatchdog".equals(callingClass) | ||
|| ("net.minecraft.client.Minecraft".equals(callingClass) && "net.minecraft.client.Minecraft".equals(callingParent)) | ||
|| ("net.minecraft.server.dedicated.DedicatedServer".equals(callingClass) && "net.minecraft.server.MinecraftServer".equals(callingParent))) | ||
) { | ||
throw new FMLSecurityManager.ExitTrappedException(); | ||
} | ||
} else if ("setSecurityManager".equals(permName)) { | ||
throw new SecurityException("Cannot replace the FML (Skytils) security manager"); | ||
} | ||
} | ||
|
||
private void showMessage() { | ||
try { | ||
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
// This makes the JOptionPane show on taskbar and stay on top | ||
JFrame frame = new JFrame(); | ||
frame.setUndecorated(true); | ||
frame.setAlwaysOnTop(true); | ||
frame.setLocationRelativeTo(null); | ||
frame.setVisible(true); | ||
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); | ||
|
||
JButton openLogs = new JButton("Open Logs Folder"); | ||
openLogs.addMouseListener(new MouseAdapter() { | ||
@Override | ||
public void mouseClicked(MouseEvent event) { | ||
try { | ||
Desktop.getDesktop().open(new File("./logs")); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
}); | ||
|
||
JButton openLatestLog = new JButton("Open Latest Log"); | ||
openLatestLog.addMouseListener(new MouseAdapter() { | ||
@Override | ||
public void mouseClicked(MouseEvent event) { | ||
try { | ||
Desktop.getDesktop().open(new File("./logs/latest.log")); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
}); | ||
|
||
Object[] options = new Object[]{openLatestLog, openLogs}; | ||
JOptionPane.showOptionDialog( | ||
frame, | ||
"The game crashed whilst launching.", | ||
"Minecraft Crash", | ||
JOptionPane.DEFAULT_OPTION, | ||
JOptionPane.ERROR_MESSAGE, | ||
null, | ||
options, | ||
options[0] | ||
); | ||
} | ||
|
||
static void overrideSecurityManager(Boolean isForge) { | ||
try { | ||
SecurityManager s = new SkytilsSecurityManager(isForge); | ||
|
||
if (s.getClass().getClassLoader() != null) { | ||
AccessController.doPrivileged((PrivilegedAction<Object>) () -> { | ||
s.getClass().getProtectionDomain().implies | ||
(SecurityConstants.ALL_PERMISSION); | ||
return null; | ||
}); | ||
} | ||
|
||
Field field = TweakerUtil.findField(System.class, "security"); | ||
field.setAccessible(true); | ||
field.set(null, s); | ||
} catch (Throwable e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
Oops, something went wrong.