big change: move the json loading and random server selection to main PieCannon class (so that it has a reason to live :)) and implement loading the json file in the android app.
I deliberated between re-using the json file in the android app and using a sqlite database or some such thing, given that a UI for editing these will have to be implemented at some point. I figure the json file is more config than data, and probably should be as consistent as possible between platforms. For now at least this makes the app usable for me, without having to hard-code my server credentials.
This commit is contained in:
44
lib/.classpath
Normal file
44
lib/.classpath
Normal file
@@ -0,0 +1,44 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" output="target/classes" path="src/main/java">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
<attribute name="test" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" path="target/generated-sources/annotations">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
<attribute name="ignore_optional_problems" value="true"/>
|
||||
<attribute name="m2e-apt" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
<attribute name="ignore_optional_problems" value="true"/>
|
||||
<attribute name="m2e-apt" value="true"/>
|
||||
<attribute name="test" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="output" path="target/classes"/>
|
||||
</classpath>
|
11
lib/.factorypath
Normal file
11
lib/.factorypath
Normal file
@@ -0,0 +1,11 @@
|
||||
<factorypath>
|
||||
<factorypathentry kind="VARJAR" id="M2_REPO/com/github/mwiede/jsch/0.1.60/jsch-0.1.60.jar" enabled="true" runInBatchMode="false"/>
|
||||
<factorypathentry kind="VARJAR" id="M2_REPO/com/google/guava/guava/30.0-android/guava-30.0-android.jar" enabled="true" runInBatchMode="false"/>
|
||||
<factorypathentry kind="VARJAR" id="M2_REPO/com/google/guava/failureaccess/1.0.1/failureaccess-1.0.1.jar" enabled="true" runInBatchMode="false"/>
|
||||
<factorypathentry kind="VARJAR" id="M2_REPO/com/google/guava/listenablefuture/9999.0-empty-to-avoid-conflict-with-guava/listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar" enabled="true" runInBatchMode="false"/>
|
||||
<factorypathentry kind="VARJAR" id="M2_REPO/com/google/code/findbugs/jsr305/3.0.2/jsr305-3.0.2.jar" enabled="true" runInBatchMode="false"/>
|
||||
<factorypathentry kind="VARJAR" id="M2_REPO/org/checkerframework/checker-compat-qual/2.5.5/checker-compat-qual-2.5.5.jar" enabled="true" runInBatchMode="false"/>
|
||||
<factorypathentry kind="VARJAR" id="M2_REPO/com/google/errorprone/error_prone_annotations/2.3.4/error_prone_annotations-2.3.4.jar" enabled="true" runInBatchMode="false"/>
|
||||
<factorypathentry kind="VARJAR" id="M2_REPO/com/google/j2objc/j2objc-annotations/1.3/j2objc-annotations-1.3.jar" enabled="true" runInBatchMode="false"/>
|
||||
<factorypathentry kind="VARJAR" id="M2_REPO/org/projectlombok/lombok/1.18.16/lombok-1.18.16.jar" enabled="true" runInBatchMode="false"/>
|
||||
</factorypath>
|
34
lib/.project
Normal file
34
lib/.project
Normal file
@@ -0,0 +1,34 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>libpiecannon</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.m2e.core.maven2Builder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||
</natures>
|
||||
<filteredResources>
|
||||
<filter>
|
||||
<id>1603692553450</id>
|
||||
<name></name>
|
||||
<type>30</type>
|
||||
<matcher>
|
||||
<id>org.eclipse.core.resources.regexFilterMatcher</id>
|
||||
<arguments>node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
|
||||
</matcher>
|
||||
</filter>
|
||||
</filteredResources>
|
||||
</projectDescription>
|
4
lib/.settings/org.eclipse.jdt.apt.core.prefs
Normal file
4
lib/.settings/org.eclipse.jdt.apt.core.prefs
Normal file
@@ -0,0 +1,4 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.apt.aptEnabled=true
|
||||
org.eclipse.jdt.apt.genSrcDir=target/generated-sources/annotations
|
||||
org.eclipse.jdt.apt.genTestSrcDir=target/generated-test-sources/test-annotations
|
9
lib/.settings/org.eclipse.jdt.core.prefs
Normal file
9
lib/.settings/org.eclipse.jdt.core.prefs
Normal file
@@ -0,0 +1,9 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
|
||||
org.eclipse.jdt.core.compiler.compliance=1.8
|
||||
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
|
||||
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
|
||||
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
|
||||
org.eclipse.jdt.core.compiler.processAnnotations=enabled
|
||||
org.eclipse.jdt.core.compiler.release=disabled
|
||||
org.eclipse.jdt.core.compiler.source=1.8
|
4
lib/.settings/org.eclipse.m2e.core.prefs
Normal file
4
lib/.settings/org.eclipse.m2e.core.prefs
Normal file
@@ -0,0 +1,4 @@
|
||||
activeProfiles=
|
||||
eclipse.preferences.version=1
|
||||
resolveWorkspaceProjects=true
|
||||
version=1
|
@@ -22,6 +22,11 @@
|
||||
<artifactId>guava</artifactId>
|
||||
<version>30.0-android</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.8.6</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
|
@@ -2,18 +2,62 @@ package net.monarchpass.piecannon;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.IOException;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Random;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.common.io.CharSource;
|
||||
import com.google.common.io.ByteStreams;
|
||||
|
||||
public class PieCannon {
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
|
||||
import net.monarchpass.piecannon.util.ServerFactory;
|
||||
|
||||
public class PieCannon {
|
||||
public List<Server> servers;
|
||||
|
||||
public List<Server> loadServersFrom (final File serversJson) throws IOException {
|
||||
try (final InputStream in = new FileInputStream(serversJson)) {
|
||||
return loadServersFrom(in);
|
||||
}
|
||||
}
|
||||
|
||||
public List<Server> loadServersFrom (final InputStream in) throws IOException {
|
||||
final ServerFactory factory = new ServerFactory();
|
||||
final JsonParser parser = new JsonParser();
|
||||
final List<Server> loadedServers = new ArrayList<>();
|
||||
for (final JsonElement element : parser.parse(new InputStreamReader(in)).getAsJsonArray()) {
|
||||
if (!element.isJsonObject()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
loadedServers.add(factory.apply((JsonObject)element));
|
||||
}
|
||||
servers = loadedServers;
|
||||
return loadedServers;
|
||||
}
|
||||
|
||||
public Server selectServer () {
|
||||
if (servers == null) {
|
||||
throw new IllegalStateException("Servers are not loaded");
|
||||
}
|
||||
|
||||
return servers.get(new Random().nextInt(servers.size()));
|
||||
}
|
||||
|
||||
public static boolean testServer (final Server server) {
|
||||
final String testString = "piecannon-test-" + System.currentTimeMillis();
|
||||
final URI result = server.upload("piecannon.test", CharSource.wrap(testString).asByteSource(Charsets.UTF_8));
|
||||
|
@@ -0,0 +1,30 @@
|
||||
package net.monarchpass.piecannon.util;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import net.monarchpass.piecannon.Server;
|
||||
import net.monarchpass.piecannon.impl.SftpServer;
|
||||
|
||||
public class ServerFactory implements Function<JsonObject, Server> {
|
||||
public Server apply (final JsonObject object) {
|
||||
return makeSftpServer(object);
|
||||
}
|
||||
|
||||
private Server makeSftpServer (final JsonObject object) {
|
||||
final String host = object.getAsJsonPrimitive("host").getAsString();
|
||||
final String label = object.has("label") ? object.getAsJsonPrimitive("label").getAsString() : host;
|
||||
final int port = object.has("port") ? object.getAsJsonPrimitive("port").getAsInt() : 22;
|
||||
final String username = object.getAsJsonPrimitive("username").getAsString();
|
||||
final String password = object.getAsJsonPrimitive("password").getAsString();
|
||||
final String path = object.getAsJsonPrimitive("path").getAsString();
|
||||
final String url = object.getAsJsonPrimitive("url").getAsString();
|
||||
|
||||
return new SftpServer(
|
||||
label, host, port, username, password,
|
||||
path, URI.create(url)
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user