JKetelaar
2015-08-27 21:15:02 +02:00
parent f11339bce5
commit 5f567bbfea
16 changed files with 611 additions and 383 deletions
+9 -13
View File
@@ -10,21 +10,17 @@ import org.parabot.core.network.proxy.ProxyType;
import org.parabot.core.ui.BotUI;
import org.parabot.core.ui.ServerSelector;
import org.parabot.core.ui.utils.UILog;
import org.parabot.environment.api.utils.WebUtil;
import javax.swing.*;
import java.awt.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
/**
* Parabot v2.1
*
* @author Everel/Parnassian/Clisprail, Paradox, Matt, Dane
* @author Everel/Parnassian/Clisprail, Paradox/JKetelaar, Matt, Dane
* @version 2.1
* @see <a href="http://www.parabot.org">Homepage</a>
*/
@@ -91,14 +87,14 @@ public final class Landing {
* @return String array with username and password
*/
private static String[] getCredentials() {
try {
BufferedReader bufferedReader = WebUtil.getReader(new URL(
Configuration.GET_PASSWORD));
if (bufferedReader.readLine() != null) {
}
} catch (IOException e) {
e.printStackTrace();
}
// try {
// BufferedReader bufferedReader = WebUtil.getReader(new URL(
// Configuration.GET_PASSWORD));
// if (bufferedReader.readLine() != null) {
// }
// } catch (IOException e) {
// e.printStackTrace();
// }
return null;
}
@@ -21,5 +21,5 @@ public class Configuration {
public static final String DATA_API = "http://bdn.parabot.org/api/v2/data/";
public static final String ITEM_API = DATA_API + "items/";
public static final String BOT_VERSION = "2.1.12";
public static final String BOT_VERSION = "2.1.14";
}
+21 -25
View File
@@ -6,6 +6,7 @@ import org.parabot.Landing;
import org.parabot.environment.api.utils.WebUtil;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.security.MessageDigest;
@@ -99,8 +100,6 @@ public class Core {
*/
@SuppressWarnings("unused")
private static boolean checksumValid(){
String checksum = "";
File f = new File(Landing.class.getProtectionDomain().getCodeSource().getLocation().getFile());
if (f.isFile()) {
try {
@@ -152,11 +151,13 @@ public class Core {
JSONObject object = (JSONObject) WebUtil.getJsonParser().parse(br);
version = (String) object.get("result");
}
if (version != null) {
if (!Configuration.BOT_VERSION.equals(version)) {
Core.verbose("Our version: " + Configuration.BOT_VERSION);
Core.verbose("Latest version: " + version);
return false;
}
}
} catch (NumberFormatException | IOException | ParseException e) {
e.printStackTrace();
} finally {
@@ -172,36 +173,30 @@ public class Core {
return true;
}
private static boolean policyValid(){
return new File(Directories.getSettingsPath() + "/java.policy").exists();
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;
}
}
}
private static void createPolicy(){
File policy = new File(Directories.getSettingsPath() + "/java.policy");
if (!policy.exists()){
try {
final BufferedReader in = WebUtil.getReader(Configuration.DATA_API + "policy");
if (in != null) {
String line;
PrintWriter printWriter = new PrintWriter(Directories.getSettingsPath() + "/java.policy");
while ((line = in.readLine()) != null) {
if (line.contains("%parabot_resources%")){
line = line.replace("%parabot_resources%", Directories.getResourcesPath().getAbsolutePath());
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");
}
printWriter.println(line);
}
printWriter.close();
in.close();
}
} catch (IOException e) {
} catch (MalformedURLException | ParseException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args){
createPolicy();
}
/**
* Checks for updates.
@@ -210,6 +205,7 @@ public class Core {
*/
public static boolean isValid() {
Core.verbose("Checking for updates...");
validateCache();
if (versionValid() && checksumValid()){
Core.verbose("No updates available.");
@@ -194,4 +194,13 @@ public class Directories {
}
}
public static void clearCache() {
File[] cache = getCachePath().listFiles();
if (cache != null) {
for (File f : cache) {
Core.verbose("Clearing " + f.getName() + " from cache...");
f.delete();
}
}
}
}
@@ -2,14 +2,17 @@ package org.parabot.core.forum;
import org.json.simple.JSONObject;
import org.parabot.core.Configuration;
import org.parabot.core.Context;
import org.parabot.core.Core;
import org.parabot.core.parsers.scripts.BDNScripts;
import org.parabot.core.parsers.servers.PublicServers;
import org.parabot.core.ui.components.VerboseLoader;
import org.parabot.environment.api.utils.PBPreferences;
import org.parabot.environment.api.utils.WebUtil;
import org.parabot.environment.scripts.executers.BDNScriptsExecuter;
import org.parabot.environment.servers.executers.PublicServerExecuter;
import javax.swing.*;
import java.io.BufferedReader;
import java.net.URL;
import java.net.URLEncoder;
@@ -45,6 +48,7 @@ public final class AccountManager {
accessors.add(BDNScriptsExecuter.MANAGER_FETCHER);
accessors.add(PublicServers.MANAGER_FETCHER);
accessors.add(PublicServerExecuter.MANAGER_FETCHER);
accessors.add(PBPreferences.MANAGER_FETCHER);
for (final AccountManagerAccess accessor : accessors) {
accessor.setManager(instance);
@@ -60,30 +64,51 @@ public final class AccountManager {
return account;
}
public final boolean login(final String user, final String pass) {
public final boolean login(final String user, final String pass, boolean requestTwoStep) {
if (account != null) {
throw new IllegalStateException("Already logged in.");
}
JSONObject result;
JSONObject result = null;
if (!requestTwoStep){
try {
BufferedReader contents = WebUtil.getReader(WebUtil.getConnection(
new URL(Configuration.LOGIN_SERVER),
URLEncoder.encode(user, "UTF-8"),
URLEncoder.encode(pass, "UTF-8")));
result = (JSONObject) WebUtil.getJsonParser().parse(contents);
} catch (Throwable t) {
t.printStackTrace();
return false;
}
}else{
try {
String two = JOptionPane.showInputDialog("Please provide your two factor authentication code\nYou can find this in either your email or the app you've setup");
if (two != null && two.length() > 0) {
String contents = WebUtil.getContents(Configuration.LOGIN_SERVER,
"username=" + URLEncoder.encode(user, "UTF-8") + "&password=" + URLEncoder.encode(pass, "UTF-8") + "&2fa=" + URLEncoder.encode(two, "UTF-8")
);
result = (JSONObject) WebUtil.getJsonParser().parse(contents);
}
} catch (Throwable t) {
t.printStackTrace();
return false;
}
}
if (result != null) {
if (result.get("complete") != null) {
String api = (String) ((JSONObject) result.get("data")).get("api");
account = new Account(user, pass, api);
Context.setUsername(user);
return true;
} else if (result.get("error") != null) {
Core.verbose((String) result.get("error"));
String errorResult = (String) result.get("error");
if (errorResult.equals("2fa") || errorResult.equals("2fae")) {
return login(user, pass, true);
}
Core.verbose(errorResult);
return false;
}
}
@@ -47,14 +47,11 @@ public class NetworkInterface {
if (realMac != null)
return realMac;
try {
byte[] b = java.net.NetworkInterface.getByInetAddress(
return realMac = java.net.NetworkInterface.getByInetAddress(
InetAddress.getLocalHost()).getHardwareAddress();
if(b == null)
b = mac;
realMac = b;
} catch (Exception ignored) {
}
return realMac = mac;
return mac;
}
public static NetworkInterface getByInetAddress(InetAddress addr) {
@@ -1,25 +1,14 @@
package org.parabot.core.network.proxy;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import org.parabot.core.ui.utils.UILog;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import org.parabot.core.ui.utils.UILog;
public class ProxySocket extends Socket {
private static List<ProxySocket> connections = new ArrayList<ProxySocket>();
@@ -39,7 +39,7 @@ public class LoginUI extends JFrame {
String password = new String(txtPassword.getPassword());
if (username.length() > 0 && password.length() > 0) {
if (manager.login(username, password)) {
if (manager.login(username, password, false)) {
Core.verbose("Logged in.");
instance.dispose();
Core.verbose("Running server selector.");
@@ -53,7 +53,7 @@ public class LoginUI extends JFrame {
private void attempt(String user, String pass) {
Core.verbose("Logging in...");
if (manager.login(user, pass)) {
if (manager.login(user, pass, false)) {
Core.verbose("Logged in.");
instance.dispose();
Core.verbose("Running server selector.");
@@ -1,24 +1,11 @@
package org.parabot.core.ui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import org.parabot.core.network.NetworkInterface;
import org.parabot.core.network.proxy.ProxySocket;
import org.parabot.core.network.proxy.ProxyType;
import org.parabot.core.ui.utils.UILog;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
@@ -26,11 +13,10 @@ import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.PlainDocument;
import org.parabot.core.network.NetworkInterface;
import org.parabot.core.network.proxy.ProxySocket;
import org.parabot.core.network.proxy.ProxyType;
import org.parabot.core.ui.utils.UILog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class NetworkUI extends JFrame implements KeyListener, ActionListener,
DocumentListener {
@@ -102,12 +88,14 @@ public class NetworkUI extends JFrame implements KeyListener, ActionListener,
}
authCheckBox = new JCheckBox("Auth");
authCheckBox.setSelected(ProxySocket.auth);
authCheckBox.addActionListener(this);
authUsername = new JTextField();
authUsername.setEnabled(false);
authPassword = new JPasswordField();
authPassword.setEnabled(false);
authUsername.setEnabled(authCheckBox.isSelected());
authPassword.setEnabled(authCheckBox.isSelected());
JPanel p = createPanelUI();
add(p);
@@ -247,8 +235,11 @@ public class NetworkUI extends JFrame implements KeyListener, ActionListener,
}
}
String username = authUsername.getText();
char[] password = authPassword.getPassword();
ProxySocket
.setLogin(authUsername.getText(), authPassword.getPassword());
.setLogin(username, password);
byte[] mac = new byte[macList.length];
for (int i = 0; i < mac.length; i++)
@@ -265,8 +256,11 @@ public class NetworkUI extends JFrame implements KeyListener, ActionListener,
}
}
ProxySocket.setProxy((ProxyType) proxyType.getSelectedItem(),
proxyHost.getText(), proxyPort.getValue());
ProxyType type = (ProxyType) proxyType.getSelectedItem();
String host = proxyHost.getText();
int port = proxyPort.getValue();
ProxySocket.setProxy(type, host, port);
UILog.log("Info", "Network settings have been set!");
} catch (Exception e) {
UILog.log("Error",
@@ -4,6 +4,7 @@ import org.parabot.core.Context;
import org.parabot.core.Directories;
import org.parabot.core.desc.ScriptDescription;
import org.parabot.core.parsers.scripts.ScriptParser;
import org.parabot.environment.api.utils.WebUtil;
import org.parabot.environment.scripts.Category;
import javax.swing.*;
@@ -16,6 +17,9 @@ import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URLEncoder;
import java.util.HashMap;
/**
@@ -156,6 +160,11 @@ public final class ScriptSelector extends JFrame {
public void actionPerformed(ActionEvent e) {
String s = getScriptName(tree.getSelectionPath().toString());
if (s != null) {
try {
WebUtil.getContents("http://bdn.parabot.org/api/v2/scripts/local", "script=" + URLEncoder.encode(s, "UTF-8") + "&username=" + URLEncoder.encode(Context.getUsername(), "UTF-8"));
} catch (MalformedURLException | UnsupportedEncodingException e1) {
e1.printStackTrace();
}
runScript(format.get(s));
}
}
@@ -1,6 +1,5 @@
package org.parabot.core.ui.components;
import org.parabot.core.Context;
import org.parabot.core.Core;
import org.parabot.core.forum.AccountManager;
import org.parabot.core.forum.AccountManagerAccess;
@@ -61,8 +60,7 @@ public class VerboseLoader extends JPanel implements ProgressListener {
setOpaque(false);
if(username != null && password != null) {
if(Core.inDebugMode() || manager.login(username, password)) {
Context.setUsername(username);
if(Core.inDebugMode() || manager.login(username, password, false)) {
currentState = STATE_SERVER_SELECT;
}
}
@@ -133,7 +131,7 @@ public class VerboseLoader extends JPanel implements ProgressListener {
@Override
public void actionPerformed(ActionEvent e) {
if(manager.login(userInput.getText(), passInput.getText())) {
if(manager.login(userInput.getText(), passInput.getText(), false)) {
switchState(STATE_SERVER_SELECT);
} else {
Core.verbose("Failed to log in.");
@@ -0,0 +1,68 @@
package org.parabot.environment.api.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* @author JKetelaar
*/
public class FileUtil {
public static String getChecksum(File file){
if (file.isFile()) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
if (file.exists()) {
FileInputStream fis = new FileInputStream(file);
byte[] dataBytes = new byte[1024];
int nread;
while ((nread = fis.read(dataBytes)) != -1) {
md.update(dataBytes, 0, nread);
}
byte[] mdbytes = md.digest();
StringBuilder sb = new StringBuilder("");
for (int i = 0; i < mdbytes.length; i++) {
sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
}
return sb.toString();
}
} catch (NoSuchAlgorithmException | IOException e) {
e.printStackTrace();
}
}
return null;
}
public static byte[] getChecksumBytes(File file) {
if (file.isFile()) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
if (file.exists()) {
FileInputStream fis = new FileInputStream(file);
byte[] dataBytes = new byte[1024];
int nread;
while ((nread = fis.read(dataBytes)) != -1) {
md.update(dataBytes, 0, nread);
}
return md.digest();
}
} catch (NoSuchAlgorithmException | IOException e) {
e.printStackTrace();
}
}
return null;
}
}
@@ -0,0 +1,132 @@
package org.parabot.environment.api.utils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException;
import org.parabot.core.forum.AccountManager;
import org.parabot.core.forum.AccountManagerAccess;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URLEncoder;
import java.util.Map;
import java.util.Properties;
/**
* @author JKetelaar
*/
public class PBPreferences {
private static AccountManager manager;
private Properties properties;
private int scriptID;
public static final AccountManagerAccess MANAGER_FETCHER = new AccountManagerAccess() {
@Override
public final void setManager(AccountManager manager) {
PBPreferences.manager = manager;
}
};
public PBPreferences(int scriptID) {
this.scriptID = scriptID;
this.updateSettings();
}
private void updateSettings() {
properties = new Properties();
try {
JSONObject result = (JSONObject) WebUtil.getJsonParser().parse(
WebUtil.getContents("http://bdn.parabot.org/api/v2/user/preferences/" + scriptID,
"apikey=" + manager.getAccount().getApi())
);
JSONArray resultArray;
if ((resultArray = ((JSONArray) result.get("result"))) != null) {
for(Object rObject : resultArray) {
JSONObject resultObject = (JSONObject) rObject;
for (Object map : resultObject.entrySet()) {
Map.Entry<?, ?> pairs = (Map.Entry<?, ?>) map;
properties.put(pairs.getKey(), pairs.getValue());
}
}
}
} catch (IOException | ParseException e) {
e.printStackTrace();
}
}
/**
* Change a setting
* @param key
* @param value
*/
public void adjustSettings(Object key, Object value) {
this.addSetting(key, value);
}
/**
* Get a setting value
* @param key
* @return
*/
public Object getSetting(Object key){
return this.properties.get(key);
}
/**
* Get a setting value as string
* @param key
* @return
*/
public String getSetting(String key){
return this.properties.getProperty(key);
}
/**
* Remove a setting
* @param key
*/
public void removeSetting(Object key){
try {
JSONObject result = (JSONObject) WebUtil.getJsonParser().parse(
WebUtil.getContents("http://bdn.parabot.org/api/v2/user/preferences/set/",
"apikey=" + manager.getAccount().getApi() +
"&key=" + URLEncoder.encode(String.valueOf(key), "UTF-8") +
"&script=" + String.valueOf(scriptID)
)
);
if ((boolean)result.get("result")){
this.properties.remove(key);
}
} catch (ParseException | MalformedURLException | UnsupportedEncodingException e) {
e.printStackTrace();
}
}
/**
* Add a setting
* @param key
* @param value
*/
public void addSetting(Object key, Object value){
try {
JSONObject result = (JSONObject) WebUtil.getJsonParser().parse(
WebUtil.getContents("http://bdn.parabot.org/api/v2/user/preferences/set/",
"apikey=" + manager.getAccount().getApi() +
"&key=" + URLEncoder.encode(String.valueOf(key), "UTF-8") +
"&value=" + URLEncoder.encode(String.valueOf(value), "UTF-8") +
"&script=" + String.valueOf(scriptID)
)
);
if ((boolean)result.get("result")){
this.properties.put(key, value);
}
} catch (ParseException | MalformedURLException | UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
@@ -4,6 +4,7 @@ import org.parabot.core.Context;
import org.parabot.core.Core;
import org.parabot.core.ui.BotUI;
import org.parabot.core.ui.Logger;
import org.parabot.environment.api.utils.PBPreferences;
import org.parabot.environment.api.utils.Time;
import org.parabot.environment.scripts.framework.*;
import org.parabot.environment.scripts.framework.Frameworks;
@@ -28,9 +29,11 @@ public class Script implements Runnable {
public static final int STATE_STOPPED = 2;
private Collection<Strategy> strategies;
private PBPreferences preferences;
private AbstractFramework frameWork;
private int state;
private int frameWorkType;
private int scriptID;
public boolean onExecute() {
return true;
@@ -167,4 +170,15 @@ public class Script implements Runnable {
public int getState() {
return state;
}
public PBPreferences getPreferences(){
if (this.preferences == null){
this.preferences = new PBPreferences(scriptID);
}
return this.preferences;
}
public void setScriptID(int scriptID){
this.scriptID = scriptID;
}
}
@@ -74,6 +74,7 @@ public class BDNScriptsExecuter extends ScriptExecuter {
final Class<?> scriptClass = loader.loadClass(className);
final Constructor<?> con = scriptClass.getConstructor();
final Script script = (Script) con.newInstance();
script.setScriptID(this.id);
super.finalize(tg, script);
if (manager.getAccount().getApi() != null) {
@@ -1,9 +1,9 @@
package org.parabot.environment.scripts.executers;
import java.lang.reflect.Constructor;
import org.parabot.environment.scripts.Script;
import java.lang.reflect.Constructor;
/**
*
* Loads a locally stored script