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";
}
+27 -31
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,10 +151,12 @@ public class Core {
JSONObject object = (JSONObject) WebUtil.getJsonParser().parse(br);
version = (String) object.get("result");
}
if (!Configuration.BOT_VERSION.equals(version)) {
Core.verbose("Our version: " + Configuration.BOT_VERSION);
Core.verbose("Latest version: " + version);
return false;
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();
@@ -172,35 +173,29 @@ public class Core {
return true;
}
private static boolean policyValid(){
return new File(Directories.getSettingsPath() + "/java.policy").exists();
}
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());
}
printWriter.println(line);
}
printWriter.close();
in.close();
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;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args){
createPolicy();
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();
}
}
/**
@@ -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;
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);
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")));
} catch (Throwable t) {
t.printStackTrace();
return false;
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 != 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"));
} else if (result.get("error") != null) {
String errorResult = (String) result.get("error");
if (errorResult.equals("2fa") || errorResult.equals("2fae")) {
return login(user, pass, true);
}
Core.verbose(errorResult);
return false;
}
}
@@ -12,10 +12,10 @@ public class NetworkInterface {
private static NetworkInterface cached;
static {
try {
mac = getRealHardwareAddress();
} catch (SocketException ignored) {
}
try {
mac = getRealHardwareAddress();
} catch (SocketException ignored) {
}
}
public byte[] getHardwareAddress() {
@@ -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) {
@@ -67,7 +64,7 @@ public class NetworkInterface {
System.out.println("Setting mac address to:" + formatMac(mac2));
mac = mac2;
}
public static String formatMac(byte[] mac){
StringBuffer b = new StringBuffer();
for(int i = 0; i < 6;i++){
@@ -77,4 +74,4 @@ public class NetworkInterface {
}
return b.toString();
}
}
}
@@ -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>();
@@ -294,7 +283,7 @@ public class ProxySocket extends Socket {
}
public static void setLogin(String user, char[] pass) {
setLogin(user,new String(pass));
setLogin(user, new String(pass));
}
public static void setLogin(String user, String pass) {
@@ -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.");
+262 -268
View File
@@ -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,317 +13,324 @@ 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 {
DocumentListener {
private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 1L;
private static NetworkUI instance;
private static NetworkUI instance;
private JComboBox<ProxyType> proxyType;
private JTextField proxyHost;
private IntTextField proxyPort;
private JButton submitButton;
private JComboBox<ProxyType> proxyType;
private JTextField proxyHost;
private IntTextField proxyPort;
private JButton submitButton;
private JList<String>[] macList;
private JScrollPane[] macScrollList;
private JList<String>[] macList;
private JScrollPane[] macScrollList;
private JCheckBox authCheckBox;
private JTextField authUsername;
private JPasswordField authPassword;
private JCheckBox authCheckBox;
private JTextField authUsername;
private JPasswordField authPassword;
private NetworkUI() {
initGUI();
}
private NetworkUI() {
initGUI();
}
public static NetworkUI getInstance() {
return instance == null ? instance = new NetworkUI() : instance;
}
public static NetworkUI getInstance() {
return instance == null ? instance = new NetworkUI() : instance;
}
@Override
public void setVisible(boolean b) {
BotUI.getInstance().setEnabled(!b);
if (ProxySocket.getProxyAddress() != null)
proxyHost.setText(ProxySocket.getProxyAddress().getHostName());
proxyPort.setText("" + ProxySocket.getProxyPort());
proxyType.setSelectedItem(ProxySocket.getProxyType());
authCheckBox.setSelected(ProxySocket.auth);
setLocationRelativeTo(BotUI.getInstance());
super.setVisible(b);
}
@Override
public void setVisible(boolean b) {
BotUI.getInstance().setEnabled(!b);
if (ProxySocket.getProxyAddress() != null)
proxyHost.setText(ProxySocket.getProxyAddress().getHostName());
proxyPort.setText("" + ProxySocket.getProxyPort());
proxyType.setSelectedItem(ProxySocket.getProxyType());
authCheckBox.setSelected(ProxySocket.auth);
setLocationRelativeTo(BotUI.getInstance());
super.setVisible(b);
}
@SuppressWarnings("unchecked")
private void initGUI() {
proxyType = new JComboBox<ProxyType>(ProxyType.values());
proxyType.setSelectedItem(ProxySocket.getProxyType());
@SuppressWarnings("unchecked")
private void initGUI() {
proxyType = new JComboBox<ProxyType>(ProxyType.values());
proxyType.setSelectedItem(ProxySocket.getProxyType());
proxyType.addActionListener(this);
proxyType.addActionListener(this);
proxyHost = new JTextField();
proxyHost.addKeyListener(this);
proxyHost = new JTextField();
proxyHost.addKeyListener(this);
proxyPort = new IntTextField(80, 5);
proxyPort.setColumns(5);
proxyPort.addKeyListener(this);
proxyPort = new IntTextField(80, 5);
proxyPort.setColumns(5);
proxyPort.addKeyListener(this);
submitButton = new JButton("Submit");
submitButton.addActionListener(this);
submitButton = new JButton("Submit");
submitButton.addActionListener(this);
byte[] mac = new byte[6];
mac = NetworkInterface.mac;
macList = new JList[mac.length];
macScrollList = new JScrollPane[mac.length];
for (int i = 0; i < mac.length; i++) {
int value = mac[i] & 0xFF;
macList[i] = createMacList();
macList[i].setSelectedIndex(value);
macScrollList[i] = new JScrollPane(macList[i]);
macList[i].ensureIndexIsVisible(value > 0 ? value - 1 : value);
byte[] mac = new byte[6];
mac = NetworkInterface.mac;
macList = new JList[mac.length];
macScrollList = new JScrollPane[mac.length];
for (int i = 0; i < mac.length; i++) {
int value = mac[i] & 0xFF;
macList[i] = createMacList();
macList[i].setSelectedIndex(value);
macScrollList[i] = new JScrollPane(macList[i]);
macList[i].ensureIndexIsVisible(value > 0 ? value - 1 : value);
}
}
authCheckBox = new JCheckBox("Auth");
authCheckBox.addActionListener(this);
authCheckBox = new JCheckBox("Auth");
authCheckBox.setSelected(ProxySocket.auth);
authCheckBox.addActionListener(this);
authUsername = new JTextField();
authUsername.setEnabled(false);
authPassword = new JPasswordField();
authPassword.setEnabled(false);
authUsername = new JTextField();
authPassword = new JPasswordField();
JPanel p = createPanelUI();
add(p);
setResizable(false);
setDefaultCloseOperation(HIDE_ON_CLOSE);
pack();
setTitle("Network Settings");
}
authUsername.setEnabled(authCheckBox.isSelected());
authPassword.setEnabled(authCheckBox.isSelected());
private JPanel createPanelUI() {
JPanel ret = new JPanel();
ret.setLayout(new BoxLayout(ret, BoxLayout.LINE_AXIS));
Box main = Box.createVerticalBox();
JPanel p = createPanelUI();
add(p);
setResizable(false);
setDefaultCloseOperation(HIDE_ON_CLOSE);
pack();
setTitle("Network Settings");
}
Box type = Box.createHorizontalBox();
type.add(new JLabel("Proxy Type: "));
type.add(proxyType);
private JPanel createPanelUI() {
JPanel ret = new JPanel();
ret.setLayout(new BoxLayout(ret, BoxLayout.LINE_AXIS));
Box main = Box.createVerticalBox();
Box host = Box.createHorizontalBox();
host.add(new JLabel("Proxy Host: "));
host.add(proxyHost);
Box type = Box.createHorizontalBox();
type.add(new JLabel("Proxy Type: "));
type.add(proxyType);
Box port = Box.createHorizontalBox();
port.add(new JLabel("Proxy Port: "));
port.add(proxyPort);
Box host = Box.createHorizontalBox();
host.add(new JLabel("Proxy Host: "));
host.add(proxyHost);
Box auth = Box.createHorizontalBox();
auth.add(authCheckBox);
Box port = Box.createHorizontalBox();
port.add(new JLabel("Proxy Port: "));
port.add(proxyPort);
auth.add(Box.createHorizontalStrut(3));
Box auth = Box.createHorizontalBox();
auth.add(authCheckBox);
auth.add(new JLabel("User:"));
auth.add(authUsername);
auth.add(Box.createHorizontalStrut(3));
auth.add(Box.createHorizontalStrut(3));
auth.add(new JLabel("User:"));
auth.add(authUsername);
auth.add(new JLabel("Pass:"));
auth.add(authPassword);
auth.add(Box.createHorizontalStrut(3));
Box macBox = Box.createHorizontalBox();
macBox.add(new JLabel("MAC:"));
for (int i = 0; i < macList.length; i++) {
macBox.add(new JScrollPane(macList[i]));
macBox.add(Box.createHorizontalStrut(5));
}
auth.add(new JLabel("Pass:"));
auth.add(authPassword);
Box submit = Box.createHorizontalBox();
submit.add(submitButton);
Box macBox = Box.createHorizontalBox();
macBox.add(new JLabel("MAC:"));
for (int i = 0; i < macList.length; i++) {
macBox.add(new JScrollPane(macList[i]));
macBox.add(Box.createHorizontalStrut(5));
}
main.add(type);
Box submit = Box.createHorizontalBox();
submit.add(submitButton);
main.add(Box.createVerticalStrut(5));
main.add(host);
main.add(type);
main.add(Box.createVerticalStrut(5));
main.add(port);
main.add(Box.createVerticalStrut(5));
main.add(host);
main.add(Box.createVerticalStrut(5));
main.add(auth);
main.add(Box.createVerticalStrut(5));
main.add(port);
main.add(Box.createVerticalStrut(5));
main.add(macBox);
main.add(Box.createVerticalStrut(5));
main.add(auth);
main.add(Box.createVerticalStrut(5));
main.add(submit);
main.add(Box.createVerticalStrut(5));
main.add(macBox);
ret.add(main);
ret.setBorder(new EmptyBorder(10, 10, 10, 10));
return ret;
}
main.add(Box.createVerticalStrut(5));
main.add(submit);
@Override
public void keyPressed(KeyEvent e) {
Object source = e.getSource();
if (source == proxyPort || source == proxyHost) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
actionPerformed(null);
}
}
}
ret.add(main);
ret.setBorder(new EmptyBorder(10, 10, 10, 10));
return ret;
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
@Override
public void keyPressed(KeyEvent e) {
Object source = e.getSource();
if (source == proxyPort || source == proxyHost) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
actionPerformed(null);
}
}
}
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
@Override
public void changedUpdate(DocumentEvent arg0) {
}
}
@Override
public void changedUpdate(DocumentEvent arg0) {
@Override
public void insertUpdate(DocumentEvent arg0) {
if (proxyPort.isValid()) {
proxyPort.setText("" + proxyPort.getValue());
}
}
}
@Override
public void removeUpdate(DocumentEvent arg0) {
insertUpdate(arg0);
}
@Override
public void insertUpdate(DocumentEvent arg0) {
if (proxyPort.isValid()) {
proxyPort.setText("" + proxyPort.getValue());
}
}
@Override
public void actionPerformed(ActionEvent arg0) {
boolean b = false;
@Override
public void removeUpdate(DocumentEvent arg0) {
insertUpdate(arg0);
}
if (arg0.getSource() == proxyType) {
Object o = proxyType.getSelectedItem();
authCheckBox.setEnabled(o == ProxyType.SOCKS5);
proxyHost.setEnabled(o != ProxyType.NONE);
proxyPort.setEnabled(o != ProxyType.NONE);
b = true;
}
@Override
public void actionPerformed(ActionEvent arg0) {
boolean b = false;
if (b || arg0.getSource() == authCheckBox) {
b = authCheckBox.isSelected() && authCheckBox.isEnabled();
ProxySocket.auth = b;
authUsername.setEnabled(b);
authPassword.setEnabled(b);
return;
}
if (arg0.getSource() == proxyType) {
Object o = proxyType.getSelectedItem();
authCheckBox.setEnabled(o == ProxyType.SOCKS5);
proxyHost.setEnabled(o != ProxyType.NONE);
proxyPort.setEnabled(o != ProxyType.NONE);
b = true;
}
if (proxyType.getSelectedItem() != ProxyType.NONE) {
if (proxyPort.getText().equals("")
|| proxyHost.getText().equals("")) {
UILog.log("Error", "Please supply proxy information!",
JOptionPane.ERROR_MESSAGE);
return;
}
}
if (b || arg0.getSource() == authCheckBox) {
b = authCheckBox.isSelected() && authCheckBox.isEnabled();
ProxySocket.auth = b;
authUsername.setEnabled(b);
authPassword.setEnabled(b);
return;
}
ProxySocket
.setLogin(authUsername.getText(), authPassword.getPassword());
if (proxyType.getSelectedItem() != ProxyType.NONE) {
if (proxyPort.getText().equals("")
|| proxyHost.getText().equals("")) {
UILog.log("Error", "Please supply proxy information!",
JOptionPane.ERROR_MESSAGE);
return;
}
}
byte[] mac = new byte[macList.length];
for (int i = 0; i < mac.length; i++)
mac[i] = (byte) Short.parseShort(
(String) macList[i].getSelectedValue(), 16);
NetworkInterface.setMac(mac);
String username = authUsername.getText();
char[] password = authPassword.getPassword();
try {
if (ProxySocket.getConnectionCount() > 0) {
try {
System.out.println("Closing Existing Connections...");
ProxySocket.closeConnections();
} catch (Exception e) {
ProxySocket
.setLogin(username, password);
}
}
ProxySocket.setProxy((ProxyType) proxyType.getSelectedItem(),
proxyHost.getText(), proxyPort.getValue());
UILog.log("Info", "Network settings have been set!");
} catch (Exception e) {
UILog.log("Error",
"Unable to set proxy info!\n\nReason:" + e.getMessage());
e.printStackTrace();
}
setVisible(false);
}
byte[] mac = new byte[macList.length];
for (int i = 0; i < mac.length; i++)
mac[i] = (byte) Short.parseShort(
(String) macList[i].getSelectedValue(), 16);
NetworkInterface.setMac(mac);
private JList<String> createMacList() {
String[] hexStrings = new String[256];
for (int i = 0; i < 256; i++) {
hexStrings[i] = String.format("%02X", i);
}
JList<String> ret = new JList<String>(hexStrings);
ret.setVisibleRowCount(3);
ret.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
return ret;
}
try {
if (ProxySocket.getConnectionCount() > 0) {
try {
System.out.println("Closing Existing Connections...");
ProxySocket.closeConnections();
} catch (Exception e) {
class IntTextField extends JTextField {
/**
*
*/
private static final long serialVersionUID = 1L;
}
}
ProxyType type = (ProxyType) proxyType.getSelectedItem();
String host = proxyHost.getText();
int port = proxyPort.getValue();
public IntTextField(int defval, int size) {
super("" + defval, size);
}
ProxySocket.setProxy(type, host, port);
UILog.log("Info", "Network settings have been set!");
} catch (Exception e) {
UILog.log("Error",
"Unable to set proxy info!\n\nReason:" + e.getMessage());
e.printStackTrace();
}
setVisible(false);
}
protected Document createDefaultModel() {
return new IntTextDocument();
}
private JList<String> createMacList() {
String[] hexStrings = new String[256];
for (int i = 0; i < 256; i++) {
hexStrings[i] = String.format("%02X", i);
}
JList<String> ret = new JList<String>(hexStrings);
ret.setVisibleRowCount(3);
ret.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
return ret;
}
public boolean isValid() {
try {
int i = Integer.parseInt(getText());
return i > 0 && i <= 25565;
} catch (Exception e) {
return false;
}
}
class IntTextField extends JTextField {
/**
*
*/
private static final long serialVersionUID = 1L;
public int getValue() {
try {
return Integer.parseInt(getText());
} catch (NumberFormatException e) {
return 0;
}
}
public IntTextField(int defval, int size) {
super("" + defval, size);
}
class IntTextDocument extends PlainDocument {
/**
*
*/
private static final long serialVersionUID = 1L;
protected Document createDefaultModel() {
return new IntTextDocument();
}
public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException {
if (str == null)
return;
String oldString = getText(0, getLength());
String newString = oldString.substring(0, offs) + str
+ oldString.substring(offs);
try {
Integer.parseInt(newString.replace("-", "") + "0");
super.insertString(offs, str, a);
} catch (NumberFormatException e) {
}
}
}
}
public boolean isValid() {
try {
int i = Integer.parseInt(getText());
return i > 0 && i <= 25565;
} catch (Exception e) {
return false;
}
}
public int getValue() {
try {
return Integer.parseInt(getText());
} catch (NumberFormatException e) {
return 0;
}
}
class IntTextDocument extends PlainDocument {
/**
*
*/
private static final long serialVersionUID = 1L;
public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException {
if (str == null)
return;
String oldString = getText(0, getLength());
String newString = oldString.substring(0, offs) + str
+ oldString.substring(offs);
try {
Integer.parseInt(newString.replace("-", "") + "0");
super.insertString(offs, str, a);
} catch (NumberFormatException e) {
}
}
}
}
}
@@ -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;
@@ -12,9 +13,9 @@ import org.parabot.environment.scripts.randoms.Random;
import java.util.Collection;
/**
*
*
* Script template, scripts are 'add-ons' which executes various tasks in-game
*
*
* @author Everel
*
*/
@@ -22,43 +23,45 @@ public class Script implements Runnable {
public static final int TYPE_STRATEGY = 0;
public static final int TYPE_LOOP = 1;
public static final int TYPE_OTHER = 2;
public static final int STATE_RUNNING = 0;
public static final int STATE_PAUSE = 1;
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;
}
public void onFinish() {
}
public final void provide(final Collection<Strategy> strategies) {
this.strategies = strategies;
}
public final int getFrameWorkType() {
return frameWorkType;
}
public final void setFrameWork(int frameWorkType) {
if(frameWorkType < 0 || frameWorkType > 2) {
throw new RuntimeException("Invalid framework type");
}
this.frameWorkType = frameWorkType;
}
public final void setAbstractFrameWork(AbstractFramework f) {
this.frameWork = f;
}
public final void addRandom(Random random) {
Context.getInstance().getRandomHandler().addRandom(random);
}
@@ -70,7 +73,7 @@ public class Script implements Runnable {
Core.verbose("Initializing script...");
context.getServerProvider().initScript(this);
Core.verbose("Done.");
if(!onExecute()) {
Core.verbose("Script#onExecute returned false, unloading and stopping script...");
context.getServerProvider().unloadScript(this);
@@ -78,7 +81,7 @@ public class Script implements Runnable {
Core.verbose("Done.");
return;
}
Core.verbose("Detecting script framework...");
context.setRunningScript(this);
BotUI.getInstance().toggleRun();
@@ -101,7 +104,7 @@ public class Script implements Runnable {
if(context.getRandomHandler().checkAndRun()) {
continue;
}
if(this.state == STATE_PAUSE) {
sleep(500);
continue;
@@ -128,12 +131,12 @@ public class Script implements Runnable {
BotUI.getInstance().toggleRun();
Core.verbose("Done.");
}
/**
* Sleeps until the SleepCondition is valid.
*
*
* <B>DEPRECATED!</b> use {@link Time#sleep(SleepCondition, int)}
*
*
* @param conn
* the condition.
* @param timeout
@@ -144,7 +147,7 @@ public class Script implements Runnable {
public final boolean sleep(SleepCondition conn, int timeout) {
return Time.sleep(conn, timeout);
}
/**
* Sets the script's state
* @param state
@@ -155,7 +158,7 @@ public class Script implements Runnable {
}
this.state = state;
}
/**
* Sleeps for an amount of milliseconds
* @param ms
@@ -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