summaryrefslogtreecommitdiffstats
path: root/src/main/java/de/spline/kvm/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/de/spline/kvm/utils')
-rw-r--r--src/main/java/de/spline/kvm/utils/Kvm.java61
-rw-r--r--src/main/java/de/spline/kvm/utils/ReflectionUtils.java84
-rw-r--r--src/main/java/de/spline/kvm/utils/SSLUtils.java47
3 files changed, 192 insertions, 0 deletions
diff --git a/src/main/java/de/spline/kvm/utils/Kvm.java b/src/main/java/de/spline/kvm/utils/Kvm.java
new file mode 100644
index 0000000..896150a
--- /dev/null
+++ b/src/main/java/de/spline/kvm/utils/Kvm.java
@@ -0,0 +1,61 @@
+package de.spline.kvm.utils;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import com.gistlabs.mechanize.MechanizeAgent;
+import com.gistlabs.mechanize.Resource;
+import com.gistlabs.mechanize.document.Document;
+import com.gistlabs.mechanize.document.node.Node;
+import com.gistlabs.mechanize.parameters.Parameters;
+
+public class Kvm
+{
+ protected String host;
+ protected MechanizeAgent agent;
+
+ public Kvm(String host)
+ {
+ this.host = host;
+ this.agent = new MechanizeAgent();
+ }
+
+ public void login(String username, String password)
+ {
+ Parameters param = new Parameters();
+ param.add("login", username);
+ param.add("password", password);
+ param.add("action_login", "Login");
+
+ Resource resource = agent.post(host + "/auth.asp", param);
+ if (resource.asString().contains("Authentication failed.")) {
+ System.err.println("Invalid login!");
+ System.exit(1);
+ }
+ }
+
+ public Map<String, String> getAppletParameters()
+ {
+ Document doc = agent.get(host + "/title_app.asp");
+ if (!doc.asString().contains("<param name=\"APPLET_ID\"")) {
+ logout();
+
+ System.err.println("Error getting applet parameters.");
+ System.exit(1);
+ }
+
+ Map<String, String> params = new HashMap<>();
+ for (Node node : doc.getRoot().findAll("html body applet param")) {
+ String name = node.getAttribute("name");
+ String value = node.getAttribute("value");
+ params.put(name.toUpperCase(), value);
+ }
+
+ return params;
+ }
+
+ public void logout()
+ {
+ agent.get(host + "/logout");
+ }
+}
diff --git a/src/main/java/de/spline/kvm/utils/ReflectionUtils.java b/src/main/java/de/spline/kvm/utils/ReflectionUtils.java
new file mode 100644
index 0000000..03f3a9f
--- /dev/null
+++ b/src/main/java/de/spline/kvm/utils/ReflectionUtils.java
@@ -0,0 +1,84 @@
+package de.spline.kvm.utils;
+
+import java.awt.PopupMenu;
+import java.lang.reflect.Field;
+
+import nn.pp.rc.RemoteConsoleApplet;
+import nn.pp.rc.ServerConsolePanelBase;
+
+public class ReflectionUtils
+{
+ /**
+ * Set a field of an object with reflection. This ignores all access
+ * permissions (f.e. private) and accesses the field by name (so it works
+ * for strange field names).
+ *
+ * @param cls
+ * @param instance
+ * @param field
+ * @param value
+ */
+ public static void setField(Class<?> cls, Object instance, String field, Object value)
+ {
+ try {
+ Field f = cls.getDeclaredField(field);
+ f.setAccessible(true);
+ f.set(instance, value);
+ } catch (NoSuchFieldException | IllegalAccessException e) {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * Get a field of an object with reflection. This ignores all access
+ * permissions (f.e. private) and accesses the field by name (so it works
+ * for strange field names).
+ *
+ * @param cls
+ * @param instance
+ * @param field
+ * @return
+ */
+ public static Object getField(Class<?> cls, Object instance, String field)
+ {
+ try {
+ Field f = cls.getDeclaredField(field);
+ f.setAccessible(true);
+ return f.get(instance);
+ } catch (NoSuchFieldException | IllegalAccessException e) {
+ e.printStackTrace();
+ }
+
+ return null;
+ }
+
+ /**
+ * Get the ServerConsolePanelBase field from the applet instance. We need
+ * to use reflection here, because the obfuscation has produced strange
+ * field names and we cannot access it using normal methods.
+ *
+ * @param applet RemoteConsoleApplet instance
+ * @return ServerConsolePanelBase instance of this applet
+ */
+ public static ServerConsolePanelBase getConsolePanel(RemoteConsoleApplet applet)
+ {
+ return (ServerConsolePanelBase) ReflectionUtils.getField(RemoteConsoleApplet.class, applet, "else");
+ }
+
+ /**
+ * Get the popup option menu from a ServerConsolePanelBase instance. The
+ * field name is okay-ish here, but we also need to use reflection here
+ * because this field in private.
+ *
+ * (We use this option menu to fix a silly bug: The menu is not displayed,
+ * because the "label" is removed after initialization and this is causing
+ * NullPointerExceptions.)
+ *
+ * @param consolePanel ServerConsolePanelBase instance
+ * @return options menu
+ */
+ public static PopupMenu getOptionMenu(ServerConsolePanelBase consolePanel)
+ {
+ return (PopupMenu)ReflectionUtils.getField(ServerConsolePanelBase.class, consolePanel, "G");
+ }
+}
diff --git a/src/main/java/de/spline/kvm/utils/SSLUtils.java b/src/main/java/de/spline/kvm/utils/SSLUtils.java
new file mode 100644
index 0000000..75bb1dd
--- /dev/null
+++ b/src/main/java/de/spline/kvm/utils/SSLUtils.java
@@ -0,0 +1,47 @@
+package de.spline.kvm.utils;
+
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLSocketFactory;
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.TrustManagerFactory;
+import java.io.IOException;
+import java.io.InputStream;
+import java.security.GeneralSecurityException;
+import java.security.KeyStore;
+
+public class SSLUtils
+{
+ public static SSLSocketFactory getSocketFactory()
+ {
+ SSLContext ctx = null;
+
+ try {
+ ctx = SSLContext.getInstance("TLS");
+ ctx.init(null, getTrustManagers(), null);
+ }
+ catch (GeneralSecurityException | IOException e) {
+ e.printStackTrace();
+ System.exit(1);
+ }
+
+ return ctx.getSocketFactory();
+ }
+
+ private static TrustManager[] getTrustManagers()
+ throws GeneralSecurityException, IOException
+ {
+ TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(
+ TrustManagerFactory.getDefaultAlgorithm());
+ trustManagerFactory.init(loadKeyStore());
+ return trustManagerFactory.getTrustManagers();
+ }
+
+ private static KeyStore loadKeyStore()
+ throws GeneralSecurityException, IOException
+ {
+ KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
+ InputStream in = ClassLoader.getSystemResourceAsStream("dfn-g2.jks");
+ keyStore.load(in, "changeit".toCharArray());
+ return keyStore;
+ }
+}