Skip to content
This repository was archived by the owner on Nov 24, 2018. It is now read-only.

Commit b19891b

Browse files
committed
First release
1 parent a9e849d commit b19891b

16 files changed

+827
-0
lines changed

build.gradle

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
plugins {
2+
id 'java'
3+
id 'application'
4+
id 'com.github.johnrengelman.shadow' version '1.2.4'
5+
}
6+
group 'vHackOS'
7+
version '1.0-SNAPSHOT'
8+
9+
sourceCompatibility = 1.8
10+
11+
repositories {
12+
mavenCentral()
13+
maven { url "https://jitpack.io" }
14+
}
15+
16+
dependencies {
17+
testCompile group: 'junit', name: 'junit', version: '4.12'
18+
compile 'net.olympiccode:vHackOSAPI-Java:dev-SNAPSHOT'
19+
compile group: 'org.json', name: 'json', version: '20090211'
20+
compile 'org.reflections:reflections:0.9.10'
21+
compile 'com.google.code.gson:gson:2.8.2'
22+
compile 'ch.qos.logback:logback-classic:1.2.3'
23+
compile 'com.google.guava:guava:24.0-jre'
24+
}
25+
mainClassName = "net.olympiccode.vhackos.bot.core.vHackOSBot"
26+
shadowJar.destinationDir = file("/build/libs")
27+
shadowJar.archiveName = 'vHackOSAPI.jar'
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package net.olympiccode.vhackos.bot.core;
2+
3+
public interface BotService {
4+
void setup();
5+
void runService();
6+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package net.olympiccode.vhackos.bot.core.config;
2+
3+
import com.google.gson.*;
4+
import org.reflections.Reflections;
5+
import org.reflections.scanners.FieldAnnotationsScanner;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
9+
import java.io.File;
10+
import java.io.IOException;
11+
import java.lang.annotation.Annotation;
12+
import java.lang.reflect.Field;
13+
import java.lang.reflect.InvocationTargetException;
14+
import java.nio.charset.Charset;
15+
import java.nio.file.Files;
16+
import java.nio.file.Paths;
17+
import java.util.Arrays;
18+
import java.util.Set;
19+
import java.util.stream.Collectors;
20+
21+
public class AdvancedConfigFile {
22+
23+
public AdvancedConfigFile() {
24+
LOG.info("Creating ConfigFile...");
25+
}
26+
JsonObject configJson = new JsonObject();
27+
File file = new File("advanced.json");
28+
Logger LOG = LoggerFactory.getLogger("vHackOSBot-ConfigAdv");
29+
public void setupConfig() {
30+
LOG.info("Loading advanced config...");
31+
long time = System.currentTimeMillis();
32+
try {
33+
if (!file.exists()) {
34+
file.createNewFile();
35+
configJson = new JsonObject();
36+
} else {
37+
String config = Files.readAllLines(Paths.get("advanced.json"), Charset.forName("UTF-8")).stream().collect(Collectors.joining());
38+
if (config.startsWith("{")) configJson = (JsonObject) new JsonParser().parse(config);
39+
}
40+
Set<Field> fields = new Reflections("net.olympiccode.vhackos.bot.core", new FieldAnnotationsScanner()).getFieldsAnnotatedWith(AdvancedConfigOption.class);
41+
fields.forEach(field -> {
42+
Annotation annotation = field.getAnnotation(AdvancedConfigOption.class);
43+
try {
44+
String path = (String) AdvancedConfigOption.class.getMethod("path").invoke(annotation);
45+
String defaultValue = (String) AdvancedConfigOption.class.getMethod("defaultValue").invoke(annotation);
46+
String[] options = (String[]) AdvancedConfigOption.class.getMethod("options").invoke(annotation);
47+
if (!configJson.has(path)) configJson.add(path, new JsonParser().parse(defaultValue));
48+
if (field.getType().isEnum()) {
49+
Class<?> en = field.getType();
50+
field.set(field.getDeclaringClass(), Enum.valueOf((Class<Enum>) field.getType(), configJson.get(path).getAsString()));
51+
} else if (field.getType().isArray()) {
52+
JsonArray array = configJson.get(path).getAsJsonArray();
53+
String[] larray = new String[array.size()];
54+
for (int i = 0; i < array.size(); i++) {
55+
larray[i] = array.get(i).getAsString();
56+
}
57+
field.set(field.getDeclaringClass(), larray);
58+
} else if (field.getType().equals(boolean.class) || field.getType().equals(Boolean.class)) {
59+
field.set(field.getDeclaringClass(), configJson.get(path).getAsBoolean());
60+
} else if (field.getType().equals(int.class) || field.getType().equals(Integer.class)) {
61+
field.set(field.getDeclaringClass(), configJson.get(path).getAsInt());
62+
} else {
63+
if (Arrays.asList(options).get(0).isEmpty() || Arrays.asList(options).contains(configJson.get(path).getAsString())) {
64+
field.set(field.getDeclaringClass(), configJson.get(path).getAsString());
65+
} else {
66+
configJson.add(path, new JsonParser().parse(defaultValue));
67+
field.set(field.getDeclaringClass(), defaultValue);
68+
}
69+
}
70+
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
71+
e.printStackTrace();
72+
}
73+
});
74+
} catch (IOException e) {
75+
e.printStackTrace();
76+
}
77+
LOG.info("Loaded advanced config in " + (System.currentTimeMillis() - time) + "ms.");
78+
save();
79+
}
80+
81+
public void save() {
82+
LOG.info("Saving advanced config...");
83+
long time = System.currentTimeMillis();
84+
try {
85+
Gson gson = new GsonBuilder().setPrettyPrinting().create();
86+
87+
Files.write(Paths.get("advanced.json"), gson.toJson(configJson).toString().getBytes());
88+
} catch (IOException e) {
89+
e.printStackTrace();
90+
}
91+
LOG.info("Saved advanced config in " + (System.currentTimeMillis() - time) + "ms.");
92+
}
93+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package net.olympiccode.vhackos.bot.core.config;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Retention(RetentionPolicy.RUNTIME)
9+
@Target(ElementType.FIELD)
10+
public @interface AdvancedConfigOption {
11+
String path();
12+
String defaultValue();
13+
String[] options();
14+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package net.olympiccode.vhackos.bot.core.config;
2+
3+
import ch.qos.logback.classic.Level;
4+
5+
public class AdvancedConfigValues {
6+
7+
@AdvancedConfigOption(path = "loglevel", defaultValue = "info", options = {"all", "debug", "error", "info", "off", "trace", "warn"})
8+
public static String logLevel;
9+
10+
@AdvancedConfigOption(path = "networking.runTime", defaultValue = "3000", options = {""})
11+
public static int a;
12+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package net.olympiccode.vhackos.bot.core.config;
2+
3+
import com.google.gson.*;
4+
import org.reflections.Reflections;
5+
import org.reflections.scanners.FieldAnnotationsScanner;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
9+
import java.io.File;
10+
import java.io.IOException;
11+
import java.lang.annotation.Annotation;
12+
import java.lang.reflect.Field;
13+
import java.lang.reflect.InvocationTargetException;
14+
import java.nio.charset.Charset;
15+
import java.nio.file.Files;
16+
import java.nio.file.Paths;
17+
import java.util.Arrays;
18+
import java.util.Set;
19+
import java.util.stream.Collectors;
20+
21+
public class ConfigFile {
22+
23+
JsonObject configJson = new JsonObject();
24+
File file = new File("config.json");
25+
Logger LOG = LoggerFactory.getLogger("vHackOSBot-Config");
26+
public ConfigFile() {
27+
LOG.info("Creating ConfigFile...");
28+
}
29+
30+
public void setupConfig() {
31+
LOG.info("Loading config...");
32+
long time = System.currentTimeMillis();
33+
try {
34+
if (!file.exists()) {
35+
file.createNewFile();
36+
configJson = new JsonObject();
37+
} else {
38+
String config = Files.readAllLines(Paths.get("config.json"), Charset.forName("UTF-8")).stream().collect(Collectors.joining());
39+
if (config.startsWith("{")) configJson = (JsonObject) new JsonParser().parse(config);
40+
}
41+
Set<Field> fields = new Reflections("net.olympiccode.vhackos.bot.core", new FieldAnnotationsScanner()).getFieldsAnnotatedWith(ConfigOption.class);
42+
fields.forEach(field -> {
43+
Annotation annotation = field.getAnnotation(ConfigOption.class);
44+
try {
45+
String path = (String) ConfigOption.class.getMethod("path").invoke(annotation);
46+
String defaultValue = (String) ConfigOption.class.getMethod("defaultValue").invoke(annotation);
47+
String[] options = (String[]) ConfigOption.class.getMethod("options").invoke(annotation);
48+
if (!configJson.has(path)) configJson.add(path, new JsonParser().parse(defaultValue));
49+
if (field.getType().isEnum()) {
50+
Class<?> en = field.getType();
51+
field.set(field.getDeclaringClass(), Enum.valueOf((Class<Enum>) field.getType(), configJson.get(path).getAsString()));
52+
} else if (field.getType().isArray()) {
53+
JsonArray array = configJson.get(path).getAsJsonArray();
54+
String[] larray = new String[array.size()];
55+
for (int i = 0; i < array.size(); i++) {
56+
larray[i] = array.get(i).getAsString();
57+
}
58+
field.set(field.getDeclaringClass(), larray);
59+
} else if (field.getType().equals(boolean.class) || field.getType().equals(Boolean.class)) {
60+
field.set(field.getDeclaringClass(), configJson.get(path).getAsBoolean());
61+
} else if (field.getType().equals(int.class) || field.getType().equals(Integer.class)) {
62+
field.set(field.getDeclaringClass(), configJson.get(path).getAsInt());
63+
} else {
64+
if (Arrays.asList(options).get(0).isEmpty() || Arrays.asList(options).contains(configJson.get(path).getAsString())) {
65+
field.set(field.getDeclaringClass(), configJson.get(path).getAsString());
66+
} else {
67+
configJson.add(path, new JsonParser().parse(defaultValue));
68+
field.set(field.getDeclaringClass(), defaultValue);
69+
}
70+
}
71+
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
72+
e.printStackTrace();
73+
}
74+
});
75+
} catch (IOException e) {
76+
e.printStackTrace();
77+
}
78+
LOG.info("Loaded config in " + (System.currentTimeMillis() - time) + "ms.");
79+
save();
80+
}
81+
82+
public void save() {
83+
LOG.info("Saving config...");
84+
long time = System.currentTimeMillis();
85+
try {
86+
Gson gson = new GsonBuilder().setPrettyPrinting().create();
87+
88+
Files.write(Paths.get("config.json"), gson.toJson(configJson).toString().getBytes());
89+
} catch (IOException e) {
90+
e.printStackTrace();
91+
}
92+
LOG.info("Saved config in " + (System.currentTimeMillis() - time) + "ms.");
93+
}
94+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package net.olympiccode.vhackos.bot.core.config;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Retention(RetentionPolicy.RUNTIME)
9+
@Target(ElementType.FIELD)
10+
public @interface ConfigOption {
11+
String path();
12+
String defaultValue();
13+
String[] options();
14+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package net.olympiccode.vhackos.bot.core.log;
2+
3+
import ch.qos.logback.classic.Level;
4+
import ch.qos.logback.classic.Logger;
5+
import ch.qos.logback.classic.turbo.TurboFilter;
6+
import ch.qos.logback.core.spi.FilterReply;
7+
import org.slf4j.Marker;
8+
9+
import java.util.Arrays;
10+
import java.util.stream.Collectors;
11+
12+
public class LogFilter extends TurboFilter {
13+
private String loggerName = "org.reflections.Reflections";
14+
private String last = "";
15+
int dupe = 0;
16+
@Override
17+
public FilterReply decide(Marker marker, Logger logger,
18+
Level level, String format, Object[] params, Throwable t) {
19+
if (!logger.getName().equals(loggerName) && last.equals(format + paramsAsString(params, logger))) {
20+
if (dupe > 1) {
21+
return FilterReply.DENY;
22+
} else {
23+
dupe++;
24+
}
25+
} else {
26+
last = format;
27+
dupe = 0;
28+
}
29+
if (loggerName == null) {
30+
return FilterReply.NEUTRAL;
31+
} else if (loggerName.equals(logger.getName())) {
32+
return FilterReply.DENY;
33+
} else
34+
return FilterReply.NEUTRAL;
35+
}
36+
37+
private String paramsAsString(final Object[] params, final Logger logger) {
38+
if (params != null) {
39+
return Arrays.stream(params).map(Object::toString).collect(Collectors.joining("_"));
40+
}
41+
42+
return "";
43+
}
44+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package net.olympiccode.vhackos.bot.core.misc;
2+
3+
import net.olympiccode.vhackos.bot.core.config.ConfigOption;
4+
5+
public class MiscConfigValues {
6+
7+
@ConfigOption(path = "misc.enabled", defaultValue = "true", options = {"true", "false"})
8+
public static boolean enabled;
9+
10+
@ConfigOption(path = "misc.enableMiner", defaultValue = "true", options={"true", "false"})
11+
public static boolean enableMiner;
12+
13+
14+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package net.olympiccode.vhackos.bot.core.misc;
2+
3+
import net.olympiccode.vhackos.api.entities.AppType;
4+
import net.olympiccode.vhackos.api.network.ExploitedTarget;
5+
import net.olympiccode.vhackos.bot.core.BotService;
6+
import net.olympiccode.vhackos.bot.core.vHackOSBot;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
10+
import java.util.concurrent.Executors;
11+
import java.util.concurrent.ScheduledExecutorService;
12+
import java.util.concurrent.ThreadFactory;
13+
import java.util.concurrent.TimeUnit;
14+
15+
public class MiscService implements BotService {
16+
ScheduledExecutorService miscService;
17+
Logger LOG = LoggerFactory.getLogger("MiscService");
18+
19+
public MiscService() {
20+
LOG.info("Creating MiscService...");
21+
miscService = Executors.newScheduledThreadPool(1, new MiscServiceFactory());
22+
}
23+
24+
public void setup() {
25+
LOG.info("Setting up MiscSerice...");
26+
miscService.scheduleAtFixedRate(() -> runService(), 0, 60000 * 60, TimeUnit.MILLISECONDS);
27+
}
28+
29+
public void runService() {
30+
try {
31+
if (MiscConfigValues.enableMiner) {
32+
if (vHackOSBot.api.getAppManager().getApp(AppType.NCMiner).isInstalled()) {
33+
if (vHackOSBot.api.getMiner().start()) {
34+
LOG.info("Collected and restarted miner");
35+
} else {
36+
LOG.info("Failed to collect and restart miner");
37+
}
38+
} else {
39+
LOG.warn("MiscService ran but miner was not installed.");
40+
}
41+
}
42+
} catch (Exception e) {
43+
e.printStackTrace();
44+
}
45+
}
46+
47+
public class MiscServiceFactory implements ThreadFactory {
48+
public Thread newThread(Runnable r) {
49+
return new Thread(r, "vHackOSBot-MiscService");
50+
}
51+
}
52+
53+
}

0 commit comments

Comments
 (0)