package the.bytecode.club.bootloader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import me.konloch.kontainer.io.HTTPRequest;
import org.apache.commons.io.FileUtils;
import the.bytecode.club.bootloader.resource.EmptyExternalResource;
import the.bytecode.club.bootloader.resource.ExternalResource;
import the.bytecode.club.bytecodeviewer.BytecodeViewer;
import the.bytecode.club.bytecodeviewer.api.ExceptionUI;
import the.bytecode.club.bytecodeviewer.util.ZipUtils;
/***************************************************************************
* Bytecode Viewer (BCV) - Java & Android Reverse Engineering Suite *
* Copyright (C) 2014 Kalen 'Konloch' Kinloch - http://bytecodeviewer.com *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see . *
***************************************************************************/
/**
* @author Konloch
* @author Bibl (don't ban me pls)
* @created 19 Jul 2015 03:22:37
*/
public class Boot {
/*flags*/
public static boolean globalstop = false;
public static boolean completedboot = false;
public static boolean downloading = false;
private static InitialBootScreen screen;
private static final List libsList = new ArrayList();
private static final List libsFileList = new ArrayList();
private static final List urlList = new ArrayList();
static {
try {
screen = new InitialBootScreen();
} catch (Exception e) {
new ExceptionUI(e);
}
}
public static void boot(String[] args, boolean CLI) throws Exception {
bootstrap();
ILoader> loader = findLoader();
if (!CLI)
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
screen.setVisible(true);
}
});
create(loader, args.length > 0 ? Boolean.valueOf(args[0]) : true);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
screen.setVisible(false);
}
});
}
public static void hide() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
screen.setVisible(false);
}
});
}
@SuppressWarnings({"rawtypes", "unchecked"})
private static void create(ILoader> loader, boolean clean) throws Exception {
setState("Bytecode Viewer Boot Screen - Checking Libraries...");
final File libsDirectory = libsDir();
populateUrlList();
if (globalstop) {
while (true) {
Thread.sleep(100);//just keep this thread halted.
}
}
if (urlList.isEmpty()) {
JOptionPane.showMessageDialog(null, "Bytecode Viewer ran into an issue, for some reason github is not "
+ "returning what we're expecting. Please try rebooting, if this issue persists please contact "
+ "@Konloch.", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
if (clean)
libsDirectory.delete();
if (!libsDirectory.exists())
libsDirectory.mkdir();
populateLibsDirectory();
screen.getProgressBar().setMaximum(urlList.size() * 2);
int completedCheck = 0;
for (String s : urlList) {
String fileName = s.substring("https://github.com/Konloch/bytecode-viewer/blob/master/libs/".length()
);
File file = new File(libsDirectory, fileName);
boolean passed = false;
while (!passed) {
if (!libsList.contains(fileName)) {
downloading = true;
setState("Bytecode Viewer Boot Screen - Downloading " + fileName + "...");
System.out.println("Downloading " + fileName);
InputStream is = null;
FileOutputStream fos = null;
try {
is = new URL("https://github.com/Konloch/bytecode-viewer/raw/master/libs/" + fileName).openConnection().getInputStream();
fos = new FileOutputStream(file);
System.out.println("Downloading from " + s);
byte[] buffer = new byte[8192];
int len;
int downloaded = 0;
boolean flag = false;
while ((len = is.read(buffer)) > 0) {
fos.write(buffer, 0, len);
fos.flush();
downloaded += 8192;
int mbs = downloaded / 1048576;
if (mbs % 5 == 0 && mbs != 0) {
if (!flag)
System.out.println("Downloaded " + mbs + "MBs so far");
flag = true;
} else
flag = false;
}
} finally {
try {
if (is != null) {
is.close();
}
} finally {
if (fos != null) {
fos.flush();
}
if (fos != null) {
fos.close();
}
}
}
try {
setState("Bytecode Viewer Boot Screen - Verifying " + fileName + "...");
System.out.println("Verifying " + fileName + "...");
File f = new File(BytecodeViewer.tempDirectory, "temp");
if (!f.exists()) {
f.getParentFile().mkdirs();
}
ZipUtils.zipFile(file, f);
f.delete();
libsFileList.add(file.getAbsolutePath());
System.out.println("Download finished!");
passed = true;
} catch (Exception e) {
e.printStackTrace();
System.out.println("Jar or Zip" + file.getAbsolutePath() + " is corrupt, redownloading.");
file.delete();
}
} else if (BytecodeViewer.verify) { //verify its not corrupt each boot (adds 3 seconds boot time)
try {
setState("Bytecode Viewer Boot Screen - Verifying " + fileName + "...");
System.out.println("Verifying " + fileName + "...");
File f = new File(BytecodeViewer.tempDirectory, "temp");
ZipUtils.zipFile(file, f);
f.delete();
passed = true;
} catch (Exception e) {
e.printStackTrace();
System.out.println("Jar or Zip" + file.getAbsolutePath() + " is corrupt, redownloading.");
libsFileList.remove(file.getAbsolutePath());
file.delete();
}
} else {
passed = true;
}
}
completedCheck++;
screen.getProgressBar().setValue(completedCheck);
}
setState("Bytecode Viewer Boot Screen - Checking & Deleting Foreign/Outdated Libraries...");
System.out.println("Checking & Deleting foreign/outdated libraries");
for (String s : libsFileList) {
File f = new File(s);
boolean delete = true;
for (String urlS : urlList) {
String fileName =
urlS.substring("https://github.com/Konloch/bytecode-viewer/blob/master/libs/".length()
);
if (fileName.equals(f.getName()))
delete = false;
}
if (delete) {
f.delete();
System.out.println("Detected & Deleted Foreign/Outdated Jar/File: " + f.getName());
}
}
setState("Bytecode Viewer Boot Screen - Loading Libraries...");
System.out.println("Loading libraries...");
for (String s : libsFileList) {
if (s.endsWith(".jar")) {
File f = new File(s);
if (f.exists()) {
setState("Bytecode Viewer Boot Screen - Loading Library " + f.getName());
System.out.println("Loading library " + f.getName());
try {
ExternalResource res = new EmptyExternalResource