Merge pull request #179 from Parabot/development

[MERGE] Development into master
This commit is contained in:
Jeroen Ketelaar
2017-02-16 22:45:06 +01:00
committed by GitHub
17 changed files with 288 additions and 232 deletions
+2 -2
View File
@@ -7,7 +7,7 @@
<groupId>org.parabot</groupId>
<artifactId>client</artifactId>
<version>2.6.4</version>
<packaging>jar</packaging>
<properties>
@@ -73,7 +73,7 @@
<dependency>
<groupId>org.parabot</groupId>
<artifactId>internal-api</artifactId>
<version>1.4.46</version>
<version>1.51.1</version>
</dependency>
</dependencies>
+4 -23
View File
@@ -20,6 +20,7 @@ import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
/**
* The core of parabot
@@ -206,31 +207,11 @@ public class Core {
}
/**
* Validates the cache and removes the cache contents if required
* Method that removes the cache contents after 3 days
*/
private static void validateCache() {
File[] cache = Directories.getCachePath().listFiles();
Integer lowest = null;
if (cache != null) {
for (File f : cache) {
int date = (int) (f.lastModified() / 1000);
if (lowest == null || date < lowest) {
lowest = date;
}
}
}
try {
JSONObject object = (JSONObject) WebUtil.getJsonParser().parse(WebUtil.getContents("http://bdn.parabot.org/api/v2/bot/cache", "date=" + lowest));
if ((boolean) object.get("result")) {
Core.verbose("Making space for the latest cache files");
Directories.clearCache();
} else {
Core.verbose("Cache is up to date");
}
} catch (MalformedURLException | ParseException e) {
e.printStackTrace();
}
// Already handled by Directories initiating
// Method will be used once BDN V3 has a functionality for this
}
public static void downloadNewVersion() {
@@ -1,5 +1,6 @@
package org.parabot.core.asm.redirect;
import org.parabot.core.Core;
import org.parabot.core.asm.RedirectClassAdapter;
import org.parabot.environment.scripts.Script;
@@ -11,9 +12,11 @@ import java.lang.reflect.Method;
public class ClassRedirect {
public static Object newInstance(Class<?> c) throws IllegalAccessException, InstantiationException {
if (validStack()){
if (validStack()) {
return c.newInstance();
}
System.err.println(c.getName() + ".newInstance() Blocked.");
throw RedirectClassAdapter.createSecurityException();
}
@@ -22,7 +25,7 @@ public class ClassRedirect {
return c.getDeclaredField(s);
}
System.out.println(c.getName() + "." + c.getDeclaredField(s) + " Blocked.");
System.err.println(c.getName() + "." + c.getDeclaredField(s) + " Blocked.");
throw RedirectClassAdapter.createSecurityException();
}
@@ -31,17 +34,20 @@ public class ClassRedirect {
return c.getDeclaredMethod(name, params);
}
System.out.println(c.getName() + "#" + c.getDeclaredMethod(name, params) + " Blocked.");
System.err.println(c.getName() + "#" + c.getDeclaredMethod(name, params) + " Blocked.");
throw RedirectClassAdapter.createSecurityException();
}
public static Class<?> forName(String name) throws ClassNotFoundException {
if (name.contains("parabot"))
if (name.contains("parabot")) {
throw new ClassNotFoundException();
}
Core.verbose("Received #forName(" + name + ") call");
return Class.forName(name);
}
public static ClassLoader getClassLoader(Class<?> c) {
System.err.println(c.getName() + "#getClassLoader()" + " Blocked.");
throw RedirectClassAdapter.createSecurityException();
}
@@ -49,7 +55,7 @@ public class ClassRedirect {
if (validStack()) {
return c.getDeclaredFields();
}
System.out.println(c.getName() + "#getDeclaredFields()" + " Blocked.");
System.err.println(c.getName() + "#getDeclaredFields()" + " Blocked.");
throw RedirectClassAdapter.createSecurityException();
}
@@ -57,31 +63,31 @@ public class ClassRedirect {
if (validStack()) {
return c.getDeclaredMethods();
}
System.out.println(c.getName() + "#getDeclaredMethods()" + " Blocked.");
System.err.println(c.getName() + "#getDeclaredMethods()" + " Blocked.");
throw RedirectClassAdapter.createSecurityException();
}
public static Field[] getFields(Class<?> c){
public static Field[] getFields(Class<?> c) {
if (validStack()) {
return c.getFields();
}
System.out.println(c.getName() + "#getFields()" + " Blocked.");
System.err.println(c.getName() + "#getFields()" + " Blocked.");
throw RedirectClassAdapter.createSecurityException();
}
public static Annotation[] getAnnotations(Class<?> c){
if (validStack()){
public static Annotation[] getAnnotations(Class<?> c) {
if (validStack()) {
return c.getAnnotations();
}
System.out.println(c.getName() + "#getFields()" + " Blocked.");
System.err.println(c.getName() + "#getFields()" + " Blocked.");
throw RedirectClassAdapter.createSecurityException();
}
public static Method[] getMethods(Class<?> c){
public static Method[] getMethods(Class<?> c) {
if (validStack()) {
return c.getMethods();
}
System.out.println(c.getName() + "#getMethods()" + " Blocked.");
System.err.println(c.getName() + "#getMethods()" + " Blocked.");
throw RedirectClassAdapter.createSecurityException();
}
@@ -89,7 +95,7 @@ public class ClassRedirect {
if (validStack()) {
return c.getMethod(name, params);
}
System.out.println(c.getName() + "#getMethod()" + " Blocked.");
System.err.println(c.getName() + "#getMethod()" + " Blocked.");
throw RedirectClassAdapter.createSecurityException();
}
@@ -98,13 +104,14 @@ public class ClassRedirect {
if (validStack()) {
return c.getField(name);
}
System.out.println(c.getName() + "#getField()" + " Blocked.");
System.err.println(c.getName() + "#getField()" + " Blocked.");
throw RedirectClassAdapter.createSecurityException();
}
public static String getName(Class<?> c) {
if (c.getName().contains("parabot"))
if (c.getName().contains("parabot")) {
return "java.lang.String";
}
return c.getName();
}
@@ -2,4 +2,7 @@ package org.parabot.core.asm.redirect;
public class ProcessBuilderRedirect {
public static ProcessBuilder redirectErrorStream(ProcessBuilder pb, boolean redirectErrorStream) {
return pb;
}
}
@@ -5,35 +5,40 @@ import org.parabot.core.asm.RedirectClassAdapter;
import java.io.IOException;
public class RuntimeRedirect {
public static Runtime getRuntime(){
return Runtime.getRuntime();
}
public static int availableProcessors(Runtime r){
return 2;
}
public static long totalMemory(Runtime runtime){
return (long) 1024;
}
public static Runtime getRuntime() {
return Runtime.getRuntime();
}
public static long freeMemory(Runtime runtime){
return (long) 1024;
}
public static Process exec(Runtime r,String s){
if (s.contains("ping")){
System.out.println("Faked attempted command: " + s);
try {
return r.exec("ping 127.0.0.1");
} catch (IOException e) {
throw RedirectClassAdapter.createSecurityException();
}
}else{
System.out.println("Blocked attempted command: " + s);
throw RedirectClassAdapter.createSecurityException();
}
}
public static int availableProcessors(Runtime r) {
return 2;
}
public static long totalMemory(Runtime runtime) {
return (long) 1024;
}
public static long freeMemory(Runtime runtime) {
return (long) 1024;
}
public static Process exec(Runtime r, String[] s) {
System.out.println("Blocked attempted command: " + s);
throw RedirectClassAdapter.createSecurityException();
}
public static Process exec(Runtime r, String s) {
if (s.contains("ping")) {
System.out.println("Faked attempted command: " + s);
try {
return r.exec("ping 127.0.0.1");
} catch (IOException e) {
throw RedirectClassAdapter.createSecurityException();
}
} else {
System.out.println("Blocked attempted command: " + s);
throw RedirectClassAdapter.createSecurityException();
}
}
}
@@ -26,14 +26,12 @@ public class SystemRedirect {
public static String getProperty(String s) {
String value;
switch (s) {
case "user.home":
value = Directories.getCachePath().getAbsolutePath();
break;
case "java.class.path":
value = ".";
break;
default:
value = System.getProperty(s);
break;
}
System.out.printf("GetSystemProp %s = %s\n", s, value);
return value;
@@ -42,9 +40,6 @@ public class SystemRedirect {
public static String getProperty(String s, String s2) {
String value = null;
switch (s2) {
case "user.home":
value = Directories.getCachePath().getAbsolutePath();
break;
case "java.class.path":
value = ".";
break;
@@ -52,14 +47,12 @@ public class SystemRedirect {
if (value == null) {
switch (s) {
case "user.home":
value = Directories.getCachePath().getAbsolutePath();
break;
case "java.class.path":
value = ".";
break;
default:
value = System.getProperty(s);
break;
}
}
System.out.printf("GetSystemProp %s = %s\n", s, value);
@@ -1,9 +1,5 @@
package org.parabot.core.build;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
/**
*
* Class used for adding urls to the buildpath
@@ -11,16 +7,6 @@ import java.net.URLClassLoader;
* @author Everel
*
*/
public class BuildPath {
public static void add(final URL url) {
try {
Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
method.setAccessible(true);
method.invoke((URLClassLoader) ClassLoader.getSystemClassLoader(), url);
} catch (Exception e) {
e.printStackTrace();
}
}
public class BuildPath extends org.parabot.api.io.build.BuildPath {
}
@@ -1,67 +1,10 @@
package org.parabot.core.lib;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
/**
*
* @author Everel
*
*/
public abstract class Library {
/**
* Determines if this library jar has already been downloaded.
* @return <b>false</b> if library jar has not been downloaded, otherwise <b>true</b>
*/
public boolean hasJar() {
return getJarFile().exists();
}
/**
* Adds the library to the buildpath and validates if it has been added
*/
public abstract void init();
/**
* Determines if library has been added to the buildpath
* @return <b>true</b> if library has been added to the buildpath, otherwise <b>false</b>.
*/
public abstract boolean isAdded();
/**
* Gets the local file target/location of the jar file
* @return local file (target) to library
*/
public abstract File getJarFile();
/**
* Gets download url to the library
* @return url
*/
public abstract URL getDownloadLink();
/**
* Defines if the system requires a jar
* @return boolean
*/
public abstract boolean requiresJar();
/**
* Fetches URL from {@link Library#getJarFile()}
* @return URL to local library jar file
*/
public URL getJarFileURL() {
try {
return getJarFile().toURI().toURL();
} catch (MalformedURLException e) {
e.printStackTrace();
}
return null;
}
public abstract String getLibraryName();
public abstract class Library extends org.parabot.api.io.libraries.Library {
}
@@ -1,10 +1,12 @@
package org.parabot.core.ui;
import javafx.application.Application;
import org.parabot.core.Configuration;
import org.parabot.core.Context;
import org.parabot.core.Directories;
import org.parabot.core.ui.components.GamePanel;
import org.parabot.core.ui.components.VerboseLoader;
import org.parabot.core.ui.components.notifications.NotificationUI;
import org.parabot.core.ui.images.Images;
import org.parabot.core.ui.utils.SwingUtil;
import org.parabot.environment.OperatingSystem;
@@ -105,6 +107,9 @@ public class BotUI extends JFrame implements ActionListener, ComponentListener,
JMenuItem cacheClear = new JMenuItem("Clear cache");
cacheClear.setIcon(new ImageIcon(Images.getResource("/storage/images/trash.png")));
JMenuItem notifications = new JMenuItem("Notifications");
notifications.setIcon(new ImageIcon(Images.getResource("/storage/images/bell.png")));
screenshot.addActionListener(this);
proxy.addActionListener(this);
randoms.addActionListener(this);
@@ -113,6 +118,7 @@ public class BotUI extends JFrame implements ActionListener, ComponentListener,
explorer.addActionListener(this);
exit.addActionListener(this);
cacheClear.addActionListener(this);
notifications.addActionListener(this);
run.addActionListener(this);
pause.addActionListener(this);
@@ -131,6 +137,7 @@ public class BotUI extends JFrame implements ActionListener, ComponentListener,
scripts.add(stop);
features.add(cacheClear);
features.add(notifications);
menuBar.add(file);
menuBar.add(scripts);
@@ -237,6 +244,9 @@ public class BotUI extends JFrame implements ActionListener, ComponentListener,
case "Clear cache":
Directories.clearCache();
break;
case "Notifications":
Application.launch(NotificationUI.class);
break;
default:
System.out.println("Invalid command: " + command);
}
@@ -0,0 +1,20 @@
package org.parabot.core.ui.components.notifications;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import org.parabot.core.Configuration;
public class NotificationUI extends Application {
@Override
public void start(Stage stage) throws Exception {
//noinspection RedundantCast
BorderPane root = (BorderPane) FXMLLoader.load(this.getClass().getResource("/storage/ui/notifications.fxml"));
stage.setTitle(Configuration.BOT_TITLE);
stage.setScene(new Scene(root));
stage.show();
}
}
@@ -0,0 +1,72 @@
package org.parabot.core.ui.components.notifications;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ListView;
import javafx.stage.Stage;
import org.parabot.api.notifications.NotificationManager;
import org.parabot.api.notifications.types.NotificationType;
import java.net.URL;
import java.util.ResourceBundle;
/**
* @author JKetelaar
*/
public class NotificationUIController implements Initializable {
@FXML // fx:id="availableTypes"
private ListView availableTypes;
@FXML // fx:id="enabledTypes"
private ListView enabledTypes;
@Override
public void initialize(URL location, ResourceBundle resources) {
this.refreshTypes();
}
private void refreshTypes() {
availableTypes.getItems().clear();
for (NotificationType notificationType : NotificationManager.getContext().getAvailableNotificationTypes()) {
availableTypes.getItems().add(notificationType.getName());
}
enabledTypes.getItems().clear();
for (NotificationType notificationType : NotificationManager.getContext().getEnabledTypes()) {
enabledTypes.getItems().add(notificationType.getName());
}
}
@FXML
private void enableType() {
Object object = availableTypes.getSelectionModel().getSelectedItem();
if (object != null) {
String name = (String) object;
NotificationType type = NotificationManager.getContext().getNotificationType(name);
NotificationManager.getContext().enableNotificationType(type);
this.refreshTypes();
}
}
@FXML
private void disableType() {
Object object = enabledTypes.getSelectionModel().getSelectedItem();
if (object != null) {
String name = (String) object;
NotificationType type = NotificationManager.getContext().getNotificationType(name);
NotificationManager.getContext().disableNotificationType(type);
this.refreshTypes();
}
}
@FXML
private void save() {
Stage stage = (Stage) this.enabledTypes.getScene().getWindow();
stage.close();
}
}
@@ -12,40 +12,51 @@ import java.util.LinkedList;
/**
*
* Initiliazes the bot environment
*
* @author Everel
*
* Initializes the bot environment
*
* @author Everel, JKetelaar
*/
public class Environment {
public class Environment extends org.parabot.api.io.libraries.Environment {
/**
* Loads a new environment
*
* @param desc
*/
public static void load(final ServerDescription desc) {
LinkedList<Library> libs = new LinkedList<>();
libs.add(new JavaFX());
for(Library lib : libs) {
if (lib.requiresJar()) {
if (!lib.hasJar()) {
Core.verbose("Downloading " + lib.getLibraryName() + "...");
VerboseLoader.setState("Downloading " + lib.getLibraryName() + "...");
WebUtil.downloadFile(lib.getDownloadLink(), lib.getJarFile(), VerboseLoader.get());
Core.verbose("Downloaded " + lib.getLibraryName() + ".");
}
Core.verbose("Initializing " + lib.getLibraryName());
lib.init();
}
}
Core.verbose("Loading server: " + desc.toString() + "...");
private static LinkedList<Library> libs = new LinkedList<>();
ServerParser.SERVER_CACHE.get(desc).run();
}
static {
libs.add(new JavaFX());
}
/**
* Loads a new environment
*
* @param desc
*/
public static void load(final ServerDescription desc) {
for (Library lib : libs) {
loadLibrary(lib, true);
}
Core.verbose("Loading server: " + desc.toString() + "...");
ServerParser.SERVER_CACHE.get(desc).run();
}
/**
* Loads library into environment
*
* @param library
* @param verboseLoader defines if verboseLoader should be enabled
*/
public static void loadLibrary(Library library, boolean verboseLoader) {
if (library.requiresJar()) {
if (!library.hasJar()) {
Core.verbose("Downloading " + library.getLibraryName() + "...");
if (verboseLoader) {
VerboseLoader.setState("Downloading " + library.getLibraryName() + "...");
}
WebUtil.downloadFile(library.getDownloadLink(), library.getJarFile(), VerboseLoader.get());
Core.verbose("Downloaded " + library.getLibraryName() + ".");
}
Core.verbose("Initializing " + library.getLibraryName());
library.init();
}
}
}
@@ -1,52 +1,10 @@
package org.parabot.environment.api.utils;
import org.parabot.api.misc.StringUtil;
/**
* @author mkyong, JKetelaar
*/
public class StringUtils {
public class StringUtils extends StringUtil {
private static java.util.Random random = new java.util.Random();
private static char[] chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".toCharArray();
public static String convertHexToString(String hex) {
StringBuilder sb = new StringBuilder();
StringBuilder temp = new StringBuilder();
for (int i = 0; i < hex.length() - 1; i += 2) {
// grab the hex in pairs
String output = hex.substring(i, (i + 2));
// convert hex to decimal
int decimal = Integer.parseInt(output, 16);
// convert the decimal to character
sb.append((char) decimal);
temp.append(decimal);
}
return sb.toString();
}
public static String implode(String separator, String... data) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < data.length - 1; i++) {
//data.length - 1 => to not add separator at the end
if (!data[i].matches(" *")) {//empty string are ""; " "; " "; and so on
sb.append(data[i]);
sb.append(separator);
}
}
sb.append(data[data.length - 1].trim());
return sb.toString();
}
public static String randomString(final int length) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 20; i++) {
char c = chars[random.nextInt(chars.length)];
sb.append(c);
}
return sb.toString();
}
}
@@ -6,9 +6,9 @@ package org.parabot.environment.randoms;
public enum RandomType {
SCRIPT(0, "Script"),
ON_SCRIPT_START(0, "On script start"),
ON_SERVER_START(0, "On server start"),
ON_SCRIPT_FINISH(0, "On script finish");
ON_SCRIPT_START(1, "On script start"),
ON_SERVER_START(2, "On server start"),
ON_SCRIPT_FINISH(3, "On script finish");
private int id;
private String name;
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="231.0"
prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="org.parabot.core.ui.components.notifications.NotificationUIController">
<bottom>
<Button mnemonicParsing="false" text="Save" BorderPane.alignment="CENTER" onAction="#save">
<BorderPane.margin>
<Insets bottom="10.0" top="10.0"/>
</BorderPane.margin>
</Button>
</bottom>
<left>
<ListView fx:id="availableTypes" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER"/>
</left>
<right>
<ListView fx:id="enabledTypes" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER"/>
</right>
<center>
<VBox alignment="CENTER" prefHeight="200.0" prefWidth="100.0" BorderPane.alignment="CENTER">
<children>
<Button mnemonicParsing="false" onAction="#enableType" text="&gt;&gt;"/>
<Separator orientation="VERTICAL" prefHeight="40.0" prefWidth="0.0" visible="false"/>
<Button mnemonicParsing="false" onAction="#disableType" text="&lt;&lt;"/>
</children>
</VBox>
</center>
<top>
<Separator orientation="VERTICAL" prefHeight="7.0" prefWidth="15.0" visible="false"
BorderPane.alignment="CENTER"/>
</top>
</BorderPane>
@@ -0,0 +1,32 @@
package org.parabot;
import org.junit.Assert;
import org.junit.Test;
import org.parabot.core.Directories;
import java.io.File;
import java.io.IOException;
/**
* @author JKetelaar
*/
public class CacheValidationTest {
@Test
public void test() throws IOException {
Directories.validate();
File fileOne = new File(Directories.getCachePath(), "should-exist.tmp");
File fileTwo = new File(Directories.getCachePath(), "should-not-exist.tmp");
fileOne.createNewFile();
fileTwo.createNewFile();
fileTwo.setLastModified(System.currentTimeMillis() / 1000 - 350000);
Directories.clearCache(259200, false);
Assert.assertTrue(fileOne.exists());
Assert.assertTrue(!fileTwo.exists());
}
}