Added WebUtil#getContents String, String - added checksum - improved version check

This commit is contained in:
JKetelaar
2015-06-04 12:26:38 +02:00
parent 0be2a6e00c
commit afdc4901b3
6 changed files with 103 additions and 25 deletions
+12 -1
View File
@@ -14,9 +14,11 @@ 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;
/**
@@ -46,8 +48,17 @@ public final class Landing {
if (!Core.inDebugMode() && !Core.isValid()) {
UILog.log(
"Updates",
"Please download the newest version of parabot at http://www.parabot.org/",
"Please download the newest version of Parabot at " + Configuration.DOWNLOAD_BOT,
JOptionPane.INFORMATION_MESSAGE);
URI uri = URI
.create(Configuration.DOWNLOAD_BOT);
try {
Desktop.getDesktop().browse(uri);
} catch (IOException e1) {
JOptionPane.showMessageDialog(null, "Connection Error",
"Error", JOptionPane.ERROR_MESSAGE);
e1.printStackTrace();
}
return;
}
@@ -13,12 +13,13 @@ public class Configuration {
public static final String GET_SERVER_PROVIDERS = "http://bdn.parabot.org/api/get.php?action=server_providers";
public static final String GET_SERVER_PROVIDER = "http://bdn.parabot.org/api/get.php?action=server_provider&name=";
public static final String GET_SERVER_PROVIDER_INFO = "http://bdn.parabot.org/api/get.php?action=server_information&name=";
public static final String GET_BOT_VERSION = "http://bot.parabot.org/version.txt";
public static final String GET_BOT_VERSION = "http://bdn.parabot.org/api/v2/bot/version";
public static final String DOWNLOAD_BOT = "http://bdn.parabot.org/versions/";
public static final String REGISTRATION_PAGE = "https://www.parabot.org/community/index.php?app=core&module=global&section=register";
public static final String GET_PASSWORD = "http://bdn.parabot.org/api/get.php?action=password";
public static final String GET_RANDOMS = "http://bdn.parabot.org/api/get.php?action=randoms";
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 double BOT_VERSION = 2.1;
public static final String BOT_VERSION = "2.1.12";
}
+47 -20
View File
@@ -1,12 +1,13 @@
package org.parabot.core;
import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException;
import org.parabot.Landing;
import org.parabot.environment.api.utils.WebUtil;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.DigestInputStream;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
@@ -46,10 +47,10 @@ public class Core {
public static void setDebug(final boolean debug) {
Core.debug = debug;
}
/**
* Enables dump mode
*
*
* @param dump
*/
public static void setDump(final boolean dump) {
@@ -69,7 +70,7 @@ public class Core {
public static boolean inVerboseMode() {
return verbose;
}
/**
* @return if parabot should dump injected jar
*/
@@ -104,17 +105,35 @@ public class Core {
if (f.isFile()) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
try (InputStream is = Files.newInputStream(Paths.get(f.getAbsolutePath()))) {
DigestInputStream dis = new DigestInputStream(is, md);
} catch (IOException e) {
e.printStackTrace();
}
byte[] digest = md.digest();
File location = new File(Landing.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
if (location.exists()) {
FileInputStream fis = new FileInputStream(location);
byte[] dataBytes = new byte[1024];
for (byte aDigest : digest) {
checksum += Integer.toString((aDigest & 0xff) + 0x100, 16).substring(1);
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));
}
String result;
if ((result = WebUtil.getContents("http://bdn.parabot.org/api/v2/bot/checksum", "checksum=" + URLEncoder.encode(sb.toString(), "UTF-8"))) != null) {
JSONObject object = (JSONObject) WebUtil.getJsonParser().parse(result);
if (!(boolean) object.get("result")){
Core.verbose("Latest checksum: " + sb.toString());
Core.verbose("Latest checksum: " + object.get("current"));
return false;
}
}
}
} catch (NoSuchAlgorithmException e) {
} catch (NoSuchAlgorithmException | ParseException | IOException | URISyntaxException e) {
e.printStackTrace();
}
}
@@ -128,15 +147,23 @@ public class Core {
private static boolean versionValid(){
BufferedReader br = WebUtil.getReader(Configuration.GET_BOT_VERSION);
try {
double version = Double.parseDouble(br.readLine());
if (Configuration.BOT_VERSION < version) {
String version = null;
if (br != null) {
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;
}
} catch (NumberFormatException | IOException e) {
} catch (NumberFormatException | IOException | ParseException e) {
e.printStackTrace();
} finally {
try {
br.close();
if (br != null) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
@@ -192,7 +219,7 @@ public class Core {
return false;
}
}
public static void debug(int i) {
if(mDebug) {
System.out.println("DEBUG: " + i);
@@ -0,0 +1,8 @@
package org.parabot.core.ui.utils;
/**
* @author JKetelaar
*/
public class DialogHelper {
}
@@ -53,6 +53,10 @@ public class WebUtil {
return getContents(new URL(location));
}
public static String getContents(final String location, String parameters) throws MalformedURLException {
return getContents(new URL(location), parameters);
}
/**
* Get contents from URL
*
@@ -63,6 +67,10 @@ public class WebUtil {
return getContents(getConnection(url));
}
public static String getContents(final URL url, final String parameters) {
return getContents(getConnection(url), parameters);
}
/**
* Gets contents from URLConnection
*
@@ -74,10 +82,33 @@ public class WebUtil {
final BufferedReader in = getReader(urlConnection);
final StringBuilder builder = new StringBuilder();
String line;
if (in != null) {
while ((line = in.readLine()) != null) {
builder.append(line);
}
in.close();
}
return builder.toString();
} catch (Throwable t) {
t.printStackTrace();
}
return null;
}
public static String getContents(URLConnection urlConnection, String parameters) {
try {
urlConnection.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(urlConnection.getOutputStream());
wr.write(parameters);
wr.flush();
wr.close();
final BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
final StringBuilder builder = new StringBuilder();
String line;
while ((line = in.readLine()) != null) {
builder.append(line);
}
in.close();
return builder.toString();
} catch (Throwable t) {
t.printStackTrace();
@@ -90,7 +90,7 @@ public class RandomHandler {
public boolean checkAndRun() {
for(Random r : this.activeRandoms) {
if(r.activate()) {
Logger.addMessage("Running random '" + r.getName() + "'.");
Logger.addMessage("Running random '" + r.getName() + "'");
r.execute();
return true;
}